Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Graphs

2 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 to walk across all bridges once and return to the initial land area? The answer is no, but how do we prove that?

3 Konigsberg = Graph Problem The Konigsberg is an instance of a graph problem The Konigsberg is an instance of a graph problem Definition of a graph: Definition of a graph: Graph G: Consists of two sets, V and E Graph G: Consists of two sets, V and E V: A finite, non-empty set of vertices V: A finite, non-empty set of vertices E: A set of pairs of vertices, where the pairs are called edges. E: A set of pairs of vertices, where the pairs are called edges.

4 Example Graphs 0 12 3 V: 0, 1, 2, 3 E: (0,1), (0,2), (0,3) (1,2), (1,3), (2,3) 0 12 3 V: 0, 1, 2, 3 E: Empty 0 12 3 V: 0, 1, 2, 3 E: (0,1), (0,2), (1,3) Trees are a subset of graphs

5 Graph Definitions Undirected graph: Pair of vertices representing any edge is unordered Undirected graph: Pair of vertices representing any edge is unordered (u,v) is the same edge as (v,u) (u,v) is the same edge as (v,u) Directed graph: Each edge is represented by a directed pair (u,v) Directed graph: Each edge is represented by a directed pair (u,v) Drawn with an arrow from u to v indicating the direction of the edge Drawn with an arrow from u to v indicating the direction of the edge (u,v) is not the same edge as (v,u) (u,v) is not the same edge as (v,u)

6 Directed vs. Undirected 0 12 3 0 12 3 0 12 3 Graph A: Undirected (1,0) is a valid edge Same as (0,1) edge Graph B: Directed Not equivalent to A (1,0) not valid edge Graph C: Directed Equivalent to A (1,0) is a valid edge A (0,1) edge also exists

7 Graph Restrictions For now lets assume vertices and edges are sets For now lets assume vertices and edges are sets No self edges (vertice back to itself) No self edges (vertice back to itself) No repeated edges (multigraph) No repeated edges (multigraph) 0 12 3 Repeated Self

8 Motivating Problem: Graph Restrictions Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): 0 1 3 2 This is the appropriate graph representation. We’re not going to solve it for now because of our assumption of no repeated edges.

9 Graph Definitions Maximum possible number of distinct unordered pairs (u,v) (undirected graph) in a graph with n vertices is n*(n-1) / 2. Maximum possible number of distinct unordered pairs (u,v) (undirected graph) in a graph with n vertices is n*(n-1) / 2. A graph with this many edges is called a complete graph. A graph with this many edges is called a complete graph. 0 12 3 Complete: 6 edges = (4 * 3) / 2 0 12 3 Not Complete: (4*3)/2 != 4 edges

10 Graph Definitions Directed Graph: Directed Graph: Maximum of (n * (n-1)) edges. [Twice that for undirected because 2 directed are equivalent to one undirected] Maximum of (n * (n-1)) edges. [Twice that for undirected because 2 directed are equivalent to one undirected] Proof of (n * (n – 1)) bounds: n nodes, can point to every other node except for themselves n-1 edges connecting to each of the n nodes

11 Graph Definitions If (u, v) is an edge in E(G), If (u, v) is an edge in E(G), Vertices u and v are called adjacent Vertices u and v are called adjacent The edge (u,v) is called incident on vertices u and v. The edge (u,v) is called incident on vertices u and v. Examples: Examples: 0 12 3 Vertex 0 is adjacent to 1 and 2 Vertex 1 is adjacent to 0, 2, and 3 Vertex 2 is adjacent to 0 and 1 Vertex 3 is adjacent to 1 Edges incident on vertex 2: (0,2), (1,2) Edges incident on vertex 3: (1,3)

12 Graph Definitions A subgraph of graph G called G’ is a graph such that V(G’) is a subset V(G) and E(G’) is a subset of E(G) A subgraph of graph G called G’ is a graph such that V(G’) is a subset V(G) and E(G’) is a subset of E(G) 0 12 3 Subgraphs 0 12 1 3 0 12

13 Graph Definitions A path from vertex u to vertex v in a graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) are edges in G. A path from vertex u to vertex v in a graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) are edges in G. If in a directed graph, the edges have to be in the right direction. If in a directed graph, the edges have to be in the right direction. The length of a path is the number of edges on the path. The length of a path is the number of edges on the path. A simple path is a path in which all vertices except possibly the first and last are distinct. A simple path is a path in which all vertices except possibly the first and last are distinct.

14 Graph Definitions 0 12 3 Paths from 1 to 3: (1,3) 1,3 [Simple] Length = 1 (1,2), (2,3)1,2,3 [Simple] Length = 2 (1,0),(0,2),(2,1),(1,3)1,0,2,1,3 Length = 4 (1,2),(2,0),(0,1),(1,3)1,2,0,1,3 Length = 4 (1,0),(0,2),(2,3)1,0,2,3 [Simple] Length = 3 Many more that repeat internally, Not simple, Length > 4

15 Graph Definitions A cycle is a simple path where the first and last vertices are the same. A cycle is a simple path where the first and last vertices are the same. 0 12 3 Cycles to 1: 1,0,2,3,1 1,0,2,1 1,3,2,1 1,2,1 1,0,1 1,3,1

16 Graph Definitions Two vertices u and v are connected if there is a path in G from u to v. Two vertices u and v are connected if there is a path in G from u to v. An undirected graph is said to be connected (at the graph level) if and only if for every pair of distinct vertices u and v in V(G) there is a path from u to v in G. An undirected graph is said to be connected (at the graph level) if and only if for every pair of distinct vertices u and v in V(G) there is a path from u to v in G. A connected component of a graph is a maximal connected subgraph A connected component of a graph is a maximal connected subgraph

17 Graph Definitions 0 12 3 4 56 7 Graph G4: V(G4): 0,1,2,3,4,5,6,7 E(G4): (0,1), (0,2), (1,3), (2,3), (4,5), (5,6), (6,7) There are two connected components of G4: H1 (0-3) and H2 (4-7) Verify that H1 and H2 components are connected: Path between all pairs of vertices Directed graphs – different because paths are directed, harder to get connected components

18 Graph Definitions A tree is a connected, acyclic graph A tree is a connected, acyclic graph For any node there is path to any other node (usually back through a “parent node”) For any node there is path to any other node (usually back through a “parent node”) Acyclic property forces a unique path between nodes Acyclic property forces a unique path between nodes 0 14 32

19 Graph Definitions Need a corollary to “connected” for directed graphs Need a corollary to “connected” for directed graphs A directed graph G is strongly connected if for every pair of distinct vertices in V(G), u and v, there is a directed path from u to v and from v to u. A directed graph G is strongly connected if for every pair of distinct vertices in V(G), u and v, there is a directed path from u to v and from v to u. A strongly connected component is a maximal subgraph of a directed graph that is strongly connected A strongly connected component is a maximal subgraph of a directed graph that is strongly connected

20 Graph Definitions 0 1 Graph G5: V(G5): 0,1,2 E(G5): (0,1), (1,0), (1,2) G5 is not strongly connected (No path from 2 to 1) There are two strongly connected components of G4: H1 (0-1) and H2 (2) Verify that H1 and H2 components are strongly connected: Directed path between all pairs of vertices 2

21 Graph Definitions The degree of a vertex v is the number of edges incident to that vertex. The degree of a vertex v is the number of edges incident to that vertex. For a directed graph, For a directed graph, The in-degree of a vertex v is the number of edges for which the arrow points at v. The in-degree of a vertex v is the number of edges for which the arrow points at v. The out-degree is defined as the number of edges for which the arrow points away from v. The out-degree is defined as the number of edges for which the arrow points away from v.

22 Graph Definitions 0 12 3 Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree 3 0 12 0 Out-Degree : 1 In-Degree: 0 1 Out-Degree : 1 In-Degree: 0 2 Out-Degree : 0 In-Degree: 2

23 Graph Definitions For undirected graph whose vertices v_i have degree d_i, the number of edges, |E|, is (the sum from 0 to n- 1 of degree_i ) / 2 For undirected graph whose vertices v_i have degree d_i, the number of edges, |E|, is (the sum from 0 to n- 1 of degree_i ) / 2 Essentially just counting the edges. Essentially just counting the edges. Divide by 2 because double counting (if node1, node2 share an edge, that edge is in both of their degrees) Divide by 2 because double counting (if node1, node2 share an edge, that edge is in both of their degrees) Useful for computing max number of edges if you only know number of vertices and their degree (ie they are all binary => |E| = 2 * |V| / 2 = |V|) Useful for computing max number of edges if you only know number of vertices and their degree (ie they are all binary => |E| = 2 * |V| / 2 = |V|) Given our graph assumptions (no repeated edges, no self edges) degree has to be <= |V|-1 Given our graph assumptions (no repeated edges, no self edges) degree has to be <= |V|-1

24 Graph Definitions 0 12 3 Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree 3 |E| = Sum of Degrees / 2 = (2+2+3+3)/2 = 10/2 = 5 {Correct!} 0 12 |E| max = Sum of Degrees / 2 = (2 + 2 + 2) / 2 = 6/2 = 3 {Correct!} |E| Max = ?, All Binary

25

26 Graph Representations What core functionality do we need in representation? What core functionality do we need in representation? Set of vertices Set of vertices Set of edges Set of edges Two major representations: Two major representations: Adjacency matrix [Array based] Adjacency matrix [Array based] Adjacency list [Linked List based] Adjacency list [Linked List based]

27 Adjacency Matrix G = (V,E) graph with |V| = n, n >= 1 G = (V,E) graph with |V| = n, n >= 1 Adjacency matrix: Adjacency matrix: 2 dimensional n x n array called A with property that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise. 2 dimensional n x n array called A with property that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise. 0 12 3 V0V1V2V3 V00110 V11001 V21001 V30110

28 Adjacency Matrix Directed Graphs: Rows are the out indicators (data in a row indicates that there is an outgoing link) Directed Graphs: Rows are the out indicators (data in a row indicates that there is an outgoing link) 0 12 3 V0V1V2V3 V00010 V11001 V20000 V30010

29 Adjacency Matrix Note how the adjacency matrix for an undirected graph is symmetric. Can save approximately half the space by storing only upper triangle or lower triangle Note how the adjacency matrix for an undirected graph is symmetric. Can save approximately half the space by storing only upper triangle or lower triangle 0 12 3 V0V1V2V3 V00110 V11001 V21001 V30110

30 Adjacency Matrix 0 12 3 V0V1V2V3 V00110 V11001 V21001 V30110 Given a complete adjacency matrix, can easily: Determine if there is an edge between any two vertices (look in appropriate column) [Undirected Graph] Compute degree of a node (sum over row)

31 Adjacency Matrix Given a complete adjacency matrix, can easily: [Directed Graph] Compute out degree of a node (sum over row) Compute in degree of a node (sum over column) 0 12 3 V0V1V2V3 V00010 V11001 V20000 V30010

32 Adjacency Matrix What if we want to compute a non-trivial answer? What if we want to compute a non-trivial answer? How many total edges are there in the graph? How many total edges are there in the graph? Is the graph connected? Is the graph connected? Total edges: Requires O(n^2) operations Total edges: Requires O(n^2) operations (n^2 entries – n [diagonals always 0]) (n^2 entries – n [diagonals always 0]) 0 12 3 V0V1V2V3 V00110 V11001 V21001 V30110

33 Adjacency Matrix What if we have a sparse graph? What if we have a sparse graph? Sparse = Very few connections out of all possible Sparse = Very few connections out of all possible 0 12 3 4 56 7 V0V0V0V0 V1V1V1V1 V2V2V2V2 V3V3V3V3 V4V4V4V4 V5V5V5V5 V6V6V6V6 V7V7V7V7 V001100000 V110010000 V210010000 V301100000 V400000100 V500001010 V600000101 V700000010

34 Adjacency Matrix Would really like to do O(|E|) operations when counting edges Would really like to do O(|E|) operations when counting edges O(n^2) is a given when using adjacency matrix O(n^2) is a given when using adjacency matrix For dense graphs, |E| is close to n^2 For dense graphs, |E| is close to n^2 Not for sparse graphs (|E| << n^2) Not for sparse graphs (|E| << n^2) Solution: Use linked lists and store only those edges that are really represented in the graph (no 0’s for things that aren’t present). Solution: Use linked lists and store only those edges that are really represented in the graph (no 0’s for things that aren’t present). Slightly more complicated to implement but saves a lot of time Slightly more complicated to implement but saves a lot of time

35 Adjacency List N rows of adjacency matrix (vertices) are represented as n linked lists. N rows of adjacency matrix (vertices) are represented as n linked lists. Nodes in list i are those nodes adjacent to the corresponding vertex v_i. Nodes in list i are those nodes adjacent to the corresponding vertex v_i. 0 12 3 0 1 2 3 12 30 03 12 Order in linked list doesn’t matter Array of Head Node Pointers

36 Adjacency List Undirected Graph: n vertices, e edges – Requires n head nodes, 2 * e list nodes Undirected Graph: n vertices, e edges – Requires n head nodes, 2 * e list nodes For any vertex, computing degree (number of incident edges) is counting size of corresponding list. For any vertex, computing degree (number of incident edges) is counting size of corresponding list. Number of edges for whole graph is computed in O(n + e) << O(n^2) Number of edges for whole graph is computed in O(n + e) << O(n^2)

37 Adjacency List for Directed Graph N rows of adjacency matrix (vertices) are represented as n linked lists. N rows of adjacency matrix (vertices) are represented as n linked lists. Nodes in list i are those nodes that one can reach from leaving the corresponding vertex Nodes in list i are those nodes that one can reach from leaving the corresponding vertex 0 1 2 3 2 30 2 Order in linked list doesn’t matter Array of Head Node Pointers 0 1 2 3

38 Adjacency List For a directed graph, nodes in a list are those that you can reach leaving from the corresponding vertex For a directed graph, nodes in a list are those that you can reach leaving from the corresponding vertex Computing out degree for any vertex – Count number of nodes in corresponding list Computing out degree for any vertex – Count number of nodes in corresponding list Computing number of total edges in graph: Adding all outdegrees => O(n + e) [visit all head nodes and all items in lists] O(n + e) [visit all head nodes and all items in lists] << O(n^2)

39 Adjacency List For directed graphs, this approach isn’t very useful for determining in-degree For directed graphs, this approach isn’t very useful for determining in-degree This was trivial in an adjacency matrix (sum down a column) This was trivial in an adjacency matrix (sum down a column) You can build the inverse adjacency list at the same time building adjacency list. You can build the inverse adjacency list at the same time building adjacency list. 0 1 2 3 1 03 1 Array of Head Node Pointers 0 1 2 3

40 Primitive Graph Operations In essence, can just think of a graph as a container, holding edges and vertices (with a lot of special properties): In essence, can just think of a graph as a container, holding edges and vertices (with a lot of special properties): class Graph {public: Graph(); // create an empty graph void InsertVertex(Vertex v); // Insert v into graph with no incident edges void InsertEdge(Vertex u, Vertex v); // Insert edge (u,v) into graph void DeleteVertex(Vertex v); // Delete v and all edges incident to it void DeleteEdge(Vertex u, Vertex v); Delete edge (u,v) from graph bool IsEmpty(); // if graph has no vertices return true List Adjacent(Vertex v); // return list of all vertices adjacent to vertex v }

41 Adjacency Matrix/List Construction Note we will be updating these data structures as we call our graph class methods: Note we will be updating these data structures as we call our graph class methods: InsertVertex(Vertex v) InsertVertex(Vertex v) InsertEdge(Vertex u, Vertex v) InsertEdge(Vertex u, Vertex v) DeleteVertex(Vertex v) DeleteVertex(Vertex v) DeleteEdge(Vertex u, Vertex v) DeleteEdge(Vertex u, Vertex v) Linked list approach likely more useful over arrays if don’t know beforehand what adding to graph (how many edges, etc). Linked list approach likely more useful over arrays if don’t know beforehand what adding to graph (how many edges, etc).

42 Weighted Edges Often see weighted edges on graphs Often see weighted edges on graphs Common uses: Common uses: Distance between vertices Distance between vertices Cost of moving from one vertex to another vertex Cost of moving from one vertex to another vertex Think of vertices as cities – could be real distances or flight costs and want to know shortest/cheapest path Think of vertices as cities – could be real distances or flight costs and want to know shortest/cheapest path How do we incorporate these weights into our graph representations? How do we incorporate these weights into our graph representations?

43 Weighted Edges 01 2 3 Mehran Ilam Sarableh Ivan 60 40 55 50 Could store weights in adjacency matrix (just need a non-zero entry) 0 1 2 3 0 0 60 40 55 1 60 0 0 0 2 40 0 0 50 3 55 0 50 0 If using lists, add another field to node

44 Elementary Graph Operations First Operation: Traversal of graphs First Operation: Traversal of graphs Already saw how this worked in binary trees (inorder, preorder, postorder, depth-order) Already saw how this worked in binary trees (inorder, preorder, postorder, depth-order) Similar idea for general graphs Similar idea for general graphs Given a graph G = (V,E) and a vertex v in V(G), visit all vertices in G that are reachable from v Given a graph G = (V,E) and a vertex v in V(G), visit all vertices in G that are reachable from v This is the subset of the graph that is connected to v. This is the subset of the graph that is connected to v.

45 Graph Traversal Depth-First Search Depth-First Search Similar to descending down binary tree Similar to descending down binary tree Basic algorithm: Basic algorithm: Begin by starting at vertex v. Begin by starting at vertex v. Select an edge from v, say it’s the edge (v,w). Select an edge from v, say it’s the edge (v,w). Move to vertex w and recurse. Move to vertex w and recurse. When a node has been visited that has all of its adjacent vertices visited, back up to the last node with an unvisited adjacent vertex and recurse. When a node has been visited that has all of its adjacent vertices visited, back up to the last node with an unvisited adjacent vertex and recurse.

46 DFS Traversal void Graph::DFS() // driver { visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; DFS(0); delete [] visited; } void Graph::DFS(const int v) // workhorse { visited[v] = true; for (each vertex w adjacent to v) //use adjacency matrix or lists if (!visited[w]) DFS(w); }

47 DFS Traversal – Graph To Traverse 0 12 34 56 7 1 2 3 4 5 6 7 012 034 056 17 17 27 27 3456

48 DFS Traversal 0 12 34 56 7 Order of Traversal: 0, 1, 3 7, 4, 5, 2, 6 Note that performing DFS can find connected components. In this case, the whole graph is connected and thus all nodes were visited.

49 Analysis of DFS – From a single node Running time dependent on graph structure and representation Running time dependent on graph structure and representation If graph G is represented by adjacency lists If graph G is represented by adjacency lists Determine vertices adjacent to vertex v by following chain of links. Determine vertices adjacent to vertex v by following chain of links. Each node in each lists is visited at most once, and there are 2*e list nodes. Thus, the running time is bounded by the number of edges. Each node in each lists is visited at most once, and there are 2*e list nodes. Thus, the running time is bounded by the number of edges. If graph G is represented by an adjacency matrix If graph G is represented by an adjacency matrix Have to look at n items (n = number of vertices) for each vertex (scanning down each row in the matrix) Have to look at n items (n = number of vertices) for each vertex (scanning down each row in the matrix) Running time is O(n*n). Running time is O(n*n).

50 Breadth First Traversal Similar to descending across levels of a binary tree. Similar to descending across levels of a binary tree. Visit the starting vertex v. Visit the starting vertex v. Visit all unvisited vertices directly adjacent to v. Visit all unvisited vertices directly adjacent to v. Recurse, visiting all unvisited vertices directly adjacent to those from the previous step. Recurse, visiting all unvisited vertices directly adjacent to those from the previous step.

51 BFS Traversal Algorithm void Graph::BFS(int v) { visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; visited[v] = true; Queue q; q.insert(v); while (!q.isEmpty()) { v = *q.Delete(v); for (all vertices w adjacent to v) if (!visited[w]) { q.insert(w); visited[w] = true; }} delete [] visited; }

52 BFS Traversal – Graph to Traverse 0 12 34 56 7 1 2 3 4 5 6 7 012 034 056 17 17 27 27 3456

53 BFS Traversal 0 12 34 56 7 Order of Traversal: 0, 1, 2, 3, 4, 5, 6, 7 Note that performing BFS can find connected components. In this case, the whole graph is connected and thus all nodes were visited.

54 Analysis of BFS Each visited vertex enters queue exactly once (n vertices). Each visited vertex enters queue exactly once (n vertices). Once in the queue, have to review list of neighbors. Once in the queue, have to review list of neighbors. For adjacency matrix, that list is n items long, meaning the total time is O(n*n) For adjacency matrix, that list is n items long, meaning the total time is O(n*n) For adjacency list, that list has degree(vertex) items, and the sum of the degrees for all n vertices is O(e), so total cost is bounded by number of edges For adjacency list, that list has degree(vertex) items, and the sum of the degrees for all n vertices is O(e), so total cost is bounded by number of edges Same cost as DFS Same cost as DFS

55 Connected Components How do we find all connected components? How do we find all connected components? Calling BFS or DFS will find a connected component Calling BFS or DFS will find a connected component Those vertices connected to the start node Those vertices connected to the start node To find all connected components, To find all connected components, Select a start vertex, call DFS Select a start vertex, call DFS Select another start vertex which hasn’t been visited by a previous DFS, call DFS Select another start vertex which hasn’t been visited by a previous DFS, call DFS Repeat until all vertices have been visited. Repeat until all vertices have been visited.

56 Connected Components Algorithm void Graph::Components() { visited = new bool[n]; for (int i = 0; i < n; i++) visited = false; for (i = 0; i < n; i++) { if (!visited[i]) { DFS(i); outputComponent(); } } delete [] visited; }

57 Analysis of Connected Components Using adjacency lists, Using adjacency lists, Any call to DFS is O(e’) where e’ is the set of edges present in the particular connect component the start node is in. Any call to DFS is O(e’) where e’ is the set of edges present in the particular connect component the start node is in. Sum over e’ has to equal |E|, the total number of edges Sum over e’ has to equal |E|, the total number of edges For loop itself takes O(n) time and calls DFS for non-visited nodes For loop itself takes O(n) time and calls DFS for non-visited nodes DFS not called every time as mark many nodes with each DFS DFS not called every time as mark many nodes with each DFS Total time: O(n+e) Total time: O(n+e) With adjacency matrix, O(n*n) [have to look at all columns for all rows] With adjacency matrix, O(n*n) [have to look at all columns for all rows]

58 Spanning Trees If a graph G is connected, a DFS or BFS starting at any vertex visits all vertices in G. If a graph G is connected, a DFS or BFS starting at any vertex visits all vertices in G. There are a set of particular edges that are traversed during this process. There are a set of particular edges that are traversed during this process. Let T be the set of edges that are traversed, and N be the remaining edges. Let T be the set of edges that are traversed, and N be the remaining edges.

59 BFS Traversal 0 12 34 56 7 Order of Traversal: 0, 1, 2, 3, 4, 5, 6, 7 Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6) (3,7) Not traversed[N]: (4,7), (5,7), (6,7)

60 A Spanning Tree Algorithm Not hard to record traversed edges in BFS (or DFS) – add to a dynamic list whenever encounter new edge. void Graph::BFS(int v) { visited = new bool[n]; LinkedList tEdgeList = new LinkedList(); for (int i = 0; i < n; i++) visited[i] = false; visited[v] = true; Queue q; q.insert(v); while (!q.isEmpty()) { v = *q.Delete(v); for (all vertices w adjacent to v) if (!visited[w]) { q.insert(w);tEdgeList.append(v,w); visited[w] = true; } } delete [] visited; }

61 Spanning Trees This set of edges is called a spanning tree for the graph. This set of edges is called a spanning tree for the graph. A spanning tree is any tree that consists solely of edges from G and that includes all vertices in G (the tree “spans” all vertices of the graph). A spanning tree is any tree that consists solely of edges from G and that includes all vertices in G (the tree “spans” all vertices of the graph). Spanning trees are not unique. Spanning trees are not unique. Generated from DFS – called depth-first spanning tree Generated from DFS – called depth-first spanning tree Generated from BFS – called breadth-first spanning tree Generated from BFS – called breadth-first spanning tree

62 BFS Spanning Tree 0 12 34 56 7 Order of Traversal: 0, 1, 2, 3, 4, 5, 6, 7 0 12 34 56 7 Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6) (3,7) BFS Spanning Tree

63 DFS Spanning Tree 0 12 34 56 7 Order of Traversal: 0, 1, 3 7, 4, 5, 2, 6 0 12 34 56 7 DFS Spanning Tree Edges traversed [T]: (0,1), (1,3), (3,7), (7,4), (7,5), (5,2), (2,6)

64 Spanning Trees Spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected Spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected Given all vertices from original graph, it is the smallest set of edges one needs for the graph to be connected (to be able to get from any one node to any other) Given all vertices from original graph, it is the smallest set of edges one needs for the graph to be connected (to be able to get from any one node to any other) Any connected graph with n vertices must have n-1 edges or more. Any connected graph with n vertices must have n-1 edges or more. Minimally, every vertex has incoming and outgoing (2n) except last and first (-2) => 2n-2. Every edge is counted twice (as its outgoing and incoming) so divide by 2 => [(2n-2)/2] = n-1 Minimally, every vertex has incoming and outgoing (2n) except last and first (-2) => 2n-2. Every edge is counted twice (as its outgoing and incoming) so divide by 2 => [(2n-2)/2] = n-1 Thus, a spanning tree has exactly n-1 edges (it is the minimal subgraph of G that is connected). Thus, a spanning tree has exactly n-1 edges (it is the minimal subgraph of G that is connected).

65 Applications of Spanning Trees Very useful in determining optimal connections Very useful in determining optimal connections Example: Communication networks (laying out cable): Assume vertices are cities and edges are communication links between cities. Example: Communication networks (laying out cable): Assume vertices are cities and edges are communication links between cities. All possible spanning trees are all the possible ways you could set up communication links so that everyone could talk to everyone else. All possible spanning trees are all the possible ways you could set up communication links so that everyone could talk to everyone else. Usually interested in finding the cheapest set of links: Requires setting weights (costs) on links and finding “minimum spanning tree” [will see algorithm soon!] Usually interested in finding the cheapest set of links: Requires setting weights (costs) on links and finding “minimum spanning tree” [will see algorithm soon!] Interesting problem – there are potentially many spanning trees? How do you find the minimal efficiently (can’t look at them all!) Interesting problem – there are potentially many spanning trees? How do you find the minimal efficiently (can’t look at them all!)

66 Finding Minimum Cost Spanning Trees Given a weighted directed graph and a spanning tree for the graph, define the cost of the spanning tree as the sum of the weights of the trees’ edges. Given a weighted directed graph and a spanning tree for the graph, define the cost of the spanning tree as the sum of the weights of the trees’ edges. The minimum cost spanning tree is the tree with minimal cost (the smallest sum over edge weights). The minimum cost spanning tree is the tree with minimal cost (the smallest sum over edge weights). 3 different efficient algorithms for finding minimum spanning tree 3 different efficient algorithms for finding minimum spanning tree

67 Greedy Algorithms All 3 algorithms are greedy: All 3 algorithms are greedy: Work in stages Work in stages From the set of feasible decisions, make the best decision possible (given some metric of best) for current stage. From the set of feasible decisions, make the best decision possible (given some metric of best) for current stage. Can’t change mind later Can’t change mind later Feasible ensures that the solution will obey problem constraints Feasible ensures that the solution will obey problem constraints Repeat for rest of stages, given the decisions you have already made and what’s left to do. Repeat for rest of stages, given the decisions you have already made and what’s left to do. Best is usually defined as least cost or highest profit. Best is usually defined as least cost or highest profit. Useful in many other programming domains Useful in many other programming domains


Download ppt "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."

Similar presentations


Ads by Google