Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Similar documents
Advanced algorithms. strongly connected components algorithms, Euler trail, Hamiltonian path. Jiří Vyskočil, Radek Mařík 2012

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

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

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

Minimum Spanning Trees

Minimum Spanning Trees My T. UF

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

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

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

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

7.3 Spanning trees Spanning trees [ ] 61

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

Randomized Graph Algorithms

Minimum Spanning Trees

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

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

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

Distributed minimum spanning tree problem

6.1 Minimum Spanning Trees

23.2 Minimum Spanning Trees

Lecture 3: Graphs and flows

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

Minimum spanning trees

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

Chapter 23. Minimum Spanning Trees

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

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

Greedy Algorithms Part Three

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum Spanning Trees

Discrete mathematics

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.

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

Solutions to relevant spring 2000 exam problems

Algorithm Design (8) Graph Algorithms 1/2

Week 11: Minimum Spanning trees

CS521 \ Notes for the Final Exam

Lecture 22 Tuesday, April 10

Algorithms for Minimum Spanning Trees

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

Kruskal s MST Algorithm

Data Structures and Algorithms. Werner Nutt

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

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

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

Announcements Problem Set 5 is out (today)!

Minimum cost spanning tree

The minimum cost spanning tree problem

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CS 6783 (Applied Algorithms) Lecture 5

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

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Lecture 10: Analysis of Algorithms (CS ) 1

New approach to find minimum spanning tree for undirected graphs

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

Minimum Spanning Trees Ch 23 Traversing graphs

Notes 4 : Approximating Maximum Parsimony

Minimum Spanning Trees

Introduction to Algorithms

Minimum Spanning Trees

Minimum Spanning Tree

Minimum Spanning Trees

Outline. Graphs. Divide and Conquer.

Advanced algorithms. binary heap, d-ary heap, binomial heap, amortized analysis, Fibonacci heap. Jiří Vyskočil, Radek Mařík 2013

Info 2950, Lecture 16

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

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

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

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

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Introduction to Algorithms Third Edition

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

2 A Template for Minimum Spanning Tree Algorithms

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

CS 161: Design and Analysis of Algorithms

Minimum-Cost Spanning Tree. Example

The minimum spanning tree and duality in graphs

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

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

looking ahead to see the optimum

11.9 Connectivity Connected Components. mcs 2015/5/18 1:43 page 419 #427

Example of why greedy step is correct

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Minimum Spanning Trees. COMPSCI 355 Fall 2016

Week 12: Minimum Spanning trees and Shortest Paths

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CS 310 Advanced Data Structures and Algorithms

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

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

Transcription:

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 In other words, a subgraph is created so that: Some vertices of the original graph are removed. All edges incident to the removed vertices and possible some other edges are removed. 2/28

DFS for the entire graph recursively input: Graph G. 1 2 procedure DFS Graph G for each Vertex v in V G state v UNVISITED; p v null; 3 time 0; 4 5 6 for each Vertex v in V G if state v UNVISITED then DFS Walk v ; 7 8 9 10 11 12 procedure DFS Walk Vertex u state u OPEN; d u time; for each Vertex v in Neighbors u if state v UNVISITED then p v u; DFS Walk v ; state u CLOSED; f u time; output: array p pointing to predecessor vertex, array d with times of vertex opening and array f with time of vertex closing. 3/28

Topological ordering topological ordering (topological sorting) of graph vertices Let graph G be DAG. Let s define binary relation R of topological ordering over vertices of graph G such as R(x,y) is valid iff there exists a directed path from x to y, that is, whenever y is reachable from x. In other words: All vertices of graph G are assigned with numbers so that x y holds for every pair of vertices x and y iff there is a directed path from x to y. Then relation is a topological ordering over graph G with numbered vertices. an implementation using the previous DFS algorithm The numbering vertices through array f with relation is a topological order. 4/28

Other uses of modified DFS Testing graph acyclicity Testing graph connectivity Searching for graph connected components Transformation of a graph to a directed forest. 5/28

Connected component A connected component of graph G V,E with regard to vertex v is a set C v u V there exists a path in G from u to v. In other words: If a graph is disconnected, then parts from which is composed from and that are themselves connected, are called connected components. c b a C a C b a,b e d C c C d C e c,d,e 6/28

Spanning tree graph spanning tree Let G=(V,E) be a graph. A Spanning tree of the graph G is such a subgraph H of the graph G that V(G)=V(H) and H is a tree. 7/28

Minimum spanning tree Minimum spanning tree Let G V,E be a graph and w : E be its weight function. A minimum spanning tree of the graph G is such a tree K V,E K of the graph G, that is minimal. 3 1 2 3 1 2 2 2 1 2 2 1 2 2 3 3 8/28

cut Cut of graph A cut of graph G V,E is a subset of edges F E such that U V : F u,v E u U, v U. Lemma: Let G be a graph, w be its injective real-valued weight function, F be a cut of graph G and f be its lightest edge of cut F (crossing), then every minimum spanning tree K of graph G contains f E K. Proof by contradiction: Let K be a minimum spanning tree and f u,v E K. Then there is a path P K connecting u and v. The path has to cross the cut at least once. Therefore there is an edge e P F and furthermore w f w e. Let s consider K K e f. This graph is also a spanning tree of graph G, because the graph splits into two components by removing of the edge e and it merges back by adding of the edge f. Then w K w K w e w f w K. K is also a minimum spanning tree. 9/28

Jarník (Prim) s algorithm input: A graph G with a weight function w : E G. 1 Select an arbitrary vertex v 0 V G. 2 3 4 5 6 K : v 0,. while V K V G Select edge u,v E G, where u V K and v V K so that w u,v is minimum. K : K edge u,v. output: a minimum spanning tree K. 10 / 28

Jarník (Prim) s algorithm 8 15 13 7 11 18 27 2 26 3 20 12 14 17 10 5 22 9 23 24 25 19 21 4 1 16 6 11 / 28

Jarník (Prim) s algorithm Lemma: Jarník s algorithm stops after maximum V G steps and the result is a minimum spanning tree of the graph G. In every iteration just one vertex is added to K, so the loop must stop after V G iteration in maximum. The result graph K is a tree because only a leaf is always added to the tree. Furthermore, K has V G vertices it is a spanning tree. The edges among vertices of the tree K and the rest of the graph G determines a cut. The algorithm always adds the lightest edge of this cut to K. Following the previous lemma, all edges of K must belong to every minimum spanning tree. As K is a tree, then it must be a minimum spanning tree. 12 / 28

Jarník (Prim) s algorithm implementations: straightforward Maintain which vertices and edges belong to the tree K and which not. The time complexity is O(n m) where n V G and m E G. improvements Store D v min w u,v u K for v V K. During every iteration of the main loop we search through all D v (it takes O(n) time) and we check all neighbors D s for v,s E when a vertex v is added to K and its value is decreased if necessary (O(1) for each edge). Time complexity is improved to O(n 2 + m) = O(n 2 ). The time complexity might be further improved using a suitable type of heap up to O(log (n) m) (technically up to O(m + log (n) n) with so called Fibonacci heap). 13 / 28

Borůvka s algorithm input: A graph G with a weight function w : E G, where all weights are different. 1 K : V G,. 2 while K has at least two connected components 3 For all components T i of graph K the light incident edge 1 t i is chosen. 4 All edges t i are added to K. 5 output: a minimum spanning tree K. 1 A light incident edge is an edge connecting a connected component Ti with another connected component while a weight of this edge is the lowest. 14 / 28

Borůvka s algorithm 8 15 13 7 11 18 27 2 26 3 20 12 14 17 10 5 22 9 23 24 25 19 21 4 1 16 6 15 / 28

Borůvka s algorithm theorem: Borůvka s algorithm stops after max. log 2 V G and the result is a minimum spanning tree of the graph G. After k iterations all components of the graph K have at least 2 k vertices. induction: Initially, all components consist of just one vertex. In each iteration, each component is merged with at least another neighboring one so that the size of components is at least doubled. Therefore, after log 2 V G iterations, the size of any component must be at least a number of all vertices of graph G and then the algorithm stops. The edges between each connected component and the rest of graph determines a cut. Then all edges added to K must belong to a unique minimum spanning tree. Graph K G is always a forest (= a set of trees disconnected to each other) and when the algorithm stops it will be equal to a minimum spanning tree. 16 / 28

Borůvka s algorithm Iteration implementation: The forest is decomposed to connected components using DFS. Each vertex is assigned to a number of its component. For each edge we find out to which component it belongs and we store the lightest edge only. Therefore each iteration takes O( E G ) time and the entire algorithm running time is O( E G log V G ). 17 / 28

Kruskal s ( greedy ) algorithm input: A graph G with a weight function w : E G. 1 2 3 4 5 Sort all edges e 1,, e m E G from E G so that w e 1 w e m. K : V G,. for i : 1 to m if K edge u,v is an acyclic graph then K : K edge u,v. output: a minimum spanning tree K. 18 / 28

Kruskal s ( greedy ) algorithm 8 15 13 7 11 18 27 2 26 3 20 12 14 17 10 5 22 9 23 24 25 19 21 4 1 16 6 19 / 28

Kruskal s ( greedy ) algorithm theorem: Kruskal s algorithm stops after E G iterations and returns a minimum spanning tree. Each iteration of the algorithm processes just one edge, so the number of iterations is E G. By induction we prove that K is always a subgraph of a minimum spanning tree: the empty initial K is a subgraph of anything (including a minimum spanning tree). Each added edge has the lowest weight in the cut separating a component of K from the rest of the graph (the remaining unprocessed edges of this cut are heavier). In opposite way, no edge that is not added to K cannot belong to a minimum spanning tree because it creates a cycle with edges already assigned to a minimum spanning tree. 20 / 28

Kruskal s ( greedy ) algorithm implementation Sorting time is O( E G log E G ) = O( E G log V G ). We can stop the main loop earlier. When we successfuly add V G -1 edges to K then we can stop the algorithm because K has already reached a spanning tree. We need to maintain connected components of graph K so that we can recognize quickly if the current processed edge creates a cycle. Thus we need a structure for connected component maintenance which we can ask E G -times if two vertices belong to the same component (operation Find), and we merge just ( V G 1)-times two components to a single one (operation Union). 21 / 28

Union-Find problem Let s have graph G V,E. Question: Do vertices u and v belong to the same connected component of graph G?. Sometimes the problem is called as incremental connected components or equivalence maintenance. One representative is selected in each connected component. For sake of simplicity the representative of component C v is labeled as r v. If u and v belong to the same component then r u = r v. The task might be accomplished using the following operations: FIND v r v, the operation returns the representative of connected component C v. UNION u, v merges connected components C u and C v. This reflects adding edge u, v into the graph. 22 / 28

Union-Find problem A simple solution: Let s assume all vertices are assigned with a number from 1 to n. Let s use an array R 1..n, wherer i r i, i.e. the number of component C i representative. Operation FIND v just returns value R v and so it takes O(1). To perform UNION u, v we find representatives r u FIND u and r v FIND v. If they are different then we process all items of array R. Any value of r u is rewritten to r v. It takes O(n ) time. 23 / 28

Union-Find problem An improved solution (using a directed tree): Each component is stored as a tree directed towards the root every vertex has a pointer to its father, every root stores the size of the component. The root of each component serves as its representative. Operation FIND v climbs from vertex v to the root that is returned. To perform UNION u, v we find representatives r u FIND u and r v FIND v. If they different then the root of smaller component is merged to the root of the bigger component. The size of new component is updated in its root. 24 / 28

Union-Find problem 25 / 28

Union-Find problem An improved solution (using a directed tree): lemma: Union-Find tree of a depth h has at least 2 h items. By induction: If UNION merges a tree of the depth h with another tree of a depth smaller than h, then a depth of the result tree remains h. If two trees of the same depth h are merged, then the result tree has a depth h 1. By induction assumption we know that a tree of depth h has at least 2 h vertices. Therefore the result tree of a depth h 1 has at least 2 h 1 vertices. A consequence: Time complexity of operation UNION and FIND is O(log V ). The best known solution is O(α V ) for both operations, where function α is inverse Ackermann function. 26 / 28

Kruskal s ( greedy ) algorithm Kruskal s algorithm complexity: Sorting takes time: O( E G log E G ) = O( E G log V G ). Then we need a structure for connected component maintenance which we can ask E G -times if two vertices belong to the same component (operation Find), and we merge just ( V G 1)-times two components to a single one (operation Union). If the simple solution is used then the complexity of the algorithm is: O( E G log V G + E G + V G 2 )= O( E G log V G + V G 2 ) If the improved solution using a directed tree is used then the complexity of the algorithm is: O( E G log V G + E G log V G + V G log V G )= O( E G log V G ) 27 / 28

References Matoušek, J.; Nešetřil, J. Kapitoly z diskrétní matematiky. Karolinum. Praha 2002. ISBN 978-80-246-1411-3. Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). Introduction to Algorithms (2nd ed.). MIT Press and McGraw-Hill. ISBN 0-262-53196-8. 28 / 28