Minimum Spanning Tree.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Greed is good. (Some of the time)
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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 Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Tree By Jonathan Davis.
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.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Analysis of Algorithms
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.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Union-find Algorithm Presented by Michael Cassarino.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Search Applications, Minimum Spanning Tree
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Short paths and spanning trees
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Spanning Trees.
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum spanning trees
Minimum Spanning Trees (MSTs)
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Minimum-Cost Spanning Tree
Data Structures and Algorithms
Minimum Spanning Trees
Presentation transcript:

Minimum Spanning Tree

Spanning Trees A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. Spanning trees are defined for connected undirected graphs Since there are trees  They have no cycles Graph A Some Spanning Trees from Graph A or or or

Complete Graph All 16 of its Spanning Trees

Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. Defined for connected, undirected, and weighted graphs Complete Graph Minimum Spanning Tree 7 2 2 5 3 3 4 1 1

Minimum Spanning Trees (MST) Complete Graph Minimum Spanning Tree 7 2 2 5 3 3 4 1 1 If each edge ei = (u,v) has a weight or cost wi Minimum spanning tree is the smallest subset of edges MST that connect all nodes such that: Σ wi is minimized

Example: Laying Telephone Wire Central office

Wiring: Naive Approach Central office Expensive!

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

Algorithms for Obtaining the Minimum Spanning Tree Kruskal's Algorithm Prim's Algorithm Both of these are Greedy Algorithms

Kruskal's Algorithm: Overview The algorithm creates a forest of trees (many trees). Initially each forest consists of a single node (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree Skip this edge. The greedy choice

Kruskal's Algorithm: Overview 1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a min-priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. If we start with n nodes (n separate trees) Each step we connect two trees Then we need (n-1) edges to get a single tree

Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

List of all edges All nodes, no edges 4 1 A B A D 4 4 B C B D 4 10 2 B J C E 4 2 1 1 5 C F D H A 4 E F 1 6 2 2 D J E G D 3 10 5 G 3 5 F G F I 5 6 3 4 3 4 I G I G J H 3 2 J 2 3 H J I J

Sort the edges Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize) 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

Minimum Spanning Tree (10 nodes, 9 edges) Complete Graph 4 B C 4 B C 4 4 2 1 2 1 A E A 4 F E 1 F 1 2 D 2 D 3 10 G 5 G 3 5 6 3 4 I I H H 3 2 3 J 2 J Sum of the weights is the smallest = 22

Kruskal's Algorithm Each node is a set by itself Get the smallest edge at each time If the two nodes are in different sets (trees), then this edge will not form a cycle Add this edge and union the two sets (merge the two trees)

Analysis of Kruskal's Algorithm: Naive O(V) Need to sort the edges first O(E Log E) --Naïve implementation needs O(V) each time --Will be called E times (at worst)  O(EV) Naïve implementation  O(V) + O(E Log E) + O(E V)  O(E V)

Analysis of Kruskal's Algorithm: Efficient O(V) Put the edges in a minHeap O(E Log E) ** Can be done in linear time O(E) --Can be done with some tricks in O (Log V) --Will be called E times (at worst)  O(E LogV) Naïve implementation  O(V) + O(E Log E) + O(E Log V)  O(E Log E) // Since E < V2 , then Log E < 2 Log V  O(E Log V)

Algorithms for Obtaining the Minimum Spanning Tree Kruskal's Algorithm Prim's Algorithm Both of these are Greedy Algorithms

Prim's Algorithm This algorithm maintains two groups of nodes (Old graph) and (New graph). The New graph starts with one node. (At then end it will be the MST) At each step, select a node from the Old graph and add it to the New graph Select the node whose connecting edge has the smallest weight to any node in the New graph. The greedy choice

Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Old Graph New Graph -Think of the black edges as cost ∞ (not valid) -Only the blue ones are valid 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3 2 10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

Complete Graph Minimum Spanning Tree Sum of the weights is the smallest = 22 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A E F 1 2 D 3 2 10 D 5 G G 5 6 3 3 4 I H I H 3 2 J 3 2 J It is possible that Kruskal and Prim algorithms generate different trees but the cost will be the same.

Prim's Algorithm: Pseudocode For all nodes, make the cost of each one ∞ and White (not selected yet) Select a root (make its cost 0) Select a root (make its cost 0) Select the smallest from Q (the node to add to the New graph) Update the cost of all adjacent nodes to u

Prim's Algorithm: Analysis O(V) O(V Log V) The loop repeats V times Total of V calls: O(V Log V) The loop repeats overall E times (for the V calls) Total of E times: O(E Log V)

Prim's Algorithm: Analysis O(V) Total: O(V) + O(V Log V) + O(V Log V) + O(E Log V)  O( E Log V) O(V Log V) The loop repeats V times Total of V calls: O(V Log V) The loop repeats overall E times (for the V calls) Total of E times: O(E Log V)

Algorithms for Obtaining the Minimum Spanning Tree Kruskal's Algorithm Prim's Algorithm Both of these are Greedy Algorithms