CSE 100: GRAPH ALGORITHMS

Similar documents
CSE 100 Disjoint Set, Union Find

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100: UNION-FIND HASH

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

CS 310 Advanced Data Structures and Algorithms

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

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

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

Lecture Summary CSC 263H. August 5, 2016

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSE 100: GRAPH ALGORITHMS

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

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

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

Lecture 13: Minimum Spanning Trees Steven Skiena

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

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

Greedy Approach: Intro

Minimum Spanning Trees. CSE 373 Data Structures

Algorithm Design (8) Graph Algorithms 1/2

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

CS 161: Design and Analysis of Algorithms

Complexity of Prim s Algorithm

Minimum Spanning Trees My T. UF

looking ahead to see the optimum

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

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

Minimum Spanning Trees

Minimum Spanning Trees

6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro

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

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

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

Course Review for Finals. Cpt S 223 Fall 2008

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

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

Introduction to Optimization

2 A Template for Minimum Spanning Tree Algorithms

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

23.2 Minimum Spanning Trees

CS : Data Structures

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Chapter 5. Greedy algorithms

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Disjoint Sets and the Union/Find Problem

Lecture 6 Basic Graph Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

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

Chapter 23. Minimum Spanning Trees

COMP 251 Winter 2017 Online quizzes with answers

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum-Cost Spanning Tree. Example

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

CMSC 341 Lecture 20 Disjointed Sets

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Priority queue ADT part 1

Homework Assignment #3 Graph

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Week 12: Minimum Spanning trees and Shortest Paths

CS521 \ Notes for the Final Exam

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

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

CSci 231 Final Review

CS302 - Data Structures using C++

Algorithm Design and Analysis

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

) $ f ( n) " %( g( n)

Final Examination CSE 100 UCSD (Practice)

Disjoint Sets. The obvious data structure for disjoint sets looks like this.

LECTURE 26 PRIM S ALGORITHM


Minimum spanning trees

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

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

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

Undirected Graphs. Hwansoo Han

Chapter 9 Graph Algorithms

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

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CMSC 341 Lecture 20 Disjointed Sets

What is a minimal spanning tree (MST) and how to find one

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

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

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

CSE 373: Practice Final

( ) n 3. n 2 ( ) D. Ο

( D. Θ n. ( ) f n ( ) D. Ο%

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CMSC 341 Lecture 20 Disjointed Sets

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Union-Find: A Data Structure for Disjoint Set Operations

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Transcription:

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 (s, 0) into a priority queue. This queue contain pairs (v, cost) where cost is the best cost path found so far from s to v. It will be ordered by cost, with smallest cost at the head. While the priority queue is not empty: Dequeue the pair (v, c) from the head of the queue. If v s done is true, continue Else set v s done to true. We have found the shortest path to v. (It s prev and dist field are already correct). For each of v s adjacent nodes, w: Calculate the best path cost, c, to w via v by adding the edge cost for (v, w) to v s dist. If c is less than w s dist, replace w s dist with c, replace its prev by v and enqueue (w, c) V0 5 1 V1 6 When a node comes out of the priority queue, how do you know you ve found the shortest path to the node? V3 2 1 V2 V4

Spanning trees We will consider spanning trees for undirected graphs A spanning tree of an undirected graph G is an undirected graph that... contains all the vertices of G contains only edges of G has no cycles is connected A spanning tree is called spanning because it connects all the graph s vertices A spanning tree is called a tree because it has no cycles (recall the definition of cycle for undirected graphs) What is the root of the spanning tree? you could pick any vertex as the root; the vertices adjacent to that one are then the children of the root; etc.

Spanning trees: examples Consider this undirected graph G: V0 V1 V2 V3 V4 V5 V6

Spanning tree? Ex. 1 V0 V1 V2 V3 V4 V5 V6 Is this graph a spanning tree of G? A. Yes B. No

Spanning tree? Ex. 2 V0 V1 V2 V3 V4 V5 V6 Is this graph a spanning tree of G? A. Yes B. No

Spanning tree? Ex. 3 Is this graph a spanning tree of G? A. Yes B. No

Minimum Spanning tree: Spanning tree with minimum total cost 10 1 V0 V1 1 V2 12 V3 2 V4 3 V5 4 1 V6 8 Is this graph a minimum spanning tree of G? A. Yes B. No

Minimum spanning trees in a weighted graph A single graph can have many different spanning trees They all must have the same number of edges, but if it is a weighted graph, they may differ in the total weight of their edges Of all spanning trees in a weighted graph, one with the least total weight is a minimum spanning tree (MST) It can be useful to find a minimum spanning tree for a graph: this is the least-cost version of the graph that is still connected, i.e. that has a path between every pair of vertices How to do it?

Prim s MST Algorithm 10 1 V0 V1 1 V2 12 V3 2 V4 3 V5 4 1 V6 8 Start with any vertex and grow like a mold, one edge at a time Each iteration choose the cheapest crossing edge

Fast implementation of Prim s MST Algorithm 10 1 V0 V1 1 V2 12 V3 2 V4 3 V5 4 1 V6 8 Iteration 1

Finding a minimum spanning tree: Prim s algorithm As we know, minimum weight paths from a start vertex can be found using Djikstra s algorithm At each stage, Djikstra s algorithm extends the best path from the start vertex (priority queue ordered by total path cost) by adding edges to it To build a minimum spanning tree, you can modify Djikstra s algorithm slightly to get Prim s algorithm At each stage, Prim s algorithm adds the edge that has the least cost from any vertex in the spanning tree being built so far (priority queue ordered by single edge cost) Like Djikstra s algorithm, Prim s algorithm has worst-case time cost O( E log V ) We will look at another algorithm: Kruskal s algorithm, which also is a simple greedy algorithm Kruskal s has the same big-o worst case time cost as Prim s, but in practice it can be made to run faster than Prim s, if efficient supporting data structures are used

Fast Implementation of Prim s MST Algorithm 1. Initialize the vertex vector for the graph. Set all done fields to false. Pick an arbitrary start vertex s. Set its prev field to -1, and its done field to true. Iterate through the adjacency list of s,and put those edges in the priority queue. 2. Is the priority queue empty? Done! 3. Remove from the priority queue the edge (v, w, cost) with the smallest cost. 4. Is the done field of the vertex w marked true? If Yes: this edge connects two vertices already connected in the spanning tree, and we cannot use it. Go to 1. 5. Accept the edge: Mark the done field of vertex w true, and set the prev field of w to indicate v. 6. Iterate through w s adjacency list, putting each edge in the priority queue. 7. Go to 1. The resulting spanning tree is then implicit in the values of prev fields in the vertex objects.

Weighted minimum spanning tree: Kruskal s algorithm Prim s algorithm starts with a single vertex, and grows it by adding edges until the MST is built: Kruskal s algorithm starts with a forest of single-node trees (one for each vertex in the graph) and joins them together by adding edges until the MST is built; Why Kruskal?

Kruskal s algorithm:

Kruskal s algorithm: output Show the result here: What is the total cost of this spanning tree? Is there another spanning tree with lower cost? With equal cost?

Naïve Implementation of Kruskal s algorithm: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E If T U {e i } has no cycles Add e i to T Ref: Tim Roughgarden (stanford)

Running Time of Naïve Implementation of Kruskal s algorithm: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E If T U {e i } has no cycles Add e i to T Ref: Tim Roughgarden (stanford)

Towards a fast implementation for Kruskal s Algorithm 19 What is the work that we are repeatedly doing with Kruskal s Algo?

Towards a fast implementation for Kruskal s Algorithm 20 What is the work that we are repeatedly doing with Kruskal s Algo? Checking for cycles: Linear with BFS, DFS Union-Find Data structure allows us to do this in almost constant time!

Graph algorithm time costs: a summary The graph algorithms we have studied have fast algorithms: Find shortest path in unweighted graphs Solved by basic breadth-first search: O( V + E ) worst case Find shortest path in weighted graphs Solved by Dijkstra s algorithm: O( E log V ) worst case Find minimum-cost spanning tree in weighted graphs Solved by Prim s or Kruskal s algorithm: O( E log V ) worst case The greedy algorithms used for solving these problems have polynomial time cost functions in the worst case since E V 2, Dijkstra s, Prim s and Kruskal s algorithms are O( V 3 ) As a result, these problems can be solved in a reasonable amount of time, even for large graphs; they are considered to be tractable problems However, many graph problems do not have any known polynomial time solutions...!

Towards a fast implementation of Kruskal s Algorithm Q: Which of the following algorithms can be used to check if adding an edge (v, w) to an existing Graph creates a cycle? 22 A. DFS B. BFS C. Either A or B D. None of the above

BFS: Running Time The basic idea is a breadth-first search of the graph, starting at source vertex s Initially, give all vertices in the graph a distance of INFINITY Start at s; give s distance = 0 Enqueue s into a queue While the queue is not empty: Dequeue the vertex v from the head of the queue For each of v s adjacent nodes that has not yet been visited: Mark its distance as 1 + the distance to v Enqueue it in the queue What is the time complexity (in terms of V and E ) of this algorithm? A. O( V ) B. O( V E ) C. O( V + E ) D. O( V ^2) E. Other s V0 V3 V1 V2 V4

Running Time of Naïve Implementation of Kruskal s algorithm using BFS for cycle checks: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E If T U {e i } has no cycles Add e i to T Running Time Ref: Tim Roughgarden (stanford)

Towards a fast implementation for Kruskal s Algorithm 25 What is the work that we are repeatedly doing with Kruskal s Algo?

Towards a fast implementation for Kruskal s Algorithm 26 What is the work that we are repeatedly doing in Kruskal s Algo? Checking for cycles: Linear with BFS, DFS Union-Find Data structure allows us to do this in nearly constant time!

27 The Union-Find Data Structure Efficient way of maintaining partitions Supports only two operations Union Find v 2 v 0 v 1 v 4 v 5 v 3 v 6

Equivalence Relations 28 An equivalence relation E(x,y) over a domain S is a boolean function that satisfies these properties for every x,y,z in S E(x,x) is true If E(x,y) is true, then E(y,x) is true If E(x,y) and E(y,z) are true, then E(x,z) is true (reflexivity) (symmetry) (transitivity) Example 1: E(x,y): Are the integers x and y equal? Then E() is an equivalence relation over integers Example 2: Given vertices x and y in a Graph G E(x,y): Are x and y connected?

Equivalence Classes An equivalence relation E() over a set S defines a system of equivalence classes within S The equivalence class of some element x S is that set of all y S such that E(x,y) is true Note that every equivalence class defined this way is a subset of S The equivalence classes are disjoint subsets: no element of S is in two different equivalence classes The equivalence classes are exhaustive: every element of S is in some equivalence class 29 Example 1: E(x,y): Are the integers x and y equal? Then E() is an equivalence relation over integers The equivalence classes in this case is:

Equivalence Classes for Kruskal s 30 For Kruskal s algo we will partition all vertices of the graph into disjoint sets, based on the equivalence relation: Are two vertices connected? Q: The above equivalence relation partitions the graph into which of the following equivalence classes? A. Connected subgraphs B. Fully-connected (Complete) subgrapahs

Application of Union-Find to Kruskal s MST Vertices that form a connected subgraph will be in the same group Connected subgraphs that are disconnected from each other will be in different groups The graph data structure The union-find data structure 31 Q1: How can we check if adding an edge (v, w) to the graph creates a cycle using the operations supported by union-find? Q2: In Kruskal s algo what would we like to do if adding the edge does not create a cycle?

Union-find using up-trees 32 1 0 2 6 7 4 3 5 8 Each subtree represents a disjoint set The root node represents the set that any node belongs to Perform these operations: Find(4) = Find(3) = Union(1,0)= Find(4)=

Running Time of Kruskal s algorithm with union find data structure: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E if (find(u)!=find(v)){ //If T U {e i =u,v} has no cycles Add e i to T } union(find(u), find(v)) 33 Ref: Tim Roughgarden (stanford)

34 Cycle Detection in a Graph DFS BFS Union-Find

Union-find using up-trees 35 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)=

36 Array representation of Up-trees A compact and elegant implementation Each entry is the up index -1 for the roots Write the forest of trees, showing parent pointers and node labels, represented by this array 1 0 3 2 5 6 7 8 Find(4) 4 0 1 2 3 4 5 6 7-1 0-1 0 1 2-1 -1 7 8

37 Performing a union operation Union(6,7) 0 2 6 7 Fill in 6, 7 and 8 in the array 1 3 5 8 4 0 1 2 3 4 5 6 7 8-1 0-1 0 1 2-1 -1 7-1 0-1 0 1 2

Disjoint subsets using up-trees (Simple Union and Find) 38 Start with 4 items: 0, 1, 2, 3 Suppose Union(i,j) makes the node Find(i) the parent of the node Find(j) Perform these operations: Union(2,3) Union(1,2) Find(0) = Find(3) = Union(0,1) Find(1) = 0 1 2 3-1 -1-1 -1

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 in terms of the number of elements N? A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) 39

40 Improving Union operations In the Union operation, a tree becomes like a linked list if, when joining two trees, the root of the smaller tree (e.g. a single node) always becomes the root of the new tree We can avoid this by making sure that in a Union operation, the larger of the two trees roots becomes the root of the new tree (ties are broken arbitrarily) a c g h c g h a g h b d f i a f i b d c i e b d e f e Henry Kautz, U. Washington

Smarter Union operations Scott B. Baden / CSE 100-A / Spring 2013 41 Avoid this problem by having the larger of the 2 trees become the root Union-by-size Each root stores the size (# nodes) of its respective tree The root with the larger size becomes the parent Update its size = sum of its former size and the size of its new child Break ties arbitrarily a c b d f e

Disjoint subsets using trees (Union-by-size and simple Find) Start with 4 items: 0, 1, 2, 3 42 Assume that Union(i,j) makes the root of the smaller tree a child of the root of the larger tree. Perform these operations: Union(2,3) Union(1,2) Find(0) = Find(3) = Union(0,2) Find(1) = Find(0) =

Smarter Union operations Scott B. Baden / CSE 100-A / Spring 2013 43 Avoid this problem by having the larger of the 2 trees become the root Union-by-size Union-by-height (also called union by rank) Each root stores the height of its respective tree If one root has a greater height than the other, it becomes the parent. Its stored height doesn t need to be updated If the roots show equal height, pick either one as the parent Its stored height should be increased by one Break ties arbitrarily a c b d f e

Disjoint subsets using trees (Union-by-height and simple Find) Start with 4 items: 0, 1, 2, 3 44 Assume that Union(i,j) makes the root of the shorter tree a child of the root of the taller tree. Perform these operations: Union(2,3) Union(1,2) Find(0) = Find(3) = Union(0,2) Find(1) = Find(0) =

Cost of disjoint subsets operations with smarter Union and simple Find 45 Either union-by-size or union-by-height will guarantee that the height of any tree is no more than log 2 N, where N is the total number of nodes in all trees Q: What is the worst case run time of find and union operations in terms of the number of elements N? A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) Solo vote!

Bounding the height of the up-tree using union by size. Initially all the nodes are in singleton trees (with height 1) Take the perspective of a single node. The only time the height of the tree which the node is part of increases by 1 is when the node joins a larger group i.e. if the height of the node s uptree increases by 1, the number of nodes in that tree would have at least doubled The maximum number of nodes in any tree is N, so the height of the resulting tree can be at most log N 46

Cost of disjoint subsets operations with smarter Union and simple Find 47 Q: What is the worst case run time of find and union operations in terms of the number of elements N? Discuss and revote A. find: O(N), union: O(1) B. find: O(1), union: O(N) C. find: O(log 2 N), union: O(1) D. find: O(1), union: O(log 2 N) Therefore, doing N-1 union operations (the maximum possible) and M find operations takes time O(N + M log 2 N) worst case With simple unions the complexity was: This is a big improvement; but we can do still better, by a slight change to the Find operation: adding path compression

Kruskal s algorithm run time with smart union path compression find: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E if (find(u)!=find(v)){ //If T U {e i =u,v} has no cycles Add e i to T } union(find(u), find(v)) 48 What is the improvement from simple union find? Ref: Tim Roughgarden (stanford)

Union-Find Code 49 Using the array representation for disjoint subsets, the code for implementing the Disjoint Subset ADT s methods is compact class DisjSets{ int *array; } /** * Construct the disjoint sets object * numelements is the initial number of disjoint sets */ DisjSets( int numelements ) { array = new int [ numelements ]; for( int i = 0; i < numelements; i++ ) array[ i ] = -1; }

50 Union-by-height /** * Union two disjoint sets using the height heuristic. * For simplicity, we assume root1 and root2 are distinct * and represent set labels. * * root1 is the root of set 1 root2 is root of set 2 * returns the root of the union */ int union ( int root1, int root2 ) { if( array[ root2 ] < array[ root1 ] ) { // root2 is higher array[ root1 ] = root2; return root2; // Make root2 new root } else { if( array[ root1 ] == array[ root2 ] ) array[ root1 ]--; // Update height if same // Make root1 new root array[ root2 ] = root1; return root1; } }

51 Simple Find In a disjoint subsets structure using parent-pointer trees, the basic Find operation is implemented as: Go to the node corresponding to the item you want to Find the equivalence class for Traverse parent pointers from that node to the root of its tree Return the label of the root This has worst-case time cost O(log 2 N) The time cost of doing another Find operation on the same item is the same

52 Find with Path-compression The path-compression Find operation is implemented as: Go to the node corresponding to the item you want to Find the equivalence class for Traverse parent pointers from that node to the root of its tree Return the label of the root But. as part of the traversal to the root, change the parent pointers of every node visited, to point to the root of the tree (all become children of the root) worst-case time cost is still O(log N)

53 Example of path compression find(e) as part of the traversal to the root, change the parent pointers of every node visited, to point to the root of the tree (all become children of the root) worst-case time cost is still O(log N) c g c g a f h a f h b d b e d i e i Henry Kautz

54 Cost of find with path compression find(e) What is the time cost of doing another Find operation on the same item, or on any item that was on the path to the root? c g A. O(log N) B. O(N) C. O(1) c g a f h a f h b d b e d i e i Henry Kautz

Disjoint subsets using trees (Union-by-height and path- compression Find) 55 Start with 4 items: 0, 1, 2, 3 Union(i,j) makes the root of the shorter tree a child of the root of the taller tree We perform path compression If an array element contains a negative int, then that element represents a tree root, and the value stored there is -1 times (the height of the tree plus 1) Perform these operations: Union(2,3) Union(0,1) Find(0) = Find(3) = Union(0,2) Find(1) = Find(3) = Find(3) =

56 Find with path compression /** * Perform a find with path compression * Error checks omitted again for simplicity * @param x the label of the element being searched for * @return the label of the set containing x */ int find( int x ) { } if( array[ x ] < 0 ) return x; else return array[ x ] = find( array[ x ] ); Note that this path-compression find method does not update the disjoint subset tree heights; so the stored heights (called ranks ) will overestimate of the true height A problem for the cost analysis of the union-by-height method (which now is properly called union-by-rank)

Self-adjusting data structures Path-compression Find for disjoint subset structures is an example of a self-adjusting structure Other examples of self-adjusting data structures are splay trees, self-adjusting lists, skew heaps, etc In a self-adjusting structure, a find operation occasionally incurs high cost because it does extra work to modify (adjust) the data structure, with the hope of making subsequent operations much more efficient Does this strategy pay off? Amortized cost analysis is the key to the answering that question... 57

58 Amortized cost analysis Amortization corresponds to spreading the cost of an infrequent expensive item (car, house) over the use period of the item. The amortized cost should be comparable to alternatives such as renting the item or taking a loan. Amortized analysis of a data structure considers the average cost over many actions.

Amortized cost analysis results for path compression 59 Find It can be shown (Tarjan, 1984) that with Union-by-size or Union-by-height, using path-compression Find makes any combination of up to N-1 Union operations and M Find operations have a worst-case time cost of O(N + M log* N) This is very good: it is almost constant time per operation, when amortized over the N-1 + M operations!

60 Amortized cost analysis results for path compression Find log* N = log star of N = smallest k such that log (k) n <= 1 or # times you can take the log base-2 of N, before we get a number <= 1 Also known as the single variable inverse Ackerman function log* 2 = 1 log* 4 = 2 log* 16 = 3 log* 65536 = 4 log* 2 65536 = 5 log (k) n = log (log (log (log n))) 4 = (2) 2 16 = (2) 4 k logs 65536 = (2) 16 (2 65536 = a huge number of 20000 digits) log* N grows extremely slowly as a function of N It is not constant, but for all practical purposes, log*n is never more than 5

Running Time of Kruskal s algorithm with union find data structure: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E S1=find(u); S2=find(v); if (S1!=S2){ //If T U {e i =u,v} has no cycles Add e i to T } union(s1, S2) 61 Ref: Tim Roughgarden (stanford)

Dijkstra s Algorithm: Run time 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 (s, 0) into a priority queue. This queue contain pairs (v, cost) where cost is the best cost path found so far from s to v. It will be ordered by cost, with smallest cost at the head. While the priority queue is not empty: Dequeue the pair (v, c) from the head of the queue. If v s done is true, continue Else set v s done to true. We have found the shortest path to v. (It s prev and dist field are already correct). For each of v s adjacent nodes, w: Calculate the best path cost, c, to w via v by adding the edge cost for (v, w) to v s dist. If c is less than w s dist, replace w s dist with c, replace its prev by v and enqueue (w, c) What is the running time of this algorithm in terms of V and E? (More than one might be correct which is tighter?) A. O( V 2 ) B. O( E + V ) C. O( E log( V )) D. O( E log( E ) + V ) E. O( E * V ) Assume adjacency list representation for the graph V0 V3 5 1 V1 6 2 1 V2 V4

Prim s MST Algorithm: Run Time 1. Create an empty graph T. Initialize the vertex vector for the graph. Set all done fields to false. Pick an arbitrary start vertex s. Set its done field to true. Iterate through the adjacency list of s,and put those edges in the priority queue. 2. While the priority queue is not empty: Remove from the priority queue the edge (v, w, cost) with the smallest cost. Is the done field of the vertex w marked true? If Yes: this edge connects two vertices already connected in the spanning tree, and we cannot use it. Go to 2. Else accept the edge: Mark the done field of vertex w true, and add the edge (v, w) to the spanning tree T. Iterate through w s adjacency list, putting each edge in the priority queue. What is the running time of this algorithm in terms of V and E? (More than one might be correct which is tighter?) A. O( V 2 ) B. O( E + V ) C. O( E log( V )) D. O( E log( E ) + V ) E. O( E * V )