22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets
|
|
- Lindsay Palmer
- 6 years ago
- Views:
Transcription
1 22c:1 Algorithms Kruskal s Algorithm for MST Union-Find for Disjoint Sets 1
2 Kruskal s Algorithm for MST Work with edges, rather than nodes Two steps: Sort edges by increasing edge weight Select the first V 1 edges that do not generate a cycle
3 Walk-Through 5 H A 10 F 8 B 2 G 6 C 1 E D Consider an undirected, weight graph
4 5 H A Sort the edges by increasing edge weight F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
5 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
6 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
7 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Accepting edge (E,G) would create a cycle
8 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
9 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
10 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
11 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
12 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
13 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
14 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
15 5 H A Select first V 1 edges which do not generate a cycle F 10 C edge d v edge d v (D,E) 1 (B,E) 8 6 (D,G) 2 (B,F) B 2 G 1 E D (E,G) (C,D) (G,H) (C,F) (B,C) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
16 F C Select first V 1 edges which do not generate a cycle edge d v edge d v 5 H A G B 2 1 E D (D,E) 1 (D,G) 2 (E,G) (C,D) (G,H) (C,F) (B,C) (B,E) (B,F) (B,H) (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 } not considered Done Total Cost = d v = 21
17 Complexity of Kruskal s Algorithm 1. Sort all the edges T = {} 2. For each edge e, if T U {e} doesn t have a cycle, T := T U { e } Step 1 takes O(E lg E), where E is # of edges. Step 2 takes O(E) using Disjoint Sets
18 Equivalence Relations Relation R : For every pair of elements (a, b) in a set S, a R b is either true or false. If a R b is true, then a is related to b. An equivalence relation satisfies: 1. (Reflexive) a R a 2. (Symmetric) a R b iff b R a. (Transitive) a R b and b R c implies a R c 18
19 Equivalence Classes Given a set of things { grapes, blackberries, plums, apples, oranges, peaches, raspberries, lemons, bananas } define the equivalence relation All citrus fruit is related, all berries, all stone fruits, partition them into related subsets { grapes }, { blackberries, raspberries }, { oranges, lemons }, { plums, peaches }, { apples }, { bananas } Everything in an equivalence class is related to each other. 19
20 Determining equivalence classes Idea: give every equivalence class a name { oranges, limes, lemons } = like-oranges { peaches, plums } = like-peaches Etc. To answer if two fruits are related: FIND the name of one fruit s e.c. FIND the name of the other fruit s e.c. Are they the same name? 20
21 Building Equivalence Classes Start with disjoint, singleton sets: { apples }, { bananas }, { peaches }, As you gain information about the relation, take UNION of sets that are now related: { peaches, plums }, { apples }, { bananas }, E.g. if peaches R limes, then we get { peaches, plums, limes, oranges, lemons } 21
22 Disjoint Union - Find Maintain a set of pairwise disjoint sets. {,5,7}, {,2,8}, {9}, {1,6} Each set has a unique name, one of its members {,5,7}, {,2,8}, {9}, {1,6} 22
23 Union Union(x,y) take the union of two sets named x and y {,5,7}, {,2,8}, {9}, {1,6} Union(5,1) {,5,7,1,6}, {,2,8}, {9}, 2
24 Find Find(x) return the name of the set containing x. {,5,7,1,6}, {,2,8}, {9}, Find(1) = 5 Find() = 8 2
25 Example S {1,2,7,8,9,1,19} {} {} {5} {6} {10} {11,17} {12} {1,20,26,27} {15,16,21}.. {22,2,2,29,9,2,,5,6} Find(8) = 7 Find(1) = 20 Union(7,20) S {1,2,7,8,9,1,19,1,20 26,27} {} {} {5} {6} {10} {11,17} {12} {15,16,21}.. {22,2,2,29,9,2,,5,6} 25
26 Implementing Disjoint Sets n elements, Total Cost of: m finds, n-1 unions Target complexity: O(m+n) i.e. O(1) amortized per operation. O(1) worst-case for find as well as union would be great, but Known result: find and union can be done practically in O(1) time 26
27 Implementing Disjoint Sets Observation: trees let us find many elements given one root Idea: if we reverse the pointers (make them point up from child to parent), we can find a single root from many elements Idea: Use one tree for each equivalence class. The name of the class is the tree root. 27
28 Up-Tree for Uinon/Find Initial state Intermediate state Roots are the names of each set. 6 28
29 Find Operation Find(x) follow x to the root and return the root Find(6) =
30 Union Operation Union(i,j) - assuming i and j roots, point i to j. 1 7 Union(1,7)
31 Simple Implementation Array of indices up Up[x] = 0 means x is a root
32 Union void Union(int[] up, int x, int y) { //precondition: x and y are roots Up[x] := y } Constant Time! 2
33 Exercise Design Find operator Recursive version Iterative version int Find(int[] up, int x) { //precondition: // x is in the range 0 to size-1??? }
34 A Bad Case 1 2 n 2 n Union(1,2) n n Union(2,) : : Union(n-1,n) 2 Find(1) n steps!! 1
35 Now this doesn t look good Can we do better? Yes! 1. Improve union so that find only takes O(log n) Union-by-size Reduces complexity to Θ(m log n + n) 2. Improve find so that it becomes even better! Path compression Reduces complexity to almost O(m + n) 5
36 Weighted Union Weighted Union Always point the smaller tree to the root of the larger tree W-Union(1,7)
37 Example Again 1 2 n 2 n Union(1,2) Union(2,) n : : Union(n-1,n) 1 2 n Find(1) constant time 7
38 Analysis of Weighted Union With weighted union an up-tree of height h has weight at least 2 h. Proof by induction Basis: h = 0. The up-tree has one node, 2 0 = 1 Inductive step: Assume true for all h < h. Minimum weight up-tree of height h formed by weighted unions T W(T 1 ) > W(T 2 ) > 2 h-1 T 1 T 2 h-1 Weighted union Induction hypothesis W(T) > 2 h h-1 = 2 h 8
39 Analysis of Weighted Union Let T be an up-tree of weight n formed by weighted union. Let h be its height. n > 2 h log 2 n > h Find(x) in tree T takes O(log n) time. Can we do better? 9
40 Worst Case for Weighted Union n/2 Weighted Unions n/ Weighted Unions 0
41 Example of Worst Cast (cont ) After n -1 = n/2 + n/ Weighted Unions log 2 n If there are n = 2 k nodes then the longest path from leaf to root has length k = log 2 (n). Find 1
42 Elegant Array Implementation up weight
43 Weighted Union void W_Union(int i,j){ //Pre: i and j are roots// int wi = weight[i]; int wj = weight[j]; if (wi < wj) { up[i] = j; weight[j] = wi + wj; } else { up[j] =i; weight[i] = wi + wj; } }
44 Path Compression On a Find operation point all the nodes on the search path directly to the root PC-Find()
45 Self-Adjustment Works PC-Find(x) x 5
46 Exercise: Draw the result of Find(e) c g a f h b d i e 6
47 Path Compression Find int PC_Find(int i) { int r = i; while (up[r]!= 0) //find root r = up[r]; if (i!= r) { //compress path// int k = up[i]; while (k!= r) { up[i] = r; i = k; k = up[k]; } } return r; } 7
48 Interlude: A Really Slow Function Ackermann s function is a really big function A(x, y) with inverse (x, y) which is really small. How fast does (x, y) grow? (x, y) = for x far larger than the number of atoms in the universe (2 00 ) shows up in: Computation Geometry (surface complexity) Combinatorics of sequences 8
49 A More Comprehensible Slow Function log* x = number of times you need to compute log to bring value down to at most 1 E.g. log* 2 = 1 log* = log* 2 2 = 2 log* 16 = log* 2 22 = (log log log 16 = 1) log* 6556 = log* = (log log log log 6556 = 1) log* = = 5 Take this: (m,n) grows even slower than log* n!! 9
50 Disjoint Union / Find with Weighted Union and Path Compression Worst case time complexity for a W-Union is O(1) and for a PC-Find is O(log n). Time complexity for m n operations on n elements is O(m log* n) Log * n < 7 for all reasonable n. Essentially constant time per operation! 50
51 Amortized Complexity For disjoint union / find with weighted union and path compression. average time per operation is essentially a constant. worst case time for a PC-Find is O(log n). An individual operation can be costly, but over time the average cost per operation is not. 51
52 Cute Application Build a random maze by erasing edges. 52
53 Pick Start and End Cute Application Start End 5
54 Cute Application Repeatedly pick random edges to delete. Start End 5
55 Desired Properties None of the boundary is deleted Every cell is reachable from every other cell. There are no cycles no cell can reach itself by a path unless it retraces some part of the path. 55
56 A Cycle, not allowed Start End 56
57 A Good Solution Start End 57
58 A Hidden Tree Start End 58
59 Number the Cells We have disjoint sets S ={ {1}, {2}, {}, {}, {6} } each cell is unto itself. We have all possible edges E ={ (1,2), (1,7), (2,8), (2,), } 60 edges total. Start End 59
60 Basic Algorithm S = set of sets of connected cells E = set of edges While there is more than one set in S pick a random, unused edge (x,y) from E u := Find(x); v := Find(y); if u v { Union(u,v); remove (x, y) from E } else mark (x, y) as used ; All remaining members of E form the maze 60
61 Example Step Pick (8,1) Start End S {1,2,7,8,9,1,19} {} {} {5} {6} {10} {11,17} {12} {1,20,26,27} {15,16,21}.. {22,2,2,29,0,2,,5,6} 61
62 S {1,2,7,8,9,1,19} {} {} {5} {6} {10} {11,17} {12} {1,20,26,27} {15,16,21}.. {22,2,2,29,9,2,,5,6} Example Find(8) = 7 Find(1) = 20 Union(7,20) S {1,2,7,8,9,1,19,1,20 26,27} {} {} {5} {6} {10} {11,17} {12} {15,16,21}.. {22,2,2,29,9,2,,5,6} 62
63 Start Example Pick (19,20) End S {1,2,7,8,9,1,19 1,20,26,27} {} {} {5} {6} {10} {11,17} {12} {15,16,21}.. {22,2,2,29,9,2,,5,6} 6
64 Example at the End Start End S {1,2,,,5,6,7, 6} 6
65 Problem 6-6 Suppose we are given the minimum spanning tree T of a given graph G (with n vertices and m edges) and a new edge e = (u; v) of weight w that we will add to G. Give an efficient algorithm to find the minimum spanning tree of the graph G + e. Your algorithm should run in O(n) time to receive full credit, although slower but correct algorithms will receive partial credit.
66 Problem [5] Let G = (V,E) be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source v.
67 Short Paths in a DAG If we update the edges in topologically sorted order, we correctly compute the shortest paths. Reason: the only paths to a vertex come from vertices before it in the topological sort. s
68 Code for Shortest Paths in DAG 1 DIJKSTRA(G, w, s) // DAG, weights, start vertex 2 for each vertex v in V[G] do d[v] p[v] NIL 5 d[s] 0 6 Q the topological order of V[G] starting at s 7 while Q is not empty do 8 u = EXTRACT-FIRST(Q) 9 for each vertex v in Adj[u] 10 d[v] = min{ d[v], d[u]+w(u, v)} 11 update p[v] if necessay
69 Shortest Paths in a DAG Theorem: For any vertex u in a DAG, if all the vertices before u in a topological sort of the dag have been updated, then d[u] is d(s,u), the shortest distance from s to u. Proof: By induction on the position of a vertex in the topological sort. Base case: d[s] is initialized to 0. Inductive hypothesis: Assume all vertices before u have been updated, and for all such vertices v, d[v] is d(s,v), the shortest distance
70 Proof, Continued Inductive case: Some edge (v,u) where v is before u, must be on the shortest path to u, since there are no other paths to u. When v was updated, we set d[u] to d[v]+w(v,u) = d(s,v) + w(v,u) = d(s,u) Running time: q(v+e), same as topological sort
Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics:
Today s Outline Disjoint Sets and Dynamic Equivalence Relations Announcements Assignment # due Thurs 0/ at pm Today s Topics: Disjoint Sets & Dynamic Equivalence CSE Data Structures and Algorithms 0//0
More informationReading. Disjoint Union / Find. Union. Disjoint Union - Find. Cute Application. Find. Reading. CSE 326 Data Structures Lecture 14
Reading Disjoint Union / Find Reading Chapter 8 CE Data tructures Lecture Disjoint Union/Find - Lecture Disjoint Union - Find Maintain a set of pairwise disjoint sets {,,, {,,8, {9, {, Each set has a unique
More informationCSE 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 informationCSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015
CSE: Data Structures & Algorithms Lecture : Implementing Union-Find Lauren Milne Spring 05 Announcements Homework due in ONE week Wednesday April 9 th! TA sessions Catie will be back on Monday. The plan
More informationCSE 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 informationUnion-Find: A Data Structure for Disjoint Set Operations
Union-Find: A Data Structure for Disjoint Set Operations 1 The Union-Find Data Structure Purpose: Great for disjoint sets Operations: Union ( S 1, S 2 ) Find ( x ) Performs a union of two disjoint sets
More informationAutumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem
Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning
More informationWeek 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 informationCSE 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 informationUnion-Find: A Data Structure for Disjoint Set Operations
Union-Find: A Data Structure for Disjoint Set Operations 1 Equivalence Relations A equivalence relation R is defined on a set S, if for every pair of elements (a,b) in S, a R b is either false or true
More informationDisjoint Sets. Based on slides from previous iterations of this course.
Disjoint Sets Based on slides from previous iterations of this course Today s Topics Exam Discussion Introduction to Disjointed Sets Disjointed Set Example Operations of a Disjointed Set Types of Disjointed
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Disjoin Sets and Union-Find Structures CLRS 21.121.4 University of Manitoba 1 / 32 Disjoint Sets Disjoint set is an abstract data type
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationCS 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 informationCSE332: 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 informationRepresentations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs
Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects
More informationLecture 10. Elementary Graph Algorithm Minimum Spanning Trees
Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo
More informationCSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo
CSE 431/531: Analysis of Algorithms Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast algorithms to solve
More informationMinimum 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 informationShortest path problems
Next... Shortest path problems Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra s Algorithm Bellman-Ford Algorithm Shortest-Paths
More informationUnion/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 informationAlgorithms (IX) Yijia Chen Shanghai Jiaotong University
Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest
More informationChapter 5. Greedy algorithms
Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem
More informationBuilding a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)
Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.
More informationCSE373: 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 informationLecture 5: Graph algorithms 1
DD2458, Problem Solving and Programming Under Pressure Lecture 5: Graph algorithms 1 Date: 2008-10-01 Scribe(s): Mikael Auno and Magnus Andermo Lecturer: Douglas Wikström This lecture presents some common
More informationCSE 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 informationSingle Source Shortest Path (SSSP) Problem
Single Source Shortest Path (SSSP) Problem Single Source Shortest Path Problem Input: A directed graph G = (V, E); an edge weight function w : E R, and a start vertex s V. Find: for each vertex u V, δ(s,
More informationCSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.
CSC 505, Fall 2000: Week 7 In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion. Page 1 of 18 Prim s algorithm without data structures
More informationDijkstra 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 informationMinimum 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 informationDisjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50
Disjoint Sets (Disjoint Sets) Data Structures and Programming Spring 2017 1 / 50 Making a Good Maze What s a Good Maze? Connected Just one path between any two rooms Random The Maze Construction Problem
More informationLecture 13: Minimum Spanning Trees Steven Skiena
Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Your job
More informationLecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY
Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem of the Day Your job is to
More informationUnit 2: Algorithmic Graph Theory
Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw
More informationCSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li
CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast
More informationCIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018
CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 Q1: Prove or disprove: You are given a connected undirected graph G = (V, E) with a weight function w defined over its
More informationCIS 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 informationLecture 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 informationGraph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]
Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E VxV. Then the pair G=(V,E) is called a directed graph.
More informationCOMP 251 Winter 2017 Online quizzes with answers
COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a
More informationPart VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths
Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1 Chapter 22 Elementary Graph Algorithms Representations of graphs
More informationCS 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 informationDisjoint Sets 3/22/2010
Disjoint Sets A disjoint-set is a collection C={S 1, S 2,, S k } of distinct dynamic sets. Each set is identified by a member of the set, called representative. Disjoint set operations: MAKE-SET(x): create
More information1 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 informationMinimum Spanning Trees
Minimum Spanning Trees 1 Minimum- Spanning Trees 1. Concrete example: computer connection. Definition of a Minimum- Spanning Tree Concrete example Imagine: You wish to connect all the computers in an office
More informationDisjoint Sets. The obvious data structure for disjoint sets looks like this.
CS61B Summer 2006 Instructor: Erin Korber Lecture 30: 15 Aug. Disjoint Sets Given a set of elements, it is often useful to break them up or partition them into a number of separate, nonoverlapping groups.
More informationCS521 \ Notes for the Final Exam
CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )
More informationlooking ahead to see the optimum
! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible
More informationMinimum Spanning Trees Ch 23 Traversing graphs
Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs
More informationAdvanced Set Representation Methods
Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation
More informationCOP 4531 Complexity & Analysis of Data Structures & Algorithms
COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 8 Data Structures for Disjoint Sets Thanks to the text authors who contributed to these slides Data Structures for Disjoint Sets Also
More informationCS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019
CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction
More informationLecture 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 informationComplexity 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 informationMinimum 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 informationHomework Assignment #3 Graph
CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are
More informationDisjoint Sets and the Union/Find Problem
Disjoint Sets and the Union/Find Problem Equivalence Relations A binary relation R on a set S is a subset of the Cartesian product S S. If (a, b) R we write arb and say a relates to b. Relations can have
More informationTheory 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 informationGreedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63
CSci 3110 Greedy Algorithms 1/63 Greedy Algorithms Textbook reading Chapter 4 Chapter 5 CSci 3110 Greedy Algorithms 2/63 Overview Design principle: Make progress towards a solution based on local criteria
More informationW4231: Analysis of Algorithms
W4231: Analysis of Algorithms 10/21/1999 Definitions for graphs Breadth First Search and Depth First Search Topological Sort. Graphs AgraphG is given by a set of vertices V and a set of edges E. Normally
More informationData Structures and Algorithms. Werner Nutt
Data Structures and Algorithms Werner Nutt nutt@inf.unibz.it http://www.inf.unibz/it/~nutt Chapter 10 Academic Year 2012-2013 1 Acknowledgements & Copyright Notice These slides are built on top of slides
More informationLecture 6 Basic Graph Algorithms
CS 491 CAP Intro to Competitive Algorithmic Programming Lecture 6 Basic Graph Algorithms Uttam Thakore University of Illinois at Urbana-Champaign September 30, 2015 Updates ICPC Regionals teams will be
More informationData Structures Brett Bernstein
Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of
More informationMinimum 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 informationWeek 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 informationGraph Algorithms (part 3 of CSC 282),
Graph Algorithms (part of CSC 8), http://www.cs.rochester.edu/~stefanko/teaching/10cs8 1 Schedule Homework is due Thursday, Oct 1. The QUIZ will be on Tuesday, Oct. 6. List of algorithms covered in the
More informationUC 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 informationAdvanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012
topological ordering, minimum spanning tree, Union-Find problem Jiří Vyskočil, Radek Mařík 2012 Subgraph subgraph A graph H is a subgraph of a graph G, if the following two inclusions are satisfied: 2
More informationData Structures for Disjoint Sets
Data Structures for Disjoint Sets Manolis Koubarakis 1 Dynamic Sets Sets are fundamental for mathematics but also for computer science. In computer science, we usually study dynamic sets i.e., sets that
More informationMinimum 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 informationfrom notes written mostly by Dr. Carla Savage: All Rights Reserved
CSC 505, Fall 2000: Week 9 Objectives: learn about various issues related to finding shortest paths in graphs learn algorithms for the single-source shortest-path problem observe the relationship among
More informationCSci 231 Final Review
CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle
More informationDecreasing 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 information2 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 informationGreedy algorithms is another useful way for solving optimization problems.
Greedy Algorithms Greedy algorithms is another useful way for solving optimization problems. Optimization Problems For the given input, we are seeking solutions that must satisfy certain conditions. These
More informationOutline. Graphs. Divide and Conquer.
GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.
More information2.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 informationFinal Examination CSE 100 UCSD (Practice)
Final Examination UCSD (Practice) RULES: 1. Don t start the exam until the instructor says to. 2. This is a closed-book, closed-notes, no-calculator exam. Don t refer to any materials other than the exam
More informationAlgorithms 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 informationShortest Path Problem
Shortest Path Problem CLRS Chapters 24.1 3, 24.5, 25.2 Shortest path problem Shortest path problem (and variants) Properties of shortest paths Algorithmic framework Bellman-Ford algorithm Shortest paths
More informationMinimum 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 informationAlgorithms 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 informationMinimum Spanning Tree (undirected graph)
1 Minimum Spanning Tree (undirected graph) 2 Path tree vs. spanning tree We have constructed trees in graphs for shortest path to anywhere else (from vertex is the root) Minimum spanning trees instead
More informationTutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].
Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source
More informationGreedy 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 informationCS 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 informationCSE 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 information6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro
6,8:15 MA/CSSE 473 Day 37 Student Questions Kruskal data structures Disjoint Set ADT Complexity intro Data Structures for Kruskal A sorted list of edges (edge list, not adjacency list) Edge e has fields
More informationStrongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.
Directed Graph In a directed graph, each edge (u, v) has a direction u v. Thus (u, v) (v, u). Directed graph is useful to model many practical problems (such as one-way road in traffic network, and asymmetric
More informationShortest Path Algorithms
Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E
More informationGraph Representation
Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]
More informationCSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)
Name: Sample Solution Email address: CSE 373 Autumn 0: Midterm # (closed book, closed notes, NO calculators allowed) Instructions: Read the directions for each question carefully before answering. We may
More informationOutlines: Graphs Part-2
Elementary Graph Algorithms PART-2 1 Outlines: Graphs Part-2 Graph Search Methods Breadth-First Search (BFS): BFS Algorithm BFS Example BFS Time Complexity Output of BFS: Shortest Path Breath-First Tree
More informationCSE 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 informationUnion-find algorithms. (1) How do you determine whether a vertex x is connected to a vertex y in a graph?
Union-find algorithms Consider the following problems: (1) How do you determine whether a vertex x is connected to a vertex y in a graph? (2) Let vertices correspond to objects and edges mean "is in the
More information