Solutions to relevant spring 2000 exam problems

Similar documents
CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Lecture 3: Graphs and flows

Unit 2: Algorithmic Graph Theory

CS521 \ Notes for the Final Exam

Minimum Spanning Trees

Minimum Spanning Trees Ch 23 Traversing graphs

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

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

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.

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

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

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Copyright 2000, Kevin Wayne 1

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Week 11: Minimum Spanning trees

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

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

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

CSE 331: Introduction to Algorithm Analysis and Design Graphs

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Graph Representation

Analysis of Algorithms

Shortest Path Problem

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

Shortest path problems

Trees. Arash Rafiey. 20 October, 2015

Graph. Vertex. edge. Directed Graph. Undirected Graph

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Reference Sheet for CO142.2 Discrete Mathematics II

Algorithm Design and Analysis

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

Chapter 9 Graph Algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

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

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

Sample Solutions to Homework #4

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

Minimum Spanning Trees

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CISC 320 Midterm Exam

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

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

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

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

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

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

Algorithm Design and Analysis

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Lecture Summary CSC 263H. August 5, 2016

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

W4231: Analysis of Algorithms

Graph Algorithms. Definition

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

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

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Problem Pts Score Grader Problem Pts Score Grader

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

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

Graph: representation and traversal

Single Source Shortest Path

Graph representation

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

Lecture 22 Tuesday, April 10

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

Design and Analysis of Algorithms

Practice Problems for the Final

CS61BL. Lecture 5: Graphs Sorting

3.1 Basic Definitions and Applications

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

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

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

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

CS261: Problem Set #1

CSI 604 Elementary Graph Algorithms

CSCE f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

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

CS4800: Algorithms & Data Jonathan Ullman

Shortest Path Algorithms

Elementary Graph Algorithms

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

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

CS 310 Advanced Data Structures and Algorithms

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Announcements Problem Set 5 is out (today)!

Exam 3 Practice Problems

Shortest Paths. Nishant Mehta Lectures 10 and 11

Module 5 Graph Algorithms

n 2 ( ) ( ) + n is in Θ n logn

Lecture Notes for Chapter 23: Minimum Spanning Trees

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

Chapter 9 Graph Algorithms

) $ f ( n) " %( g( n)

Announcements. HW3 is graded. Average is 81%

Transcription:

Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for (each v Adj[u]) { if (v Q && w(u,v) < key[v]) { π[v] = u; key[v] = w(u,v); Part a asked you to give an estimate of the worst-case running time on a graph with n vertices and e edges. The priority queue is used in five places: it s initialized in the assignment to Q, it s checked in the while condition, the vertex u is extracted from it, a value of one of its elements v may be updated as a result of relaxation, and v is tested for membership in Q. Initialization of the queue is O(n) and checking for an empty queue is O(). There are two places that using an unsorted linked list affects the running time: ExtractMin, where all elements must be checked to determine the element with smallest value, and updating an element, which may require movement in a binary heap but requires no extra processing in an unsorted list. The test for membership in the queue can be implemented merely by a test of a vertex s pointer into the queue; a null pointer means that the vertex is not queued. A less efficient implementation is to search the queue, which requires O(n) operations. This would not be any less efficient than the corresponding operation using a binary heap, however. At any rate, CLR s analysis noted that the outer loop in each algorithm executed once for each vertex, while the inner loop was executed once for each edge. Thus ExtractMin contributes n O(n) operations, and updating contributes e O() for a total of O(n 2 + e) = O(n 2 ) in each algorithm. The membership test in Prim s algorithm, if coded inappropriately, contributes e O(n) operations, which dominates the total running time. Interestingly, ExtractMin exhibits worst-case behavior with any assignment of weights to edges, since every element in the queue must be examined to find the minimum. A more detailed analysis, however, reveals that the number of updates will depend on the assignment of edge weights; in the worst case, a neighbor s key value will be updated every time the neighbor is examined. A pattern that produces

the worst case and an example of a corresponding assignment of edge weights are shown below. vertex action v update v 2, v 3, and v 4 v v 2 v 3 update v 3 and v 4 each for the second time update v 4 for the third time 5 6 v 4 4 v 3 v 2 Problem 3, exam This problem involved getting a Θ value for the solution to the recurrence The recurrence can be solved with the Master Theorem. The variables a, b, and f have values a = 9, b = 3, and f(n) = n 2 + n lg n, which is Θ(n 2 ). The value of log b a is 2 since 9 = 3 2. Case 2 of the Master Theorem applies to give an estimate of Θ(n 2 lg n). An alternate form of the Master Theorem, given on page 9 of the Readings, uses a d variable, which is 2 in the recurrence. Using this version of the Master Theorem was fine. With difficulty, one could produce a solution using the iteration method instead of the Master Theorem. Problem 4, exam Tn ( ) = 9 T n 3 --- + n 2 + n lg n You were to prove that a directed graph G = (V, E) with no isolated vertices is strongly connected if and only if there is a cycle in G that includes every edge at least once (and possibly more than once). As with any if and only if proposition, there were two things to prove:. If G is strongly connected, then there is a cycle that contains every edge at least once. 2. If there is a cycle that contains every edge at least once, then G is strongly connected. The second part is the easier of the two. Since the assumed cycle contains every edge and there are no isolated vertices, the cycle also contains every vertex. Consider any two vertices u and v. A path from u to v is found by starting at u on the cycle and following it around to v; a path from v to u is found in the same way. These two paths, 2

found for all pairs of vertices in the graph, satisfy the conditions for strong connectivity. There were two successful approaches to the first part: a proof by contradiction that involved assuming that no cycle could include every edge; a proof by construction, involving the repeated augmenting of a cycle to include more edges. Here are sample solutions for each approach. Suppose that G is strongly connected, yet no cycle includes every edge in the graph. Let C be a cycle that includes the maximum number of edges of G, and let (u,v) be an edge not included. Let w be some vertex on the cycle. Since G is strongly connected, there is a path P from w to u and a path P 2 from v to w. These two paths, along with (u,v) and C, comprise a longer cycle. It starts somewhere on C, reaches w, detours on P through (u,v), returns to w on P 2, and continues along C to the starting point. It also contains all the edges in C plus an edge not in C, namely (u,v), which contradicts the assumption that C included the maximum number of edges possible. Thus there is no cycle that includes as many edges as possible without including them all, which is what we wanted to prove. A proof by construction is similar in many respects. (It is similar in addition to the Euler tour proof on an early homework assignment.) The construction starts with some cycle C that, say, contains a vertex w. For each edge (u,v) that s not on C, we find the path from w to u and the path from v to w; this cycle, added to C in the same way as described in the previous proof, creates a cycle C 2. Repeat this process until some C k includes all the edges in G. Another constructive proof gradually increases a path until it includes all the edges, then finds a way back to where it started. This approach picks a starting vertex, finds an edge (u,v) that s not yet traversed, and travels to u and then across the edge. Strong connectivity provides the path to u. From v, the algorithm finds another untraversed edge, and extends the path through that edge in the same way. This process continues until all edges have been traversed, at which point it finds a path back to the start vertex provided by the strong connectivity assumption. Few solutions were completely correct. One false claim was that one of the endpoints of an edge not on a cycle was unreachable from the cycle. Another was that since all vertices u and v are connected by paths from u to v and back, the collection of cycles that results covers all the edges of G. The graph below shows a counterexample in which the dotted edge is not in any of a particular set of cycles. 3

Problem 5, exam You were to find an efficient algorithm that, given a directed acyclic graph G = (V, E) and a vertex a in V, counts the number of paths from a to each other vertex. (The original problem said all other vertices, but this was corrected at the exam. Another clarification made at the exam was that G contains no multiple edges; that is, for any u and v, the number of edges connecting u to v is at most, and the number of edges connecting v to u is at most.) There were a number of solution approaches. The best one took advantage of the fact that G is a DAG and can therefore be topologically sorted. This leads to an algorithm that runs in time O(n+e): Set the path counts of all vertices to 0, and set a s path count to. Topologically sort the vertices. For each vertex u in topological order, do the following: For each neighbor v of u, add u s path count to v s path count. To prove this, one would note that a vertex u isn t reached until all the path counts of vertices between it and a have been updated; this is because of the topological ordering. Thus, when u is processed in the outer loop, the number of paths to each of its predecessors is known and tallied in u s own count. Not taking account of multiple ways to get to a vertex from a was the flaw in another common solution, namely applying CLR s DFS or BFS algorithm directly and merely increment a vertex s path count each time it is visited. The problem with this is demonstrated in the graph below. The first time vertex c is encountered, it s marked; a c b when c is next encountered on the second path out of a, there will be no way to go past it to add the second path into b s count. 4

A third approach attempted to solve the problem of missing paths by continuing the search through marked vertices. This search again, either depth first or breadth first traverses every path from a to every other vertex. Unfortunately, the tradeoff is speed; this algorithm run in time exponential in the number of vertices. An example on which it performs badly is shown below. If k is approximately equal to the... k vertices square root of n, the number of paths from one end of the graph to the other is ( n) 2 approximately n. One last approach arose from noting that the (u,v)-th entry in the kth power of the adjacency matrix of G is the number of paths of length k from u to v. If the first n powers of G s adjacency matrix are added, the resulting matrix contains the desired path counts. Matrix multiplication (at least the standard algorithm) is O(n 3 ), and there are n multiplications, yielding a runtime estimate of O(n 4 ). Problem 4, final exam (n 2)/k levels Part a The lightest edge might not be in a shortest path tree, for example: 4 5 root 2 3 Part b The shortest path might change. Suppose there are two shortest paths, one with k edges and one with k+ edges. After the increase, the path with k edges is now shorter than the path with k+ edges. 5

Problem 5, final exam Solution: Root the tree, then compare the set of vertices on levels 0, 2, 4, with the set of vertices on levels, 3, 5, At least one of these sets must contain at least half the vertices in the tree. Another approach uses a proof by induction. The base cases are a single vertex and a single edge. In the induction step, we consider two cases: either the tree has an odd number of vertices or it has an even number of vertices. If the tree has an even number of vertices, we remove a leaf and find an independent set of size ceiling((n-)/2) vertices in the tree that remains. Since ceiling((n-)/2) = ceiling(n/2) when n is even, this set qualifies. If the tree has an odd number of vertices, we need the following lemma: Any tree with at least three vertices has a leaf with no sibling or has a leaf with another leaf for a sibling. Proof is by induction. All trees of size 3 and 4 qualify. From a larger graph, remove a leaf and find the designated leaf in the result; either it qualifies as the designated leaf in the larger graph, or it is the parent of the leaf that was removed, which itself qualifies as the designated leaf, or it is a sibling of the parent of the leaf that was removed, in which case the removed leaf also qualifies as the designated leaf. Now the tree with an even number of vertices must contain a leaf x that either has 0 siblings or has a leaf for a sibling. In the first case, we remove x and its parent from the tree and find an independent set of size (n-2)/2, then we add x to that set. In the second case, we find the independent set of size ceiling(n-)/2 that results from removing x. If x s parent is not in the set, we add x; otherwise we replace x s parent by x and its sibling. A proof based on creating the desired set out of leaves goes as follows. Suppose there are k leaves in the tree: include all of them in the independent set. Then remove the leaves and their parents. (This may disconnect the tree and create a forest.) The number of leaves is at least as large as the number of parents, so the remaining trees collectively have n n-2k vertices. Any isolated vertices were formerly attached to the parents, not the leaves, so they may also be included in the independent set. Now find independent sets in all the individual trees; this produces a set of at least ceiling((n-2k)/2) vertices. Then put the k leaves back in. Note that the leaves alone may not provide the desired set. Consider, for example, the following tree: 6

Problem 6, final exam The algorithm involves two steps. First, verify that G' is a tree, either by counting its edges there must be exactly V or by doing a depth-first search to make sure G' contains no cycles. (These both work since G' is connected.) Then make sure every edge in G' is also in G. G' has to be in adjacency list format or edge list format to make the edge counting fast. G' has to be in adjacency list format to make cycle checking fast. G has to be in adjacency matrix format to make the edge checking fast. Counting the edges is O( E' ); verifying containment is O( E' ) since checking for an edge in G takes constant time; the depth-first search is O( E' ). (Note that E' = V, at least after the edge count is verified.) Problem 9 (parts a and b), final exam Part a Given S and F and k, is there a hitting set of size at most k? (You were allowed to say size equal to k.) Part b The certificate for showing that the minimum hitting set is in NP is the hitting set itself; we check the hitting set s size, then verify (in at worst O(n 3 ) time) that it shares at least one element with all the sets in F. Now pick a graph G in which to find vertex cover of size at most k. Consider the set S that contains all the vertices of G, and the set F = {F j such that F j = {u,v for each edge (u,v). (F can be easily computed.) There is a vertex cover of size at most k in G if and only if there is a hitting set of size at most k for F. Proof: => Assume G has a vertex cover of size at most k. For every edge (u,v), either u is in the cover or v is in the cover. Each F j contains the endpoints of an edge, so for each F j, whichever of the endpoints is in the cover also hits F j. <= Assume F has a hitting set H of size at most k. Since at least one item from each F j appears in H, the set {u,v is hit, so either u is in H or v is in H. But then either the edge {u,v is covered by including u or v (whichever is in H) in the vertex cover, thus producing a cover of the same size as the hitting set. 7