Minimal Spanning Trees

Slides:



Advertisements
Similar presentations
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greedy Algorithms Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs III (Trees, MSTs) (Chp 11.5, 11.6)
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Combinatorial Algorithms
Discussion #36 Spanning Trees
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.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
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.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
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.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs Presented by R 李孟哲 R 陳翰霖 R 張仕明 Sanjiv Kapoor and.
SPANNING TREES Lecture 21 CS2110 – Spring
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.
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:
Lectures on Greedy Algorithms and Dynamic Programming
Introduction to Graph Theory
Chapter 9 Finding the Optimum 9.1 Finding the Best Tree.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Minimum Spanning Trees
CSCE 411 Design and Analysis of Algorithms
Chapter 5 : Trees.
Shortest Paths and Minimum Spanning Trees
Spanning Trees.
Minimal Spanning Trees
Spanning Trees Lecture 21 CS2110 – Fall 2016.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Algorithms and Data Structures Lecture XIII
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Short paths and spanning trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
CSE 421: Introduction to Algorithms
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Minimum Spanning Trees
More Graphs Lecture 19 CS2110 – Fall 2009.
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Minimal Spanning Trees

Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,ET E, R) Tree has same set of nodes All tree edges are graph edges Root of tree is R Think: “smallest set of edges needed to connect everything together”

Spanning trees 1 A A B 1 B 9 2 G G 5 2 3 8 F 7 F C C I H I H 6 4 D D E A A B 1 B 9 2 G G 5 2 3 8 F 7 F C C I H I H 6 4 D D E E Breadth-first Spanning Tree Depth-first spanning tree

Property 1 of spanning trees Graph: G = (V,E) , Spanning tree: T = (V,ET,R) For any edge c in G but not in T, there is a simple cycle containing only edge c and edges in spanning tree. edge (I,H): simple cycle is (I,H,G,I) edge (H,C): simple cycle is (H,C,B,A,G,H) Proof? A B G F C I H E D

Proof of Property 1 Edge is c goes u v If u is ancestor of v, result is easy (u v, then v u form a cycle) Otherwise, there are paths root u and root v (b/c it is a tree). Let p be the node furthest from root on both of these paths. Now p u, then u v, then v p form a cycle. A B edge (I,H): p is node G simple cycle is (I,H,G,I) edge (H,C): p is node A simple cycle is (H,C,B,A,G,H) G F C I H E D

Useful lemma about trees In any tree T = (V,E), |E|=|V| -1 - Proof?

Useful lemma about trees In any tree T = (V,E), |E|=|V| -1 - Proof: (by induction on |V|) * If |V| = 1, we have the trivial tree containing a single node, and the result is obviously tree. * Assume result is true for all trees for which 1 <= |V| <n, and consider a tree S=(ES, VS) with |V| = n. Such a tree must have at least one leaf node; removing the leaf node and edge incident on that node gives a smaller tree T with less than n nodes. By inductive assumption, |ET| = |VT|+1. Since |ES| = |ET|+1 and |VS|=|VT|+1, the required result follow. Converse also true: an undirected graph G = (V,E) which (1) has a single connected component, and (2) has |E| = |V|-1  must be a tree.

Property 2 of spanning trees Graph: G = (V,E), Spanning tree: T = (V,ET,R) For any edge c in G but not in T, there is a simple cycle Y containing only edge c and edges in spanning tree. Moreover, inserting edge c into T and deleting any edge in Y gives another spanning tree T’. A B edge (H,C): simple cycle is (H,C,B,A,G,H) adding (H,C) to T and deleting (A,B) gives another spanning tree G F C I H D E

Proof of Property 2 - Outline T’ is a connected component. - Proof? In T’, numbers of edges = number of nodes –1 - Proof ? Therefore, from lemma earlier, T’ is a tree.

Proof of Property 2 T’ is a connected component. - Otherwise, assume node a is not reachable from node b in T’. In T, there must be a path from b to a that contains edge (s→t). In this path, replace edge (s→t) by the path in T’ obtained by deleting (s→t) from the cycle Y, which gives a path from b to a. Contradiction, thus a must be reachable from b In T’, numbers of edges = number of nodes –1 - Proof: by construction of T’ and fact that T is a tree. T’ is same as T, with one edge removed, one edge added. Therefore, from lemma, T’ is a tree.

Building BFS/DFS spanning trees dummy Use sequence structure as before, but put/get edges, not nodes Get edge (s,d) from structure If d is not in done set, add d to done set (s,d) is in spanning tree add out-edges (d,t) to seq structure if t is not in done set Example: BFS (Queue) 1 A B 2 G F C I H D E [(dummy,A)] [(A,B),(A,G),(A,F)] [(A,G),(A,F),(B,G),(B,C)]…..

Weighted Spanning Trees Assume you have an undirected graph G = (V,E) with weights on each edge Spanning tree of graph G is tree T = (V,ET E) Tree has same set of nodes All tree edges are graph edges Weight of spanning tree = sum of tree edge weights Minimal Spanning Tree (MST) Any spanning tree whose weight is minimal In general, a graph has several MST’s Applications: circuit-board routing, networking, etc.

Example A B A B 2 2 G 4 G 4 9 5 9 5 6 6 2 5 2 5 1 1 F F 5 5 C C 3 4 1 4 1 6 I H 6 3 I H 2 2 1 1 D E D E Graph SSSP tree A B 2 G 4 9 5 6 2 5 1 F 5 C Minimal spanning tree 4 6 3 I H 1 2 1 D E

Caution: in general, SSSP tree is not MST Intuition: SSSP: fixed start node MST: at any point in construction, we have a bunch of nodes that we have reached, and we look at the shortest distance from any one of those nodes to a new node 4 4 1 4 4 4 1 SSSP Tree MSP

Property 3 of minimal spanning trees 2 Edge(GH): 5 Cycle edges: (GI), (IE), (ED),(HD) all have weights less than (GH) G 4 9 5 6 2 5 1 F 5 C 4 6 3 I H 1 2 E 1 D Graph: G = (V,E) , Spanning tree: T = (V,ET,R) For any edge: c in G but not in T, there is a simple cycle Y containing only edge c and edges in spanning tree (already proved). Moreover, weight of c must be greater than or equal to weight of any edge in this cycle. Proof?

Property 3 of minimal spanning trees 2 Edge(GH): 5 Cycle edges: (GI), (IE), (ED),(HD) all have weights less than (GH) G 4 9 5 6 2 5 1 F 5 C 4 6 3 I H 1 2 E 1 D Graph: G = (V,E) , Spanning tree: T = (V,ET,R) Edge c … weight of c must be greater than or equal to weight of any edge in this cycle. Proof: Otherwise, let d be an edge on cycle with lower weight. Construct T’ from T by removing c and adding d. T’ is less weight than T, so T not minimal. Contradiction., so d can’t exist.

Building Minimal Spanning Trees Prim’s algorithm: simple variation of Dijkstra’s SSSP algorithm Change Dijkstra’s algorithm so the priority of bridge (fn) is length(f,n) rather than minDistance(f) + length(f,n) Intuition: Starts with any node. Keep adding smallest border edge to expand this component. Algorithm produces minimal spanning tree!

Prim’s MST algorithm Tree MST = empty tree; Heap h = new Heap(); //any node can be the root of the MST h.put((dummyRoot  anyNode), 0); while (h is not empty) { get minimum priority (= length) edge (tf); if (f is not lifted) { add (tf) to MST;//grow MST make f a lifted node; for each edge (fn) if (n is not lifted) h.put((fn), length(f,n)); }

Steps of Prim’s algorithm [((dummyA), 0)] [] add (dummyA) to MST [((AB),2), ((AG),5),((AF),9)] [((AG),5),((AF),9)] add (AB) to MST [((AG),5),((AF),9), (BG),6),((BC),4)] [((AG),5),((AF),9),((BG),6)] add (BC) to MST [((AG),5),((AF),9),((BG),6),((C,H),5), ((C,D), 2)] ……….. A B 2 G 4 9 5 6 F 2 5 1 C 5 4 6 3 I H 1 2 1 D E

Property of Prim’s algorithm At each step of the algorithm, we have a spanning tree for “lifted” nodes. This spanning tree grows by one new node and edge at each iteration. A B 2 G 4 9 5 6 1 2 5 C 5 4 6 3 I H 1 2 1 D E

Proof of correctness (part 1) Suppose the algorithm does not produce MST. Each iteration adds one new node and edge to tree. First iteration adds the root to tree, and at least that step is “correct”. “Correct” means partial spanning tree built so far can be extended to an MST. Suppose first k steps were correct, and then algorithm made the wrong choice. Partial spanning tree P built by first k steps can be extended to an MST M Step (k+1) adds edge (uv) to P, but resulting tree cannot be extended to an MST Where to go from here?

Proof (contd.) Consider simple cycle formed by adding (uv) to M. Let p be the lowest ancestor of v in M that is also in P, and let q be p’s child in M that is also an ancestor of v. So (pq) is a bridge edge at step (k+1) as is (uv). Since our algorithm chose (uv) at step (k+1), weight(uv) is less than or equal to weight(pq). From Property (3), weight of (uv) must be greater than or equal to weight(pq). p u (wrong choice) u q v v Partial spanning tree P Minimal Spanning Tree M

Proof (contd.) Therefore, weight(pq) = weight(uv). This means that the tree obtained by taking M, deleting edge (pq) and adding edge (uv) is a minimal spanning tree as well, contradicting the assumption that there was no MST that contained the partial spanning tree obtained after step (k+1). Therefore (by induction!), our algorithm is correct.

Complexity of Prim’s Algorithm Every edge is examined once and inserted into PQ when one of its two end points is first lifted. Every edge is examined again when its other end point is lifted. Number of insertions and deletions into PQ is |E| + 1 Complexity = O(|E|log(|E|)) Same as Dijkstra’s (of course)

Editorial notes Dijkstra’s algorithm and Prim’s algorithm are examples of greedy algorithms: making optimal choice at each step of the algorithm gives globally optimal solution In most problems, greedy algorithms do not yield globally optimal solutions (eg) TSP (Travelling Salesman Problem) (eg) greedy algorithm for puzzle graph search: at each step, choose move that minimizes the number of tiles that are out of position Problem: we can get stuck in “local” minima and never find the global solution