Minimal Spanning Trees

Slides:



Advertisements
Similar presentations
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.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
1 Spanning Trees Lecture 20 CS2110 – Spring
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
3/29/05Tucker, Sec Applied Combinatorics, 4th Ed. Alan Tucker Section 4.2 Minimal Spanning Trees Prepared by Amanda Dargie and Michele Fretta.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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.
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.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
COSC 2007 Data Structures II Chapter 14 Graphs III.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Introduction to Graph Theory
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs Upon completion you will be able to:
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
CSE373: Data Structures & Algorithms
Discrete Mathematicsq
Lecture 12 Graph Algorithms
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Ellen Walker CPSC 201 Data Structures Hiram College
Spanning Trees, greedy algorithms
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Minimal Spanning Trees
Short paths and spanning trees
Spanning Trees Longin Jan Latecki Temple University based on slides by
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Graph Algorithm.
CSE 421: Introduction to Algorithms
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Tree Construction (BFS, DFS, MST) Chapter 5
Minimum Spanning Tree.
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.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Spanning Trees.
Graphs.
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
CSE 373: Data Structures and Algorithms
Spanning Trees Longin Jan Latecki Temple University based on slides by
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Spanning Trees, greedy algorithms
Lecture 11 Graph Algorithms
Prims’ spanning tree algorithm
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Minimal Spanning Trees Reading. Weiss, sec. 24.2.2 Real quotes from a Dilbert-quotes contest:   "As of tomorrow, employees will be able to access the building only using individual security cards.  Pictures will be taken next Wednesday and employees will receive their cards in two weeks." (Microsoft Corp. in Redmond, WA) "What I need is an exact list of specific unknown problems we might encounter.” (Lykes Lines Shipping) "E-mail is not to be used to pass on information or data.  It should be used only for company business.” (Electric Boat Company) "This project is so important, we can't let things that are more important interfere with it.” (United Parcel Service)

Assume you have a directed or undirected graph. Spanning Tree Assume you have a directed or undirected graph. Spanning tree of a graph is tree such that Tree has same set of nodes Set of tree edges is a subset of graph edges A B C D E F G H I A B C D E F G H I Tree Another tree

Nodes numbered in the order visited BFS and DFS Walk Pre-order traversal gives a depth-first search (DFS) of a tree. Breadth-first search (FBS) as in second diagram 1 1 2 5 7 2 3 4 4 3 6 5 6 7 DFS BFS Nodes numbered in the order visited

Two spanning trees of the same graph 1 A is root of tree. Tree edges are red. A 1 B A B 9 2 G G 5 2 3 8 F F 7 C C I H I H 6 E D 4 D E Breadth-first Spanning Tree Depth-first Spanning Tree To build a spanning tree: (1) start with one node, A, as root. (2) At each step, add to tree one edge from a node in tree to a node that is not yet in the tree.

Build a DFS spanning tree (V, E). Use a stack V = {A}; E = {}; s = (A,F), (A,G), (A,B) // s: stack of edges Step 1: Take (A,F) off stack and add to E; push onto s edges leaving F. V = {A,F}; E = {(A,F)}; s = (F,E), (F,I), (F,A) (A,G), (A,B) Step 2: Take (F,E) off s and add to E; push onto s edges leaving E. V = {A,F,E}; E = {(A,F),(F,E)}; s = (E,F), (E,I), (E,D), (F,I), (F,A) (A,G), (A,B) C A B D E F G H I After taking an edge (v,w) off stack: if w already in V, don’t do anything

Build a DFS spanning tree (V, E) V = {A}; E = {}; //start off with one-node tree s = stack of edges to neighbors of A; // s is a stack of edges /** invariant:(V,E) is a tree. For all edges (v,w) in s: v is in V and (v,w) not in E. Any node in graph that is not in V is reachable from the end node of some edge in s. **/ while (s is not empty do) { (v, w) = pop(s); // Take top edge (v,w) off s; if (w is not in V) { Add w to V; add (v,w) to E; Push onto s all edges with start node w; } A B D E F G H I C

Build a BFS spanning tree (V, E). Use a queue. V = {A}; E= {}; s = (A,F), (A,G), (A,B) // s: queue of edges Step 1: Take (A,F) off s and add to E; push onto s edges leaving F. V = {A,F}; E = {(A,F)}; s = (A,G), (A,B), (F,E), (F,I), (F,A) Step 2: Take (A,G) off s and add to E; push onto s edges leaving G. V = {A,F,G}; E = {(A,F),(A,G)}; s = (A,B), (F,E), (F,I), (F,A), (G,I), (G,H), (G,B), (G,A) A B G F C After taking an edge (v,w) off queue: if w already in V, don’t do anything I H E D

Build a BFS spanning tree (V, E) V = {A}; E = {}; //start off with one-node tree s = queue of edges to neighbors of A; // s is a queue of edges /** invariant:(V,E) is a tree. For all edges (v,w) in s: v is in V and (v,w) not in E. Any node in graph that is not in V is reachable from the end node of some edge is s. **/ while (s is not empty do) { (v, w) = pop(s); // Take top edge (v,w) off s; if (w is not in V) { Add w to V; add (v,w) to E; Push onto s all edges with start node w; } A B D E F G H I C

Build a DFS or BFS spanning tree (V, E) Building a DFS spanning tree and building a BFS spanning tree are essentially the same algorithm. DFS algorithm uses a stack of edges to process BFS algorithm uses a stack of A B D E F G H I C

Property 1 of spanning trees Graph: G = (V,E) Spanning tree: T = (V,ET,R) Choose any edge: c = (u,v) in G but not in T There is a simple cycle containing only edge c and edges in spanning tree. Proof: Let w be the first node in common to paths from u to root of tree and from v to root of tree. The paths uv, vw,wu can be catenated to form the desired cycle. edge (I,H): w is node G simple cycle is (I,H,G,I) edge (H,C): w is node A simple cycle is (H,C,B,A,G,H) A B C D E F G H I

In any tree T = (V,E), |E|=|V| – 1 For all n>0, P(n) holds, where Useful lemma In any tree T = (V,E), |E|=|V| – 1 For all n>0, P(n) holds, where P(n) for a tree with n (>0) nodes: |E| = |V| – 1 Proof by induction on n * n = 1. tree with node has 0 edges. 0 = 1 - 1. * Assume P(n) for some n, 0 < n. Consider a tree S=(VS, ES) with n+1 nodes. S has a leaf. Remove 1 leaf (and the edge to it) to give a tree T with n nodes. By inductive assumption, P(n), |ET| = |VT|-1. Since |ES| = |ET|+1 and |VS|=|VT|+1, the result follows. An undirected graph G = (V,E) is a tree iff (1) it is connected (2) |E| = |V| – 1

Property 2 of spanning trees Graph: G = (V,E) Spanning tree: T = (V,ET,R) Choose any edge: c = (u,v) 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 (st) in Y gives another spanning tree T1. A B C D E F G H I 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

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

Weighted Spanning Trees Assume an undirected graph G = (V,E) with weights on each edge Spanning tree of graph G is tree T = (V,ET) 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 A graph can have several MST’s Applications: phone network design etc.

Example weight 18 SSSP tree (nodes in Graph order chosen by 2 2 B 9 5 G 4 weight 18 6 5 9 G 6 4 2 5 2 5 1 1 F F 5 5 C C 3 4 H 1 4 H 6 I 3 I 1 6 2 2 1 E 1 D E D SSSP tree (nodes in order chosen by Dijkstra’s shortest path algoritm Graph A B 2 9 5 G 6 4 weight 16 2 1 5 F 5 C 4 H 6 3 I 1 2 Minimal spanning tree D E 1

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

Prims’s algorithm Use the DFS algorithm given earlier, but consider s to be a set rather than a stack. At each iteration, choose the edge (v,w) from s that has minimum weight. Hence, maintain s as a min-heap! That’s it! Proof that this actually constructs a minimal spanning tree is not given here. This is a greedy algorithm: At each step, it chooses the best.

Kruskal’s algorithm // Find a minimum-weight spanning tree for graph (V, E). Sort the edges by weight; VT= V; ET= empty // the vertices and edges of tree for each edge (v,w) (in order of increasing weight): if (adding (v,w) does not create a cycle) { Add (v,w) to ET; } Difficulty is testing whether a cycle is created. Greedy algorithm: at each stage it picks the next best edge to add.

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 (e.g.) Traveling Salesman Problem