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:

Slides:



Advertisements
Similar presentations
Decision Maths Networks Kruskals Algorithm Wiltshire Networks A Network is a weighted graph, which just means there is a number associated with each.
Advertisements

Lecture 15. Graph Algorithms
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm.
Discussion #36 Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
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.
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 10 Instructor: Paul Beame.
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.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
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.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Minimum Spanning Trees Prof. Sin-Min Lee Dept. of Computer Science, San Jose State University.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Properties and Applications of Depth-First Search Trees and Forests
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graph Search Applications, Minimum Spanning Tree
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
CSE373: Data Structures & Algorithms
Spanning Trees.
Lecture 22 Minimum Spanning Tree
Kruskal’s Algorithm Elaicca Ronna Ordoña. A cable company want to connect five villages to their network which currently extends to the market town of.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Graph Algorithm.
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Spanning Trees.
Minimum Spanning Tree.
Minimum Spanning Tree.
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.
Spanning Trees.
Minimum-Cost Spanning Tree
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Networks Kruskal’s Algorithm
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Minimum spanning trees
Kruskal’s Algorithm AQR.
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost Spanning Tree
Presentation transcript:

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. S is connected, and 2. S is acyclic. Example: E AD BCF E AD BCF One of many Spanning Trees Why “tree”? Pick any node as the root  is a tree. Why “spanning”? Every node is (still) connected to every other node.

Algorithm for Generating a Spanning Tree A DFS algorithm works. Just keep the tree edges. E AD BCF O(m) = O(n 2 ) (worse case) C A D B EF

E AD BCF Spanning Trees for Weighted Graphs (Only) two minimal spanning trees Weighted Graph E AD BCF E AD BCF A minimal spanning tree for a weighted, connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that: 1. S is connected, and 2. S is acyclic, and 3. S has minimal weight. Examples: Note: all weights positive.

Kruskal’s Algorithm Sort edges by weight (smallest first) For each edge e = {x, y} (in order) if nodes x and y are not in the same connected component, then add e E AD BCF {A,B}1 {D,F}2 {B,C}4 {A,D}5 {C,D}5 {E,D}6 {A,E}7 {B,D}8  2        E C A B D F Could have stopped here… we had n-1 edges.

Kruskal’s Examples City connectivity –$35 Atlanta Chicago –$70 Atlanta Denver –$40 Atlanta New York –$65 Chicago Denver –$50 Chicago New York –$60 Chicago San Francisco –$45 Denver San Francisco V = {a, b, c, d, e, f}, E = {{a,b}, {a,d}, {b,c}, {b,d}, {b,f}, {c,f}, {d,e}, {e,f}} –{a,b}=8 {a,d}=11 {b,c}=7{b,d}=6 {b,f}=4 {c,f}=14 {d,e}=1 {e,f}=2 S C N \ / \ / D A

F 2 (4) E 6 (5) D 5 (3) C 4 (2) Prim’s Algorithm Initialize a spanning tree S containing a single vertex, chosen arbitrarily from the graph Until n-1 edges have been added –find the edge e = {x, y} such that x is in S and y is not in S e is the smallest weighted edge left –Add e and y to S E AD BCF B 1 (1) A

Correctness Proofs & Complexity Analysis Correctness –Detailed proofs  lengthy –Essential idea: Prove connected: add edges until all nodes connected Prove acyclic: don’t add an edge that would create a cycle Prove minimal weight: only add overall or local minimal-weight edges Complexity –Straightforward implementations are easy to analyze. –Both algorithms have clever implementations that make them run faster.

Correctness of Prim’s Algorithm Prim’s algorithm produces a minimal spanning tree S from a connected, weighted, undirected graph G. Proof (sketch) –Connected: Assume not, then there are at least two disconnected components, C 1 and C 2 in S. But since G is connected, there is a smallest weight edge {x, y} with x in C 1 and y in C 2 that would have been found. Thus, after running Prim’s algorithm, C 1 and C 2 must be connected. –Acyclic: Assume not, then there was a first edge {x, y} added to make a cycle. But to have been added, x must have been in S and y must not have been in S. Since y must not have been in S, there was no path from x to y at the time {x, y} was added, and thus no cycle was created when {x, y} was added. –Minimal weight: By induction with loop invariant: After k iterations the growing spanning tree S has minimal weight. Basis: 0 iterations: The initialization step selects a single node and the weight of S is 0, the minimum possible since all weights are assumed to be positive. Induction: By the induction hypothesis, after k iterations S has minimal weight. Then, after k+1 iterations S has minimal weight. For suppose not, then the chosen edge e must not have been the smallest-weight edge left.

Complexity of Prim’s Algorithm Initialize a spanning tree S containing a single vertex, chosen arbitrarily from the graph Until n-1 edges have been added –find the edge e = {x, y} such that x is in S and y is not in S e is the smallest weighted edge left –Add e and y to S O(1) + O(n(m+1)) = O(nm) = O(n 3 ) in the worst case O(n) O(1) O(m) O(1)

A Faster Prim’s Algorithm To make Prim’s Algorithm faster, we need a way to find the edge e faster. Can we avoid looking through all edges in each iteration? –We can if we sort them first and then make a sorted list of incident edges for each node. –In the initialization step this takes O(mlogm) to sort and O(m) to make lists for each node  O(mlogm) = O(n 2 logn 2 ) in the worst case. –Now for each of the n  1 iterations, we find the edge {x, y} by looking only at the first edge of at most n lists  O(n 2 ) over all iterations. We must, of course, discard edges on these lists as we build S, so that the edge we want will be first, but with appropriate links, this is O(1). –Thus, the sort in the initialization dominates, which makes the algorithm O(mlogm) = O(n 2 logn 2 ) in the worst case. To make the algorithm even faster, we must somehow avoid the sort. Can we?

An Even Faster Prim’s Algorithm Start with the smallest-weight edge e = {x, y} in S –discard x and y from the list of nodes to consider –for both x and y, find the closest node, if any (among the non- discarded nodes), and keep them and their edge weights Until n-1 edges have been added –find the edge e = {x, y} such that x is in S and y is not in S e is the smallest weighted edge left –add e and y to S –discard y from the list of nodes to consider, and for both x and y, find the closest node, if any (among the non-discarded nodes), and keep them and their edge weights O(m) + O(n(n+1+n)) = O(n 2 ) in the worst case O(m) O(n) O(1) O(n)

Correctness of Kruskal’s Algorithm Kruskal’s algorithm produces a minimal spanning tree S from a connected, weighted, undirected graph G. Proof (sketch) –Connected: Assume not, then there are at least two disconnected components, C 1 and C 2 in S. But since G is connected, there is a smallest weight edge {x, y} with x in C 1 and y in C 2 that would have been added. Thus, after running Kruskal’s algorithm C 1 and C 2 must be connected. –Acyclic: Assume not, then there was a first edge {x, y} added to make a cycle. But to have been added, x and y must have not been connected in S. Since x and y must not have been connected in S, there was no path from x to y at the time {x, y} was added, and thus no cycle was created when {x, y} was added. –Minimal weight: …

Complexity of Kruskal’s Algorithm Sort edges by weight (smallest first) For each edge e={x, y}, until n  1 edges added if nodes x and y are not in the same connected component, add e O(mlogm) + O(nm) = O(n 2 logn 2 ) + O(n 3 ) = O(n 3 ) Can Kruskal’s algorithm be improved? –Often already sorted (omit first step) –Faster way to check “in same connected component” O(mlogm) O(m) O(n), i.e. using DFS* O(1) *O(n)  not O(m)  because the edges for the DFS check are the edges added to the spanning tree so far, and there can never be more than n  1 edges in the spanning tree. O(n 2 logn)