Algorithms (Winter 2021) Ungraded Problem Set 5 Due: Not for Submission Topic: Graphs: Basics + DFS
Total Page:16
File Type:pdf, Size:1020Kb
CS 31: Algorithms (Winter 2021) Ungraded Problem Set 5 Due: Not for submission Topic: Graphs: Basics + DFS Problem 1 (Some operations with graphs). For the parts below, describe your algorithm summary in English and also provide a clear pseudocode. You may assume the graph G = (V; E) is input as adjacency lists. a. In this part the graph G is directed. You need to return the reverse graph Grev where all edges have been reversed. Grev should also be represented as adjacency lists. What is the runtime of your algo- rithm? K b. Recall the definition of a walk from vertex u to vertex v and a path from vertex u to v. Design an algorithm which takes input a walk from u to v, and returns a path from u to v. K Problem 2 (DFS firsts and lasts). For each of the following assertions, prove or give a counterexample. The graph G, unless mentioned, is directed. When you give a counter-example, you should explain why it is a counter-example. (a) If G contains a path from u to v and if first[u] < first[v] in a depth-first search of G, then last[v] < last[u]. K (b) If (u=v1; v2; : : : ; vk =v) is a path from u to v in G such that last[vi] < last[v] for all 1 ≤ i < k, then first[v] < first[vi] for all 1 ≤ i < k. K (c) If we run depth-first search on G in an arbitrary order to obtain first’s and last’s, and let σ be the increasing order of last[v]’s. Then running DFS on G in this order returns the strongly connected components of G. K (d) Suppose we run DFS on a graph G with an arbitrary order, and let v be the vertex with the largest last[v]. Suppose u is a vertex and there is a path from u to v in G. Then there is a path from v to u in G as well. K Problem 3 (Counting number of paths in a DAG). KK Let G = (V; E) be a directed acyclic graph. Given two vertices s and t, count the number of paths from s to t using dynamic programming. The algorithm should run in O(m+n) time where jV j = n and jEj = m. Problem 4 (Cheapest Reachable Vertex). KK We are given a directed graph G = (V; E) with jV j = n and jEj = m. Each vertex v has a cost cost(v) which is a positive integer. You need to design an algorithm to output should be an array/dictionary cheapest indexed by the vertices of G, such that cheapest[u] should contain just the cost of the cheapest vertex reachable from u. a. Assume G is a directed acyclic graph and design an O(n+m) time dynamic programming algorithm. 1 b. Then, use this to give an O(n + m) time algorithm for any directed graph. Problem 5. This is another remarkable application of strongly connected components. For this exercise, it may be worthwhile to refresh your knowledge of Boolean variables and Propositional Logic from CS 30 In a 2SAT formula, you are given a set of clauses, where each clause is the disjunction (OR) of two literals (a literal is a Boolean variable or the negation of a Boolean variable). A 2SAT formula is satisfiable if there is a way to assign a value true or false to each of the variables so that all clauses are true , that is, there is at least one true literal in each clause. For example, here’s an example of a 2SAT formula: (x1 _ x2) ^ (x1 _ x3) ^ (x1 _ x2) ^ (x3 _ x4) ^ (x1 _ x4) (1) This formula is satisfiable since we can set x1; x2; x3; x4 to true ,false ,false , and true respectively. Note that all the clauses are satisfied, and thus the ^ of all these clauses are satisfied too. (a) Construct a 2SAT formula with four variables which is not satisfiable. K The 2SAT problem takes input a 2SAT formula and returns YES if it is satisfiable and NO if not. The purpose of this problem to solve the 2SAT problem via a reduction to the SCC problem. In particular, given a 2SAT formula with n variables and m clauses, construct the following directed graph G = (V; E). • G has 2n vertices, one for each variable xi and xi. • G has 2m directed edges: for each clause of the form (α _ β) (where α is either a variable or its negation), G has an edge from α to β and from β to α. Note that the clause (α _ β) is equivalent to either of the implications α ) β or β ) α. In this sense the above graph records all the implications. (b) Construct the graph G for the formula given above in (1). K (c) Prove that if G has a strongly connected component containing both the vertices x and x for some variable x, then the formula can have no satisfying assignment. KK (d) Now prove the negation: prove that if no strongly component of G has a variable and its negation, then the formula must be satisfiable. KKK Conclude that there is a O(n + m)-time algorithm for the 2SAT problem. 2.