Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
IS 2610: Data Structures Graph April 5, 2004.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
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.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Foundations of Discrete Mathematics
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Data Structures & Algorithms Graphs
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures 13th Week
Unit 3 Graphs.
Graph Algorithms Using Depth First Search
Graph Operations And Representation
Minimum-Cost Spanning Tree
Graphs Chapter 11 Objectives Upon completion you will be able to:
Minimum-Cost Spanning Tree
2018, Fall Pusan National University Ki-Joune Li
Graph Operations And Representation
Minimum-Cost Spanning Tree
2017, Fall Pusan National University Ki-Joune Li
Graphs Chapter 7 Visit for more Learning Resources.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graph Operations And Representation
Chapter 9 Graph algorithms
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Chapter 6 Graphs

2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

3 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Undirected edge has no orientation (u,v). Directed edge has an orientation (u,v). uv uv

4 Graph Undirected graph => no oriented edge Directed graph => every edge has an orientation Proper graph  No loops  No multi-edges

5 Undirected Graph

6 Directed Graph (Digraph)

7 Examples of Graphlike Structures (a) Graph with a self edge (b) Multigraph

8 Applications — Communication Network Vertex = city, edge = communication link

9 Driving Distance/Time Map Vertex = city, edge weight = driving distance/time. Weighted undirected graph

10 Street Map Some streets are one way

Terminologies Vertex, edge, directed, undirected, weighted, unweighted Adjacenct Subgraph Path  A path from u to v in graph G is a sequence of vertices u,i 1, i 2,…,j k, v such that (u, i 1 )…(i k, v) are edges in E(G).  The length of a path is the number of edges on it. Simple path  All vertices except possibly the first and last are distinct Cycle  A simple path whose first and last vertices are the same

12 G 1 and G 3 Subgraphs (a) Some subgraphs of G 1 (a) Some subgraphs of G 3 (i) (ii) (iii) (iv) (i) (ii) (iii) (iv)

13 Terminologies Undirected graph  Connected Vertices Graph  Connected component Maximal connected subgraph  degree

14 Terminologies Directed graph  Strongly connected  Strongly connected component A maximal subgraph that is strongly connected  indegree  outdegree Acyclic  Cycle free graph

15 Complete Undirected Graph Has all possible edges. n = 1 n = 2 n = 3 n = 4

16 Number Of Edges — Undirected Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is the same as edge (v,u), the number of edges in a complete undirected graph is n(n-1)/2. Number of edges in an undirected graph is <= n(n-1)/2.

17 Number Of Edges — Directed Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is not the same as edge (v,u), the number of edges in a complete directed graph is n(n-1). Number of edges in a directed graph is <= n(n-1).

18 Sum Of Vertex Degrees Sum of degrees = 2e (e is number of edges)

19 Sum Of In- And Out-Degrees each edge contributes 1 to the in-degree of some vertex and 1 to the out-degree of some other vertex sum of in-degrees = sum of out-degrees = e, where e is the number of edges in the digraph

20 Graph Representation Adjacency Matrix Adjacency Lists

21 Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i,j) = 1 iff (i,j) is an edge

22 Adjacency Matrix Properties Diagonal entries are zero. Adjacency matrix of an undirected graph is symmetric.  A(i,j) = A(j,i) for all i and j.

23 Adjacency Matrix (Digraph) Diagonal entries are zero. Adjacency matrix of a digraph need not be symmetric.

24 Adjacency Matrix n 2 bits of space For an undirected graph, may store only lower or upper triangle (exclude diagonal).  (n-1)n/2 bits O(n) time to find vertex degree and/or vertices adjacent to a given vertex.

25 Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3)

26 Linked Adjacency Lists Each adjacency list is a chain aList[1] aList[5] [2] [3] [4] Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

27 Weighted Graphs Cost adjacency matrix.  C(i,j) = cost of edge (i,j) Adjacency lists => each list element is a pair (adjacent vertex, edge weight)

28 Elementary graph operations Visit a Graph (graph search) Connected components Spanning trees Biconnected components

29 Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u

30 Graph Search Methods A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v

31 Graph Search Methods Many graph problems solved using a search method.  Path from one vertex to another.  Is the graph connected?  Find a spanning tree.  Etc. Commonly used search methods:  Depth-first search.  Breadth-first search.

32 Depth-First Search Example Start search at vertex Label vertex 1 and do a depth first search from either 2 or Suppose that vertex 2 is selected.

33 Depth-First Search Example Label vertex 2 and do a depth first search from either 3, 5, or Suppose that vertex 5 is selected.

34 Depth-First Search Example Label vertex 5 and do a depth first search from either 3, 7, or Suppose that vertex 9 is selected.

35 Depth-First Search Example Label vertex 9 and do a depth first search from either 6 or Suppose that vertex 8 is selected.

36 Depth-First Search Example Label vertex 8 and return to vertex From vertex 9 do a DFS(6). 6

37 Depth-First Search Example Label vertex 6 and do a depth first search from either 4 or Suppose that vertex 4 is selected.

38 Depth-First Search Example Label vertex 4 and return to From vertex 6 do a DFS(7). 7

39 Depth-First Search Example Label vertex 7 and return to Return to 9.

40 Depth-First Search Example Return to 5.

41 Depth-First Search Example Do a DFS(3). 3

42 Depth-First Search Example Label 3 and return to Return to 2.

43 Depth-First Search Example Return to 1.

44 Depth-First Search Example Return to invoking method.

45 Depth-First Search DFS() { for (i=0 to n-1) visited[i]=0; for (v=0 to n-1) { if (visted[v]==0) DFV(v); } DFV(v) { visited[v]=1; //Label vertex v as visited. for ( each unvisited vertex u adjacent from v ) DFV(u); }

46 Depth-First Search Property All vertices reachable from the start vertex (including the start vertex) are visited. Time complexity  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

47 Breadth-First Search Visit start vertex and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.

48 Breadth-First Search Example Start search at vertex

49 Breadth-First Search Example Visit/mark/label start vertex and put in a FIFO queue FIFO Queue 1

50 Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue 1

51 Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

52 Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

53 Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

54 Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

55 Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

56 Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

57 Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

58 Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

59 Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

60 Breadth-First Search Example Remove 6 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

61 Breadth-First Search Example Remove 6 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

62 Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

63 Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

64 Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

65 Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

66 Breadth-First Search Example Remove 8 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

67 Breadth-First Search Example Queue is empty. Search terminates FIFO Queue

BFS(int v) { for (i=0 to n-1) visited[i]=0; queue_insert(v); visited[v]=1; while (queue is not empty) { v=queue_delete(); for each unvisited vertex w adjacent to v { queue_insert(w); visited[w]=1; } 68

69 BFS() { for (i=0 to n-1) visited[i]=0; while (there is any unvisted node v) { queue_insert(v); visited[v]=1; while (queue is not empty) { v=queue_delete(); for each unvisited vertex w adjacent to v { queue_insert(w); visited[w]=1; }

70 Breadth-First Search Properties Same complexity as DFS. Time complexity  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

71 Is The Graph Connected? Start a depth-first search at any vertex of the graph. Graph is connected iff all n vertices get visited. Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

72 Connected Components Start a depth-first search at any as yet unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited.

73 Connected Components

74 Connected component DFS() { for (i=0 to n-1) visited[i]=0; i=0; for (v=0 to n-1) { if (visted[v]==0) { output (connected component i); i++; DFV(v); } DFV(v) { visired[v]=1; //Label vertex v as visited. for ( each unreached vertex u adjacenct from v ) DFV(u); }

75 Time Complexity  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

76 Spanning Tree Depth-first search from vertex 1. Depth-first spanning tree

77 Spanning Tree Start a depth-first search at any vertex of the graph. If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (depth-first spanning tree). Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)  Breadth-first spanning tree

78 Biconnected Components Definition: A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components. Definition: A biconnected graph is a connected graph that has no articulation points. Definition: A biconnected component of a connected graph G is a maximal biconnected subgraph H of G. By maximal, we mean that G contains no other subgraph that is both biconnected and properly contains H.

79 A Connected Graph and Its Biconnected Components (a) A connected graph (b) Its biconnected components Maximal without articulation point

80 Biconnected Components (Cont.) Two biconnected components of the same graph can have at most one vertex in common. No edge can be in two or more biconnected components. The biconnected components of G partition the edges of G.

81 Biconnected Components (Cont.) The biconnected components of a connected, undirected graph G can be found by using any depth-first spanning tree of G. A nontree edge (u, v) is a back edge with respect to a spanning tree T iff either u is an ancestor of v or v is an ancestor of u. A nontree edge that is not back edge is called a cross edge. No graph can have cross edges with respect to any of its depth-first spanning trees.

82 Biconnected Components (Cont.) The root of the depth-first spanning tree is an articulation point iff it has at least two children. Any other vertex u is an articulation point iff it has at least one child, w, such that it is not possible to reach an ancestor of u using a path composed solely of w, descendants of w, and a single back edge. ( w 必經之 vertex)

83 Define low(w) as the lowest depth-first number that can be reached from w using a path of descendants followed by, at most, one back edge. Use descendent ’ s back edge Use a back edge connecting with w

84 Depth-First Spanning Tree

85 dfn and low values for the Spanning Tree vertex dfn low Min{4,5,dfn(3)=1} Min{8,9,dfn(5)=6} Min{7,min{6,10,9}}=min{7,6}: use descendent back edge Articulation points: 1, 4, 5, 7 where it (u) has children w, low(w) >=dfn(u)

86 u is an articulation point iff u is either the root of the spanning tree and has two or more children or u is not the root and u has a child w such that low(w) ≥ dfn(u). implying this descendent w cannot use another way going back to the parent of u

87 Biconnected void Biconnected() { num = 1; //num is an int data member of graph dfn = new int[n]; //dfn is declared as int* in Graph low = new int[n]; //low is declared as int* in Graph set entries in dfn and low to 0; Biconnected(0,-1); //start at vertex 0 }

88 Biconnected void biconnected(const int u, const int v) {//compute dfn and low, and output the edges of G by their biconnected //components. v is the parent(if any) of u in the resulting spanning tree. //s is an initially empty stack declared as a data member of Graph. dfn[u] = low[u] = num++; for(each vertex w adjacent from u){//actual code uses an iterator if((v != w)&&(dfn[w] < dfn[u]) ) add(u, w) to stack s; if(dfn[w] == 0) { //w is an unvisited vertex Biconnected(w, u); low[u] = min(low[u],low[w]); if(low[w] >= dfn[u]){ cout<<“New Biconnected Component: ” << endl; do{ delete an edge from the stack s; let this edge be (x, y); cout<<x<<“,”<<y<<endl; }while((x, y) and (u, w) are not the same edge) } else if(w != v) low[u] = min(low[u], dfn[w]); //back edge }

89 Famous Graph Problems Bipartite graph Minimum Spanning tree Shortest path problem  Single pair shorted path  All pair shorted path

90 Bipartite graph Two colorable

91 Bipartite graph DFS() { for (i=0 to n-1) visited[i]=0; for (v=0 to n-1) { if (visted[v]==0) paint(v,0); } output (bipartite graph); } paint(v,c) { visired[v]=1; //Label vertex v as visited. color[v]=c; //Set vertex v ’ s color as c. for ( each vertex u adjacenct from v ) if (visited[u] and color[u]!=1-c) { output (not a bipartite graph!); exit(1); } else if (!visited[u]) paint(u,1-c); }

92 Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum cost

93 Example Network has 10 edges. Spanning tree has only n - 1 = 7 edges. Need to either select 7 edges or discard

94 Edge Selection Strategies Start with an n-vertex 0-edge forest. Consider edges in ascending order of cost. Select edge if it does not form a cycle together with already selected edges.  Kruskal ’ s method. Start with a 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge.  Prim ’ s method.

95 Kruskal ’ s Method Start with a forest that has no edges Consider edges in ascending order of cost. Edge (1,2) is considered first and added to the forest.

96 Kruskal ’ s Method Edge (7,8) is considered next and added Edge (3,4) is considered next and added. 4 Edge (5,6) is considered next and added. 6 Edge (2,3) is considered next and added. 7 Edge (1,3) is considered next and rejected because it creates a cycle.

97 Kruskal ’ s Method Edge (2,4) is considered next and rejected because it creates a cycle Edge (3,5) is considered next and added Edge (3,6) is considered next and rejected. 7 Edge (5,7) is considered next and added. 14

98 Kruskal ’ s Method n - 1 edges have been selected and no cycle formed. So we must have a spanning tree. Cost is 46. Min-cost spanning tree is unique when all edge costs are different

99 Pseudocode For Kruskal ’ s Method Start with an empty set T of edges. while (E is not empty && |T| != n-1) { Let (u,v) be a least-cost edge in E. E = E - {(u,v)}. // delete edge from E if ((u,v) does not create a cycle in T) Add edge (u,v) to T. } if (| T | == n-1) T is a min-cost spanning tree. else Network has no spanning tree.

100 Prim ’ s Method Start with any single vertex tree Get a 2-vertex tree by adding a cheapest edge. 6 6 Get a 3-vertex tree by adding a cheapest edge Grow the tree one edge at a time until the tree has n - 1 edges (and hence has all n vertices)

101 Program 6.7: Prim`s algorithm //Assume that G has at least one vertex. TV = {0};//start with vertex 0 and no edges for(T = Φ; T contains fewer than n-1 edges; add(u, v) to T) { 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; } if(T contains fewer than n-1 edges) cout<<“no spanning tree”<<endl;

102 Minimum-Cost Spanning Tree Methods Can prove that all stated edge selection/rejection result in a minimum-cost spanning tree. Prim ’ s method is fastest.  O(n 2 ) using an implementation similar to that of Dijkstra ’ s shortest-path algorithm.  O(e + n log n) using a Fibonacci heap. Kruskal ’ s uses union-find trees to run in O(n + e log e) time.

103 Dijkstra ’ s Algorithm Assumes no negative-weight edges. Maintains a set S of vertices whose SP from s has been determined. Repeatedly selects u in V–S with minimum SP estimate (greedy choice). Store V–S in priority queue Q. Initialize(G, s); S :=  ; Q := V[G]; while Q   do u := Extract-Min(Q); S := S  {u}; for each v  Adj[u] do Relax(u, v, w) od Initialize(G, s); S :=  ; Q := V[G]; while Q   do u := Extract-Min(Q); S := S  {u}; for each v  Adj[u] do Relax(u, v, w) od

104 Example 0    s uv x y

105 Example 0  5  10 s uv x y

106 Example s uv x y

107 Example s uv x y

108 Example s uv x y

109 Example s uv x y