Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
CSE332: Data Abstractions Lecture 26: Minimum Spanning Trees Dan Grossman Spring 2010.
Advertisements

1 Spanning Trees Lecture 20 CS2110 – Spring
Discussion #36 Spanning Trees
Spanning Trees.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees
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.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
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.
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
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.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Spanning Trees.
Minimal Spanning Trees
Cse 373 May 8th – Dijkstras.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Instructor: Lilian de Greef Quarter: Summer 2017
CSCE350 Algorithms and Data Structure
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
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
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CSE 373: Data Structures and Algorithms
CSE 332: Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 22: Implementing Dijkstra’s
Topological Sorting Minimum Spanning Trees Shortest Path
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:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 18: Minimum Spanning Trees (Graphs) Instructor: Lilian de Greef Quarter: Summer 2017

Today Spanning Trees Minimum Spanning Trees Approach #1: DFS Approach #2: Add acyclic edges Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm

Announcements Midterms I brought midterms with me, can get them after class Next week, will only have them at CSE220 office hours Reminder: hw4 due on Friday!

Spanning Trees & Minimum Spanning Trees For undirected graphs

Introductory Example Spanning Tree! All the roads in Seattle are covered in snow. You were asked to shovel or plow snow from roads so that Seattle drivers can travel. Because you don’t want to shovel/plow that many roads, what is the smallest set of roads to clear in order to reconnect Seattle? Spanning Tree!

Spanning Trees Goal: 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

Observations 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 edges So every solution to the spanning tree problem has edges

Two Approaches 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

Approach #1: Using DFS (Example) Do a graph traversal, keeping track of edges that form a tree Stack: 1 2 3 4 5 6 7 Output:

Approach #1: Spanning Tree via DFS Correctness: DFS reaches each node. We add one edge to connect it to the already visited nodes. Order affects result, not correctness. Time: 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

Approach #2: Add Acyclic Edges (Example) Iterate through edges; add to output any edge that does not create a cycle 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) 1 2 3 4 5 6 7 Output:

Approach #2: Add Acyclic Edges 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 ( Not covered: there is a way to detect these cycles at almost average O(1) )

Summary So Far The spanning-tree problem – two approaches: Add nodes to partial tree approach Add acyclic edges approach More compelling: we have a weighted undirected graph and we want a spanning tree with minimum total weight a.k.a. the spanning-tree problem

Introductory Example: version 2 All the roads in Seattle are covered in snow. You were asked to shovel or plow snow from roads so that Seattle drivers can travel. Because you don’t want to shovel/plow that many roads, what is the smallest set of roads to clear in order to reconnect Seattle? Because you want to do the minimum amount of effort, what is the shortest total distance to clear in order to reconnect Seattle? A B C D F E G 2 1 5 6 3 10 Minimum Spanning Tree!

Minimum Spanning Tree: Example Uses How to most efficiently lay out… Telephone lines Electrical power lines Hydraulic pipes TV cables Computer networks (like the Internet!)

Minimum Spanning Tree Algorithms The minimum-spanning-tree problem Given a weighted undirected graph, give a spanning tree of minimum weight Same two approaches, with minor modifications, will work Algorithm for Unweighted Graph Similar Algorithm for Weighted Graph BFS for shortest path Algorithm (shortest path) DFS for spanning tree (minimum spanning tree) Adding acyclic edges approach for spanning tree

Prim’s Algorithm: Idea Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight that connects “known” to “unknown.” Recall Dijkstra “picked edge with closest known distance to source” That is not what we want here Otherwise identical (!)

Prim’s Algorithm: Pseudocode 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; }

Practice Time! Using Prim’s Algorithm starting at vertex A, what’s the minimum spanning tree? A B C D F E G 2 1 5 6 3 10 Let’s start here vertex known? cost prev A B C D E F G (A,B), (A,C), (A,D), (D,E), (C,F), (E,G) (B,E), (C,D), (D,A), (E,D), (F, C), (G,E) (B,A), (C,A), (D,A), (E,D), (F, C), (G,E) (B,A), (C,D), (D,A), (E,D), (F, C), (G,D)

(extra space for scratch-work)

Prim’s Algorithm: Example vertex known? cost prev A B C D E F G Let’s start here A B C D F E G 2 1 5 6 3 10

(extra space for scratch-work)

Analysis Correctness ?? Run-time 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

Kruskal’s Algorithm: Idea 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

Kruskal’s Algorithm: Pseudocode Sort edges by weight (better: put in min-heap) Each node in its own set While output size < |V|-1 Consider next smallest edge (u,v) If adding edge(u,v) doesn’t introduce cycles, output (u,v)

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

(extra space for scratch-work)

Kruskal’s Algorithm: Correctness Draw diagram!! It clearly generates a spanning tree. Call it TK. Suppose TK is not minimum: Pick another spanning tree Tmin with lower cost than TK Pick the smallest edge e1=(u,v) in TK that is not in Tmin Tmin already has a path p in Tmin from u to v  Adding e1 to Tmin will create a cycle in Tmin Pick an edge e2 in p that Kruskal’s algorithm considered after adding e1 (must exist: u and v unconnected when e1 considered)  cost(e2)  cost(e1)  can replace e2 with e1 in Tmin without increasing cost! Keep doing this until Tmin is identical to TK  TK must also be minimal – contradiction! We already know this makes a spanning tree because our maze generation algorithm made a spanning tree. But, does this find the minimum spanning tree? Let’s assume it doesn’t. Then, there’s some other better spanning tree. Let’s try and make that tree more like Kruskal’s tree. (otherwise, Kruskal’s would have considered and chosen e1 before ever reaching e2) BTW, this is another proof technique for showing that a greedy algorithm finds the global optimal. - For Dijkstra’s/Prim’s, we saw a proof of the type “greedy stays ahead” -- that is, we assume that there is some other algorithm which is optimal. Then we show that the greedy algorithm does the same thing (or better) as the optimal algorithm. - This time around, we use an “exchange argument” proof. We show that, through exchanges which do not affect the value of the greedy solution, that our greedy algorithm finds the optimal result.