Sample Solutions to Homework #4

Similar documents
Elementary Graph Algorithms

Representations of Graphs

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Design and Analysis of Algorithms

Graph implementations :

Outlines: Graphs Part-2

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Unit 2: Algorithmic Graph Theory

Graph Representation

Minimum Spanning Trees

Chapter 22. Elementary Graph Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Basic Graph Algorithms (CLRS B.4-B.5, )

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Introduction to Algorithms. Lecture 11

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

COT 6405 Introduction to Theory of Algorithms

Graph representation

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

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

Minimum Spanning Trees My T. UF

Graph Search. Adnan Aziz

TIE Graph algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Minimum Spanning Trees Ch 23 Traversing graphs

Graph: representation and traversal

CSI 604 Elementary Graph Algorithms

Single Source Shortest Path

Single Source Shortest Paths

Shortest Path Problem

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

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

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

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

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Graph Algorithms. Definition

6.006 Introduction to Algorithms Spring 2008

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

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

Outline. Graphs. Divide and Conquer.

Figure 1: A directed graph.

Algorithm Design, Anal. & Imp., Homework 4 Solution

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

Lecture 11: Analysis of Algorithms (CS ) 1

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Data Structures and Algorithms. Werner Nutt

Solutions to relevant spring 2000 exam problems

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Graph. Vertex. edge. Directed Graph. Undirected Graph

Module 5 Graph Algorithms

W4231: Analysis of Algorithms

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Konigsberg Bridge Problem

Graph Algorithms (part 3 of CSC 282),

Shortest path problems

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

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

Data Structures and Algorithms. Chapter 7. Graphs

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

COL 702 : Assignment 1 solutions

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Review: Graph Theory and Representation

Algorithm Design and Analysis

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Chapter 25: All-Pairs Shortest-Paths

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

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

Graph: representation and traversal

Week 12: Minimum Spanning trees and Shortest Paths

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Single Source Shortest Path (SSSP) Problem

Chapter 9 Graph Algorithms

Shortest Paths. Nishant Mehta Lectures 10 and 11

CS521 \ Notes for the Final Exam

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

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Topological Sort. Version of October 11, Version of October 11, 2014 Topological Sort 1 / 14

Minimum Spanning Trees

Shortest Paths. Nishant Mehta Lectures 10 and 11

Introductory Computer Programming: Shortest Path Algorithms

Topological Sort. Version of September 23, Version of September 23, 2016 Topological Sort 1 / 1

Week 11: Minimum Spanning trees

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

csci 210: Data Structures Graph Traversals

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

COMP 251 Winter 2017 Online quizzes with answers

Transcription:

National Taiwan University Handout #25 Department of Electrical Engineering January 02, 207 Algorithms, Fall 206 TA: Zhi-Wen Lin and Yen-Chun Liu Sample Solutions to Homework #4. (0) (a) Both of the answers returned by the FIND-SET operations are x. The resulting data structure is shown in Figure. (b) Both of the answers returned by the FIND-SET operations are x 6. The resulting data structures are shown in Figure 2. Figure : The result of the FIND-SET operations. 2. (0) Figure 2: (a) The result of FIND-SET(x 2 ); (b) The result of FIND-SET(x 9 ). Given a directed acyclic graph G = (V, E), we wish to find another way to perform topological sorting by repeatedly finding a vertex of in-degree 0, outputting it, and removing this vertex and all of its outgoing edges from the graph.

NEW TOPOLOGICAL SORTING(G) for each vertex v V [G] 2 do in degree[v] 0 3 for each vertex u V [G] 4 for each vertex v Adj[u] 5 do in degree[v] in degree[v] + 6 Q φ 7 for each vertex v V [G] 8 do if in degree[v] = 0 9 then ENQUEUE(Q, v) 0 while Q φ do v DEQUEUE(Q) 2 print v 3 for each vertex k Adj[v] 4 do in degree[k] in degree[k] 5 if in degree[k] = 0 6 then ENQUEUE(Q, k) 7 for each vertex v V [G] 8 if in degree[v] 0 9 then print There is a cycle! Computing all vertices in-degrees takes Θ(V + E) because V is the number of adjacency lists that are accessed and totally E edges are found in these lists. As we process each vertex from the queue, we effectively remove its outgoing edges from the graph by decrementing the in-degree of each vertex one of those edges enters, and we enqueue any vertex whose in-degree goes to 0. Each vertex is enqueued/dequeued at most once because it is enqueued only if it starts out with in-degree 0 or if its in-degree becomes 0 after being decremented some number of times. The processing of a vertex from the queue happens O(V ) times because no vertex can be enqueued more than once. The adjacency list of each vertex is scanned at most once because each vertex is dequeued at most once. Since the sum of the lengths of all the adjacency lists is Θ(E), at most O(E) time is spent in total scanning adjacency lists. For each edge in an adjacency list, Θ() work is done, for a total of O(E) time. Thus the total time taken by the algorithm is O(V + E). If there are cycles it will detect, because some in-degrees will never become 0. 3. (0) (a) G has an Euler tour in-degree(v) = out-degree(v) for each vertex v: Given a graph G with a Euler tour, we can find a tour that goes through all edges in G without repetition with the same source and target. It is clear that, for each vertex v in the cycle, the numbers of edges that go in and go out v must be equal. in-degree(v) = out-degree(v) for each vertex v G has an Euler tour: Since the in-degree and out-degree of eachvertex v V are equal, we can find a cycle from an arbitrary vertex. Then, the edges in the cycle are removed. We also can find a cycle from a remaining vertex that still has edges connecting to it. The process repeats until noedge is available. By traversing edges in the depth first order once a cycles is met, we can find a Euler tour (see Figure 3 for an illustration). (b) The algorithm to find an Euler tour on G is described in the above. Since we continue remove edges from G until no edge is available, the time complex is O(E). 4. (0) Construct an undirected graph G, where each vertex represents the corresponding unshaded square. The edges are constructed between each pair of neighboring unshaded squares. Then, the shortest tour can be found by applying BFS algorithm on G. Since the number of neighbors of each square is at most 4, it implies that E = O(4V ) in G, where V and E denotes the numbers of vertices and edges separately. So the time complexity is O(V + E) = O(V ). 2

Figure 3: An Euler tour. 4/5 0/ 6/7 3/4 7/8 2/3 5/2 6/ 9/0 Figure 4: (a) The resulting graph G after applying DFS (a value associated with each node denotes the finishing time). (b) Compute G T, and then apply DFG on G T. (c) The resulting graph composed of four strongly connected components denoted by different colors. 5. (0) 6. (0) (a) Construct an undirected graph G(V, E), where V = {A, B, C, D}, and every edge e = (v, v 2 ) E if and only if there is a bridge connecting v and v 2. Associate every edge with a direction to make G a directed graph G such that there exists a Euler tour in G. (b) No. By Problem 3, the degree of every vertex in G should be even in order to derive a Euler tour. However, the degree of every vertex in G is odd. The process to find strongly connected components are shown in Figure 4: 7. (0) Exercise 23.2-7 (page 637) Let T = (V, E T ) be the given minimum spanning tree of G = (V, E). Let T = (V {v}, E T ) be a minimum spanning tree of G = (V {v}, E E v ), where v is the new added vertex to G and E v = {(u, v) : u V } is the set of corresponding incident edges. We show that we can update T into T in O(V lg V ) time as follows. First, we claim that E T (E T E v ); that is, T contains only the edges in E T E v. To prove this claim, consider a spanning tree T s of G, where T s contains an edge set E s G E T. For any edge e s = (x, y) E s, we can always find an edge e t E T E v, where e t incidents on x or y and e t s weight is less than or equal to e s s weight because T is a minimum spanning tree of G. That is, we can always replace all edges in E s by the corresponding edges in E T E v and obtain a spanning tree whose cost is less than or equal to the cost of T s. Thus, for any minimum spanning tree containing edges in G E T, we can always find another minimum spanning tree containing only the edges in E T E v. Second, because E T E T E v holds true, T is the subgraph of G r = (V {v}, E r ) = (V {v}, E T E v ). Because E T = V and E v V, we immediately know that E r = E T E v E T + E v < 2 V = O(V ). Thus, we can find T in G r using a minimum-spanning-tree algorithm (such as Kruskal s algorithm) in O(E r lg V ) = O(V lg V ) time. 3

8. (5) Problem 23-3 (page 640) (a) Assume a minimum spanning tree T which is not a bottleneck spanning tree T b. The maximum weight w of edge e in T must be larger than the maximum weight w b of edge e b in T b. In other words, w is larger than any edge weights in T b. Thus, we could replace edge e by an edge in T b and T remains a spanning tree. Then the total edge weight of T would be smaller than original T. Thus, T is impossible to be a minimum spanning tree. In conclusion, a minimum spanning tree T must be a bottleneck spanning tree. (b) To determines whether the value of the bottleneck spanning tree is at most b. We can apply DFS once on a random vertex of G ignoring edge weight is bigger than b to see if every edge of G is visited. If is, then the value of the bottleneck spanning tree is at most b. CHECKBOTTLENECK(G, b) for each vertex u V [G] do 2 color[u] WHITE 3 u randomly chosen vertex in G 4 DFS(u, b) 5 for each vertex u V [G] do 6 if color[u] = WHITE then 7 return f alse 8 return true Figure 5: The Pseudo-code for the CHECKBOTTLENECK procedure. DFS(u, b) color[u] GRAY 2 for each v Adj[u] do 3 if color[v] = WHITE and w(u, v) b then 4 DFS(v, b) 5 color[u] BLACK Figure 6: The Pseudo-code for the DFS procedure. (c) We could use an algorithm like binary search to find the value of bottleneck spanning tree. Let b represent the value of the bottleneck spanning tree. We first find the median weight w m of every edge. Then remove all the edges with weights larger than w m and use the CHECKBOTTLENECK procedure to determine if w m < b. If it does, we recursively find w m in the remain edges and do the same procedure. Otherwise, we contract (in textbook Section B.4) all the edge visited in the DFS procedure of the CHECKBOTTLENECK procedure. Then, recursively find w m in the removed edges in this iteration and do the same procedure. The bottleneck spanning tree T and its value b can be found when there is only one edge in the contracted graph. Since we either contract all the visited edges or decrease half of the remaining edges, the time complexity of this algorithm can be ensured as linear time. 9. (0) Exercise 24.2-3 (page 657) We ll give two ways to transform a PERT chart G = (V, E) with weights on vertices to a PERT chart G = (V, E ) with weights on edges. In each way, we ll have that V 2 V and E V + E. We can then run on G the same algorithm to find a longest path through a directed acyclic graph (in Section 24.2 of the textbook). In the first way, we transform each vertex v V into two vertices v and v in V. All edges in E that enter v will enter v in E, and all edges in E that leave v will leave v in E. In other words, 4

if (u, v) E, then (u, v ) E. All such edges have weight 0. We also put edges (v, v ) into E for all vertices v V, and these edges are given the weight of the corresponding vertex v in G. Thus, V = 2 V, E = V + E, and the edge weight of each path in G equals the vertex weight of the corresponding path in G. In the second way, we leave vertices in V alone, but we add one new source vertex s to V, so that V = V {s}. All edges of E are in E, and E also includes an edge (s, v) for every vertex v V that has in-degree 0 in G. Thus, the only vertex with in-degree 0 in G is the new source s. The weight of edge (u, v) E is the weight of vertex v in G. In other words, the weight of each entering edge in G is the weight of the vertex it enters in G. In effect, we have pushed back the weight of each vertex onto the edges that enter it. Here, V = V +, E V + E (since no more than V vertices have in-degree 0 in G), and again the edge weight of each path in G equals the vertex weight of the corresponding path in G. 0. (20) We first run topological sort on the graph G(V, E), which takes O( V + E ) time. Given two vertex s and t, we know that any path that runs between s and t must use only the vertices located between s and t in the topological sort. If there was a vertex a < s in the topological sort, then there cannot be a path from s a in G. Likewise there can be no vertex b > t on a path from s to t. Then, we can use dynamic programming to calculate the number of paths. For each vertex v i V, where i means that v i is the i-th vertex in the topological sort, we calculate paths[i], the number of paths ending at v i. Then we sum up these derived paths[i] s to get the total number of paths in G. Note that the sum-up action takes O( V ) time. Here shows the dynamic programming to derive paths[i] s. First, the recurrence for paths[i] s are shown as follows: { + (v paths[i] = paths[j], in-degree(v j,v i) E i) > 0, (), otherwise. Using Equation, we calculate paths[i] from i = to V. The calculations take also O( V + E ) time. Note that we need O( V ) space to store paths[i] s. Then the total number of paths in G is V i= paths[i], which takes O( V ) time. From the above analysis, our algorithm takes O( V + E ) time and uses O( V ) space to store additional items.. (0) After applying Bellman-Ford algorithm, we could find the value of x 5 that still could be modified. The negative cycle is: x 5 x x 4 x 2 x 3 x 5 (See Figure 7). Thus, there is no feasible solution exists. 2. (0) Problem 24-3 (page 679) (a) We can use the Bellman-Ford algorithm on a suitable weighted, directed graph G = (V, E), which we form as follows. There is one vertex in V for each currency, and for each pair of currencies c i and c j, there are directed edges (v i, v j ) and (v j, v i ). (Thus, V = n and E = ( n 2 ).) To determine edge weights, we start by observing that if and only if R[i, i 2 ] R[i 2, i 3 ] R[i k, i k ] R[i k, i ] > R[i, i 2 ] R[i 2, i 3 ] R[i k, i k ] R[i k, i ] <. Taking logs of both sides of the inequality above, we express this condition as lg R[i, i 2 ] + lg R[i 2, i 3 ] + + lg R[i k, i k ] + lg R[i k, i ] < 0. 5

Figure 7: The negative cycle of exercise 24.4-2 Therefore, if we define the weight of edge (v i, v j ) as w(v i, v j ) = lg = lg R[i, j], R[i, j] then we want to find whether there exists a negative-weight cycle in G with these edge weights. We can determine whether there exists a negative-weight cycle in G by adding an extra vertex v 0 with 0-weight edges (v 0, v i ) for all v i V, running BELLMAN-FORD from v 0, and using the boolean result of BELLMAN-FORD (which is TRUE if there are no negative-weight cycles and FALSE if there is a negative-weight cycle) to guide our answer. That is, we invert the boolean result of BELLMAN-FORD. This method works because adding the new vertex v 0 with 0-weight edges from v 0 to all other vertices cannot introduce any new cycles, yet it ensures that all negative-weight cycles are reachable from v 0. It takes Θ(n 2 ) time to create G, which has Θ(n 2 ) edges. Then it takes O(n 3 ) time to run BELLMAN-FORD. Thus, the total time is O(n 3 ). Another way to determine whether a negative-weight cycle exists is to create G and, without adding v 0 and its incident edges, run either of the all-pairs shortest paths algorithms. If the resulting shortest-path distance matrix has any negative values on the diagonal, then there is a negative-weight cycle. (b) Assuming that we ran BELLMAN-FORD to solve part (a), we only need to find the vertices of a negative-weight cycle. We can do so as follows. First, relax all the edges once more. Since there is a negative-weight cycle, the d value of some vertex u will change. We just need to repeatedly follow the π values until we get back to u. In other words, we can use the recursive method given by the PRINT-PATH procedure of Section 22.2, but stop it when it returns to vertex u. 6

The running time is O(n 3 ) to run BELLMAN-FORD, plus O(n) to print the vertices of the cycle, for a total of O(n 3 ) time. 3. (5) Exercise 25.2- (699) The matrices D (k) are D (0) = D (3) = 0 0 2 4 0 3 7 0 0 2 0 2 3 4 0 5 5 7 9 0 4. (0) Exercise 25.3-4 (705), D() =, D(4) = 0 0 2 0 4 0 5 7 0 0 6 8 2 0 2 3 4 2 0 5 5 7 9 0, D(2) =. 0 0 2 0 4 0 5 8 7 9 0 It changes shortest paths. Consider the following graph. V = {s, x, y, z}, and there are 4 edges: w(s, x) = 2, w(x, y) = 2, w(s, y) = 5, and w(s, z) = 0. So we would add 0 to every weight to make ŵ. With w, the shortest path from s to y is s x y, with weight 4. With ŵ, the shortest path from s to y is s y, with weight 5. (The path s x y has weight 24.) The problem is that by just adding the same amount to every edge, you penalize paths with more edges, even if their weights are low. 5. (0) (a) In Dijkstra s algorithm, use the reliability as the edge weight and substitute. max (and EXTRACT-MAX) for min (and EXTRACT-MIN) in relaxation and the queue. for + in relaxation. (identity for ) for 0 (identity for +) and (identity for min) for (identity for max). For example, the algorithm in Figure 8 is used instead of the usual RELAX procedure. This algorithm is isomorphic to the one above: It performs the same operations except that it is working with the original probabilities instead of the transformed ones. The time complexity is O(V lgv +E)., RELAX-RELIABILITY(u, v, r) if d[v] < d[u] r(u, v) 2 then d[v] d[u] r(u, v) 3 π[v] u Figure 8: The Pseudo-code for the RELAX procedure. (b) To find the maximum reliability path between each pair of vertices, we modify each entry of W matrix as follows: if i = j, w ij = µ ij if i j and (i, j) E, 0 if i j and (i, j) E. 6. (40) DIY. we can use the algorithm in Figure 9. d (k) ij is the maximum reliability on path i j using vertices with index k. Further, we modify the predecessor matrix Π for k as follows: π (k) ij = { π (k ) ij π (k ) kj if d (k ) ij if d (k ) ij d (k ) ik < d (k ) ik d (k ) kj, d (k ) kj. 7

Max-Reliability(W ) n rows[w ] 2 D (0) W 3 for k to n 4 for i to n 5 for j to n 6 d (k) ij 8 return D (n) max(d (k ) ij, d (k ) ik d (k ) kj ) Figure 9: Determine the maximum capacity path between each pair of vertices. 8