CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

Size: px
Start display at page:

Download "CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018"

Transcription

1 SE 33: Data Structures & Parallelism Lecture : Minimum Spanning Trees Ruth nderson Winter 08

2 Minimum Spanning Trees iven an undirected graph =(V,E), find a graph =(V, E ) such that: E is a subset of E E = V - is connected is a minimum spanning tree. c uv is minimal ( u, v) E' pplications: Example: Electrical wiring for a house or clock wires on a chip Example: road network if you cared about asphalt cost rather than travel time /8/08

3 Student ctivity j 4 7 ind the MST k 9 m 5 n B 7 6 H D 4 E /8/08 3

4 Two Different pproaches Prim s lgorithm lmost identical to Dijkstra s Kruskals s lgorithm ompletely different! /8/08 4

5 Two Different pproaches Prim s lgorithm lmost identical to Dijkstra s One node, grow greedily Kruskals s lgorithm ompletely different! orest of MSTs, Union them together. (Need a new data structure for this) /8/08 5

6 Prim s algorithm Idea: row a tree by picking a vertex from the unknown set that has the smallest cost. Here cost = cost of the edge that connects that vertex to the known set. Pick the vertex with the smallest cost that connects known to unknown. node-based greedy algorithm Builds MST by greedily adding nodes v known /8/08 6

7 Prim s lgorithm 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 ompare to slides in Dijkstra lecture! /8/08 7

8 Prim s lgorithm for MST. or each node v, set v.cost = and v.known = false. hoose any node v. (this is like your start vertex in Dijkstra) a) Mark v as known b) or each edge (v,u) with weight w: set u.cost=w and u.prev=v 3. While there are unknown nodes in the graph a) Select the unknown node v with lowest cost b) Mark v as known and add (v, v.prev) to output (the MST) c) or each edge (v,u) with weight w, if(w < u.cost) { } u.cost = w; u.prev = v; /8/08 8

9 Example: ind MST using Prim s B 5 E D vertex known? cost prev 0 B D Order added to known set: E /8/08 9

10 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set: vertex known? cost prev Y 0 B D E?????? /8/08 0

11 Example: ind MST using Prim s 0 B D 0 E 5 3 Order added to known set:, D 5 vertex known? cost prev Y 0 B D D Y E D 6 D 5 D /8/08

12 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set:, D, 5 vertex known? cost prev Y 0 B Y D D Y E D 5 D /8/08

13 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set:, D,, E 3 vertex known? cost prev Y 0 B E Y D D Y E Y D 3 E /8/08 3

14 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set:, D,, E, B 3 vertex known? cost prev Y 0 B Y E Y D D Y E Y D 3 E /8/08 4

15 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set:, D,, E, B, 3 vertex known? cost prev Y 0 B Y E Y D D Y E Y D Y 3 E /8/08 5

16 Example: ind MST using Prim s 0 B 6 5 D 0 E 5 3 Order added to known set:, D,, E, B,, 3 vertex known? cost prev Y 0 B Y E Y D D Y E Y D Y Y 3 E /8/08 6

17 Student ctivity Start with V ind MST using Prim s v 4 4 v v v 3 7 v 5 v V Kwn Distance path 5 8 v 4 6 v v3 v4 v5 v6 v7 V Order Declared Known: Total ost: v 7 /8/08 7

18 Prim s nalysis orrectness?? bit tricky Intuitively similar to Dijkstra Might return to this time permitting (unlikely) Run-time Same as Dijkstra O( E log V ) using a priority queue /8/08 8

19 Kruskal s MST lgorithm Idea: row a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. =(V,E) v /8/08 9

20 Kruskal s lgorithm for MST n edge-based greedy algorithm Builds MST by greedily adding edges. Initialize with empty MST all vertices marked unconnected all edges unmarked. While all vertices are not connected a. Pick the lowest cost edge (u,v) and mark it b. If u and v are not already connected, add (u,v) to the MST and mark u and v as connected to each other /8/08 0

21 side: Union-ind aka Disjoint Set DT Union(x,y) take the union of two sets named x and y iven sets: {3,5,7}, {4,,8}, {9}, {,6} Union(5,) Result: {3,5,7,,6}, {4,,8}, {9}, To perform the union operation, we replace sets x and y by (x y) ind(x) return the name of the set containing x. iven sets: {3,5,7,,6}, {4,,8}, {9}, ind() returns 5 ind(4) returns 8 We can do Union in constant time. We can get ind to be amortized constant time (worst case O(log n) for an individual ind operation). /8/08

22 Kruskal s pseudo code void raph::kruskal(){ int edgesccepted = 0; DisjSet s(num_verties); } while (edgesccepted < NUM_VERTIES ){ e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset!= vset){ edgesccepted++; s.unionsets(uset, vset); } } /8/08

23 Kruskal s pseudo code void raph::kruskal(){ int edgesccepted = 0; DisjSet s(num_verties); while (edgesccepted < NUM_VERTIES ){ e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset!= vset){ edgesccepted++; s.unionsets(uset, vset); } } } E finds V unions E heap ops /8/08 3

24 Kruskal s pseudo code void raph::kruskal(){ } int edgesccepted = 0; DisjSet s(num_verties); while (edgesccepted < NUM_VERTIES ){ } e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset!= vset){ } edgesccepted++; s.unionsets(uset, vset); E log E + E log V + V O( E log E ) = O( E log V ) b/c log E < log V = log V E finds V unions On heap of edges Deletemin = log E E heap ops One for each vertex in the edge ind = log V Union = O() /8/08 4

25 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: Note: t each step, the union/find sets are the trees in the forest /8/08 5

26 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D) Note: t each step, the union/find sets are the trees in the forest /8/08 6

27 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D) Note: t each step, the union/find sets are the trees in the forest /8/08 7

28 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E) Note: t each step, the union/find sets are the trees in the forest /8/08 8

29 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E), (D,E) Note: t each step, the union/find sets are the trees in the forest /8/08 9

30 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E), (D,E) Note: t each step, the union/find sets are the trees in the forest /8/08 30

31 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E), (D,E), (,) Note: t each step, the union/find sets are the trees in the forest /8/08 3

32 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E), (D,E), (,) Note: t each step, the union/find sets are the trees in the forest /8/08 3

33 Example: ind MST using Kruskal s D B E 5 3 Edges in sorted order: : (,D), (,D), (B,E), (D,E) : (,B), (,), (,) 3: (E,) 5: (D,), (B,D) 6: (D,) 0: (,) Output: (,D), (,D), (B,E), (D,E), (,), (E,) Note: t each step, the union/find sets are the trees in the forest /8/08 33

34 Student ctivity ind MST using Kruskal s 3 B H 4 D E 4 Total ost: Now find the MST using Prim s method. Under what conditions will these methods give the same result? /8/08 34

35 D B E 4 H Draw the UpTree Nodes B D E H Parent Size /8/08 35

36 orrectness Kruskal s algorithm is clever, simple, and efficient But does it generate a minimum spanning tree? How can we prove it? irst: it generates a spanning tree Intuition: raph started connected and we added every edge that did not create a cycle Proof by contradiction: Suppose u and v are disconnected in Kruskal s result. Then there s a path from u to v in the initial graph with an edge we could add without creating a cycle. But Kruskal would have added that edge. ontradiction. Second: There is no spanning tree with lower total cost /8/08 36

37 The inductive proof set-up Let (stands for forest ) be the set of edges Kruskal has added at some point during its execution. laim: is a subset of one or more MSTs for the graph (Therefore, once = V -, we have an MST.) Proof: By induction on Base case: =0: The empty set is a subset of all MSTs Inductive case: =k+: By induction, before adding the (k+) th edge (call it e), there was some MST T such that -{e} T /8/08 37

38 Staying a subset of some MST laim: is a subset of one or more MSTs for the graph So far: -{e} T: Two disjoint cases: If {e} T: Then T and we re done Else e forms a cycle with some simple path (call it p) in T Must be since T is a spanning tree /8/08 38

39 Staying a subset of some MST laim: is a subset of one or more MSTs for the graph So far: -{e} T and e forms a cycle with p T e There must be an edge e on p such that e is not in Else Kruskal would not have added e laim: e.weight == e.weight /8/08 39

40 Staying a subset of some MST laim: is a subset of one or more MSTs for the graph So far: -{e} T e forms a cycle with p T e on p is not in e e laim: e.weight == e.weight If e.weight > e.weight, then T is not an MST because T-{e}+{e} is a spanning tree with lower cost: contradiction If e.weight < e.weight, then Kruskal would have already considered e. It would have added it since T has no cycles and -{e} T. But e is not in : contradiction /8/08 40

41 Staying a subset of some MST laim: is a subset of one or more MSTs for the graph So far: -{e} T e forms a cycle with p T e on p is not in e.weight == e.weight e e laim: T-{e}+{e} is an MST It s a spanning tree because p-{e}+{e} connects the same nodes as p It s minimal because its cost equals cost of T, an MST Since T-{e}+{e}, is a subset of one or more MSTs Done. /8/08 4

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015 CSE33: Data Abstractions Lecture 5: Minimum Spanning Trees Ruth Anderson via Conrad Nied Winter 05 A quick note about Gradescope 3/06/05 Today s XKCD 3/06/05 3 You guys are awesome 3/06/05 4 Do you still

More information

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS Spanning Trees S: ata Structures & lgorithms Lecture : Minimum Spanning Trees simple problem: iven a connected undirected graph =(V,), find a minimal subset of edges such that is still connected graph

More information

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013 CSE373: Data Structures & Algorithms Lecture 7: Minimum Spanning Trees Dan Grossman Fall 03 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such

More information

Announcements. HW4 due Friday. Summer 2016 CSE373: Data Structures & Algorithms 1

Announcements. HW4 due Friday. Summer 2016 CSE373: Data Structures & Algorithms 1 Announcements HW4 due Friday Summer 0 Minimum Spanning Trees Hunter Zahn Summer 0 Summer 0 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such

More information

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees CSE 33 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 0 August 3, 0 CSE 33 Data Abstractions, Summer 0 Making Connections You have a set of nodes (numbered -9)

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29 Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms

More information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

Kruskal s MST Algorithm

Kruskal s MST Algorithm Kruskal s MST Algorithm CLRS Chapter 23, DPV Chapter 5 Version of November 5, 2014 Main Topics of This Lecture Kruskal s algorithm Another, but different, greedy MST algorithm Introduction to UNION-FIND

More information

6.1 Minimum Spanning Trees

6.1 Minimum Spanning Trees CS124 Lecture 6 Fall 2018 6.1 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND CSE 373 MAY 0 TH SPANNING TREES AND UNION FIND COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend HW5 out tomorrow

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

Minimum Spanning Trees

Minimum Spanning Trees CS124 Lecture 5 Spring 2011 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016 CS125 Lecture 2 Fall 2016 2.1 Greedy Algorithms We will start talking about methods high-level plans for constructing algorithms. One of the simplest is just to have your algorithm be greedy. Being greedy,

More information

Minimum Spanning Trees. CSE 373 Data Structures

Minimum Spanning Trees. CSE 373 Data Structures Minimum Spanning Trees CSE 373 Data Structures Reading Chapter 3 Section 3.7 MSTs 2 Spanning Tree Given (connected) G(V,E) a spanning tree T(V,E ): Spans the graph (V = V) Forms a tree (no cycle); E has

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, 2003 Notes 8 for CS 170 1 Minimum Spanning Trees A tree is an undirected graph that is connected

More information

CSE 100 Disjoint Set, Union Find

CSE 100 Disjoint Set, Union Find CSE 100 Disjoint Set, Union Find Union-find using up-trees The union-find data structure 1 0 2 6 7 4 3 5 8 Perform these operations: Find(4) =! Find(3) =! Union(1,0)=! Find(4)=! 2 Array representation

More information

Minimum Spanning Trees and Shortest Paths

Minimum Spanning Trees and Shortest Paths Minimum Spanning Trees and Shortest Paths Prim's algorithm ijkstra's algorithm November, 017 inda eeren / eoffrey Tien 1 Recall: S spanning tree Starting from vertex 16 9 1 6 10 13 4 3 17 5 11 7 16 13

More information

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP) Spanning Tree (015) Lecture1: Graph III ohyung Han S, POSTH bhhan@postech.ac.kr efinition and property Subgraph that contains all vertices of the original graph and is a tree Often, a graph has many different

More information

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, ) Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 1. if >. 2. error new key is greater than current key 3.. 4.. 5. if NIL and.

More information

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

Chapter 23. Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees Chapter 23. Minimum Spanning Trees We are given a connected, weighted, undirected graph G = (V,E;w), where each edge (u,v) E has a non-negative weight (often called length) w(u,v). The Minimum Spanning

More information

Minimum Spanning Trees and Shortest Paths

Minimum Spanning Trees and Shortest Paths Minimum Spanning Trees and Shortest Paths Kruskal's lgorithm Prim's lgorithm Shortest Paths pril 04, 018 inda eeren / eoffrey Tien 1 Kruskal's algorithm ata types for implementation Kruskalslgorithm()

More information

Week 11: Minimum Spanning trees

Week 11: Minimum Spanning trees Week 11: Minimum Spanning trees Agenda: Minimum Spanning Trees Prim s Algorithm Reading: Textbook : 61-7 1 Week 11: Minimum Spanning trees Minimum spanning tree (MST) problem: Input: edge-weighted (simple,

More information

Minimum Spanning Trees

Minimum Spanning Trees CSMPS 2200 Fall Minimum Spanning Trees Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 11/6/ CMPS 2200 Intro. to Algorithms 1 Minimum spanning trees Input: A

More information

CSE 373 Data Structures & Algorithms

CSE 373 Data Structures & Algorithms CSE 373 Data Structures & Algorithms Lectures 24 Final Review 1 Third Midterm (a.k.a. Final) Friday, 12:30 1:30, here in class Logistics: Closed Book Comprehensive Everything up to and including Network

More information

Lecture Notes for Chapter 23: Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees Lecture Notes for Chapter 23: Minimum Spanning Trees Chapter 23 overview Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has

More information

Single Source, Shortest Path Problem

Single Source, Shortest Path Problem Lecture : From ijkstra to Prim Today s Topics: ijkstra s Shortest Path lgorithm epth First Search Spanning Trees Minimum Spanning Trees Prim s lgorithm overed in hapter 9 in the textbook Some slides based

More information

Minimum Spanning Trees My T. UF

Minimum Spanning Trees My T. UF Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication

More information

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Dijkstra s algorithm for shortest paths when no edges have negative weight. Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

More information

Section 7: Dijkstra s

Section 7: Dijkstra s Section 7: ijkstra s Slides by lex Mariakakis with material Kellen onohue, avid Mailhot, and an rossman Late days Things to iscuss o cse33-lateday@cs.washington.edu o 3 assignments left o an use late days

More information

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees CSE1 Introduction to Algorithms Lecture 1 Minimum Spanning Trees Antoine Vigneron antoine@unist.ac.kr Ulsan National Institute of Science and Technology July 11, 201 Antoine Vigneron (UNIST) CSE1 Lecture

More information

Greedy Algorithms Part Three

Greedy Algorithms Part Three Greedy Algorithms Part Three Announcements Problem Set Four due right now. Due on Wednesday with a late day. Problem Set Five out, due Monday, August 5. Explore greedy algorithms, exchange arguments, greedy

More information

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree CSC 1700 Analysis of Algorithms: Minimum Spanning Tree Professor Henry Carter Fall 2016 Recap Space-time tradeoffs allow for faster algorithms at the cost of space complexity overhead Dynamic programming

More information

2 A Template for Minimum Spanning Tree Algorithms

2 A Template for Minimum Spanning Tree Algorithms CS, Lecture 5 Minimum Spanning Trees Scribe: Logan Short (05), William Chen (0), Mary Wootters (0) Date: May, 0 Introduction Today we will continue our discussion of greedy algorithms, specifically in

More information

Priority queue ADT part 1

Priority queue ADT part 1 http://www.mainjava.com/java/core-java/priorityqueue-class-in-java-with-programming-example/ Priority queue ADT part 1 CS 146 - Spring 2017 Today Dijkstra s algorithm The minimum spanning tree problem

More information

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree. Week 9 Student Responsibilities Reading: hapter 3.3 3. (Tucker),..5 (Rosen) Mat 3770 Spring 01 Homework Due date Tucker Rosen 3/1 3..3 3/1 DS & S Worksheets 3/6 3.3.,.5 3/8 Heapify worksheet ttendance

More information

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445 CS 5 Union/Find Aka: Disjoint-set forest Alon Efrat Problem definition Given: A set of atoms S={1, n E.g. each represents a commercial name of a drugs. This set consists of different disjoint subsets.

More information

CS 161: Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2015S-18 Spanning Trees David Galles Department of Computer Science University of San Francisco 18-0: Spanning Trees Given a connected, undirected graph G A subgraph

More information

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees CIS 121 Data Structures and Algorithms Minimum Spanning Trees March 19, 2019 Introduction and Background Consider a very natural problem: we are given a set of locations V = {v 1, v 2,..., v n }. We want

More information

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees msc20 Intro to lgorithms hapter. Greedy lgorithms: Spanning Trees and Minimum Spanning Trees The concept is relevant to connected undirected graphs. Problem: Here is a diagram of a prison for political

More information

Minimum-Cost Spanning Tree. Example

Minimum-Cost Spanning Tree. Example 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 cost Example 2 4 12 6 3 Network has 10 edges.

More information

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies 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 cost Example 2 4 12 6 3 Network has 10 edges.

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

Announcements Problem Set 5 is out (today)!

Announcements Problem Set 5 is out (today)! CSC263 Week 10 Announcements Problem Set is out (today)! Due Tuesday (Dec 1) Minimum Spanning Trees The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each

More information

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding We ve done Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding Matroid Theory Now Matroids and weighted matroids Generic

More information

CS 561, Lecture 9. Jared Saia University of New Mexico

CS 561, Lecture 9. Jared Saia University of New Mexico CS 561, Lecture 9 Jared Saia University of New Mexico Today s Outline Minimum Spanning Trees Safe Edge Theorem Kruskal and Prim s algorithms Graph Representation 1 Graph Definition A graph is a pair of

More information

Outline and Reading. Minimum Spanning Tree. Minimum Spanning Tree. Cycle Property. Minimum Spanning Trees ( 12.7)

Outline and Reading. Minimum Spanning Tree. Minimum Spanning Tree. Cycle Property. Minimum Spanning Trees ( 12.7) Outline and Reading Minimum Spanning Tree PV 1 1 1 1 JK 1 15 SO 11 1 LX 15 Minimum Spanning Trees ( 1.) efinitions crucial fact Prim-Jarnik s lgorithm ( 1..) Kruskal s lgorithm ( 1..1) 111 // :1 PM Minimum

More information

Greedy Algorithms. COMP 215 Lecture 6

Greedy Algorithms. COMP 215 Lecture 6 Greedy Algorithms COMP 215 Lecture 6 Wrapping Up DP A few words on traveling salesperson problem. The problem. Brute force algorithm. Dynamic programming algorithm. Greedy Algorithms If we can view our

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo ALORITHM DSIN RDY ALORITHMS University of Waterloo LIST OF SLIDS - List of Slides reedy Approaches xample: Making Change 4 Making Change (cont.) 5 Minimum Spanning Tree 6 xample 7 Approaches that Don t

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Dec 7, 2016 Lecture 38: Union-Find Assignment 10: Due Monday Dec 5 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

Complexity of Prim s Algorithm

Complexity of Prim s Algorithm The main loop is: Complexity of Prim s Algorithm while ( not ISEMPTY(Q) ): u = EXTRACT-MIN(Q) if p[u]!= NIL: A = A U {(p[u],u)} for v in adjacency-list[u]: if v in Q and w(u,v) < priority[v] : DECREASE-PRIORITY(v,

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 0 Advanced Data Structures and Algorithms Weighted Graphs July 0, 07 Tong Wang UMass Boston CS 0 July 0, 07 / Weighted Graphs Each edge has a weight (cost) Edge-weighted graphs Mostly we consider only

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 17: Finish Dijkstra s Algorithm, Preserving Abstractions (Software Design), Spanning Trees Instructor: Lilian de Greef Quarter: Summer 2017 Dijkstra s Algorithm

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8 Union Find and Greedy Algorithms CSE 101: Design and Analysis of Algorithms Lecture 8 CSE 101: Design and analysis of algorithms Union find Reading: Section 5.1 Greedy algorithms Reading: Kleinberg and

More information

Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree 1 Minimum Spanning Tree G=(V,E) is an undirected graph, where V is a set of nodes and E is a set of possible interconnections between pairs of nodes. For each edge (u,v) in E, we

More information

CSE 100: UNION-FIND HASH

CSE 100: UNION-FIND HASH CSE 100: UNION-FIND HASH Run time of Simple union and find (using up-trees) If we have no rule in place for performing the union of two up trees, what is the worst case run time of find and union operations

More information

CSE 326: Data Structures Dijkstra s Algorithm. James Fogarty Autumn 2007

CSE 326: Data Structures Dijkstra s Algorithm. James Fogarty Autumn 2007 SE 6: Data Structures Dijkstra s lgorithm James Fogarty utumn 007 Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer

More information

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 9 Greedy Technique Copyright 2007 Pearson Addison-Wesley. All rights reserved. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that

More information

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms Professor Henry Carter Fall 2016 Recap Greedy algorithms iterate locally optimal choices to construct a globally optimal solution

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees 04 6 33 135 49 1 40 146 61 14 15 0 111 34 Minimum Spanning Trees 1 Minimum Spanning Trees ( 1.) Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees 04 6 33 135 49 PVD 1 40 146 61 JFK 14 15 0 111 34 Minimum Spanning Trees 1 Outline and Reading Minimum Spanning Trees Definitions A crucial fact The Prim-Jarnik Algorithm Kruskal's

More information

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Greedy Algorithms. At each step in the algorithm, one of several choices can be made. Greedy Algorithms At each step in the algorithm, one of several choices can be made. Greedy Strategy: make the choice that is the best at the moment. After making a choice, we are left with one subproblem

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 17: Finish Dijkstra s Algorithm, Preserving Abstractions (Software Design), Spanning Trees Instructor: Lilian de Greef Quarter: Summer 2017 Today Wrap up

More information

Minimum spanning trees

Minimum spanning trees Minimum spanning trees [We re following the book very closely.] One of the most famous greedy algorithms (actually rather family of greedy algorithms). Given undirected graph G = (V, E), connected Weight

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 6: Dijkstra s Algorithm (Graphs) Instructor: Lilian de Greef Quarter: Summer 207 Today Announcements Graph Traversals Continued Remarks on DFS & BFS Shortest

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees 04 6 33 49 PVD 1 40 146 61 14 15 0 Minimum Spanning Trees 1 Minimum Spanning Trees ( 1.) Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning tree 1

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 9: Minimum Spanning Trees Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: MST cut and cycle properties Prim, Kruskal greedy algorithms

More information

Partha Sarathi Manal

Partha Sarathi Manal MA 515: Introduction to Algorithms & MA5 : Design and Analysis of Algorithms [-0-0-6] Lecture 20 & 21 http://www.iitg.ernet.in/psm/indexing_ma5/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.

More information

Outline. Computer Science 331. Analysis of Prim's Algorithm

Outline. Computer Science 331. Analysis of Prim's Algorithm Outline Computer Science 331 Analysis of Prim's Algorithm Mike Jacobson Department of Computer Science University of Calgary Lecture #34 1 Introduction 2, Concluded 3 4 Additional Comments and References

More information

Priority Queues. 04/10/03 Lecture 22 1

Priority Queues. 04/10/03 Lecture 22 1 Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method

More information

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES Announcements Algorithms ROBERT SEDGEWICK KEVIN WAYNE Exam Regrades Due by Wednesday s lecture. Teaching Experiment: Dynamic Deadlines (WordNet) Right now, WordNet is due at PM on April 8th. Starting Tuesday

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees CS : Advanced Algorithms Minimum Spanning Trees Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Minimum Spanning

More information

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof.

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof. Preliminary Denitions Graph Algorithms There is a lot of terminology associated with graphs and graph algorithms, and so we start by dening the terms we will use for the remainder of this chapter. Denition.

More information

Minimum Spanning Trees Shortest Paths

Minimum Spanning Trees Shortest Paths Minimum Spanning Trees Shortest Paths Minimum Spanning Tree Given a set of locations, with positive distances to each other, we want to create a network that connects all nodes to each other with minimal

More information

Minimum Spanning Trees. COMPSCI 355 Fall 2016

Minimum Spanning Trees. COMPSCI 355 Fall 2016 Minimum Spanning Trees COMPSCI all 06 Spanning Tree Spanning Tree Spanning Tree Algorithm A Add any edge that can be added without creating a cycle. Repeat until the edges form a spanning tree. Algorithm

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 018 D/Q Greed SP s DP LP, Flow B&B, Backtrack Metaheuristics P, NP Design and Analysis of Algorithms Lecture 8: Greed Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Optimization

More information

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg CSE 1 Greedy Alg: Union Find/Dijkstra s Alg Shayan Oveis Gharan 1 Dijkstra s Algorithm Dijkstra(G, c, s) { d s 0 foreach (v V) d[v] //This is the key of node v foreach (v V) insert v onto a priority queue

More information

MST & Shortest Path -Prim s -Djikstra s

MST & Shortest Path -Prim s -Djikstra s MST & Shortest Path -Prim s -Djikstra s PRIM s - Minimum Spanning Tree - A spanning tree of a graph is a tree that has all the vertices of the graph connected by some edges. - A graph can have one or more

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 10 Implementing MST Algorithms Adam Smith Minimum spanning tree (MST) Input: A connected undirected graph G = (V, E) with weight function w : E R. For now, assume

More information

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck Theory of Computing Lecture 10 MAS 714 Hartmut Klauck Data structures: Union-Find We need to store a set of disjoint sets with the following operations: Make-Set(v): generate a set {v}. Name of the set

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms, Lecture 1 /1/200 Introduction to Algorithms.04J/1.401J LECTURE 11 Graphs, MST, Greedy, Prim Graph representation Minimum spanning trees Greedy algorithms hallmarks. Greedy choice

More information

Depth-first Search (DFS)

Depth-first Search (DFS) Depth-first Search (DFS) DFS Strategy: First follow one path all the way to its end, before we step back to follow the next path. (u.d and u.f are start/finish time for vertex processing) CH08-320201:

More information

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck Theory of Computing Lecture 10 MAS 714 Hartmut Klauck Seven Bridges of Königsberg Can one take a walk that crosses each bridge exactly once? Seven Bridges of Königsberg Model as a graph Is there a path

More information

CS 6783 (Applied Algorithms) Lecture 5

CS 6783 (Applied Algorithms) Lecture 5 CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7 Minimum Spanning Trees and Union Find CSE 101: Design and Analysis of Algorithms Lecture 7 CSE 101: Design and analysis of algorithms Minimum spanning trees and union find Reading: Section 5.1 Quiz 1 is

More information

23.2 Minimum Spanning Trees

23.2 Minimum Spanning Trees 23.2 Minimum Spanning Trees Kruskal s algorithm: Kruskal s algorithm solves the Minimum Spanning Tree problem in O( E log V ) time. It employs the disjoint-set data structure that is similarly used for

More information

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms Elementary Graph Algorithms: Summary CmSc250 Intro to Algorithms Definition: A graph is a collection (nonempty set) of vertices and edges A path from vertex x to vertex y : a list of vertices in which

More information