Solution to exercise sheet

Similar documents
Pairwise alignment using shortest path algorithms, Gunnar Klau, November 29, 2005, 11:

Additional Divide and Conquer Algorithms. Skipping from chapter 4: Quicksort Binary Search Binary Tree Traversal Matrix Multiplication

BIJECTIONS FOR PLANAR MAPS WITH BOUNDARIES

Generalized Edge Coloring for Channel Assignment in Wireless Networks

CS521 \ Notes for the Final Exam

Generalized Edge Coloring for Channel Assignment in Wireless Networks

2-connected graphs with small 2-connected dominating sets

Lecture 3: Graphs and flows

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

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

Single Source Shortest Path

Solutions to Tutorial 1 (Week 8)

1 Shortest Path Problems

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

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

Shortest Path Problem

Jessica Su (some parts copied from CLRS / last quarter s notes)

Graph Representation

The Reconstruction of Graphs. Dhananjay P. Mehendale Sir Parashurambhau College, Tilak Road, Pune , India. Abstract

arxiv: v1 [math.co] 15 Dec 2017

Computer Organization

LECTURES 3 and 4: Flows and Matchings

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

DFS & STRONGLY CONNECTED COMPONENTS

W4231: Analysis of Algorithms

Variable Independence and Resolution Paths for Quantified Boolean Formulas

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.

6.854J / J Advanced Algorithms Fall 2008

Intensive Hypercube Communication: Prearranged Communication in Link-Bound Machines 1 2

Graph Representations and Traversal

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

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

Frequent Pattern Mining. Frequent Item Set Mining. Overview. Frequent Item Set Mining: Motivation. Frequent Pattern Mining comprises

Optimal Oblivious Path Selection on the Mesh

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

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

Throughput Characterization of Node-based Scheduling in Multihop Wireless Networks: A Novel Application of the Gallai-Edmonds Structure Theorem

Divide-and-Conquer Algorithms

1 Surprises in high dimensions

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Skyline Community Search in Multi-valued Networks

Lecture 10,11: General Matching Polytope, Maximum Flow. 1 Perfect Matching and Matching Polytope on General Graphs

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

arxiv: v2 [math.co] 5 Jun 2018

Proving Vizing s Theorem with Rodin

CS350 - Exam 4 (100 Points)

Exercise set 2 Solutions

Lecture 1 September 4, 2013

Chapter 22. Elementary Graph Algorithms

Algorithm Design and Analysis

CS 106 Winter 2016 Craig S. Kaplan. Module 01 Processing Recap. Topics

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.

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Basic Graph Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Introduction to Algorithms A graph G =(V, E) V = set of vertices E = set of edges

Elements of Graph Theory

6.854J / J Advanced Algorithms Fall 2008

Lecture 20 : Trees DRAFT

Design and Analysis of Algorithms

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

1. Lecture notes on bipartite matching

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Distributed Line Graphs: A Universal Technique for Designing DHTs Based on Arbitrary Regular Graphs

Reference Sheet for CO142.2 Discrete Mathematics II

Design of Policy-Aware Differentially Private Algorithms

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

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.

PERFECT ONE-ERROR-CORRECTING CODES ON ITERATED COMPLETE GRAPHS: ENCODING AND DECODING FOR THE SF LABELING

3.1 Basic Definitions and Applications

Online Appendix to: Generalizing Database Forensics

Representations of Graphs

Graph and Digraph Glossary

Principles of B-trees

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

6. Lecture notes on matroid intersection

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

Mathematical and Algorithmic Foundations Linear Programming and Matchings

COL 702 : Assignment 1 solutions

Graph Algorithms. Definition

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

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

Shortest path problems

Solution for Homework set 3

Non-homogeneous Generalization in Privacy Preserving Data Publishing

Loop Scheduling and Partitions for Hiding Memory Latencies

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

Matching Theory. Figure 1: Is this graph bipartite?

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Treewidth and graph minors

Lecture 10 Graph algorithms: testing graph properties

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Transcription:

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 1 02.04.2013 Exercise 1: Prove Observation 1: Every forest is bipartite. Solution: Let F =(V,E) be a forest. Then F is acyclic. Divie V into its connecte components (i.e., trees) V = V 1 V k. In such a component there exists a unique path from one vertex to another. Now efine for each component V i an an arbitrary vertex u i œ V i a function i : V i V i æ N such that i (u i,v):= fi for the u i -v-path fi. Now we are able to efine the bipartition of V into A an B as follows: k A := {v œ V i i (u i,v) 2 0} fi {u i }, i=1 k B := {v œ V i i (u i,v) 2 1}. i=1 Let u, v œ A be some noes. Assume for contraiction that (u, v) œ E. As u, v œ V i for some 1 Æ i Æ k we have that i (u i,v) = i (u i,u) because u = v an V i is a tree. W.l.o.g. i (u i,v) 2 0 an then i (u i,u) 21. Hence u œ B an v œ A which is a contraiction. Exercise 2: Prove Observation 2: Let e be an ege of graph G. Then the following claims are equivalent. 1. e is a brige. 2. e is not part of any circle in G. 3. It hols Ÿ(G)+1=Ÿ(G e). Solution: We will prove the circular argument. Let G =(V,E) be a graph. 1 2: Let e be a brige. Assume that e = {u, v} is part of a circle in G. Consier V Õ V as the connecte component insie which fi is locate. Hence for all vertices w œ V Õ it hols that there are v-w-paths. Observe that in the Graph G e there is still a u-v-path through the remainer of the previous circle. Hence Ÿ(G) =Ÿ(G e) which is a contraiction. 2 3: Now e is not part of any circle in G. Let e = {u, v}. Hence there exists no u-v-path fi such that {u, v} /œ fi (because otherwise we woul have a circle with e). Hence G e contains no u-v-path wherefore Ÿ(G e) =Ÿ(G)+1. 3 1: follows by efinition of a brige.

Exercise 3: Let G =(V,E) be an unirecte connecte graph an fi 1, fi 2 two longest simple paths in G. Prove that fi 1 fl fi 2 = ÿ, where fi 1 fl fi 2 := {v œ V v œ fi 1 an v œ fi 2 }. Solution: Given the unirecte connecte graph G =(V,E) an two longest simple paths fi 1 = p 1,...,p n an fi 2 = q 1,...,q n we observe that fi 1 = fi 2 hols. Assume for contraiction that there is no common vertex in fi 1 an fi 2. As G is connecte we can construct a path fi from the initial noe p 1 of fi 1 to the initial noe q 1 of fi 2. fi must leave fi 1 on some vertex x. Hence x ivies fi into two pieces. Lets say the longer part is fl 1 an is the path from either the first or the last vertex to x. fi also ivies fi 2 an efines a longer subpath in fi 2 which will be enote with fl 3 an moves from the first or the last noe of fi 2 to the vertex y where fi meets fi 2. The subpath in fi which is between x an y is enote with fl 2. As fi 1 an fi 2 have no common vertex (by assumption) fl 2 must consist of at least one ege. Through fl 1, fl 2, an fl 3 we efine a new path fi Õ. Through the previous choice of the longer subpaths it must hol fl 1 Ø fi 1 2 an fl 3 Ø fi 2 2. Combining with fl 2 Ø 1 we get for fi Õ : fi Õ Ø fi 1 2 +1+ fi 2 2 = fi 1 +1= fi 2 +1. As fi 1, fi 2 are two longest paths in G we have a contraiction because fi Õ is longer. Hence there must be a common vertex.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 2 09.04.2013 Exercise 1: Let G =(V,E) be a graph with Prove that G is connecte. (G)+ (G)+1Ø V. Solution: We prove this by confutation of the opposite: there is no unconnecte graph G such that (G)+ (G)+1= V. Hence there cannot be any unconnecte graph G with (G)+ (G)+1Ø V, because one woul have to a further eges. Thus for all graphs G with (G)+ (G)+1Ø V that they are connecte. Let G be an unconnecte graph. Then G can be partitione into the connecte components K 1,K 2,...,K n. Let enote with K max the connecte component with the maximum number of vertices. Only in this component a vertex egree of V Kmax 1 can be achieve. Similarly for the component K min consisting of the fewest vertices that the minimum vertex egree can be at most V Kmin 1. Now it hols that (G) = max vœv eg G v Æ V Kmax 1, (G) =min vœv eg G v Æ V Kmin 1, whence we get for the initial equation: (G)+ (G) + 1 = max vœv eg G v +min vœv eg G v +1Æ V Kmax 1+ V Kmin 1+1 = V Kmax + V Kmin 1 < V. For every unconnecte graph this equation oes not hol. Hence the opposite is true. Exercise 2: State an algorithm to etermine the connecte components of a given unirecte graph using BFS. Solution: We coul solve this exercise by the computation of strongly connecte components as in the following exercise after aing for each unirecte ege both irecte ones. However there is an algorithm that oes not make use of the SCCs which is fairly easy to fin:

Algorithm 1: Connecte-Components(G). Input 1 i Ω 0; : Unirecte graph G =(V,E) 2 while there is a vertex v œ V without a marking o 3 call BFS(G, v) anmarkallverticesu s.t. mark[u] =truewithi; 4 i Ω i +1; Exercise 3: State an algorithm to etermine the strongly an weakly connecte components of a given igraph. Solution: We will start by outputting the strongly connecte components (SCCs): Naturally we construct a epth-first-algorithm DFS from our BFS algorithm by using a stack instea of the queue. However we nee to exten the algorithm a little bit by introucing iscovery an finishing times. This is require to built an algorithm computing SCCs. So at first the DFS algorithm: Algorithm 2: Depth-first search DFS(G, s) Input :GraphG =(V,E). 1 Init route[v] with for every v œ V ; 2 stack S Ωÿ; 3 time Ω 0; 4 while there is an unmarke vertex s œ V o 5 S.push(s); 6 while S is not empty o 7 w Ω S.pop(); 8 time Ω time+1; 9 if w has a iscovery time then f(w) Ω time; // finishing time 10 else 11 (w) Ω time; // iscovery time 12 S.push(w); 13 forall the eges e =(w, v) œ E o 14 if not mark[v] then 15 mark[v] Ω true; 16 S.push(v); 17 route[v] Ω route[w]+1; Further we nee a last ingreient for the final algorithm: transposes of graphs. Let G = (V,E) be a irecte graph. Then G T = (V,E T is the transpose of G, where E T := {(u, v) (v, u) œ E}, i.e., we have reverse the irections. By ajacency-list representation it requires time O( V + E ) to create G T. Now the algorithm that computes the SCCs of a igraph is the following: Claim 1. The algorithm Strongly-Connecte-Components(G) is correct.

Algorithm 3: Strongly-Connecte-Components(G) Input : A igraph G =(V,E) 1 call DFS(G) to compute finishing times f(u) for each vertex u; 2 compute G T ; 3 call DFS(G T ), but now consier the eges in the forall-loop w.r.t. ecreasing orer of the finishing times of the vertices; 4 output the vertices of each tree in the epth-first forest forme in the previous line as a separate SCC. Proof of Claim 1. We prove the result by inuction on the number of epth-first trees foun in line 3 of the algorithm that the vertices of each tree form a SCC. The inuction hypothesis is that the first k trees prouce are SCCs. The basis for k =0is trivial. Inuction step k æ k +1: Let u be the root of the (k + 1)st tree an u be in the SCC C. Through the choice of roots it hols f(u) =f(c) >f(c Õ ) for any SCC C Õ other than C that has yet to be visite, where f(x) is max f(x) for x œ X. Now it is easy to show that all vertices of C are escenants of u in its epth-first tree. Further any eges in G T that leave C must be to SCCs that have alreay been visite. Hence no vertex in any SCC other than C will be a escenant of u uring the fs of G T. Hence, the vertices of the epth-first tree in G T roote at u form exactly one SCC. We say that the component graph G Õ for a given graph G is efine as follows. Let S 1,...,S k be the SCCs of G. Then replace each SCC S i by a fresh vertex s i an transfer the ege connections outsie of the SCC S i to s i. Now turn towars the weakly connecte components (WCCs). Here we make use of the following observation. A graph G is weakly connecte i its component graph G Õ is a path. By this one can easily use the previous algorithm for SCCs to compute the WCCs. Exercise 4: Explain why the DJP algorithm oes not work properly for negative weights. State an example where it fails. Solution: The problem is that the algorithm woul not ecie for a worse ege to get a much better ege in the next step. This is some kin of local minimum phenomenon. b 1 1 a 4-3 c So the shortest path from a to is over c but DJP woul choose the path over b.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 3 16.04.2013 Exercise 1: Consier the following moification of the DJP algorithm to work with negative weights: Determine the smallest weight c œ Z in the weighte graph G = (V,E,w), i.e., the ege e s.t. w(e) =c. Then for all eges f œ E set w Õ (f) :=w(f) c. Then G Õ =(V,E,w Õ ) has no negative weights. Does the DJP algorithm work correctly on this type of graph? Prove your claim. Solution: Now we claim that DJP oes not work correctly on G Õ because this moification oes not maintain the shortest path property, i.e., if fi was a shortest path in G from s to t, thenfi is a shortest s-t-path in G Õ. The following counter-example proves this. a 1 7 b 3 1-2 a 3 9 b 5 3 0 c c In the left graph a, b, is the shortest path from a to. shorter. In the right graph a, is Exercise 2: Prove the upper-boun property: Let G =(V,E,w) be a weighte igraph an s œ V be a vertex. Then cost[v] Ø (s, v) for all v œ V an this invariant is maintaine over any sequence of relaxation steps on the eges of G. Moreover, once cost[v] achieves its lower boun (s, v), it never changes. So prove the invariant cost[v] Ø (s, v) for all v œ V by inuction over the number of relaxation steps. Solution: For the basis, cost[v] Ø (s, v) is certainly true after initialization, since cost[s] =0Ø (s, s) (note that (s, s) = Œ if s in on a negative-weight cycle an 0 otherwise) an cost[v] =Œ implies cost[v] Ø (s, v) for all v œ V {s}. For inuctive step, consier the relaxation of an ege (u, v). By the inuctive hypothesis, cost[x] Ø (s, x) for all x œ V prior to the relaxation. The only cost[ ] value that may change is cost[v]. If it changes, we have cost[v] =cost[u]+w(u, v) Ø (s, u)+w(u, v) Ø (s, v) (IH) (triangle property)

an so the invariant is maintaine. To see that the value of cost[v] never changes once cost[v] =(s, v), note that having achieve its lower boun, cost[v] cannot ecrease because we have just shown that cost[v] Ø (s, v), an it cannot increase because relaxation steps o not increase cost[ ] values. Exercise 3: Prove the convergence property: Let G =(V,E,w) be a weighte igraph an let (u, v) œ E. Then, immeiately after relaxing ege (u, v) in the if block we have cost[v] Æ cost[u]+w(u, v). Solution: If, just prior to relaxing ege (u, v), we have cost[v] > cost[u]+w(u, v), then cost[v] =cost[u] +w(u, v) afterwar. If, instea, cost[v] Æ cost[u] +w(u, v) just before the relaxation, then neither cost[u] nor cost[v] changes, an so cost[v] Æ cost[u]+w(u, v) afterwar. Exercise 4: Prove the path-relaxation property: If fi =(v 0,v 1,...,v k ) is a shortest path form s = v 0 to v k, an the eges of p are relaxe in the orer (v 0,v 1 ), (v 1,v 2 ),...,(v k 1,v k ), then cost[v k ]=(s, v k ). This property hols regarless of any other relaxation steps that occur, even if they are intermixe with relaxations of the eges of fi. Show by inuction that after the ith ege of fi is relaxe, we have cost[v i ]=(s, v i ). Solution: For the basis, i =0, an before any ege of fi have been relaxe, we have form the initialization that cost[v 0 ]=cost[s] =0=(s, s). By the upper-boun property, the value of cost[s] never changes after initialization. For the inuctive step, we assume that cost[v i 1 ] = (s, v i 1 ), an we examine the relaxation of ege (v i 1,v i ). By the convergence property, after this relaxation, we have cost[v i ]=(s, v i ), an this equality is maintaine at all times thereafter. Exercise 5: Prove the no-path property: Suppose that in a weighte, igraph G =(V,E,w), no path connects a source s œ V to a given vertex v œ V. Then we have cost[v] =(s, v) =Œ after initialization, an this equality is maintaine as an invariant over any sequence of relaxation steps on the eges of G. Solution: By the upper-boun property, we always have Œ = (s, v) Æ cost[v], an thus cost[v] =Œ = (s, v). Exercise 6: Prove the cost-array correctness: Let G =(V,E,w) be a weighte igraph an s œ V, an assume that G contains no negative cycles reachable from s. Then, after V 1 iterations of the for loop it hols cost[v] =(s, v) for all vertices v that are reachable from s. Solution: We prove the claim by appealing to the path-relaxation property. Consier any vertex v that is reachable from s, an let fi =(v 0,v 1,...,v k ) where v 0 = s an v k = v, be any acyclic shortest path from s to v. Path fi has at most V 1 eges,

an so k Æ V 1. Each of the V 1 iterations of the for loop relaxes all E eges. Among the eges relaxe in the ith iteration, for i =1, 2,...,k,is(v i 1,v i ). By the path-relaxation property, therefore, cost[v] =cost[v k ]=(s, v k )=(s, v). Exercise 7: Prove the preecessor-subgraph property: Let G =(V,E,w) be a weighte igraph, s œ V, an assume G contains no negativeweight cycles reachable from s. After the initialization of the algorithm execute any sequence of relaxation steps proucing cost[v] =(s, v) for all v œ V.Then,thepreecessor subgraph G route[ ] is as shortest-paths tree roote at s. Solution: We must check the three properties of Observation 6 for shortest-paths trees. For the first property, show that V route[ ] is the set of vertices reachable from s. By efinition, a shortest-path weight (s, v) is finite i v is reachable from s, hencethe vertices that are reachable from s are exactly those with finite cost[ ] values. But a vertex v œ V {s} has been assigne a finite value for cost[v] i route[v] =. Hence, the vertices in V route[ ] are exactly the reachable ones. Property two follows irectly from Lemma 1.8. It remains to prove the last property of shortest-paths trees: for each vertex v œ V route[ ] the unique simple path fi = v 0,...,v k with s = v 0 an v = v k in G route[ ] is a shortest path from s to v in G. For 1 Æ i Æ k we have both cost[v i ]=(s, v i ) an cost[v i ] Ø cost[v i 1 ]+ w(v i 1,v i ) from which we conclue w(v i 1,v i ) Æ (s, v i ) (s, v i 1 ). Summing the weights along fi yiels kÿ w(fi) = w(v i 1,v i ) i=1 kÿ! Æ (s, vi ) (s, v i 1 ) " i=1 = (s, v k ) (s, v 0 ) (sum telescopes) = (s, v k ) ((s, v 0 )=(s, s) = 0). Thus, w(fi) Æ (s, v k ).Since(s, v k ) is a lower boun on the weight of any path from s to v k, we conclue that w(fi) =(s, v k ),hencefi is a shortest path from s to v k = v.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 4 23.04.2013 Exercise 1: Given a irecte graph G =(V,E) with V = {1, 2,...,n}, weefinethe transitive closure of G as the graph G ú =(V,E ú ),where E ú = {(i, j) œ V V there is a path from i to j in G}. Usually Boolean values require less storage than wors on current computers. Now construct an e cient algorithm to compute the transitive closure of a igraph which space requirement is less than the Floy-Warshall algorithm. Solution: The following algorithm uses only Boolean values rather than integer values, its space requirement is less than the Floy-Warshall algorithm s by a factor corresponing to the size of a wor of computer storage. Define t k (i, j) to be 1 if there exists a path in G from i to j with all intermeiate vertices in {1,...,k} an 0 otherwise. We construct the transitive closure G =(V,E ú ) by putting ege (i, j) into E ú i t n (i, j) =1: t 0 (i, j) = I 0, if i = j an (i, j) /œ E, 1, if i = j or (i, j) œ E, t k (i, j) =t k 1 (i, j)! t k 1 (i, k) t k 1 (k, j) ", if k Ø 1. Algorithm 1: Transitive-Closure(G =(V,E)) 1 n Ω V ; 2 for i Ω 1 to n o 3 for j Ω 1 to n o 4 if i = j or (i, j) œ E then t 0 (i, j) Ω 1; 5 else t 0 (i, j) Ω 0; 6 for k Ω 1 to n o 7 for i Ω 1 to n o 8 for j Ω 1 to n o 9 t k (i, j) Ω t k 1 (i, j)! t k 1 (i, k) t k 1 (k, j) " ; Exercise 2: Consier the following flow network with a given flow.

2/4 a 4/9 b 5/7 s 1/8 3/4 1/7 t 4/6 c 2/3 1/2 1. What is the flow across the cut ({s, c, }, {a, b, t})? What is the capacity of this cut? 2. Show the execution of For-Fulkerson on the flow network. Solution: 1. c({s, c, }, {a, b, t}) = 4 + 4 + 7 + 2 = 20, an f({s, c, }, {a, b, t}) =2+3+( 1) + 1 + 1 = 6 2. Execution of FF (left the current flow network with the flow, right the resiual network): s 0/9 0/4 a 0/8 0/4 b 0/7 0/7 t s 9 a 4 8 4 b 7 7 t 0/6 c 0/3 0/2 6 c 3 2 Consier the augmenting path p 1 = s, a, b, t. s 4/9 4/4 a 0/8 0/4 b 4/7 0/7 t s 4 a 4 5 8 4 b 7 4 3 t 0/6 c 0/3 0/2 6 c 3 2 Augmenting path p 2 = s, c,, b, t.

s 4/9 4/4 a 0/8 0/4 b 7/7 3/7 t s 4 a 4 5 8 4 b 4 3 7 t 3/6 c 3/3 0/2 3 3 c 3 2 Augmenting path p 3 = s, c, a, b,, t. s 6/9 4/4 a 0/8 2/4 b 7/7 1/7 t s 3 a 4 6 8 2 b 6 1 7 t 5/6 c 3/3 2/2 1 5 c 3 2 Now there oes not exist any other augmenting path an the algorithm terminates.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 5 30.04.2013 Exercise 1: Six reporters Arne (A), Barbara (B), Christine (C), Daniela (D), Elvis (E) an Frank (F), are to be assigne to six news stories Politics (1), Crime (2), Financial (3), Foreign (4), Local (5) an Sport (6). The table shows possible allocations of reporters to news stories. For example, Christine can be assigne to any one of stories 1, 2 or 4. 1 2 3 4 5 6 A B C D E F 1. Show these possible allocations on a bipartite graph. 2. Use For-Fulkerson to compute a maximum matching. 3. Is there a perfect matching? Explain your answer. Solution: 1. A B C D E F 1 2 3 4 5 6 2. A =5,B =1,C =2,E =6,F =4. 3. Looking at the bipartite graph, there is only one possible pair for each of A an D, both of which can only be paire to 5. Since 5 cannot be paire with two i erent vertices, a complete matching is not possible, since one of A an D will always remain unpaire.

Exercise 2: Can the following figure be tile by ominoes (a omino being 2 ajacent squares)? Give a tiling or a short proof that no tiling exists. Solution: Consier the bipartite graph G with a vertex for each square an two squares are ajacent if they share an ege. This graph is bipartite since the squares can be colore black an white in a checkerboar pattern. Any perfect tiling gives a perfect matching by simply selecting the eges corresponing to the ominoes selecte an vice versa. We claim that the configuration shown below is a maximum one an so no perfect tiling exists. We will prove that the matching M corresponing to the shown configuration is maximum by showing that there is no augmenting path as in the lecture. (Alternatively we coul use Hall s theorem.) Let A be the set of black squares an B the set of white squares. Orient the eges of G accoring to M, i.e., all the eges in M are oriente from B to A, an the eges not in M are oriente from A to B as in the oriente version. Let v be the only expose vertex of A an w be the only expose vertex of B, an consier L to be the set of vertices reachable from v (the enclose area in the oriente version). Since w is not in L we obtain that no augmenting path exists. We can also euce the fact that no perfect matching exists from Hall s theorem by observing that the 11 black vertices in L (the enclose region on the right) have only 10 (white) neighbors.

v w Exercise 3: Consier a bipartite graph G =(V,E) with bipartition (A, B) :V = AfiB. Assume that, for some vertex sets A 1 A an B 1 B, there exists a matching M A covering all vertices in A 1 an a matching M B covering all vertices in B 1. Prove that there always exists a matching covering all vertices in A 1 fi B 1. Solution: Let G =(V,E)=(AfiB,E), subsetsa 1 µ A, B 1 µ B an matchings M A,M B that cover A 1 an B 1, respectively. We construct a matching M that covers A 1 fi B 1. Clearly, the ege set M = M A fi M B covers A 1 fi B 1, but it is not necessarily a matching. We show how to elete eges from M to make it into a matching. We know M A M B is a union of isjoint cycles an alternating paths. The vertices with some incient ege from both M A \ M B an from M B \ M A are the only ones where M fails to be a matching. We show how to elete some eges from M A M B,soM is still a matching an no vertices are uncovere. We o so in each component of M A M B. Cycle: Since G is bipartite, the cycle has even length. Therefore, we can elete every other ege an the esire properties hol. Path of o number of eges: We can elete every other ege starting from the ege that is ajacent to the last ege of the path. The esire properties hol. Note that this is possible only because the path has o number of eges. Path of even number of eges: In this case, we can elete every other ege but one enpoint will be covere an the other uncovere. We nee to prove that both enpoints cannot be in A 1 fi B 1. Thus, we elete every other ege so the enpoint that is not in A 1 fi B 1 is uncovere. We o so by contraiction: assume both enpoints are in A 1 fib 1. As the path has an even number of eges, an G is bipartite, then both enpoints must belong to the same bipartition set (A or B). W.l.o.g. say they both belong to A, an thus also belong to A 1. Note that each vertex in A 1 has exactly one incient ege from M A ; thus the path we are analyzing (that is a connecte component of M A M B )must contain these two eges. However, this path is of even length, an is alternating, so the en-eges cannot be from the same matching M A ( ). This shows that our initial assumption is wrong, i.e., it must happen that both enpoints o not belong to A 1 fi B 1, as esire.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 6 07.05.2013 Exercise 1: A bipartite graph G =(V, W, E) mit V = W is regular if all vertices v œ V fi W have the same egree eg v = 0. Show the following: Every regular bipartite graph has a perfect matching. Solution: Let G =(V,W,E) be a regular bipartite graph an the egree be k œ N. Counting the eges we get k V = k W. Hence it su ces to check Hall s conition because a matching which saturates V also saturates W an hence is a perfect matching. Let S V an let m S be the number of eges from S to (S). As G is regular with egree k it hols that m S = k S. These m S eges are incient to (S) an hence m S Æ k (S). Thus it hols k S Æ k (S), whence (S) Ø S is true for k>0 (which is true ue to the efinition of regular). As S has been chosen arbitrarily the result follows by Hall s theorem. Exercise 2: Let G =(U, V, E) be a bipartite graph, where U = {1,...,4} an V = {5,...,8}. The eges are efine as E = {{1, 5}, {1, 6}, {2, 5}, {3, 6}, {3, 8}, {4, 7}}. 1. Is there a perfect matching for G? Use Hall s theorem in your argumentation. 2. Construct a network through aing source an sink an compute a maximum matching with For-Fulkerson. Solution: 1. Yes, because for every A U it hols A Æ N(A). 2. The maximum matching we get is a perfect one: (1, 6), (2, 5), (3, 8), (4, 7). Exercise 3: Compute a maximum matching of G in the previous task with the technique of augmenting paths. Use a BFS to fin augmenting paths.

Solution: The following table shows the steps of BFS fining the augmenting paths an getting the matchings. augmenting path matching (1, 5) (1, 5) (2, 5), (5, 1), (1, 6) (2, 5), (1, 6) (3, 8) (2, 5), (1, 6), (3, 8) (7, 4) (2, 5), (1, 6), (3, 8), (7, 4) Exercise 4: Given graph G =(V, E) where V = {1,...,9} an E = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {7, 8}, {8, 9}, {1, 7}, {2, 7}, {3, 9}, {4, 9}, {5, 8}, {6, 8}}. Compute a maximum matching of G with Emons algorithm. Solution: 4 9 3 2 1 7 5 8 6

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 7 14.05.2013 Exercise 1: Generalize Euler s formula to unconnecte plane graphs. Prove your claim. Solution: Euler s formula can be generalize as follows. Let G =(V,E) be a plane graph with c components. For each component Euler s formula n m + r =2hols. Now we nee to take care of the outer region. Every component shares this outer region. Hence we have c 1 regions less. It follows n+r (c 1) = m+2 or n+r c m = 1. Exercise 2: Prove the following: Let G be a triangle-free, connecte, plane graph with n(g) Ø 3. Then it hols m(g) Æ 2 n(g) 4. Solution: In a triangle-free graph which is not a forest any frontier of every region consists of at least 4 eges. Hence it hols 4 r(g) Æ 2 m(g). From Euler s formula we get n(g) m(g)+ 1 m(g) Ø 2 m(g) Æ 2 n(g) 4. 2 Exercise 3: Deuce from the previous task that K 3,3 is not planar. Solution: In particular every bipartite graph is triangle-free. As m(k 3,3 )=9> 8= 2 6 4=2 n(k 3,3 ) 4 we get K 3,3 is not planar. Exercise 4: Construct an algorithm which colors any arbitrary graph G with at most (G)+1colors. Solution: Algorithm 1: Algorithm to color an unirecte graph G with at most Input : graph G =(V,E), some fixe vertex enumeration v 1,...,v n œ V (G) 1 for i =1to n o 2 let c œ N be the smallest number such that f(v) = c for all {v i,v} œ E; 3 set f(v i ):=c. (G)+1 colors. Claim. Let G be some graph, then (G) Æ (G)+1. Proof of claim. Suppose the vertices of the graph have an arbitrary labeling 1,...,n. W.l.o.g. suppose

the algorithm processes vertices in the orer 1 to n. Further assume that the (G)+ 1 colors are C := {1,..., (G)+1}. Now the inuction hypothesis is that after i vertices have been processe, each of the vertices 1 to i is assigne a color in the set C such that for any ege {u, v} œ E an u, i Æ i the color f(u) = f(v), wheref : V æ C is the constructe coloring in the algorithm. Inuction basis. After 1 vertex has been processe, color 1 is use for the first vertex an we are one. X Inuction step. i æ i+1:vertexi+1 has at most (G) neighbors. Hence it has at most (G) neighbors that have alreay been assigne color. Thus there is at least one color in the set C that has not been use for any of i +1 s neighbors. The algorithm takes just the smallest remaining number in C to color vertex i +1. Hence the hypothesis still hols after assigning vertex i +1a color. Exercise 5: Construct two examples of graphs G with V (G) > 4 such that the algorithm from the previous task nees (G)+1colors an one where it requires less. Solution: The following graph will be colore with exactly 1= (G)+1color by the algorithm. v 1 v 2 v 3 v 4 v 4 The following graph will be colore with exactly 5= (G)+1colors by the algorithm. K 5 :

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 8 28.05.2013 Exercise 1: Given the problem Problem: OR n Input: n Boolean values x 0,...,x n 1 Task: Compute x n 1 i=0 x i Show that OR n œ CRCW(n, 1). Solution: We use a common CRCW-PRAM with n processors. If one of the values is Algorithm 1: OR n Ècommon CRCW-PRAMÍ 1 global S : Boolean, A: array[0,...,n 1] of Boolean; 2 local p, n: integer; 3 if p =0then S = false; 4 if A[p] then S = true; true, then the n-ary OR is true, otherwise it is false. For k Æ n trues k processors write k-times in parallel a true into the fiel S. This is allowe for CRC-PRAMs. Exercise 2: In the share memory of a PRAM a variable X is store. What is the fastest way to istribute the value of X into the local memory of each processor on a PRAM? Construct for each type of the possible PRAMs an algorithm with a runtime as e cient as possible. Solution: Algorithm 2: istribute ÈCR{C,W}W-PRAMÍ 1 global X : real; 2 local p, n: integer, x: real; 3 x Ω X; Hence istribute is in CR{C,W}W(n, 1). For the EREW-PRAM we will use a binary tree technique, i.e., at first X is copie to processor 0 then to 1, thencpu0 an 1 copy the value to 2 an 3 so on an so forth.

Algorithm 3: istribute ÈEREW-PRAMÍ 1 global X : real, temp: array[0,n 1] of real; // assume n =2 j for some j œ N 2 local p, n, i: integer, x: real; 3 if p =0then temp[0] = X; 4 for i =0to (log n) 1 o 5 if p<2 i then temp[p +2 i ] Ω temp[p]; 6 x Ω temp[p]; Here the runtime is O(log n). Exercise 3: Given the problem Problem: AND n Input: n Boolean values x 0,...,x n 1 Task: Compute w n 1 i=0 x i Show that AND n œ CRCW(m, n m ) for m Æ n. Solution: Iea: For m<nprocessors the first m bits are checke in parallel an one writes a false into the global fiel if a false was rea. Afterwars the array fiels with the numbers m +1 to 2 m are checke until all bits have been consiere. The runtime is O! Á n m Ë". For the case n = m the runtime is analogously to the OR-algorithm in Exercise 1 O(1). Algorithm 4: AND n Ècommon CRCW-PRAMÍ 1 global: A : array[0,...,n 1] of integer, S, n, m : integer; 2 local: p, i : integer; 3 i Ω 0,S Ω true; 4 while i<no 5 if A[p + i] is false then S Ω false; 6 i Ω i + m; Example. n = 10,N =4. ProzessorID i 1 i 2 i 3 0 0 4 8 1 1 5 9 2 2 6 10 3 3 7 11 Arrayfel 0 1 2 3 4 5 6 7 8 9 i k 1 1 1 1 2 2 2 2 3 3

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 9 11.06.2013 Exercise 1: Given an array A[0,...,n 1] of elements an an Boolean array B[0,...,n 1] s.t. B[n 1] = 1 hols. The array B inuces a ivision of A, i.e., every subarray of A ens on an inex j with B[j] =1. Now we are looking for the segmente prefix sums, i.e., the prefix sums of the by B inuce subarray A[i +1,...,j] of A such that the following hols 1. i = 1 or B[i] =1, 2. B[r] =0for i<r<j, 3. B[j] =1. Construct an algorithm which runs in O(log n) steps on an EREW-PRAM with O(n) processors. Note: You can use the principle of pointer jumping. Solution: Iea: Use principle of pointer jumping, where the B-array is use for the linking. Hence set I nil,ifb[i] =1 B[i] = i +1, otherwise Algorithm 1: segprefix ÈEREW-PRAMÍ 1 global A, s: array[0,...,n 1] of real, B : array[0,...,n 1] of integer; 2 local p, N, i: integer; // p an N initialize, N = n 3 S[p] :=A[p]; // copy array 4 if B[p] =1then B[p] :=nil; 5 else B[p] :=p +1; 6 while B[p] = nil o 7 S[B[p]] := S[B[p]] + S[p]; 8 B[p] :=B[B[p]]; Exercise 2: Every element of an n-array list L is colore either blue or re. Construct an e cient algorithm running on a CREW-PRAM which ivies L into two lists such that one consists of only re an the other of only blue elements.

Two global variables blist an rlist shall contain the initial pointers to these lists at the en of the computation. Solution: The following algorithm solves the task. During the process he buils two lists rnext, bnext which are at the en the lists of re/blue elements. The contain the inices of the separate lists. Initially rnext[p] = p + 1if A[p] = nil; an similarly for bnext. Algorithm 2: list-sort ÈCREW-PRAMÍ 1 global rnext, bnext: array[0,...,n 1] of integer; 2 color: array[0,...,n 1] of (blue, re); 3 blist, rlist: integer; 4 local p, N, i: integer; // p, N initialize n = N 5 for i := 0 to Âlog NÊ o 6 if rnext[p] = nil an color[rnext[p]] = re then rnext[p] := rnext[rnext[p]]; 7 if bnext[p] = nil an color[bnext[p]] = blue then bnext[p] := bnext[bnext[p]]; 8 if p =0then 9 if color[0] = re then blist := bnext[blist], rlist := 0; 10 else rlist = rnext[rlist], blist := 0;

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 10 18.06.2013 Another possibility for parallel computers is to consier the message passing moel where a global storage is not use. Formally one sees processors as vertices in a unirecte graph an the eges are biirectional communication channels. Still, the processors work synchronously with the following exceptions: After execution of the operation sen(object, ProcessorI) the sener-processor can immeiately continue, receive(variable, ProcessorI) the receiver-processor waits until he gets the esire object. The unerlying net-topology has three important attributes: Diameter Maximum istance of two vertices. Degree Maximum egree of a vertex. Bisection with Divie the set of vertices into two almost equal halves. The bisection with is then the number of eges connecting both sets. Exercise 1: A processor ring is an array consisting of n processors which are linearly linke an the last processor in the array is linke to the first: p 0 p 1 p n 1 1. What are the attribute values of this topology? 2. Given an n n matrix A an an n-vector b one shall compute A b. Construct an algorithm on a processor ring with N Æ n processors running in ( n2 N ) time with communication time (N n). Solution: 1. Diameter Degree 2 Í N 2 Î Bisection with 2

2. W.l.o.g. let N be a ivisor of n an let r = n N. Now the iea is to ivie A into N sub matrices of size n r, Q R A = c a A 0 A 1 A N 1 b r r r r an b such that b i is a tuple of length r. Then it hols that A b = A 0 b 0 + A 1 b 1 + + A N 1 b N 1. Algorithm 1: matmultvec ÈRingÍ 1 N, p: integer; // initialize 0, Æ p<n 2 B : array[1,...,n][1,...,r] of real; // Submatrix A p 3 w : array[1,...,r] of real; // Subvector b p 4 y, z: array[1,...,n] of real; 5 Compute locally z = A p b p ; 6 if p =0then y := 0; 7 else receive(y, p 1); 8 y := y + z; 9 sen(y, (p + 1) mo N); 10 if p =0then receive(y, N 1); Observations: The processors compute in parallel the proucts A p b p. Then all processors with is 1 to N 1 wait for the ata of their left neighbor. P 0 sets y to A 0 b 0 an sens the result to the right neighbor etc. P 0 then waits for the full sum. The computation time is (r n) = ( n2 N ). The communication time is (N n) because P 0 has to wait until all values are summe up. Exercise 2: A processor gri consists of N = m 2 processors which are arrange as an m m matrix an are connecte as follows:

p 0,0 p 0,1 p 0,m 1 p 1,0 p 1,1 p 1,mn 1 :. :. :. p m 1,0 p m 1,1 p m 1,m 1 1. What are the attribute values of this topology? 2. Construct an algorithm computing the prouct of two n n matrices in (n) steps. Solution: 1. Diameter 2(m 1) Degree 4 Bisection with m 2. Iea: The rows of A will be entere in inverte orer from left the columns of B from top into the gri. Every processor P ij gets in every step two inputs A[i, ] an B[,j], sums the prouct an sens A[i, ] to the right an B[,j] to bottom. B[0,3] B[0,2] B[0,1] B[0,0] :. B[1,2] B[1,1] B[1,0] :. B[2,1] B[2,0] :. B[3,0] B A[0,3] A[0,2] A[0,1] A[0,0] P 0,0 P 0,1 P 0,2 P 0,3 A[1,2] A[1,1] A[1,0] P 1,0 P 1,1 P 1,2 P 1,3 A[2,1] A[2,0] P 2,0 P 2,1 P 2,2 P 2,3 A[3,0] A P 3,0 P 3,1 P 3,2 P 3,3

Algorithm 2: matmul ÈGriÍ 1 n, i,j: integer; // initialize, (i, j) = processor i 2 A, B, C : real; // A contains A[i, k] an B contains B[k, j] 3 : integer; 4 C := A B; 5 for := 1 to n 1 o 6 sen(a, (i, (j + 1) mo n)); 7 sen(b,((i + 1) mo n, j)); 8 receive(a, (i, (j 1) mo n)); 9 receive(b,((i 1) mo n, j)); 10 C := C + A B; Observations: After (n) steps processor P ij has compute the correct value of C[i, j]. Systolic algorithm. The values flow in common moe through the gri. After (n) steps matrix C can be rea. Speeup of magnitue n 2 hence optimal speeup. The coe cient of A an B can be ivie onto the processors of a torus an use the same algorithm.

E cient Algorithms Summer term 2013 Institut für Theoretische Informatik Leibniz Universität Hannover Solution to exercise sheet 11 25.06.2013 Exercise 1: A hypercube of imension consists of 2 processors which are numbere by the -ary ual-representation of processor is. Two processors are connecte through a communication channel if their is have a hamming istance of 1 (i.e., their ualrepresentation i ers in exactly one bit). The structure of a hypercube is recursively efine: The hypercube of imension +1choose two hypercubes H 1,H 2 of imension. The is of H 1 are precee with a 1 an the is of H 2 with a 0. Then connect the corresponing vertices with an ege. 100 101 0 1 =0 00 01 10 11 =1 000 001 110 111 010 011 =2 1. What are the attribute values (efine on the last exercise sheet) of this topology? 2. How many vertices an how many eges are in a hypercube of imension? 3. Given a hypercube of imension. What is the shortest path to pass a message from vertex u to the vertex v? 4. Construct an algorithm which computes the sum of the elements of a given array A of length n =2 on a hypercube of imension running in O(log n) steps. 5. Construct an algorithm which istributes a value store in p 0 through the complete net in O() steps in a hypercube of imension. Solution: 1. Diameter Degree

Bisection with 2 1 2. Vertices: 2, Eges: 2 1. 3. The shortest path between two i erent vertices u, v is 1 Æ u ü v 1 Æ. 4. We assume that A[i] is store in the local variable B of processor i. The sum q ni=0 A[i] is store at the en of the computation in P 0. Algorithm 1: sum ÈHypercubeÍ 1 N,p: integer; // initialize 0 Æ p<2 2 B,C : real; // B contains A[p] 3 m, : integer; 4 := log N; // Assumption: N = s = n 5 for m := 1 own to 0 o 6 if 2 m Æ p<2 m+1 then sen(b,p&(2 m 1)); // bitwise an 7 if p<2 m then 8 receive(c, p 2 m ); // bitwise or 9 B := B + C; In the first if-conition the processors with the is ( 0 01?? ) are chosen. m They sen to the processors ( 0 0?? ) m in the secon if-conition the processors with the is ( 0 00?? ) are cho- m sen an they receive from the processors ( 0 01?? ) m The algorithm runs in O(log n) steps.

5. Here we state an algorithm which istributes a variable x store in p 0 through the complete net. At first, p 0 sens x to p 1, then both to p 2 an p 3, an so on an Algorithm 2: istribute ÈHypercubeÍ 1 N,p: integer; // initialize, 0 Æ p<n=2 2 x: real; 3, i: integer; 4 := log N; 5 for i =0to 1 o 6 if p<2 i then // p =(0 0?? ) i 7 sen(x, p 2 i ); 8 if 2 i Æ p<2 i+1 then // p =(0 01?? ) i 9 receive(x, p&(2 i 1)); so forth. The algorithm runs in O(log n).