CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSE332: Data Abstractions Lecture 26: Minimum Spanning Trees Dan Grossman Spring 2010.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph (II) Shortest path, Minimum spanning tree GGuy
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 2012 August 13, 2012 CSE 332 Data Abstractions, Summer.
CSE332: Data Abstractions Lecture 24.5: Interlude on Intractability Dan Grossman Spring 2012.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
Graphs Chapter 20.
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Cse 373 May 8th – Dijkstras.
C.Eng 213 Data Structures Graphs Fall Section 3.
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Minimum Spanning Trees
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Minimum Spanning Tree.
Graphs Chapter 13.
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.
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Graphs.
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Chapter 11 Graphs.
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Minimum Spanning Trees
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 .
Minimum Spanning Trees
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees Linda Shapiro Winter 2016

CSE373: Data Structures & Algorithms Announcements HW 4 due Wed, May 18 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Done with Dijkstra You will implement Dijkstra’s algorithm in homework 5. Onward….. Spanning trees! Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal 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 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Observations a c |V|=4 |V|-1=3 b d 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 Problem ill-defined if original graph not connected So |E| ≥ |V|-1 A tree with |V| nodes has |V|-1 edges So every solution to the spanning tree problem has |V|-1 edges Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Spanning Trees Can we find another spanning tree in the bigger one? Pick a start node and think like a tree. 3 3 1 1 6 6 4 4 2 7 2 7 5 5 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Motivation A spanning tree connects all the nodes with as few edges as possible Example: A “phone tree” so everybody gets the message and no unnecessary calls get made 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 John Parihk Ezgi Shu Deepali Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Two Approaches Different algorithmic approaches to the (unweighted) 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 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Spanning tree via DFS spanning_tree(Graph G) { for each node i i.marked = false for some node i: f(i) } f(Node i) { i.marked = true for each j adjacent to i: if(!j.marked) { add(i,j) to output f(j) // DFS Correctness: DFS reaches each node. We add one edge to connect it to the already visited nodes. Order affects result, not correctness. Time: O(|E|) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) 2 1 3 7 4 6 5 Output: Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) 2 1 3 7 4 6 5 Output: (1,2) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) 2 1 3 7 4 6 5 Output: (1,2), (2,7) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) f(5) 2 1 3 7 4 6 5 Output: (1,2), (2,7), (7,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) f(5) f(4) 2 1 3 7 4 6 5 Output: (1,2), (2,7), (7,5), (5,4) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) f(5) f(4) f(3) 2 1 3 7 4 6 5 Output: (1,2), (2,7), (7,5), (5,4),(4,3) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) f(5) f(4) f(6) f(3) 2 1 3 7 4 6 5 Output: (1,2), (2,7), (7,5), (5,4), (4,3), (5,6) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Stack f(1) f(2) f(7) f(5) f(4) f(6) f(3) 2 1 3 7 4 6 5 Output: (1,2), (2,7), (7,5), (5,4), (4,3), (5,6) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Second Approach Iterate through edges; output any edge that does not create a cycle Correctness (hand-wavy): Goal is to build an acyclic connected graph When we add an edge, it adds a vertex to the tree Else it would have created a cycle The graph is connected, so we reach all vertices Efficiency: Depends on how quickly you can detect cycles Reconsider after the example Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4), (5,6), Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4), (5,6), (5,7) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4), (5,6), (5,7), (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4), (5,6), (5,7), (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 5 Output: (1,2), (3,4), (5,6), (5,7), (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) 2 1 3 7 4 6 Can stop once we have |V|-1 edges 5 Output: (1,2), (3,4), (5,6), (5,7), (1,5), (2,3) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Cycle Detection To decide if an edge could form a cycle is O(|V|) because we may need to traverse all edges already in the output So overall algorithm would be O(|V||E|) But there is a faster way we know Use union-find! Initially, each item is in its own 1-element set Union sets when we add an edge that connects them Stop when we have one set Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Using Disjoint-Sets Can use a disjoint-set implementation in our spanning-tree algorithm to detect cycles: Invariant: u and v are connected in output-so-far iff u and v in the same set Initially, each node is in its own set When processing edge (u,v): If find(u) equals find(v), then do not add the edge Else add the edge and union(find(u),find(v)) O(|E|) operations that are almost O(1) amortized Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {1} {2} {3} {4} {5} {6} {7} 2 1 3 7 4 6 5 Output: Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {1,2} {3} {4} {5} {6} {7} 2 1 3 7 4 6 5 Output: (1,2) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {1,2} {3,4} {5} {6} {7} 2 1 3 7 4 6 5 Output: (1,2) (3,4) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {1,2} {3,4} {5,6} {7} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {1,2} {3,4} {5,6,7} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) (5,7) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {3,4} {5,6,7,1,2} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) (5,7) (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {3,4} {5,6,7,1,2} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) (5,7) (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {3,4} {5,6,7,1,2} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) (5,7) (1,5) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Example Edges (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Sets: {3,4, 5,6,7,1,2} 2 1 3 7 4 6 5 Output: (1,2) (3,4) (5,6) (5,7) (1,5) (2,3) Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Problem Edges in arbitrary order: (2,5) (2,3) (1,2) (1,4) (2,4) (3,6) (3,5) (1,5) (2,6) (4,5) (5,6) 1 2 3 4 5 6 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Problem Edges in arbitrary order: (2,5) (2,3) (1,2) (1,4) (2,4) (3,6) (3,5) (1,5) (2,6) (4,5) (5,6) 1 2 3 (2,5) (2,3) (1,2) (1,4) (2,4) (3,6) {2,5} {2,3,5} {1,2,3,5) {1,2,3,4,5} {1,2,3,4,5,6} 4 5 6 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Practice Problem Edges in arbitrary order: (2,5) (2,3) (1,2) (1,4) (2,4) (3,6) (3,5) (1,5) (2,6) (4,5) (5,6) 1 2 3 (2,5) (2,3) (1,2) (1,4) (2,4) (3,6) 4 5 6 Spring 2016 CSE373: Data Structures & Algorithms

CSE373: Data Structures & Algorithms Summary So Far The spanning-tree problem Add nodes to partial tree approach is O(|E|) Add acyclic edges approach is almost O(|E|) Using union-find “as a black box” But really want to solve the minimum-spanning-tree problem Given a weighted undirected graph, give a spanning tree of minimum weight Same two approaches will work with minor modifications Both will be O(|E| log |V|) Spring 2016 CSE373: Data Structures & Algorithms

Minimum Spanning Tree Algorithms Algorithm #1 Shortest-path is to Dijkstra’s Algorithm as Minimum Spanning Tree is to Prim’s Algorithm (Both based on expanding cloud of known vertices, basically using a priority queue instead of a DFS stack) Algorithm #2 Kruskal’s Algorithm for Minimum Spanning Tree is Exactly our 2nd approach to spanning tree but process edges in cost order Spring 2016 CSE373: Data Structures & Algorithms