Minimum Spanning Tree.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Greed is good. (Some of the time)
Discrete Maths Chapter 3: Minimum Connector Problems Lesson 1: Prim’s and Kruskal.
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.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
Spanning Trees.
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.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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 Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
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
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.
Minimum Spanning Trees
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Graph Search Applications, Minimum Spanning Tree
Lecture ? The Algorithms of Kruskal and Prim
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Short paths and spanning trees
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Minimum 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
Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA)
Minimum-Cost Spanning Tree
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum spanning trees
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
Presentation transcript:

Minimum Spanning Tree

Difference between Trees and Graphs

Difference between Trees and Graphs

Difference between Trees and Graphs

Graph Applications

Spanning Tree

Problem: Laying Telephone Wire Central office

Wiring: Naïve Approach Central office Expensive!

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

Spanning Trees A spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G. If there are N vertices in a graph then there are n-1 edges in a spanning Tree In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph.

Spanning Trees Graph A Some Spanning Trees from Graph A or or or

An undirected graph and its minimum spanning tree. A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight. The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. Graph Minimum Spanning Tree 7 2 2 5 3 3 4 1 1 An undirected graph and its minimum spanning tree.

Minimum Spanning Tree A tree (a connected graph with no cycles) which connects all the nodes together is called a Spanning Tree For any connected graph with n nodes, each spanning tree will have n - 1 arcs The Minimum Spanning Tree is the one with the Minimum weight

Algorithms for Obtaining the Minimum Spanning Tree Kruskal's Algorithm Prim's Algorithm

Kruskal's Algorithm This algorithm creates a forest of trees. Initially the forest consists of n single node trees (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, so that this edge would not be needed.

Kruskal’s Algorithm Aim: To find a minimum spanning tree for a connected graph with n nodes: Step 1: . The forest is constructed - with each node in a separate tree Step 2: The edges are placed in a priority queue. Step 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. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.

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

4 1 A B A D 4 4 B C B D 4 10 2 B C 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 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 Complete Graph 4 B C 4 B C 4 4 2 1 2 1 A E A 4 F D 2 D 3 10 G 5 G 3 5 6 3 4 I I H H 3 2 3 J 2 J

Kruskal's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

Kruskal's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

Algorithm: Kruskal’s Lines 1–3 initialize the set A to the empty set and create |V| trees, one containing each vertex. For loop in lines 5–8 examines edges in order of weight, from lowest to highest. The loop checks, for each edge (u,v), whether the endpoints u and v belong to the same tree. If they do, then the edge (u,v) cannot be added to the forest without creating a cycle, and the edge is discarded. Otherwise, the two vertices belong to different trees. In this case, line 7 adds the edge (u,v) to A, and line 8 merges the vertices in the two trees.

Analysis: Kruskal’s Total Running Time = O(E log V) O(V) O(E log E) O(E) Initializing the set A takes O(1) time. Sorting edges takes O(E log E) or O(n logn) FIND-SET and UNION operations take O(E) time. Along with |V| MAKE-SET operations, these take a total of O((V+E)α(V)) time, where α is the very slowly growing function. O((V+E)α(V)) O((E+E)α(V)) as |E| ≥ |V| -1 O(Eα(V)) O(E log V) as α (|V| ) =O( log V) Total Running Time = O(E log V)

Prim’s Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph, each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges.

Prim’s Algorithm Aim: To find a minimum spanning tree T Step 1: Select any node to be the first node of T. Step 2: Consider the arcs which connect nodes in T to nodes outside T. Pick the one with minimum weight. Add this arc and the extra node to T. (If there are two or more arcs of minimum weight, choose any one of them.) Step 3: Repeat Step 2 until T contains every node of the graph.

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 D 3 2 10 D 5 G G 5 6 3 3 4 I H I H 3 2 J 3 2 J

Prim's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11 the root vertex

Prim's algorithm a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

a b h c d e f g i 4 8 7 9 10 14 2 6 1 11

Algorithm: Prim’s Lines 1–5 set the key of each vertex to ∞ (except for the root r, whose key is set to 0 so that it will be the first vertex processed), set the parent of each vertex to NIL, and initialize the min priority queue Q to contain all the vertices. Line 6 runs a loops while Queue doesn’t get empty i.e. we extract all vertices from the queue to make a final solution tree. Line 7 extract-min(Q) extracts the minimum from the queue and stores in vertex u. Line 8 to ownward it checks all adjacent edges of vertex u, one by one, and see if it is in the queue and its edge with u and v vertices have weight less than v.key which means lowest weight edge from vertex v. If both conditions are true then make u then parent of vertex v and update v minimum weight edge to weight of u and v vertices edge.

Analysis: Prim’s Total Running Time = O(E log V) O(V) O(V) O(V log V) O( log V) O(E) O(E log V) O(log V) Perform lines 1–5 in O(V) time The body of the while loop executes |V| times, and since each EXTRACT-MIN operation takes O(log V)time, the total time for all calls to EXTRACT-MIN is O(V log V). The for loop in lines 8–11 executes O(E)times, within the for loop, we can implement the test for membership in Q in line 9 in constant time by keeping a bit for each vertex that tells whether or not it is in Q, and updating the bit when the vertex is removed from Q. The assignment in line 11 involves an implicit DECREASE-KEY operation on the min-heap, which a binary min-heap supports in O(log V)time. Thus, the total time for Prim’s algorithm is O(V log V + E log V) = O(E log V) If implemented by binary min-heap then running time is same as Kruskal algorithm i.e. O(E log V).

Comparison Prim’s Algorithm is a quick way of finding the Minimum Spanning Tree (or minimum connector) This algorithm is said to be “greedy” since it picks the immediate best option available without taking into account the long-term consequences of the choices made. Kruskal’s Algorithm may also be used to find a minimum spanning tree, but this considers the weights themselves rather than the connecting points.