Graph Traversals and Minimum Spanning Trees 15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, 2003.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graph Traversals and Minimum Spanning Trees. Graph Terminology.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Discussion #36 Spanning Trees
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
1 Graphs: shortest paths & Minimum Spanning Tree(MST) Fundamental Data Structures and Algorithms Ananda Guna April 8, 2003.
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.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Lecture 10 Topics Application of DFS Topological Sort
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Minimum Spanning Trees
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Introduction DATA STRUCTURE – Graph electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
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.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
Graphs Upon completion you will be able to:
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
May 12th – Minimum Spanning Trees
CSE 2331/5331 Topic 9: Basic Graph Alg.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Introduction to Graphs
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Minimum Spanning Trees
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Fundamental Data Structures and Algorithms
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
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 .
Minimum Spanning Trees
Presentation transcript:

Graph Traversals and Minimum Spanning Trees : Fundamental Data Structures and Algorithms Rose Hoberman April 8, 2003

Announcements

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Announcements Readings: Chapter 14 HW5: Due in less than one week! Monday, April 14, 2003, 11:59pm

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Today More Graph Terminology (some review) Topological sort Graph Traversals (BFS and DFS) Minimal Spanning Trees After Class... Before Recitation

Graph Terminology

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Paths and cycles A path is a sequence of nodes v 1, v 2, …, v N such that (v i,v i+1 )  E for 0<i<N –The length of the path is N-1. –Simple path: all v i are distinct, 0<i<N A cycle is a path such that v 1 =v N –An acyclic graph has no cycles

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Cycles PIT BOS JFK DTW LAX SFO

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, More useful definitions In a directed graph: The indegree of a node v is the number of distinct edges (w,v)  E. The outdegree of a node v is the number of distinct edges (v,w)  E. A node with indegree 0 is a root.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Trees are graphs A dag is a directed acyclic graph. A tree is a connected acyclic undirected graph. A forest is an acyclic undirected graph (not necessarily connected), i.e., each connected component is a tree.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Example DAG Watch Socks Shoes Undershorts Pants Belt Tie Shirt Jacket a DAG implies an ordering on events

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Example DAG Watch Socks Shoes Undershorts Pants Belt Tie Shirt Jacket In a complex DAG, it can be hard to find a schedule that obeys all the constraints.

Topological Sort

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Topological Sort For a directed acyclic graph G = (V,E) A topological sort is an ordering of all of G’s vertices v 1, v 2, …, v n such that... Formally: for every edge (v i,v k ) in E, i<k. Visually: all arrows are pointing to the right

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Topological sort There are often many possible topological sorts of a given DAG Topological orders for this DAG : 1,2,5,4,3,6,7 2,1,5,4,7,3,6 2,5,1,4,7,3,6 Etc. Each topological order is a feasible schedule

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Topological Sorts for Cyclic Graphs? Impossible! 12 3 If v and w are two vertices on a cycle, there exist paths from v to w and from w to v. Any ordering will contradict one of these paths

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Topological sort algorithm Algorithm –Assume indegree is stored with each node. –Repeat until no nodes remain: Choose a root and output it. Remove the root and all its edges. Performance –O(V 2 + E), if linear search is used to find a root.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Better topological sort Algorithm: –Scan all nodes, pushing roots onto a stack. –Repeat until stack is empty: Pop a root r from the stack and output it. For all nodes n such that (r,n) is an edge, decrement n’s indegree. If 0 then push onto the stack. O( V + E ), so still O(V 2 ) in worst case, but better for sparse graphs. Q: Why is this algorithm correct?

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness Clearly any ordering produced by this algorithm is a topological order But... Does every DAG have a topological order, and if so, is this algorithm guaranteed to find one?

Quiz Break

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Quiz Prove: –This algorithm never gets stuck, i.e. if there are unvisited nodes then at least one of them has an indegree of zero. Hint: –Prove that if at any point there are unseen vertices but none of them have an indegree of 0, a cycle must exist, contradicting our assumption of a DAG.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Proof See Weiss page 476.

Graph Traversals

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Graph Traversals Both take time: O(V+E)

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Use of a stack It is very common to use a stack to keep track of: –nodes to be visited next, or –nodes that we have already visited. Typically, use of a stack leads to a depth-first visit order. Depth-first visit order is “aggressive” in the sense that it examines complete paths.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Topological Sort as DFS do a DFS of graph G as each vertex v is “finished” (all of it’s children processed), insert it onto the front of a linked list return the linked list of vertices why is this correct?

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Use of a queue It is very common to use a queue to keep track of: –nodes to be visited next, or –nodes that we have already visited. Typically, use of a queue leads to a breadth- first visit order. Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Graph Searching ??? Graph as state space (node = state, edge = action) For example, game trees, mazes,... BFS and DFS each search the state space for a best move. If the search is exhaustive they will find the same solution, but if there is a time limit and the search space is large... DFS explores a few possible moves, looking at the effects far in the future BFS explores many solutions but only sees effects in the near future (often finds shorter solutions)

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Minimum Spanning Trees

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Problem: Laying Telephone Wire Central office

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Wiring: Naïve Approach Central office Expensive!

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Minimum Spanning Tree (MST) (see Weiss, Section ) it is a tree (i.e., it is acyclic) it covers all the vertices V –contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Applications of MST Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…) Internet content distribution –$$$, also a hot research topic –Idea: publisher produces web pages, content distribution network replicates web pages to many locations so consumers can access at higher speed –MST may not be good enough! content distribution on minimum cost tree may take a long time! Provides a heuristic for traveling salesman problems. The optimum traveling salesman tour is at most twice the length of the minimum spanning tree (why??)

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, How Can We Generate a MST? a c e d b a c e d b

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s Algorithm Initialization a. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = null c. For all vertices v  V, v  r, set D(v) =  d. Insert all vertices into priority queue P, using distances as the keys a c e d b eabcd 0  VertexParent e -

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s Algorithm While P is not empty: 1. Select the next vertex u to add to the tree u = P.deleteMin() 2. Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w  P ) If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s algorithm a c e d b dbca 455  VertexParent e- be ce de The MST initially consists of the vertex e, and we update the distances and parent for its adjacent vertices VertexParent e- b- c- d- dbca  e 0

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s algorithm a c e d b acb 245 VertexParent e- be c d de a d dbca 455  VertexParent e- be ce de

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s algorithm a c e d b cb 45 VertexParent e- be c d de a d acb 245 VertexParent e- be c d de a d

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s algorithm a c e d b b 5 VertexParent e- be c d de a d cb 45 VertexParent e- be c d de a d

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s algorithm VertexParent e- be c d de a d a c e d b The final minimum spanning tree b 5 VertexParent e- be c d de a d

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Running time of Prim’s algorithm (without heaps) Initialization of priority queue (array): O(| V |) Update loop: | V | calls Choosing vertex with minimum cost edge: O(| V |) Updating distance values of unconnected vertices: each edge is considered only once during entire execution, for a total of O(| E |) updates Overall cost without heaps: When heaps are used, apply same analysis as for Dijkstra’s algorithm (p.469) (good exercise) O(| E | + | V | 2 )

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Prim’s Algorithm Invariant At each step, we add the edge (u,v) s.t. the weight of (u,v) is minimum among all edges where u is in the tree and v is not in the tree Each step maintains a minimum spanning tree of the vertices that have been included thus far When all vertices have been included, we have a MST for the graph!

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Prim’s This algorithm adds n-1 edges without creating a cycle, so clearly it creates a spanning tree of any connected graph (you should be able to prove this). But is this a minimum spanning tree? Suppose it wasn't. There must be point at which it fails, and in particular there must a single edge whose insertion first prevented the spanning tree from being a minimum spanning tree.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Prim’s Let V' be the vertices incident with edges in S Let T be a MST of G containing all edges in S, but not (x,y). Let G be a connected, undirected graph Let S be the set of edges chosen by Prim’s algorithm before choosing an errorful edge (x,y) x y

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Prim’s x y v w There is exactly one edge on this cycle with exactly one vertex in V’, call this edge (v,w) Edge (x,y) is not in T, so there must be a path in T from x to y since T is connected. Inserting edge (x,y) into T will create a cycle

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Prim’s Since Prim’s chose (x,y) over (v,w), w(v,w) >= w(x,y). We could form a new spanning tree T’ by swapping (x,y) for (v,w) in T (prove this is a spanning tree). w(T’) is clearly no greater than w(T) But that means T’ is a MST And yet it contains all the edges in S, and also (x,y)...Contradiction

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Another Approach a c e d b Create a forest of trees from the vertices Repeatedly merge trees by adding “safe edges” until only one tree remains A “safe edge” is an edge of minimum weight which does not create a cycle forest: {a}, {b}, {c}, {d}, {e}

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Kruskal’s algorithm Initialization a. Create a set for each vertex v  V b. Initialize the set of “safe edges” A comprising the MST to the empty set c. Sort edges by increasing weight a c e d b F = {a}, {b}, {c}, {d}, {e} A =  E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Kruskal’s algorithm For each edge (u,v)  E in increasing order while more than one set remains: If u and v, belong to different sets U and V a. add edge (u,v) to the safe edge set A = A  {(u,v)} b. merge the sets U and V F = F - U - V + (U  V) Return A Running time bounded by sorting (or findMin) O( |E|log|E| ), or equivalently, O( |E|log|V| ) (why???)

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Kruskal’s algorithm E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} Forest {a}, {b}, {c}, {d}, {e} {a,d}, {b}, {c}, {e} {a,d,c}, {b}, {e} {a,d,c,e}, {b} {a,d,c,e,b} A  {(a,d)} {(a,d), (c,d)} {(a,d), (c,d), (d,e)} {(a,d), (c,d), (d,e), (b,e)} a c e d b

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, After each iteration, every tree in the forest is a MST of the vertices it connects Algorithm terminates when all vertices are connected into one tree Kruskal’s Algorithm Invariant

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Kruskal’s This algorithm adds n-1 edges without creating a cycle, so clearly it creates a spanning tree of any connected graph (you should be able to prove this). But is this a minimum spanning tree? Suppose it wasn't. There must be point at which it fails, and in particular there must a single edge whose insertion first prevented the spanning tree from being a minimum spanning tree.

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Kruskal’s Let e be this first errorful edge. Let K be the Kruskal spanning tree Let S be the set of edges chosen by Kruskal’s algorithm before choosing e Let T be a MST containing all edges in S, but not e. K T S e

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Kruskal’s Proof (by contradiction): Assume there exists some edge e’ in T - S, w(e’) < w(e) Kruskal’s must have considered e’ before e K T S e Lemma: w(e’) >= w(e) for all edges e’ in T - S However, since e’ is not in K (why??), it must have been discarded because it caused a cycle with some of the other edges in S. But e’ + S is a subgraph of T, which means it cannot form a cycle...Contradiction

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Correctness of Kruskal’s Inserting edge e into T will create a cycle There must be an edge on this cycle which is not in K (why??). Call this edge e’ e’ must be in T - S, so (by our lemma) w(e’) >= w(e) We could form a new spanning tree T’ by swapping e for e’ in T (prove this is a spanning tree). w(T’) is clearly no greater than w(T) But that means T’ is a MST And yet it contains all the edges in S, and also e...Contradiction

15-211: Fundamental Data Structures and Algorithms Rose Hoberman April 8, Greedy Approach Like Dijkstra’s algorithm, both Prim’s and Kruskal’s algorithms are greedy algorithms The greedy approach works for the MST problem; however, it does not work for many other problems!

That’s All!