III Optimal Trees and Branchings

Similar documents
COMP 355 Advanced Algorithms

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

Algorithms for Minimum Spanning Trees

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

Minimum Spanning Trees

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

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

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

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

2 A Template for Minimum Spanning Tree Algorithms

Minimum Spanning Trees

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

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

1 Minimum Spanning Trees: History

4. GREEDY ALGORITHMS II

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

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

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

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

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

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

Minimum cost spanning tree

The minimum cost spanning tree problem

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

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

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.

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

CS521 \ Notes for the Final Exam

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Chapter 9 Graph Algorithms

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

CSE 521: Design and Analysis of Algorithms I

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

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

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

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

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees My T. UF

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

5 MST and Greedy Algorithms

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

Minimum Spanning Trees

5 MST and Greedy Algorithms

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

Discrete mathematics

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

Greedy Algorithms Part Three

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

Minimum Spanning Trees

Minimum Spanning Trees

4. GREEDY ALGORITHMS II

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

Design and Analysis of Algorithms

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

Week 12: Minimum Spanning trees and Shortest Paths

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

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

Theorem 2.9: nearest addition algorithm

Chapter 23. Minimum Spanning Trees

Introduction to Algorithms

Topics on Computing and Mathematical Sciences I Graph Theory (3) Trees and Matchings I

Data Structures and Algorithms. Werner Nutt

Minimum Spanning Trees

Randomized Graph Algorithms

Graphs and Network Flows IE411. Lecture 21. Dr. Ted Ralphs

Minimum Spanning Trees Shortest Paths

The Encoding Complexity of Network Coding

23.2 Minimum Spanning Trees

Minimum Spanning Trees and Prim s Algorithm

Reference Sheet for CO142.2 Discrete Mathematics II

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Greedy Approach: Intro

Week 11: Minimum Spanning trees

Minimum Spanning Trees COSC 594: Graph Algorithms Spring By Kevin Chiang and Parker Tooley

Lecture Summary CSC 263H. August 5, 2016

In this lecture, we ll look at applications of duality to three problems:

CS 310 Advanced Data Structures and Algorithms

Approximation Algorithms

Minimum Spanning Trees

Lecture 13: Minimum Spanning Trees Steven Skiena

CS 6783 (Applied Algorithms) Lecture 5

Steiner Trees and Forests

Solutions for the Exam 6 January 2014

2.2 Optimal cost spanning trees

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Index. stack-based, 400 A* algorithm, 325

looking ahead to see the optimum

Lecture 4: Primal Dual Matching Algorithm and Non-Bipartite Matching. 1 Primal/Dual Algorithm for weighted matchings in Bipartite Graphs

Example of why greedy step is correct

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

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

Outline. 1 The matching problem. 2 The Chinese Postman Problem

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

Transcription:

Efficient Graph Algorithms III Optimal Trees and Branchings III Optimal Trees and Branchings 4 1 3 2 2 8 7 3 8 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 1

MST Problem Description MST problem arises from the area of network design: connecting nodes with minimal cost / distance /... Given: Connected graph G = (V, E) with positive edge costs c e > 0 for all e E. Sought: Edge set T E, such that (V, T ) is connected and c e is minimal. If (V, T ) is a tree, it is called minimum spanning tree (MST). Observation 1 An optimal solution for the above problem is always a (minimum spanning) tree. What about......calculating all spanning trees and taking the one implying minimum weight? e T Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 2

MAX-FOREST Total enumeration does not make any sense, because: Cayley: there are n n 2 spanning trees in K n. Assume we are able to enumerate 10 6 trees per second with n = 30 this would take 30 28 /10 6 seconds = 7.25 10 27 years. MAX-FOREST: find a forest of maximal weight (with positive edge costs, creation of a forest of minimal weight is not useful at all). Definition 2 Problem classes P and Q are called equivalent if there is a linear transformation between P and Q, i.e. there are linear time functions f, g with: f transforms instances x P into instances y Q, g transforms solutions z of instance f (x) into solutions of x P, z optimal in f (x) g(z) optimal in x. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 3

MST MAX-FOREST Lemma 3 MST and MAX-FOREST are equivalent. Proof. Let G be connected and weighted. Let A be an algorithm computing a MAX-FOREST. Set M := max{ c e e E} + 1 and c e := M c e, e E. Use algorithm A to compute a MAX-FOREST F of G with respect to c e. G connected, c e > 0, e E F is a tree. Because of the complementation of c e with respect to M, F is minimal for G with original weights c e. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 4

MST MAX-FOREST Proof (cont d). Let now A be an algorithm that computes an MST. The goal is to find a MAX-FOREST. Regard complete graph K n and set M := n (max{ c e e E} + 1 and c e := { ce, for all e E with c e > 0, M, otherwise. A computes an MST T for K n with weights c e. Obviously, T \ {e T c e = M} is a MAX-FOREST in G w.r.t. the original weights c e. Alternatively, we might compute the components of G (negative edges are removed immediately), complement the edge weights and apply algorithm A on the components. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 5

A General Greedy Approach We give a general algorithm that labels edges either blue or red. During the procedure, at any time the invariant is satisfied: Among all MSTs, there is one containing all blue edges and no red edge. During the procedure, we use the following labeling rules: blue rule (cut property): Choose a cut that does not contain any blue edge. Choose the shortest unlabeled edge in this cut and label it blue. red rule (cycle property): Choose a cycle that does not contain any red edge. Choose the longest unlabeled edge in this cycle and label it red. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 6

MST(G, c) MST(G, c) 1. All edges are unlabeled. 2. Apply rules blue and red until all edges are labeled. 3. The blue edges form an MST. Proposition 4 MST(G, c) computes an MST. Proof. Initially, we show that the above invariant holds. Trivially, it holds at the beginning. Thus, it is to show that application of the blue and red rules does not violate it. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 7

Correctness of MST(G, c) (1) Correctness of the blue rule Let e E be labeled blue in Step 2. If e T, nothing is to show. Let e / T and δ(w ) be the associated cut. Then, there is a path connecting the two nodes incident with e, and at least one of its edges is in δ(w ). e e Let e be such an edge. As the invariant holds, no edge in T is labeled red and e is unlabeled. According to the blue rule, it is c e c e. If we replace e by e in T, another MST satisfying the invariant emerges. In particular, c e = c e. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 8

Correctness of MST(G, c) (2) Correctness of the red rule e and T as above. If e / T, then the invariant is satisfied. Therefore, assume e T. If we remove e from T, T is not connected anymore. Thus, the cycle used in the red rule, must have contained another edge e with endnodes in both subtrees. e e has not been labeled yet, and according to the red rule, c e c e holds. As above, by replacing e by e, we obtain a new MST satisfying the invariant. e Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 9

Correctness of MST(G, c) (3) Correctness of MST(G, c) Remains to show that all edges are labeled during MST(G, c): Let e be unlabeled. According to the invariant, all blue edges form a forest (possibly containing isolated nodes). We regard an edge e = (u, v): (Case 1) u and v are part of the same tree: apply the red rule for eliminating the cycle consisting of e and the path u v in the tree connecting u and v.(case 2) u and v are part of different trees: a cut δ(w ) is induced by the nodes of either the tree containing u, or the tree containing v. One edge (not necessarily e) is labeled blue. Note that, in the above algorithm, the labeling is non-deterministic. In order to obtain an efficient implementation, we have to specify the application of the labeling rules more precisely. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 10

Matroids Definition 5 (Matroid) A matroid is a pair (E, U), where U is a subset system of E which satisfies the following properties: 1. U 2. A B, B U = A U 3. A, B U, A < B = x B \ A with A {x} U (exchange property) The elements of E are called elements of the matroid, elements of U are called independent subsets of (E, U). Theorem 6 Let (E, U) be a matroid. Then, the canonical Greedy algorithm is optimal w.r.t. any weighting function w : E R. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 11

MST and Matroids Theorem 7 The subset system of MST (all forest of G) is a matroid. Proof. Properties 1 and 2 are clear. To show is the exchange property. Regard two acyclic subsets A, B E, A < B. Let V 1,..., V k V be the connected components of G = (V, A). As A < B n 1, A is not a spanning tree. Therefore, k 2. As B is acyclic, B may have at most V i 1 edges within a component V i. A has exactly V i 1 edges in every set V i k Therefore, B contains at most ( V i 1) = A edges within a i=1 component V i. As A < B, there is an edge e B connecting two different components V i, V j. e cannot generate a cycle in A. It follows that A {e} U. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 12

Pairwise different edge costs For simplicity, we assume pairwise different edge costs and eliminate this assumption later. Cut Property: An edge e is definitely part of an MST, if it is the edge with minimum cost in a cut induced by a non-trivial set S. Therefore, it might be safely inserted. Cycle Property: Let C be an arbitrary cycle in G, and let e be the edge of maximum cost in C. Then, no MST contains e. Therefore, it might be safely removed. Proof by means of an exchange property: Every edge e in T that has not minimum cost with respect to the cut δ(s) might be replaced by an edge e δ(s) with c e < c e yielding a spanning tree with less cost. Analogously done for the cycles. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 13

Pairwise different edge costs (cont d) We have assumed pairwise different edge costs for all edges in E. How is it possible to conclude that the algorithms are still correct even if some edge costs are identical? Idea: modify edge costs by very small amounts δ such that all edge costs are pairwise different. The modification must be small enough in order to not change the relative ordering of edges that originally had different costs. The modification is only used as a tie-breaker for identical edge costs. If a tree T is more expensive than a tree T with respect to its original costs then it is more expensive for the modified costs and vice versa. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 14

Duality of blue rule and red rule Observation 8 The blue rule is the dual of the red rule and vice versa. Declarative Programming: minimize e E:e is blue c e s.t. {e E : e is blue} is a tree s.t. maximize e E:e is red c e {e E : e is not red} is a tree Note: These are not dual Linear Programs. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 15

Blue rule or red rule? An MST contains n 1 edges, usually this is a very small subset of the edges from E. Regarding a complete graph K n, there are ( n 2) O(n 2 ) edges. The blue rule labels exactly n 1 edges blue O(n). The red rule labels up to ( n 2) (n 1) edges red O(n 2 ). There are no efficient algorithms based on the red rule. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 17

Borůvka s Algorithm Borůvka s algorithm is believed to be the first MST algorithm (1926). It uses exclusively the blue rule, but slightly modified. Borůvka(G, c) 1. Initialize n trivial blue trees consisting of one node each. 2. As long as there is more than one blue tree, for each of the blue trees choose the minimally incident edge and label it blue. 3. The blue edges form an MST. Note that the above algorithm will only work with pairwise different edge weights c e. Otherwise cycles might occur. Clearly, specifying an ordering of edges of same weight with the above modification will help in this case. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 18

Borůvka Example 8 10 5 5 5 2 3 2 3 2 3 18 16 16 16 12 14 30 12 4 26 4 4 Blue edges are tree edges. A directed edge means the target component has been chosen from a source component during an iteration. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 19

Correctness of Borůvka s Algorithm Regard an arbitrary edge e = (v, w), that is inserted into T during an iteration of Borůvka s algorithm. Let S be the node set that is accessible from v by edges from T shortly before e is inserted It is v S and w V \ S (otherwise a cycle would emerge by adding e). No edge from the cut between S und V \ S has been regarded so far. Otherwise, it would have been inserted without generating a cycle. As e is the cheapest edge from the cut δ(s), it is inserted correctly due to the cut property. The output T does not contain any cycles, because in Step 2 cycles are explicitly avoided. (V, T ) is connected: as G is connected, there would be at least one edge e in G between two different trees from (V, T ). The algorithm would have inserted that one with minimum cost. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 20

Kruskal s Algorithm (1956) Idea: Start with an empty edge set and successively add edges by increasing edge costs in such way no cycles emerge. Kruskal(G, c) 1. Initialize n blue trees consisting of one node each. 2. Sort E = {e 1, e 2,..., e m } increasingly: c e1 c e2 c em. 3. FOR i = 1 TO m DO IF the endnodes u and v of e i = (u, v) are in the same tree THEN label e i red ELSE label e i blue 4. The blue edges form an MST T. Alternative Step 3: FOR i = 1 TO m DO IF T {e i } does not contain a cycle THEN T := T {e i } Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 21

Kruskal Example 8 10 5 8 10 5 2 3 2 3 18 16 16 12 14 30 12 14 4 26 4 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 22

Correctness of Kruskal s Algorithm Regard an arbitrary edge e = (v, w), that is inserted into T during an iteration of Kruskal s algorithm. Let S be the node set that is accessible from v by edges from T shortly before e is inserted It is v S and w V \ S (otherwise a cycle would emerge by adding e). No edge from the cut between S und V \ S has been regarded so far. Otherwise, it would have been inserted without generating a cycle. As e is the cheapest edge from the cut δ(s), it is inserted correctly due to the cut property. The output T does not contain any cycles, because in every iteration cycles are explicitly avoided. (V, T ) is connected: as G is connected, there would be at least one edge e in G between two connected components from (V, T ). The algorithm would have inserted that one with minimum cost. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 23

Reverse Kruskal Reverse-Kruskal(G, c) 1. Label all edges in E blue. I.e. set (T := E). 2. Sort E = {e 1, e 2,..., e m } decreasingly: c e1 c e2 c em. 3. FOR i = 1 TO m DO IF T \ {e i } is connected THEN label e i red. 4. The blue edges form an MST T. The above procedure is also called Dual Kruskal due to duality of the blue and red rule. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 24

Correctness of Reverse Kruskal Regard an arbitrary edge e = (v, w) that is removed during the algorithm At the time of removal, e is part of a cycle C. Amongst all edges within C, e is the first edge to consider, that is e is the most expensive edge on C Due to the cycle property, the removal has been done deservedly. The output T of the algorithm T is connected due to the fact that at no time an edge is removed from T that would destroy connectivity (only edges from cycles are removed). In the end, T does not contain a cycle anymore because the most expensive edge on this cycle would have been removed. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 25

Implementation of Kruskal s Algorithm (1) Kruskal s Algorithm has an obvious invariant: the edge set T reflects a forest in G at any time of the algorithm. Obviously, the time critical operation is the test, if a cycle emerges in T {e i } by the insertion of an edge e i into T. A cycle emerges if and only if for en edge e i = (u, v), the two nodes u and v are part of the same connected component of T. We already know techniques to efficiently compute the connected components of a graph: BFS and DFS. These have linear complexity O(m + n). If we had to compute the CCs in every iteration of Kruskal s algorithm from scratch, this would amount to a total complexity of O(m 2 ). In order to avoid this, we would like to make use of a data structure that supports the operations of the algorithm efficiently. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 26

Implementation of Kruskal s Algorithm (2) We would like to make use of a data structure that supports the following operations efficiently: Given a set of nodes V of fixed size, and an edge set T that increases stepwise: in every step, an edge is inserted into T. At no time, an edge is removed from T. During the rise of T, we would like to know the connected components in every iteration. That is, for every node v V, we would like to compute its connected component efficiently. If we identify u and v to be part of two separate connected components, we would like to merge these components efficiently by insertion of an edge e = (v, w). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 27

Complexity of MST Algorithms Sorting edges according to their weights: O(m log m) e.g. Quicksort. Testing for cycles amounts to the test whether two nodes are in different connected components, or not. Ideas: Test: for every node, get efficiently the connected component it belongs to. Merge: always move the component of smaller size into the component of larger size. Hence, every component (or single node) is merged into a component of at least twice the size. Hence, every node is moved between components at most log n times. Thus, we have O(n log n) for cycle testing, that is also O(m log m) (see below). Can be done more efficiently by advanced Disjoint-Set / Union-Find data structures (in nearly linear time). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 28

Complexity of MST Algorithms (cont d) As m > n 2 in connected graphs holds, we can estimate n by m. As m < n 2, it is log m < log n 2 = 2 log n = O(log n). Therefore, the total cost is O(m log m), or O(m log n), respectively. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 29

Prim s Algorithm Jarnik (1930), Prim (1956), Dijkstra (1959) Idea: Start from an arbitrary node. In every step, grow the tree by inserting the node producing minimal cost. Prim(G, c, s) 1. Initialize n blue trivial trees (single nodes). Choose start node s. 2. WHILE trivial blue trees exist DO choose an edge of minimal weight from the cut that is induced by the nodes of the tree that contains s. Label it blue. 3. The blue edges form an MST T. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 30

Correctness of Prim s Algorithm During the WHILE loop, let S be the set of nodes, that are not trivial trees. In every iteration, the algorithms adds the edge e and a node w to the tree T such that c e is minimal from all edges e = (v, w) mit v S und w S. According to the cut property, e is part of any MST. It is easy to see that, eventually, a spanning tree is generated. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 31

Implementation of Prim s Algorithm In every iteration, it is crucial to efficiently decide which node to insert into the tree. (similar to Dijkstra s Algorithm) Amongst all nodes that are not part of the tree, we search for the one that might be connected with the tree by an edge with minimum cost. Therefore, it is convenient to save for every node that is not part of the tree the best possible cost of an insertion. We save this value as a key in every node. If the node cannot be connected with the tree by means of a connecting edge, we set the key to + (e.g. MAX_INT, etc.). In every iteration, we have to find a node that is not part of the tree with minimum key. This node is inserted. Then, the key values have to be updated. This task is supported by a specific data structure, a priority queue. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 32

Priority queues A priority queue Q is an abstract data structure managing a set of objects of type T. Every element in Q has a key of an ordered number type NT (N, Z,...) A priority queue supports the following operations: void create-pq(): create an empty priority queue Q void insert(t & x, NT y): insert the element x with key value y = key[x] into Q. T& find-min(): find the element x with the lowest key value and return reference to it. void delete-min(): remove the element with smallest key value from Q. void decrease-key(t x, NT y): decrease the key value of x to the new value Wert y. (Precondition: key[x] > y) bool empty(): returns TRUE if Q is empty. Annotation: Analogously, the data structure might deliver the element with the largest key value. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 33

Implementation of Prim s Algorithm Prim s Algorithm using a priority queue Q Prim (G = (V, E), c, s) (1) FORALL v V DO (2) key[v] = ; (3) pred[v] = null; // predecessor of v in the MST (4) key[s] = 0; (5) PriorityQueue Q.create-pq(); (6) FORALL v V DO (7) Q.insert (v, key[v]); (8) WHILE!Q.empty() DO (9) v = Q.find-min(); Q.delete-min(); (10) FORALL u Adj[v] DO (11) IF u Q und c({v, u}) < key[u] THEN (12) pred[u] = v; (13) key[u] = c({v, u}); (14) Q.decrease-key(u, key[u]); Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 34

Complexity of Prim s Algorithm lines (1)-(3): Initialization in O(n) lines (4) and (5) in O(1) lines (6) and (7): n insert-operations line (8): exactly n passes through the loop line (9): in total n find-min- und delete-min-operations lines (10) - (14): every edge is touched once, that is O(m) + m decrease-key-operations. line (11): the test if u Q can be done in O(1) with a boolean auxiliary array (must be updated upon insertion / deletion). If we implement the priority queue as a binary heap, the find-min-operations might be done in O(1), the insert-, delete-min- and decrease-key-operations in O(log n) each. In total, this amounts to a complexity of O(m log n). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 35

Round Robin Algorithm The Round Robin Algorithm is the most efficient approach to the computation of MSTs in sparse graphs. It is based on the blue rule and quite similar to Borůvka s algorithm. Round-Robin(G, c) 1. Initialize n blue trees consisting of one node each. 2. As long as there are less than n 1 blue edges, choose a blue tree T, compute the edge e of minimum cost in δ(t ) and label it blue. 3. The blue edges form an MST. It can be implemented to run in O(m log log n): in every iteration, we choose the blue tree with the least number of nodes. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 36

MST Runtimes Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 37

MST Algorithms Complexity Deterministic comparison based MST algorithms Algorithm Complexity Jarnik, Prim, Dijkstra, Kruskal, Boruvka O(m log n) Cheriton-Tarjan (1976), Yao (1975) O(m log log n) Fredman-Tarjan (1987) O(mβ(m, n)) Gabow-Galil-Spencer-Tarjan (1986) O(m log β(m, n)) Chazelle (2000) O(mα(m, n)) The holy grail would be O(m). Notable approaches Dixon-Rauch-Tarjan (1992) Karger-Klein-Tarjan (1995) O(m) verification O(m) randomized On planar graphs possible in O(m). Parallel algorithms with linear number of processors in O(log n). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 38

An Application: 1-trees MSTs might be used for the computation of lower bounds on the optimal length of TSP tours. A Hamilton tour T is defined as follows: The degree of the start node w.r.t. T is 2. T is an MST for the nodes {2, 3,..., n}. The degree of nodes {2, 3,..., n} w.r.t. T is 2. Dropping the last condition, and computing a minimal edge set satisfying the first two conditions, we obviously get a lower bound on the length of a shortest tour. Such edge sets are also called 1-trees. Onetree(G, c) 1. Compute an MST for the node set {2, 3,..., n}. Let c T be its cost. 2. Let e 1 be the shortest and e 2 be the second shortest edge in G incident with node 1. 3. T {e 1, e 2 } is an optimal 1-tree of value c T + c e1 + c e2. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 39

Part II The directed case: Maximum Branchings Clearly, Minimum Branching would not make any sense with positive edge weights as the forests would be empty. Maximum Branching Given a digraph D = (V, A) with edge weights c e for each e A. We search for a branching B A of D that maximizes c e. Recall: A branching B A is an edge set such that in the directed acyclic graph (DAG) given by (V, B), it is δ (v) 1 for all v V. Idea 1 e B Sort edges by decreasing weight and use Kruskal s algorithm: add edges of maximum weight always preserving the branching property (no cycles, δ (v) 1 for all v B). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 40

Maximum Branchings 3 0 1 2 3 2 2 2 3 0 1 2 3 2 0 1 2 3 2 2 2 Bad idea! Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 41

Delayed Decision Idea 2: Reduce Complexity by Delayed Decision Fundamental technique for the solution of complex problems: Postpone critical decisions to a point in time you are able to make them irreversibly by transforming the original problem into easier problems by applying smart reduction techniques. generating a solution to the original problem from the (greedy) solutions of the reduced problems. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 43

Edmonds Branching Algorithm Some terminology s : A V determines the start node of an edge e A. t : A V determines the end node of an edge e A. c : A R determines the weight of an edge e A. u B v means: there is a directed u-v-path in B. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 44

Critical Graphs 4 5 1 4 7 6 4 2 5 3 0 3 2 10 8 4 3 1 5 4 6 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 45

Critical Graphs (2) Definition 9 (Critical edge / subgraph) Let D = (V, A) be a directed graph with edge weights c e = c(e), e A. (a) An edge e A is called critical, if c(e) > 0 and t(e ) = t(e) c(e ) c(e), for all e A. (b) A subgraph H D is called critical, if it consists exclusively of critical edges, and every node is an end node of at most one of these edges, and H is maximal by inclusion w.r.t. this property. Lemma 10 An acyclic critical graph H is a maximal branching. Proof. Obviously, H is a branching. Let B be an arbitrary branching. For every node v V, we have c(b {e t(e) = v}) c(h {e t(e) = v}). Summing up over v V, it is c(b) c(h). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 46

Critical Graphs (3) Lemma 11 Let H be a critical graph. Every node of H is part of at most one cycle. Proof. Consider an edge e. We assume there are 2 cycles containing e. Then there must be a node u with δ (u) > 1. This contradicts the definition. Lemma 12 Let B be a branching, and u, v, w three nodes. If u B v and w B v, then it is either u B w, or w B u. Proof. W.l.o.g., let u B v the shorter path. Then, w B v must contain all edges of this path. Otherwise, one node would be the end node of two edges. Therefore, it is w B u B v. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 47

Critical Graphs (4) Definition 13 Let B be a branching, and e / B. e is called feasible w.r.t. B, if B := B {e} \ {f f B and t(f ) = t(e)} is a branching as well. Lemma 14 Let B be a branching, and e A \ B. Then, e is feasible w.r.t. B if and only if there is no path from t(e) to s(e) in B. Proof. If and only if the insertion of e generates a cycle, B is not a branching. In this case, there must be a path from t(e) to s(e) in B. Lemma 15 Let B be a branching, and C A a cycle with the following property: there is no feasible edge from C \ B w.r.t. B. Then, it is C \ B = 1. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 48

Critical Graphs (5) Proof. Clearly, C \ B > 0, because a branching does not contain any cycle. For contradiction, we assume that C \ B = k 2. Let C \ B = {e 1, e 2,..., e k }, where the e i occur in this order within C. As there is no feasible e i, there are paths t(e i ) B s(e i ) (cf. Lemma 12). Upon assumption, we have t(e i 1 ) s(e i ). From Lemma 10, it follows that B either t(e i 1 ) t(e i ), or t(e i ) t(e i 1 ). B B Let t(e i 1 ) B t(e i ) B s(e i ). The path t(e i 1 ) B s(e i ) is unique and completely contained in C (by assumption). Therefore, it is t(e i 1 ) t(e i ) s(e i ). This is impossible as another edge B C B C e j e i would exist with end node t(e j ) = t(e i ) C as (s(e i ), t(e i )) is not part of that path. For this reason, it is t(e i ) t(e i 1 ), i = 2,..., k, and t(e 1) t(e k ). B B Hence, B contains a cycle t(e k ) B t(e k 1 ) B... B t(e 1) B t(e k ). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 49

Critical Graphs (6) Corollary 16 For a branching B and a cycle C the following holds: either an edge e i C is feasible w.r.t. B, or B contains all edges of C but one. Theorem 17 Let H be a critical graph. Then, there is a branching B of maximal weight, such that for any cycle C H holds that C \ B = 1. Proof. Let B be a maximum branching that contains as many edges as possible from H. Consider a critical edge e H \ B: If there is no e B with t(e) = t(e ), we could increase the number of edges from H in B by simply adding e. This yields a branching B containing more edges of H than B. Then either B was not maximal, or B contains a circuit. If there is an e B with t(e) = t(e ): if e is feasible, exchanging e by e would deliver a maximum branching B with more edges from H than B has. This contradicts the above construction of B. It follows that there is no feasible edge e H \ B. In particular, for any cycle C H, no edge e C \ B is feasible. With the above Lemma, it follows C \ B = 1 for any cycle C in H. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 50

Critical Graphs (7) In the following, we denote by C 1, C 2,..., C k the (node disjoint) cycles of the critical graph H. From any of these cycles, we choose an edge of minimal weight a 1, a 2,..., a k. By V i = V (C i ), we denote the nodes of cycle C i. Corollary 18 Let D = (V, A) be a digraph with edge weights c e, and H a critical graph with cycles C i,..., C k. Then, there is a maximum branching B such that: (a) C i \ B = 1, for all i = 1,..., k. (b) If for any edge e B \ C i holds that t(e) / V i, then it is C i \ B = {a i }. Proof. (a) holds (cf. Theorem 15). Let B be a maximal branching with the minimum number of edges a i. We show, that (b) follows if (a) holds: If (b) does not hold, there is a cycle C i with t(e) / V i for all edges e B \ C i, but a i B. Then, a i could be exchanged by any other edge from the cycle C i preserving maximality. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 51

Critical Cycles 4 6 5 4, and 0 1 0 8 2 3 10 3 4 4 6 7 5 1 5 4 4 0 1 2 3 4 5 6 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 52

Shrinking critical cycles Let H be the critical graph of D with cycles C 1, C 2,..., C k. Let Ṽ be the set of nodes outside any cycle. For any v V i, let ẽ(v) be the edge within C i with end node v. We construct a new instance in D = ( V, Ā) with functions s, t, and c as follows: V := Ṽ {w 1, w 2,..., w k }, w i are pseudo-nodes replacing a cycle C i. Ā := A(Ṽ ) = A \ k A(V (C i )) (edges from outside any cycle). i=1 Edges are generated according to the following rules: s(e) := { s(e), if s(e) Ṽ, w i, if s(e) V i, t(e) := { t(e), if t(e) Ṽ, w i, if t(e) V i, { c(e), if t(e) Ṽ, c(e) := c(e) c(ẽ(t(e))) + c(a i ) if t(e) V i. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 53

Shrinking Example 6 3 1 2 5 2 W2 4 W1 4 5 4 7 3 5 0 7 5 There are two critical cycles: C 1 (blue), and C 2 (red). It is a 1 the edge (4, 3), and a 2 the edge (2, 0). Let e be the edge (2, 1). Then, c(e) := c(e) c(ẽ(t(e))) + c(a 1 ) = 3 c(ẽ(1)) + 4 = 3 5 + 4 = 2. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 54

Branching correspondence Let B be the set of branchings in the original graph D that satisfy (a) and (b) from Corollary 16. That is, these branchings contain all edges from critical cycles but one. If possible, the shortest edge is missing. These branchings are not necessarily optimal. For example, every branching in a DAG satisfies (a) and (b). Theorem 19 There is a bijective transformation between B and the set of branchings in D. In particular, a branching B B corresponds to a branching B := B Ā in D, and it holds k k c(b) c( B) = c(c i ) c(a i ). Therefore, an optimal branching in D is also optimal in D. i=1 i=1 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 55

Edmonds Algorithm (1967) Edmonds fundamental idea Shrink critical cycles and adjust the edge weights in the above manner until the graph is acyclic. Choose branching edges and extract pseudo nodes to the next higher level. Never revise choice of edges made in a lower level, instead choose edges within critical cycles according to that choice. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 56

Edmonds Algorithm (1967) Edmonds(D, c) 1. Compute a critical graph H D. 2. If H is acyclic, return H (H is an optimal branching). 3. Shrink the cycles in H to obtain D and c. 4. Recursively compute an opt. branching B for D by calling Edmonds( D, c) 5. Expand B to an optimal branching B for D. An efficient implementation is not trivial. There are O(mn), O(m log n), and O(m + n log n) implementations. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 57

Example Phase 1: Shrink 1) 2) W1 4-1 -1 3 10 1 1 2 3 2 8 4 5 4 7 W2 4 5 4 4 3 3 6 4 4 1 6 3-2 7 W3 3 5 5 6 3) W4-1 3 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 58

Example Phase 2: Expand 3) 2) W1 4 3 10 1 2 4 7 W2 3 6 7 4 3 3 1) W3 6 4 5 5 6 W4 3 Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 59

Concluding Annotations Regarding parallel edges in the shrunk graph, only the one of highest weight is relevant. Zero or negative weight edges might be immediately eliminated. During expansion, edges choosen at a less expanded level are always kept (cf. Theorems 15,17). Application: Edmonds algorithm might be used to compute 1-arborescences: Analogously to the 1-tree lower bound for the TSP, these might be used as lower bounds for the asymmetric TSP (ATSP). Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 60

Selected Literature Edmonds, J.: Optimum Branchings, J. Res. Nat. Bur. Standards, vol. 71B, 1967, 233 240. Tarjan, R. E.: Finding Optimum Branchings, Networks, v.7, 1977, 25 35. Karger, D. R., Klein, P. N., and Tarjan, R. E.: A randomized linear-time algorithm to find minimum spanning trees, JACM 42, 2 (Mar. 1995), 321 328. Chazelle, B.: A minimum spanning tree algorithm with inverse-ackermann type complexity, JACM 47, 6 (Nov. 2000), 1028 1047. Pettie, S. and Ramachandran, V.: An optimal minimum spanning tree algorithm, JACM 49, 1 (Jan. 2002), 16 34. Bader, D. A. and Cong, G.: Fast shared-memory algorithms for computing the minimum spanning forest of sparse graphs, J. Parallel Distrib. Comput. 66, 11 (Nov. 2006), 1366 1378. Efficient Graph Algorithms Wolfgang Stille WS 2011/2012 Chapter III - Optimal Trees and Branchings 61