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
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
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
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-4 Chapter 6 Graphs 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 C A B D g c d e b f a attaching
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 Definitions V1V1 V2V V(G) = {1, 2, 3} E(G) = { 1,2 2,3 3,1 }
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-6 Chapter 6 Graphs Examples for Graph G1G1 G2G 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.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-7 Chapter 6 Graphs (a) (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
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 n(n-1)/2 = 6
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 G3G 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
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: (i) (ii) (iii) (iv) G1G
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-11 Chapter 6 Graphs (i) (ii) (iii) (iv) 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?
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,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Path
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,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 ,2,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Simple Path
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 G2G2 tree (acyclic graph)
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
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 Two connected components of G Two strongly connected components of G G2 G1
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
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-18 Chapter 6 Graphs Network A graph with weighted edges is called. eg. 台北 花蓮 高雄 台中 新竹 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.
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 Graph Representations
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 Adjacency Matrix eg degree = d i symmetric - space need n 2 bits, but half can be saved 6 / 2 = 3... edges A
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-21 Chapter 6 Graphs Adjacency Matrix: Examples not be symmetric out-degree = in-degree = d j Eg. 2.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-22 Chapter 6 Graphs Examples for Adjacency Matrix G4G4 Adjacency Matrix: Examples
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
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 V0V0 V1V1 V2V2 0 V0V0 V1V1 V2V2 0 Head Nodes (sequential) Adjacency Lists G3G3
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-25 Chapter 6 Graphs [0] [1] [2] [3] An undirected graph with n vertices and e edges n head nodes and 2e list nodes Adjacency Lists: Example G 1
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-26 Chapter 6 Graphs [0] [1] [2] [3] [4] [5] [6] [7] G4G4 Adjacency Lists: Example G 4
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 V0V0 V1V1 V2V G3G3 Adjacency Lists: Interesting Operations n vertices# of edges vs. O(n 2 ) in adjacency matrix
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 Inverse Adjacency List Adjacency Lists: Inverse Adjacency Lists
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 ??? head node
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-30 Chapter 6 Graphs Adjacency Multilists Adjacency Multilists ( 以 edge 為主 ) mark vertex1 vertex2 path1 path 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)
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-31 Chapter 6 Graphs Adjacency Multilists (cont.) 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)
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 N1 N2 N3 N4 N5N6
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 G4G4
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) DFS: 1,2,4,5,3,6 BFS: 1,2,3,4,5, stack 123 Queue Graph Operations: Graph Traversal
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-35 Chapter 6 Graphs [0] [1] [2] [3] [4] [5] [6] [7] Example Depth First Search Adjacency Lists
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 [0] [1] [2] [3] [4] [5] [6] [7] Depth First Search: Example stack
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 Output DFS: 1,2,4,8,5,6,3,7 V1V2V3V1V2V3 V2V4V5V2V4V5 V4V8V4V8 V8V5V6V7V8V5V6V7 V5V5 V6V3V6V3 V3V7V3V7 V7V7
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-38 Chapter 6 Graphs Time complexity: Adjacency list Time complexity: Adjacency matrix 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
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 [0] [1] [2] [3] [4] [5] [6] [7] Breadth First Search
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 visited V… Breadth First Search: Algorithm Q
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
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-42 Chapter 6 Graphs Time complexity: by using Adjacency Matrix while for n n Breadth First Search: Time Complexity O(n 2 )
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 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)
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
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 Connected Components DFS: || || BFS: || || VISITED 1 1
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 Spanning Trees
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)← print(w) 3. 4.T = T ∪ {(v, w)}...(How many?) Ans : 16 ( EX.) Spanning Trees (cont.)
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 BFS: 1,2,3, DFS: 1,2,4,3 BF spanning treeDF spanning tree Spanning Trees (cont.)
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-49 Chapter 6 Graphs BFS Spanning DFS Spanning Tree vs. BFS Spanning Tree DFS Spanning