May 12th – Minimum Spanning Trees

Slides:



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

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
CSE332: Data Abstractions Lecture 26: Minimum Spanning Trees Dan Grossman Spring 2010.
Discussion #36 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.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
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 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.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 2007 Data Structures II Chapter 14 Graphs III.
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 Trees
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
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
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Spanning Trees.
Cse 373 May 8th – Dijkstras.
Minimum Spanning Trees and Shortest Paths
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Minimum Spanning Trees
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.
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Spanning Trees.
Hannah Tang and Brian Tjaden Summer Quarter 2002
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Lecture 19: Minimum Spanning Trees
Graphs.
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum-Cost Spanning Tree
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 .
Presentation transcript:

May 12th – Minimum Spanning Trees Cse 373 May 12th – Minimum Spanning Trees

Assorted Minutiae HW5 is out Write up has a Minimum spanning tree question, which we’re covering today Code due next Wednesday, as usual, Write up will be due on Friday. H2 finally graded 10 or so students no grade yet, out this weekend. Problems with the script. Feedback on HW4 code out this weekend

Today’s Lecture Minimum Spanning Trees Prim’s Algorithm (vertex based solution) Kruskal’s Algorithm (edge based solution)

Spanning Trees Given a connected undirected graph G=(V,E), find a subset of edges such that G is still connected A graph G2=(V,E2) such that G2 is connected and removing any edge from E2 makes G2 disconnected

Observations Problem not defined if original graph not connected. Therefore, we know |E| >= |V|-1 Any solution to this problem is a tree Recall a tree does not need a root; just means acyclic For any cycle, could remove an edge and still be connected Solution not unique unless original graph was already a tree A tree with |V| nodes has |V|-1 edges So every solution to the spanning tree problem has |V|-1 edges

Motivation A spanning tree connects all the nodes with as few edges as possible In most compelling uses, we have a weighted undirected graph and we want a tree of least total cost Example: Electrical wiring for a house or clock wires on a chip Example: A road network if you cared about asphalt cost rather than travel time This is the minimum spanning tree problem

Last Class Different algorithmic approaches to the spanning-tree problem: Do a graph traversal (e.g., depth-first search, but any traversal will do), keeping track of edges that form a tree Iterate through edges; add to output any edge that does not create a cycle

Spanning tree via Traversal spanning_tree(Graph G) { for each node v: v.marked = false dfs(someRandomStartNode) } dfs(Vertex a) { // recursive DFS a.marked = true for each b adjacent to a: if(!b.marked) { add(a,b) to output dfs(b)

Minimal Spanning Trees How do we get a minimal spanning tree from a traversal? What parts of a traversal can we change? Select which vertex we visit next by which is closest to an old vertex

Prim’s Algorithm A traversal Pick a start node Keep track of all of the vertices you can reach Add the vertex that is closest (has the edge with smallest weight) to the current spanning tree. Is this similar to something we’ve seen before?

Prim’s Algorithm Modify Dijkstra’s algorithm Instead of measuring the total length from start to the new vertex, now we only care about the edge from our current spanning tree to new nodes

The 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; }

Example   2 B A 1  1 5 2 E  1 D 1 3 5 C   6 2 G  10 F vertex known? cost prev A  B C D E F G  10 F

2 2 B A 1  1 5 2 E 1 1 D 1 3 5 C 2  6 2 G  10 F vertex known? cost 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

2 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 5 6 2 G 6 10 F vertex known? cost 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

2 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 5 6 2 G 2 10 F vertex known? cost 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

1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G 2 10 F vertex known? cost 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

1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G 2 10 F vertex known? cost 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

1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G 2 10 F vertex known? cost 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

1 2 B A 1 1 1 5 2 E 1 1 D 1 3 5 C 2 3 6 2 G 2 10 F vertex known? cost 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

Prim’s Algorithm Does this give us the correct solution? Why? If we consider the “known” cloud as a single vertex, we will never add edges that form a cycle Each time, we take the edge that has minimal weight going out of the vertex. This is the cheapest way of connecting the two subgraphs.

Prim’s Algorithm What is the runtime? Traversals go through all of the edges, in the worst case Need to check if an edge forms a cycle or if it has minimal weight. We can check if it forms a cycle by verifying if the other vertex is in the “known cloud” O(1) How long to check if it is minimal? O(log |V|) if we use a priority queue

Prim’s Algorithm O(|E| log |V|) We can use a priority queue to store all of our vertices, and let the edges to them be the priority. Use the decreaseKey() function when the edge to a vertex changes. This also works for Dijkstra’s algorithm, but you aren’t required to do it for HW5 Without the priority queue, both Prim’s and Dijkstra’s run in O(|E||V|)

Kruskal’s ALgorithm Prim’s algorithm works from the vertices, and builds a contiguous spanning tree The spanning tree grows out from a single vertex Kruskal’s Algorithm adds edges based on their weight Must check for cycles Use the union-find data structure to speed up this operation

Kruskal’s ALgorithm Pseudocode: Sort the edges (or place them into a heap) Create a union-find data structure with all separate vertices For each edge, add it to the minimum spanning tree if the two vertices don’t have the same representative in the union find Union the two vertices in the union find Stop after you’ve added |V|-1 edges

Example 2 B A 1 1 5 2 E 1 D 1 3 5 C 6 G 2 10 F Output: 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

Example 2 B A 1 1 5 2 E 1 D 1 3 5 C 6 G 2 10 F Output: (A,D) 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

Example 2 B A 1 1 5 2 E 1 D 1 3 5 C 6 G 2 10 F Output: (A,D), (C,D) 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

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

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

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

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

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

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

KruskaL’s Algorithm Runtime Put edges into a heap O(|E|) Floyd’s method! Until the MST is complete: Pull the minimum edge out of the heap O(log |E|) Check if it forms a cycle O(log |V|) How many times does the loop run? O(E) O(|E| log |E|)

Comparisons Prim’s O(|E| log |V|) Kruskal’s O(|E| log |E|) Since |E| must be at least |V|-1 for the graph to be connected, which do we prefer?

Comparisons Prim’s O(|E| log |V|) Kruskal’s O(|E| log |E|) Since |E| must be at least |V|-1 for the graph to be connected, which do we prefer? Since |E| is at most |V|2, log|E| is at most log(|V|2) which is 2log|V|. So log|E| is O(log|V|)

Conclusions Prim’s and Kruskal’s both run in O(|E| log |V|) An undirected graph has a unique minimum spanning tree if all of its edge weights are unique. If graphs have multiple edges of the same weight, it is possible (but not necessary) that there are many spanning trees of the same weight

Next Week Graph algorithm runtimes Conclude Graphs New Algorithm Analysis technique Recurrences Start sorting