CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.

Slides:



Advertisements
Similar presentations
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Advertisements

MINIMAL CONNECTOR PROBLEMS Problem: A cable TV company is installing a system of cables to connect all the towns in a region. The numbers in the network.
Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network using the lowest possible.
CSE332: Data Abstractions Lecture 26: Minimum Spanning Trees Dan Grossman Spring 2010.
Discussion #36 Spanning Trees
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Minimum spanning trees Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 2012 August 13, 2012 CSE 332 Data Abstractions, Summer.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
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 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum Spanning tree Prim’s algorithm 1.Select any vertex 2.Select the shortest edge connected to that vertex 3.Select the shortest edge which connects.
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Spanning Trees.
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.
Minimum Spanning Trees and Shortest Paths
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Minimum Spanning Tree Neil Tang 3/25/2010
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Hannah Tang and Brian Tjaden Summer Quarter 2002
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Example A cable company want to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum.
Minimum spanning trees
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum spanning trees
Minimum Spanning Tree Neil Tang 4/3/2008
Minimum spanning trees
Networks Kruskal’s Algorithm
Minimum Spanning Trees
Minimum Spanning tree Select any vertex
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Kruskal’s Algorithm AQR.
CSE 373: Data Structures and Algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum-Cost Spanning Tree
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016

CSE373: Data Structures & Algorithms Announcements HW 4 due Wednesday, May 18 HW 5 will be due June 1. Ben Jones is our expert in Ezgi’s absence. benjones@cs Spring 2016 CSE373: Data Structures & Algorithms

Minimum Spanning Trees The minimum-spanning-tree problem Given a weighted undirected graph, compute a spanning tree of minimum weight Spring 2016 CSE373: Data Structures & Algorithms

Two different approaches Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Idea Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s vs. Dijkstra’s Recall: Dijkstra picked the unknown vertex with smallest cost where cost = distance to the source. Prim’s pick the unknown vertex with smallest cost where cost = distance from this vertex to the known set (in other words, the cost of the smallest edge connecting this vertex to the known set) Otherwise identical  Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm For each node v, set v.cost =  and v.known = false Choose any node v Mark v as known For each edge (v,u) with weight w, set u.cost=w and u.prev=v While there are unknown nodes in the graph Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v; } Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} ∞ ∞ 2 B A 1 ∞ 1 5 2 E ∞ 1 D 1 3 5 C ∞ ∞ 6 2 G vertex known? cost prev A ?? B C D E F G ∞ 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 2 2 B A 1 ∞ 1 5 2 E 1 1 D 1 3 5 C 2 ∞ 6 2 G vertex known? cost prev A Y B 2 C D 1 E ?? F G ∞ 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 2 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 5 6 2 G vertex known? cost prev A Y B 2 C 1 D E F 6 G 5 6 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 2 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 5 6 2 G vertex known? cost prev A Y B 2 C 1 D E F G 5 2 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G vertex known? cost prev A Y B 1 E C D F 2 G 3 2 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G vertex known? cost prev A Y B 1 E C D F 2 G 3 2 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} Prim’s Example 1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G vertex known? cost prev A Y B 1 E C D F 2 G 3 2 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Example Select the unknown node v with lowest cost Mark v as known and add (v, v.prev) to output For each edge (v,u) with weight w, if(w < u.cost) { u.cost = w; u.prev = v;} 1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G vertex known? cost prev A Y B 1 E C D F 2 G 3 2 10 F Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Analysis Correctness A bit tricky Intuitively similar to Dijkstra Run-time Same as Dijkstra O(|E|log|V|) using a priority queue Costs/priorities are just edge-costs, not path-costs Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Another Example A cable company wants to connect five villages to their network which currently extends to the town of Avonford. What is the minimum length of cable needed? Avonford Fingley Brinleigh Cornwell Donster Edan 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Model the situation as a graph and find the MST that connects all the villages (nodes). A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Select any vertex A Select the shortest edge connected to that vertex (since it’s the only known one) AB 3 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Select the shortest edge that connects an unknown vertex to any known vertex. AE 4 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Select the shortest edge that connects an unknown vertex to any known vertex. ED 2 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Select the shortest edge that connects an unknown vertex to any known vertex. DC 4 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm Select the shortest edge that connects an unknown vertex to any known vertex. EF 5 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Prim’s Algorithm All vertices have been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Start with A. Make it Known. HINT: Choose the least cost edge out of it. Add that node to the Known. 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice A connects to B, C, E Lowest cost is to E 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Now we consider anything connected to A or E. Nodes considered are B, C, D. Lowest cost to B. 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Now we consider anything connected to A, B, or E. Nodes considered are C, D. Lowest cost to D. 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Now we consider anything connected to A, B, D, or E. Nodes considered are C. Lowest cost is from D to C. 5 A B 3 2 E 10 7 6 4 DONE C D 1 Spring 2016 CSE373: Data Structures & Algorithms

Minimum Spanning Tree Algorithms Prim’s Algorithm for Minimum Spanning Tree Similar idea to Dijkstra’s Algorithm but for MSTs. Both based on expanding cloud of known vertices (basically using a priority queue instead of a DFS stack) Kruskal’s Algorithm for Minimum Spanning Tree Another, but different, greedy MST algorithm. Uses the Union-Find data structure. Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Spring 2016 CSE373: Data Structures & Algorithms

Kruskal’s Algorithm Pseudocode Sort edges by weight (min-heap) Each node in its own set (up-trees) While output size < |V|-1 Consider next smallest edge (u,v) if find(u) and find(v) indicate u and v are in different sets output (u,v) union(find(u),find(v)) Recall invariant: u and v in same set if and only if connected in output-so-far Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E), (D,E) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E), (D,E) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E), (D,E), (C,F) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E), (D,E), (C,F) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Example 2 Edges in sorted order: 1: (A,D), (C,D), (B,E), (D,E) 2: (A,B), (C,F), (A,C) 3: (E,G) 5: (D,G), (B,D) 6: (D,F) 10: (F,G) B A 1 1 5 2 E 1 D 1 3 5 C 6 2 G 10 F Output: (A,D), (C,D), (B,E), (D,E), (C,F), (E,G) Note: At each step, the union/find sets are the trees in the forest Spring 2016 CSE373: Data Structures & Algorithms

Kruskal’s Algorithm Analysis Idea: Grow a forest out of edges that do not grow a cycle, just like for the spanning tree problem. But now consider the edges in order by weight So: Sort edges: O(|E|log |E|) (next course topic) Iterate through edges using union-find for cycle detection almost O(|E|) Somewhat better: Floyd’s algorithm to build min-heap with edges O(|E|) Iterate through edges using union-find for cycle detection and deleteMin to get next edge O(|E| log |E|) Not better worst-case asymptotically, but often stop long before considering all edges. Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Select the edge with min cost ED 2 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Select the next minimum cost edge that does not create a cycle ED 2 AB 3 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 AE 4 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm Select the next minimum cost edge that does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Kruskal’s Algorithm All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 3 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Order the edges. (C,D) (E,B) (A,E) (B,D) (A,B) (E,D) (E,C) (A,C) 5 A B 3 2 E 10 7 6 4 C D 1 Spring 2016 CSE373: Data Structures & Algorithms

Done with graph algorithms! Next lecture… Sorting Spring 2016 CSE373: Data Structures & Algorithms