1 / 43

CHAPTER 6

CHAPTER 6. Graph. 1. Graph theory. 2. Graph theory. Eulerian Cycle In a diagram, is there a path from a point , through every edge exactly once, and then return to the starting point?. Impossible, the necessary and sufficient conditions: the branching degree of each vertex must be even. 3.

Download Presentation

CHAPTER 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 6 Graph 1

  2. Graph theory 2

  3. Graph theory • Eulerian Cycle • In a diagram, is there a path from a point , through every edge exactly once, and then return to the starting point? Impossible, the necessary and sufficient conditions: the branching degree of each vertex must be even. 3

  4. Graph theory Eulerianchain: Achain which goes through all edges of a graph G , and uses each of G at most once is called an Eulerian chain. 4

  5. Graph Graph G = (V,E) consists ofa set of objects V={v1,v2,…,vn},n>0, called vertex, and another set E= {e1,e2,…,em},m>0, called edges。 Undirected graph: No arrow on the edge. In undirected graph, (V1,V2) & (V2,V1)are the same edge. Directed graph: The arrow on the edge. In directed graph, (V1,V2) & (V2,V1)are different edges. 5

  6. CompleteGraph A B C D Definition : A complete graph is a graph that has the maximum number of edges. For an undirected graph with n vertices, the maximum number of edges is the number of distinct, unordered pairs, (vi, vj), i≠j . This number is: 6

  7. Terminology • Subgraph : A subgraph of G is a G’ such that V(G’) V(G) and E(G’) E(G) • Path : A walk in which no vertex is repeated is called a path. A path that joins vertices u and v is called a u-v path • Path length : The length of a graph is the number of edges on it. • Cycle : It is a simple path in which the first and the last vertices are the same • Connected : In a undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1. Since G is undirected, this means that must be a path from v1 to v0. An undirected graph is connected if, for every pair of distinct vertices vi, vj, there is a path from vi to vj in G 7

  8. Terminology • Connectedcomponen→ A connected component, or simple a component, of an undirected graph is a maximal connected subgraph. • A directed graph is stronglyconnected if, for every pair of vertices vi,vj in V(G), there is a directed path from vi to vj and also from vj to vi. • The in-degree of a vertex v as the number of edges that have v as the head, and the out-degree as the number of edges that have v as the tail. • If di is the degree of a vertex i in a graph G with n vertices and e edges, then the number of edges is: 8

  9. Adjacency Matrix 9

  10. Adjacency Matrix D A D A C B C B (b) G2 (Directed graph) (a) G1 (Undirected graph) Is not necessarily symmetric matrix Must be symmetric matrix 10

  11. Adjacency List In a linked list to record each vertex adjacent vertices, each node has at least two columns: Vertex and Link If undirected graph is composed by n vertices and e edges, then this representation needs n head node and 2e linked lists nodes. In an undirected graph, the degree of each vertex is as same as the number of linked list nodes. Therefore, the number of edges in graph G are found out in O (n + e) ​​time. 11

  12. Adjacency Multilist For each edge, they are represented by a node, and the node is structured as follows: Where M is a bit of Column which storage edges whether are searched Vertex 1 and 2 belong this edge. Link1: If there are edges and vertices V1 is connected to other vertices, then Link1 points to the vertex and vertex V1 formed by the edge node, or point to nil. Link2: If there are edge and vertex V2 is connected to other vertices, then Link2 point to the vertex and vertex V2 formed by the edge node, or point to nil. 12

  13. Index Table 1 2 3 4 It storages all adjacent vertices by a one-dimensional array and uses a int type of Index [1 ... n] to record each vertex of initial address. 13

  14. GraphTraversal Graphic tracing is a graph vertex which visit other graphics. Aims to (1) Determine whether the graphic is interconnected (2) Find the graphic communication unit (3)Draw the graph spanning tree Depth-First-Search(DFS): The way that is works on a graph G is to begin at some vertex, the root, and travel as far as possible along a path P emanating from v. It then backtracks toward v until it finds a first vertex u from which there is an edge to a vertex w not yet reached. 14

  15. DFS Procedure DFS(v:integer) {Given an undirected graph G= (V,E) with n vertices and an array visited[n] initially set to false, this algorithm visits all vertices reachable from v. Visited is global.} var w: integer; Begin visited[v] := true ; for each vertex w adjacent to v do if not visited[w] then DFS(w); End ; 15

  16. (1) DFS visits the nodes from V1 in the following order: V1, V2, V4, V8, V5, V6, V3, V7(2) DFS visits the nodes from V6 in the following order: V6, V3, V1, V2, V4, V8, V5, V7 V2 V3 V5 V4 V1 V7 V6 V8 16

  17. BFS Breadth first search (BFS): Breadth first search starts at vertex v and marks it as visited. It then visits each of the vertices on v’s adjacency list. When we have visited all the vertices on v’s adjacency list, we visit all the unvisited vertices that are adjacent to the first vertex on v’s adjacency list. BFS and DFS is difference that visit all adjacent vertices after looking for others adjacent vertices on next layer. BFS use Queue, but DFS use Stack. 17

  18. BFS ProcedureBFS(v : integer); var w integer, q : queue ; Begin visited[v] := true; InitializeQueue(q); AddQueue(q,v); while not EmptyQueue(q) do Begin DeleteQueue(q,v); for all vertices w adjacent to v do if not visited[w] then Begin visited[w]:= true; AddQueue (q,w); End; End; End; 18

  19. BFS V2 V3 V5 V4 V1 V7 V6 V8 BFS visits the nodes in the following order: V1, V2, V3, V4, V5, V6, V7, V8 19

  20. SpanningTree Definition: When graph G is connected, a depth first or breadth first search starting at any vertex visits all the vertices in G. The search implicitly partitions the edges in G into two sets: T(for tree edges) and N(for nontree edges). T is the set of edges used or traversed during the search and N is the set of remaining edges. We can determine the set of tree edges by adding a statement to the if clause of either DFS or BFS that inserts the edges(v,w) into a linked list of edges. The edges in T form a tree that includes all vertices of G. A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. 20

  21. SpanningTree The complete graph: The following is a part of the spanningtree: (a) (b) (c) 21

  22. SpanningTree If G = (V, E) is a graph, and S = (V, T) is a spanning tree of G. Where T stroages edges tracking path, and K stroages edges tracking path which aren’t visited. The following points of spanning tree features: E=T+K Any two of the vertices V1 and V2 of V, has a unique path in S. Added K to any one edge in S, will cause the loop. 22

  23. V5 V5 V5 V1 V1 V3 V3 V1 V3 V2 V2 V2 V6 V7 V6 V6 V7 V7 V4 V4 V4 V8 V8 V8 BFS’s spanningTree DFS’s spanningTree 23

  24. Prim’sAlgorithm 16 1 2 5 11 21 6 3 19 6 14 33 10 5 4 18 Prim’s algorithm begins with a tree, T, that contains a single vertex. This may be any of the vertices in the original graph. Next, we add a least cost edge(u, v) to T such that T {(u, v)} is also a tree. We repeat this edge addition step until T contains n-1 edges. To make sure that the added edge does not form a cycle, at each step we choose the edge(u, v) such that exactly one of u or v is in T. 24

  25. 16 1 2 5 11 21 6 3 19 6 14 33 10 5 4 18 Step u V-U (1){1} {2.3.4.5.6} (2){1.2}{3.4.5.6} (3){1.2.3}{4.5.6} (4){1.2.3.4}{5.6} (5){1.2.3.4.6}{5} (6){1.2.3.4.5.6}0 25

  26. Kruskal’sAlgorithm 1 28 2 10 16 14 3 6 Performing Kruskal’ algorithm as follows: 7 24 25 18 5 12 22 Practices: An expansion of smallest cost in tree T accords to their costs, pick one by one from small ones to big ones. If one edge does not make up a circle with a existing T’s edge, then putting this edge into T, or giving up it. 26 4

  27. 1 2 10 16 14 1 3 28 6 7 2 10 16 14 25 3 6 7 5 12 24 25 18 5 12 22 22 4 27 4

  28. Sollin’sAlgorithm 1 Vertex 1, 2, … , 7 selected edges are: (1,6)、(2,7)、(3,4)、(4,3)、(5,4)、(6,1)、(7,2) 28 2 10 16 14 3 6 7 Delete repeating edges 24 25 18 5 12 (1,6)、(2,7)、(3,4)、(5,4) 22 Steps:(1) For each of tree, we select the smallest cost edge of the tree. (2) Delete repeating edges, leaving only one edge. (3) Repeat (1)~(2) until the rest of a tree or E=empty (4) if|T|<n-1,then〝Nospanningtree〞 28 4

  29. 1 1 2 2 10 10 16 14 14 3 3 6 6 7 7 25 5 5 12 12 22 22 Add selected set of edges form a figure (a). In the next step, the vertex set is {1,6} selected edges (6,5) and the remaining two edges (2,3) are selected . After two edges are added to a set of selected edges, the algorithm is completed, as shown in (b). 29 4 4

  30. Shortestpathproblem We have already mentioned that each edge in graph increases weight, and this kind of weight may be cost or distance. The graph can be called as network at this time, and the basic applied question of this network is: how to calculate the shortest distance or path from a starting point Vs to a end point Vt. Ⅰ.Graph is non-existence of a negative edge: Dijkstra’sAlgorithm Ⅱ.Graph is existence of a negative edge: Bellman-FordAlgorithm 30

  31. Dijkstra’sAlgorithm Steps 1: D[I] = A[F,I] (I = 1,N) S = {F} V = {1,2,…,N} D is a N of array in order to storage the minimal distance from a certain vertex to others. F means the start of vertex. A[F,I] stands for the distance between vertex F and I, V is a set of total vertics, and S is a set of vertex. Steps2: Finding a vertex t from a set V-S makes D[t] become the minimal value, and let t put into set S until V-S becomes a empty set. Steps3: According to the following formula to adjust values ​​in the array D. D[I]=min(D[I],D[t]+A[t ,I]) (I,t) among E Return to perform Steps 2. 31

  32. The following diagram (a) is a directed graph containing eight vertices, find the process of the shortest path from vertex 5? 32

  33. 33

  34. Shortest Path Algorithm ProcedureShort_path( v, cost, dist , n) Var S(1:n); for i=1 to n do S(i) = 0; dist(i) = cost(v,i); End; S(v)= 1; dist(v)←0; num←2; While num<n do choose u:dist(u) = min{dist(w),|s(w)=0}; s(u)←1; num←num+1; for all w with s(w)= 0 do dist(w)←min{dist(w),dist(u)+cost(u,w)}; End; End; 34

  35. Important assumption: there is no negative edge existing in graph, otherwise, it may not be able to find the right solution. When the third path is sure to be the shortest one, it will not to update the value of Dist[2], since S[2] = 1 is earlier, Dist[2] is still sustained as three, but the accurate value should be one (1→3→2) 35

  36. AovNetwork (一)AOV(Activity On Vertex)Network Definition: Let G = (V, E) is a directed graph that represents AOV Network,including vertex:Activity edge:The order of execution is expressed during work. Ex:Vi→Vj: Vj is worked after Vi. 36

  37. Topologicalorder V2 V5 Ans: V1, V2, V3, V4, V5, V6 (more than one answer) V1 V3 V6 Giving a AOV Network without cycle, it can be found out at least one vertex visiting sequences. Satisfied conditions: In AOV Network, if Vi is the pre-guide of Vj(it means that there is a path from Vi to Vj), Vi will definitely appear before Vj in this order. V4 37

  38. Design ideas of DataStructure: the calculation of finding Topological order includes: 1. deciding a vertex whether it has a pre-guide. 2. deleting a vertex and its connecting edges. In a array of Vertex[1…n], adding a column of Count to record the in-degree value of each vertex. 38

  39. Question : (1) What is the spanning tree of a graph ? (2) Please list all the possible spanning tree. 39

  40. Ans: (1)Letting G=Graph=(V,E)is a connection undirected graph, and let T become a set of edges which have been traced. So S=(V,T) is one of G’s spanningtrees. These situations should be satisfied by following conditions: 1. E=T+B,Bdoes not pass through the set of edge. 2. Taking any edge from B into S can form a cycle. 3. In S, any simple path among vertices is unique. 40

  41. (2) 41

  42. 42

  43. Reference • Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed〝Fundamentals of Data Structures in C〞, W. H. Freeman & Co Ltd, 1992. • Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta〝Fundamentals of Data Structures in C++〞 Silicon Pr, 2006 • Richard F.Gilberg, Behrouz A. Forouzan, 〝Data Structures: A Pseudocode Approach with C〞, SBaker & Taylor Books, 2004 • Fred Buckley, and Marty Lewinter 〝A Friendly Introduction to Graph Theory〞Prentice Hall, 2002 • 〝資料結構-使用C語言〞蘇維雅譯,松崗,2004 • 〝資料結構-使用C語言〞 蔡明志編著,全華,2004 • 〝資料結構(含精選試題)〞洪逸編著,鼎茂,2005 43

More Related