Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2000-2009 Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.

Similar presentations


Presentation on theme: "Copyright 2000-2009 Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer."— Presentation transcript:

1 Copyright 2000-2009 Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer Science Press, 2008 Fall 2009 Course, Sungkyunkwan University Hyunseung Choo choo@ece.skku.ac.kr

2 Fall 2009 Data Structures The Graph Abstract Data Type Introduction the bridges of Koenigsberg c d e ab f g A Kneiphof C B D c ab d e g f C A B D (a)(b) Networking Laboratory 2/48

3 Fall 2009 Data Structures The Graph Abstract Data Type Def) A graph G = (V,E) where V(G): nonempty finite set of vertices, E(G): finite set of edges possibly empty  undirected graph: unordered (v i,v j ) = (v j,v i )  directed graph ordered:  0 3 12 G1G1 0 21 4563 G2G2 0 1 2 G3G3 Networking Laboratory 3/48

4 Fall 2009 Data Structures The Graph Abstract Data Type restrictions on graphs  no edge from a vertex, i, back to itself (no self loop) - not allowed (v i,v i ) or  no multiple occurrences of the same edge (  multigraph) examples of a graph with feedback loops and a multigraph 02 1 1 2 3 0 (a)(b) Networking Laboratory 4/48

5 Fall 2009 Data Structures The Graph Abstract Data Type complete graph  the maximum number of edges  undirected graph with n vertices max number of edges = n(n-1)/2  directed graph with n vertices max number of edges = n(n-1) adjacent  v i and v j are adjacent if (v i,v j )  E(G) adjacent to(from) for digraphs  : a directed edge  vertex v 0 is adjacent to vertex v 1  vertex v 1 is adjacent from vertex v 0 incident  an edge e = (v i,v j ) is incident on vertices v i and v j Networking Laboratory 5/48

6 Fall 2009 Data Structures The Graph Abstract Data Type subgraph G’ of G  V(G’)  V(G) and E(G’)  E(G) 0 0 12 3 12 0 3 12 (i)(ii)(iii)(iv) some of the subgraphs of G 1 0 3 12 G1G1 Networking Laboratory 6/48

7 Fall 2009 Data Structures The Graph Abstract Data Type path (from vertex v p to vertex v q ): a sequence of vertices, v p,v i1,v i2,···,v in,v q such that (v p,v i1 ), (v i1,v i2 ), ···, (v in,v q ) are edges in an undirected graph or,,···, are edges in a directed graph length of path: # of edges on the path 0 0 1 0 1 2 0 1 2 (i) (ii) (iii)(iv) some of the subgraphs of G 3 0 1 2 G3G3 Networking Laboratory 7/48

8 Fall 2009 Data Structures The Graph Abstract Data Type Simple path: a path in which all vertices, except possibly the first and the last, are distinct Cycle: a path in which the first and last vertices are the same  simple cycle for directed graph: add the prefix “directed” to the terms cycle and path  simple directed path  directed cycle, simple directed cycle Connected  vertex v 0 and v 1 is connected, if there is a path from v 0 to v 1 in an undirected graph G  an undirected graph is connected if, for every pair of vertices v i and v j, there is a path from v i to v j Networking Laboratory 8/48

9 Fall 2009 Data Structures connected component (of an undirected graph)  maximal connected subgraph a graph with two connected components 0312 H1H1 4567 G4G4 H2H2 The Graph Abstract Data Type Networking Laboratory 9/48

10 Fall 2009 Data Structures strongly connected (in a directed graph)  for every pair of vertices v i, v j in V(G) there is a directed path from v i to v j and also from v j to v i strongly connected directed graph strongly connected component  maximal subgraph that is strongly connected strongly connected components of G 3 4 3 1 2 01 2 The Graph Abstract Data Type Networking Laboratory 10/48

11 Fall 2009 Data Structures degree (of a vertex): number of edges incident to that vertex in-degree (of a vertex v): number of edges that have v as the head (for directed graphs) out-degree (of a vertex v): number of edges that have v as the tail (for directed graphs) special types of graphs  tree: an acyclic connected graph  bipartite graph  planar graph  complete graph The Graph Abstract Data Type Networking Laboratory 11/48

12 Fall 2009 Data Structures Graph Representations Adjacency matrix  G = (V,E) with |V| = n(  1)  two-dimensional n  n array, say adj_mat[ ][ ]  adj_mat[i][j] =  “1” if (v i,v j ) is adjacent  “0” otherwise  space complexity: S(n) = n 2  symmetric for undirected graphs  asymmetric for directed graphs Networking Laboratory 12/48

13 Fall 2009 Data Structures adjacency matrices for G 1, G 3, and G 4 G1G1 G3G3 G4G4 Graph Representations Networking Laboratory 13/48

14 Fall 2009 Data Structures Graph Representations Adjacency lists  replace n rows of adjacency matrix with n linked lists  every vertex i in G has one list #define MAX_VERTICES 50 typedef struct node *node_ptr; typdef struct node { int vertex; node_ptr link; }; node_ptr graph[MAX_VERTICES]; int n = 0; /* vertices currently in use */ vertexlink Networking Laboratory 14/48

15 Fall 2009 Data Structures 10 1 2 3 2 02 01 01 headnodevertexlink 3 3 3 2 0 1 2 0 1 2 G1G1 G3G3 Graph Representations Networking Laboratory 15/48

16 Fall 2009 Data Structures adjacency lists for G 1, G 3, and G 4 0 1 2 3 4 5 6 7 12 03 03 12 5 46 57 6 G4G4 Graph Representations Networking Laboratory 16/48

17 Fall 2009 Data Structures Graph Representations Inverse adjacency lists  useful for finding in-degree of a vertex in digraphs  contain one list for each vertex  each list contains a node for each vertex adjacent to the vertex that the list represents inverse adjacency list for G 3 0 1 2 0 1 1 Networking Laboratory 17/48

18 Fall 2009 Data Structures Orthogonal representation  change the node structure of the adjacency lists orthogonal representation for graph G 3 tailheadcolumn link for headrow link for tail 012 0 1 2 0 0 1 1 2 headnodes (shown twice) 1 Graph Representations Networking Laboratory 18/48

19 Fall 2009 Data Structures vertices may appear in any order alternate order adjacency list for G 1 30 1 2 3 1 20 30 21 headnodevertexlink 2 3 1 0 Graph Representations Networking Laboratory 19/48

20 Fall 2009 Data Structures weighted edges  assign weights to edges of a graph  distance from one vertex to another, or  cost of going from one vertex to an adjacent vertex  modify representation to signify an edge with the weight of the edge  for adj matrix : weight instead of 1  for adj list : weight field network  a graph with weighted edges Graph Representations Networking Laboratory 20/48

21 Fall 2009 Data Structures graph traversals  visit every vertex in a graph  what order?  DFS(Depth First Search): similar to a preorder tree traversal  BFS(Breath First Search): similar to a level-order tree traversal v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 (a) Elementary Graph Operations Networking Laboratory 21/48

22 Fall 2009 Data Structures graph G and its adjacency lists (b) Elementary Graph Operations Networking Laboratory 22/48

23 Fall 2009 Data Structures DFS (depth first search)  easy to implement recursively  stack  a global array visited[MAX_VERTICES]  initialized to FALSE  change visited[i] to TRUE when a vertex i is visited Elementary Graph Operations #define FALSE 0 #define TRUE 1 short int visited[MAX_VERTICES]; Networking Laboratory 23/48

24 Fall 2009 Data Structures EX) v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 0 1 2 3 4 5 6 7 v0v0 v1v1 v0v0 v7v7 v3v3 v1v1 v0v0 v4v4 v7v7 v3v3 v1v1 v0v0 v7v7 v3v3 v1v1 v0v0 visited: Elementary Graph Operations Networking Laboratory 24/48

25 Fall 2009 Data Structures time complexity for dfs()  time complexity for adj list representation: O(e)  time complexity for adj matrix representation: O(n 2 ) Elementary Graph Operations void dfs(int v) { /* depth first search of a graph beginning with vertex v */ node_ptr w; visited[v] = TRUE; printf(“%5d”, v); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) dfs(w->vertex); } Networking Laboratory 25/48

26 Fall 2009 Data Structures BFS(breadth first search)  use a dynamically linked queue  each queue node contains vertex and link fields Elementary Graph Operations typedef struct queue *queue_ptr; typedef struct queue { int vertex; queue_ptr link; }; void insert(queue_ptr *, queue_ptr *, int); void delete(queue_ptr *); Networking Laboratory 26/48

27 Fall 2009 Data Structures v0v0 v1v1 v2v2 v2v2 v3v3 v4v4 v3v3 v4v4 v5v5 v6v6 v4v4 v5v5 v6v6 v7v7 v1v1 v2v2 v3v3 front v5v5 v6v6 v7v7 v4v4 v6v6 v7v7 v5v5 v7v7 v6v6 v7v7 Elementary Graph Operations Networking Laboratory 27/48

28 Fall 2009 Data Structures time complexity for bfs()  time complexity for adj list: O(e)  time complexity for adj matrix: O(n 2 ) Elementary Graph Operations void bfs(int v) { node_ptr w; queue_ptr front, rear; front = rear = NULL;/* initialize queue */ printf(“%5d”, v); visited[v] = TRUE; insert(&front, &rear, v); while (front) { v = delete(&front); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) { printf(“%5d”, w->vertex); add(&front, &rear, w->vertex); visited[w->vertex] = TRUE; } } } Networking Laboratory 28/48

29 Fall 2009 Data Structures connected components  determine whether or not an undirected graph is connected - simply calling dfs(0) or bfs(0) and then determine if there are unvisited vertices  list the connected components of a graph - make repeated calls to either dfs(v) or bfs(v) where v is an unvisited vertex  time complexity: O(n+e)  total time by dfs: O(e),  for loop: O(n) Elementary Graph Operations void connected(void) { /* determine the connected components of a graph */ int i; for (i = 0; i < n; i++) if (!visited[i]) { dfs(i); printf(“\n”); } Networking Laboratory 29/48

30 Fall 2009 Data Structures spanning trees when graph G is connected dfs or bfs implicitly partitions the edges in G into two sets:  T(for tree edges): set of edges used or traversed during the search  N(for nontree edges): set of remaining edges  edges in T form a tree that includes all vertices of G Def) A spanning tree is any tree that consists solely of edges in G and that include all the vertices in G Elementary Graph Operations Networking Laboratory 30/48

31 Fall 2009 Data Structures a complete graph and three spanning trees depth first spanning tree  use dfs to create a spanning tree breadth first spanning tree  use bfs to create a spanning tree Elementary Graph Operations Networking Laboratory 31/48

32 Fall 2009 Data Structures dfs and bfs spanning trees properties of spanning trees 1) if we add a nontree edge into a spanning tree  cycle 2) spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected 3) |E(G’)| = n - 1 where |V(G)| = n  minimum cost spanning trees v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 (a) dfs(0) spanning tree(b) bfs(0) spanning tree Elementary Graph Operations Networking Laboratory 32/48

33 Fall 2009 Data Structures biconnected components and articulation points (cut-points) articulation point  a vertex v of G  deletion of v, together with all edges incident on v, produce a graph, G’, that has at least two connected components biconnected graph  a connected graph that has no articulation points v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 Example of biconnected graph Elementary Graph Operations Networking Laboratory 33/48

34 Fall 2009 Data Structures  a connected graph which is not biconnected  articulation points are 1,3,5,7  biconnected component  maximal biconnected subgraph, H, of connected undirected graph G  two biconnected components of the same graph have no more than one vertex in common  no edge can be in two or more biconnected components of a graph  biconnected components of a graph G partition the edges of G 0 1 23 4 8 7 5 9 6 Elementary Graph Operations Networking Laboratory 34/48

35 Fall 2009 Data Structures a connected graph and its biconnected components 0 1 23 4 8 7 5 9 6 0 1 1 23 4 7 5 6 8 77 9 35 (a) connected graph (b) biconnected components Elementary Graph Operations Networking Laboratory 35/48

36 Fall 2009 Data Structures depth first number, or dfn  sequence in which the vertices are visited during the depth first search  if u is an ancestor of v in the df spanning tree, dfn(u) < dfn(v) 0 1 23 4 8 7 5 9 6 1 05 6 7 984 3 2 0 3 45 26 17 098 1 2 3 4 5 6 7 89 (a) depth first spanning tree dfs(3) (b) Elementary Graph Operations Networking Laboratory 36/48

37 Fall 2009 Data Structures cost of a spanning tree of a weighted undirected graph  sum of the costs(weights) of the edges in the spanning tree  a spanning tree of least cost  Kruskal’s, Prim’s, and Sollin’s algorithms  greedy method: construct an optimal solution in stages, make the best decision at each stage using some criterion for spanning tree: least cost criterion  use only edges within the graph  use exactly n-1 edges  may not use edges that would produce a cycle Minimum Cost Spanning Trees Networking Laboratory 37/48

38 Fall 2009 Data Structures (1) Kruskal’s algorithm  select the edges for inclusion in T in nondecreasing order of their cost  an edge is added to T if it does not form a cycle with the edges that are already in T  exactly n-1 edges are selected Minimum Cost Spanning Trees Networking Laboratory 38/48

39 Fall 2009 Data Structures Kruskal’s algorithm Minimum Cost Spanning Trees T = {}; while (T contains less than n-1 edges && E is not empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) does not create a cycle in T) add (v,w) to T; else discard (v,w); } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory 39/48

40 Fall 2009 Data Structures choosing a least cost edge(v,w) from E  a min heap  determine and delete the next least cost edge: O(log 2 e)  construction of the heap: O(e) checking that the new edge,(v,w), does not form a cycle in T  use the union-find operations Minimum Cost Spanning Trees Networking Laboratory 40/48

41 Fall 2009 Data Structures 0 1 562 4 3 28 16 12 18 22 25 24 14 10 (a) 0 1 562 4 3 (b) 0 1 562 4 3 10 (c) stages in Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 41/48

42 Fall 2009 Data Structures 0 1 562 4 3 12 10 (d) 0 1 562 4 3 12 14 10 (e) 0 1 562 4 3 16 12 14 10 (f) stages in Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 42/48

43 Fall 2009 Data Structures 0 1 562 4 3 16 12 22 14 10 (g) 0 1 562 4 3 16 12 22 25 14 10 (h) Minimum Cost Spanning Trees stages in Kruskal’s algorithm Networking Laboratory 43/48

44 Fall 2009 Data Structures summary of the Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 44/48

45 Fall 2009 Data Structures (2) Prim’s algorithm  begins with a tree, T, that contains a single vertex  add a least cost edge (u,v) to T such that T  {(u,v)} is also a tree and repeat this step until T contains n-1 edges  set of selected edges at each stage of the algorithm  form a tree in Prim’s alg.  form a forest in Kruskal’s alg. Minimum Cost Spanning Trees Networking Laboratory 45/48

46 Fall 2009 Data Structures Prim’s algorithm Minimum Cost Spanning Trees T = {}; TV = {0}; /* start with vertex 0 and no edge*/ while (T contains fewer than n-1 edges) { let (u,v) be a least cost edge such that u  TV and v  TV; if (there is no such edge) break; add v to TV; add (u,v) to T; } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory 46/48

47 Fall 2009 Data Structures 0 1 562 4 3 10 0 1 562 4 3 25 10 0 1 562 4 322 25 10 (a)(b)(c) stages in Prim’s algorithm Minimum Cost Spanning Trees Networking Laboratory 47/48

48 Fall 2009 Data Structures Minimum Cost Spanning Trees 0 1 562 4 3 12 22 25 10 0 1 562 4 3 16 12 22 25 10 0 1 562 4 3 16 12 22 25 14 10 (d)(e)(f) stages in Prim’s algorithm Networking Laboratory 48/48


Download ppt "Copyright 2000-2009 Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer."

Similar presentations


Ads by Google