Presentation is loading. Please wait.

Presentation is loading. Please wait.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.

Similar presentations


Presentation on theme: "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type."— Presentation transcript:

1 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type 6.2 Elementary Graph Operations 6.3 Minimum Cost Spanning Trees 6.4 Shortest Paths and Transitive Closure 6.5 Activity Networks

2 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-2 Chapter 6 Graphs Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

3 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-3 Chapter 6 Graphs 6.1 The Graph Abstract Data Type structure Graph is objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graph  Graph, v, v 1, and v 2  Vertices Graph Create() ::= return an empty graph Graph InsertVertex(graph, v) ::= return a graph with v inserted. Graph InsertEdge(graph, v 1, v 2 ) ::= return a graph with a new edge (v 1, v 2 ). Graph DeleteVertex(graph, v) ::= return a graph with v and all its incident edges removed. Graph DeleteEdge(graph, v 1, v 2 ) ::= return a graph with the edge (v 1, v 2 ) removed. Boolean IsEmpty(graph) ::= if (graph==empty) return TRUE else return FALSE List Adjacent(graph, v) ::= return a list of all vertices adjacent to v. p. 263

4 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-4 Chapter 6 Graphs 6.1.1 Introduction 1736, Euler Koenigberg Bridge Problem: Euler showed: there is a walk starting at any vertex, going through each edge exactly once and terminating at the start vertex iff the degree of each vertex is even called Eulerian walk. the degree of a vertex: # of edges incident to a vertex J. R. Newman: the world of Mathematics, 1956, p.573-580 C A B D g c d e b f a attaching

5 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-5 Chapter 6 Graphs A Graph, G = (V, E), consists of two sets V and E V: finite non-empty set of vertices E: set of pairs of vertices, edges, e.g.(1,2) or  1,2  Note: V(G): set of vertices of graph G E(G): set of edges of graph G Undirected Graph – the pair of vertices representing any edge is unordered. Thus, the pairs (V 1,V 2 ) and (V 2,V 1 ) represent the same edge. Directed Graph – each edge is represented by a directed pairs  V 1, V 2  Note:  V 1, V 2  and  V 2, V 1  represent two different edges. 6.1.2 Definitions V1V1 V2V2 1 2 3 V(G) = {1, 2, 3} E(G) = {  1,2   2,3   3,1  }

6 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-6 Chapter 6 Graphs  Examples for Graph 0 12 3 0 12 3 4 5 6 G1G1 G2G2 0 1 2 G3G3 V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G 3 )={0,1,2} E(G 3 )={  0,1 ,  1,0 ,  1,2  } Examples for Graph Note: Graph G2 is also a tree,  Tree is a special case of graph.

7 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-7 Chapter 6 Graphs (a) 2 1 0 3 (b) 02 1 Graph with a self edge Multigraph: multiple occurrences of the same edge Examples for Graphlike Structure Not consider as a graph in this book

8 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-8 Chapter 6 Graphs A Complete Graph is a graph that has the maximum number of edges For undirected graph with n vertices, the maximum number of edges is n(n-1)/2 For directed graph with n vertices, the maximum number of edges is n(n-1) Example: G1 is a complete graph Complete Graph 0 12 3 n(n-1)/2 = 6

9 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-9 Chapter 6 Graphs If (v0, v1) is an edge in an undirected graph, Adjacent: v0 and v1 are adjacent Incident: The edge (v0, v1) is incident on vertices v0 and v1 If is an edge in a directed graph Adjacent: v0 is adjacent to v1, and v1 is adjacent from v0 Incident: The edge is incident on v0 and v1 Adjacent and Incident 0 1 2 G3G3 0 12 3 The vertices adjacent to vertex 2: 0, 1 and 2 The edge incident on vertex 2: (0,2), (1,2) and (2,3) The vertices adjacent to vertex 1: 0, The edge incident on vertex 1:, and

10 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-10 Chapter 6 Graphs Subgraph A subgraph of G is a graph G’ → V(G’)  V(G) E(G’)  E(G) e.g. some of the subgraphs of G1: 0 0 12 3 12 0 12 3 (i) (ii) (iii) (iv) G1G1 0 12 3

11 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-11 Chapter 6 Graphs 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) 0 1 2 G3G3 Subgraph (cont.)  e.g. some of the subgraphs of G3: If G is connected with n vertices then its connected subgraph at least has n-1 edges. Why?

12 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-12 Chapter 6 Graphs Path: from v p to v q in G is a sequence of vertices v p, v i1, v i2,...,v in, v p  (v p, v i1 ), (v i1, v i2 ),..., (v in, v q )  E(G) if G is undirected or  v p, v i1 ,  v i1, v i2 ,...,  v in, v q   E(G) if G is directed eg. Length: The length of a path is the number of edges on it eg. Length of path 1,2,4 is 2 1 23 4 - 1,2,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Path

13 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-13 Chapter 6 Graphs Simple Path: is a path in which all vertices except possibly the first and last are distinct. e.g.1. 1,2,4 is a simple path, path: 1,2,4,2 is not a simple path. e.g.2. 1 2 3 1,2,3,1 also a simple path with length 3. 1,2,3,1,2? path, length 4, not simple. 1,2,3,1,3 not a path. 1 23 4 - 1,2,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Simple Path

14 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-14 Chapter 6 Graphs Cycle Cycle: is a simple path, first and last vertices are same. eg. 1,2,3,1. Note: Directed cycle, Directed path. Acyclic graph 1 2 3 0 12 3 4 5 6 G2G2 tree (acyclic graph)

15 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-15 Chapter 6 Graphs Connected Two vertices V 1 and V 2 are Connected: if in an undirected graph G,  a path in G from V 1 to V 2 (or from V 2 to V 1 ∵ undirected)  Strongly Connected V 1, V 2 are Strongly Connected: if in a directed graph (digraph) G’,  a path in G from V 1 to V 2 and also from V 2 to V 1  Graph (Strongly) Connected graph connected (graph strongly connected) if  V i, V j  V(G),  a path from V i to V j in G strongly connected 不是 strongly connected 1 23 4

16 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-16 Chapter 6 Graphs Connected Component Connected Component (Strongly Connected Component): is a maximal connected subgraph. eg. for graph for digraph 1 2 3 4 Two connected components of G1 1 2 1 2 3 Two strongly connected components of G2 1 23 4 3 G2 G1

17 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-17 Chapter 6 Graphs Degree Degree of Vertex is the # of edges incident to that vertex eg. In Direct Graph If G has n vertices and e edge, d i = degree of V i, then 3 degree of V 3 = 3 in-degree out-degree in-degree=1 out-degree=2 degree=3

18 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-18 Chapter 6 Graphs Network A graph with weighted edges is called. eg. 台北 花蓮 高雄 台中 新竹 90120 100 210 150 Diagraph G = (V,E) Network N = (G,W) is a diagraph G together with a real valued for w: E  R. R is a real number w(u,v) is the weight of edge (arc) (u,v)  E.

19 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-19 Chapter 6 Graphs  Method 1: Adjacency Matrix  Method 2: Adjacency Lists  Method 3: Adjacency Multilists 6.1.3 Graph Representations

20 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-20 Chapter 6 Graphs Adjacency Matrix Let G = (V, E) with n vertices, n  1. The adjacency matrix of G is a 2-dimensional n  n matrix, A A(i, j) = 1 iff (v i, v j ) (  v i, v j  for a diagraph)  E(G), A(i, j) = 0 otherwise. 6.1.3.1 Adjacency Matrix eg. 1 23 4 101004 210013 100012 201101 degree = d i 4321 - symmetric - space need n 2 bits, but half can be saved 6 / 2 = 3... edges A

21 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-21 Chapter 6 Graphs Adjacency Matrix: Examples 0 1 2 111 00002 21011 10100 210 - not be symmetric out-degree = in-degree = d j Eg. 2.

22 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-22 Chapter 6 Graphs  Examples for Adjacency Matrix 1 0 2 3 4 5 6 7 G4G4 Adjacency Matrix: Examples

23 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-23 Chapter 6 Graphs Questions: How many edges e are there in G? Determine the # of edges in G? Is G connected? Answers: Need check entries of the matrix.  check n 2 – n time.  spend O(n 2 ) time to determine e Note: << n 2 /2 (n: Diagonal entries are zero, need not check it) Adjacency Matrix: Determine Edges e = 7 << n 2 /2 = 32

24 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-24 Chapter 6 Graphs Adjacency Lists Adjacency matrices for G 3 Adjacency list: edge e  2e nodes. Easy random access for any particular vertex List Orthogonal list 0 1 2 0 1 0 101 000 V0V0 V1V1 V2V2 0  10 020 V0V0 V1V1 V2V2 0  10 200 Head Nodes (sequential) 6.1.3.2 Adjacency Lists G3G3

25 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-25 Chapter 6 Graphs [0] [1] [2] [3] 1 23 023 013 0 12 An undirected graph with n vertices and e edges  n head nodes and 2e list nodes 0 12 3 Adjacency Lists: Example G 1

26 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-26 Chapter 6 Graphs [0] [1] [2] [3] [4] [5] [6] [7] 1 2 0 3 0 3 12 5 4 6 57 6 1 0 2 3 4 5 6 7 G4G4 Adjacency Lists: Example G 4

27 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-27 Chapter 6 Graphs  Number of nodes in a graph: = the number of nodes in adjacency list  Number of edges in a graph = determined in O(n+e)  Out-degree of a vertex in a directed graph = the number of nodes in its adjacency list  In-degree of a vertex in a directed graph: ??? traverse the whole data structure 0 1 2 V0V0 V1V1 V2V2 0 10 200 G3G3 Adjacency Lists: Interesting Operations n vertices# of edges vs. O(n 2 ) in adjacency matrix

28 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-28 Chapter 6 Graphs  [0] [1] [2] 1 NULL 0 NULL 1 NULL  determine in-degree of a vertex in a fast way. 0 1 2  Inverse Adjacency List Adjacency Lists: Inverse Adjacency Lists

29 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-29 Chapter 6 Graphs tail head column link for head row link for tail Adjacency Lists: Orthogonal Representation  Orthogonal representation Sparse Matrix ??? 0 1 2 0 1 2 0 1 1 0 1 2 head node

30 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-30 Chapter 6 Graphs 6.1.3.3 Adjacency Multilists  Adjacency Multilists ( 以 edge 為主 ) mark vertex1 vertex2 path1 path2 0 12 3 N1 N2 N3 N4 N5N6 vertex 0: N1  N2  N3 vertex 1: N1  N4  N5 vertex 2: N2  N4  N6 vertex 3: N3  N5  N6 [0] [1] [2] [3] 0 1 N2 N4 0 2 N3 N4 0 3 N5 1 2 N5 N6 1 3 N6 2 3 N1 N2 N3 N4 N5 N6 edge(0,1) edge(0,2) edge(0,3) edge(1,2) edge(1,3) edge(2,3)

31 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-31 Chapter 6 Graphs Adjacency Multilists (cont.) 0 12 3 N1 N2 N3 N4 N5N6 vertex 0: N1  N2  N3 vertex 1: N1  N4  N5 vertex 2: N2  N4  N6 vertex 3: N3  N5  N6 mark vertex1 vertex2 Link 1 for V 1 Link 2 for V 2  Node Structures [0] [1] [2] [3] 0 1 N2 N4 0 2 N3 N4 0 3 N5 1 2 N5 N6 1 3 N6 2 3 N1 N2 N3 N4 N5 N6 edge(0,1) edge(0,2) edge(0,3) edge(1,2) edge(1,3) edge(2,3)

32 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-32 Chapter 6 Graphs Adjacency MultiLists vs. Adjacency List Adjacency Multilists ( 以 edge 為主 ) e=6  6 nodes Adjacency List 0 12 3 0 12 3 N1 N2 N3 N4 N5N6

33 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-33 Chapter 6 Graphs Traversal Given G = (V, E) and vertex v, find or visit all w  V, such that w connects v. Method 1: Depth First Search (DFS)  preorder tree traversal Method 2: Breadth First Search (BFS)  level order tree traversal Connected Components (Application 1 of Graph traversal) Spanning Trees (Application 2 of Graph traversal) Minimum Cost Spanning Tree (Application 3 of Graph traversal) … 6.2 Elementary Graph Operations 1 0 2 3 4 5 6 7 G4G4

34 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-34 Chapter 6 Graphs Graph Traversal Given: an undirected graph G = (V, E), a vertex v  V(G) Interested in: visiting all vertices connected to v. (reachable) Approach: 1. Depth First Search (DFS). 2. Breadth First Search (BFS). 1 23 45 6 DFS: 1,2,4,5,3,6 BFS: 1,2,3,4,5,6 4 2 1 stack 123 Queue Graph Operations: Graph Traversal

35 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-35 Chapter 6 Graphs 0 1 2 3 4 5 6 7 [0] [1] [2] [3] [4] [5] [6] [7] 1 2 0 3 0 5 17 27 3 4 6 17 2 7 4 5 6 Example 6.2.1 Depth First Search Adjacency Lists

36 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-36 Chapter 6 Graphs  Example 6.1: Depth first search  v 0, v 1, v 3, v 7, v 4, v 5, v 2, v 6  Program 6.1, p.273 0 1 2 3 4 5 6 7 [0] [1] [2] [3] [4] [5] [6] [7] 1 2 0 3 0 5 17 27 3 4 6 17 2 7 4 5 6 Depth First Search: Example 0 1 3 7 stack 0 1 3 7 5 2 4

37 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-37 Chapter 6 Graphs Depth First Search Procedure DFS(v) (* Array VISITED(n)  0 initially and VISITED(i)  1, if has visited*) VISITED(v)  1 print(v) for each vertex w adjacent to v do if VISITED(w) = 0 then call DFS(w) end end DFS Depth First Search: Algorithm VISITED 1 2 3 4 5 6 7 8 1 23 4567 8 Output  DFS: 1,2,4,8,5,6,3,7 V1V2V3V1V2V3 V2V4V5V2V4V5 V4V8V4V8 V8V5V6V7V8V5V6V7 V5V5 V6V3V6V3 V3V7V3V7 V7V7

38 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-38 Chapter 6 Graphs Time complexity: Adjacency list Time complexity: Adjacency matrix 128 1 2 3 8 1 2 3 4 5 6 7 8 230 1450 e edges 2e nodes 1670 Depth First Search: Time Complexity Adjacency matrix: O(n 2 ) n: # of vertex Adjacency list: O(e) e: # of edges

39 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-39 Chapter 6 Graphs  Example 6.2: Breadth first search  v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7  Program 6:2, p.275 0 1 2 3 4 5 6 7 [0] [1] [2] [3] [4] [5] [6] [7] 1 2 0 3 0 5 17 27 3 4 6 17 2 7 4 5 6 6.2.2 Breadth First Search

40 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-40 Chapter 6 Graphs Breadth First Search procedure BFS(v) VISITED (v)  1 print (v) initialize Q with v while Q not =  do call DELETE Q(v,Q) for all vertices w adjacent to v do if VISITED (w)=0 then [ call ADDQ(w,Q); VISITED(w)  1, print(w)] end end end BFS 12345678 1 visited V… Breadth First Search: Algorithm Q

41 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-41 Chapter 6 Graphs eg. V1V1 V2V2 V3V3 V4V4 V5V5 V6V6 V7V7 V8V8 Q V1V1 V2V2 V3V3 V4V4 V5V5 V6V6 V7V7 V8V8 Print: V 1, V 2, V 3, V 4, V 5, V 6, V 7, V 8. Breadth First Search: Example

42 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-42 Chapter 6 Graphs Time complexity: by using Adjacency Matrix 12345678 101100000 210011000 30 40 50 60 70 80 while for n n Breadth First Search: Time Complexity O(n 2 )

43 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-43 Chapter 6 Graphs Time complexity: by using Adjacency List: Total: d 1 + d 2 +...+ d n = O(e) where d i = degree (v i )  while for d i : degree of (v i ) didi n Breadth First Search: Time Complexity O(e)

44 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-44 Chapter 6 Graphs Application 1. Finding components of a graph Application 2. Finding a spanning tree of a connected graph Application 3. Minimum cost spanning tree Applications of Graph Traversal

45 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-45 Chapter 6 Graphs Procedure COMP(G) (* determine the connected components of G *) for i  1 to n do VISITED(i)  0 for i  1 to n do if VISITED(i) = 0 then call DFS(i); print(“||”) end end COMP Determine connected graph Invoke DFS or BFS Application 1. Finding Components of a Graph 6.2.3 Connected Components 1 23 4 5 67 8 DFS: 1 2 4 3 || 5 6 7 8 || BFS: 1 2 3 4 || 5 6 7 8 ||... 8 3 4...21 VISITED 1 1

46 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-46 Chapter 6 Graphs Application 2. Finding a Spanning Tree of a Connected Graph Def. Application 2.1– obtaining circuit equations for an electrical network Application 2.2 – minimum cost spanning tree Def. Spanning Tree Any tree consisting only edges in G and including all vertices in G is called. i.e. minimal subgraph G’ of G, such that V(G’) =V(G) and G’ is connected. 6.2.4 Spanning Trees

47 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-47 Chapter 6 Graphs eg. How to find it? Modify BFS Procedure BFS(v) If VISITTED(w)=0 thenIf VISITTED(w)=0 then 1.call ADDQ(w,Q) 1. 2.VISITTED(w)←1 2. 3.print(w) 3. 4.T = T ∪ {(v, w)}...(How many?) Ans : 16 ( EX.) Spanning Trees (cont.)

48 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-48 Chapter 6 Graphs Modify Procedure DFS :同 modify Procedure BFS e.g. Application 1– obtaining circuit equations for an electrical network 1 23 4 1 23 4 BFS: 1,2,3,4 1 23 4 DFS: 1,2,4,3 BF spanning treeDF spanning tree Spanning Trees (cont.)

49 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-49 Chapter 6 Graphs BFS Spanning DFS Spanning Tree vs. BFS Spanning Tree 0 1 2 3 4 5 6 7 DFS Spanning 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7


Download ppt "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type."

Similar presentations


Ads by Google