



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Fall 2000
Problem 2, exam 1
Here’s Prim’s algorithm, modified slightly to use C syntax.
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(1). 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 effi- cient 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(1) for a
total of O(n^2 + e) = O(n^2 ) in each algorithm. The membership test in Prim’s algo- rithm, 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
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); } } }
Fall 2000
the worst case and an example of a corresponding assignment of edge weights are shown below.
Problem 3, exam 1
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 logb 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 1
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:
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 fol- lowing it around to v; a path from v to u is found in the same way. These two paths,
vertex action v 1 update v 2 , v 3 , and v 4 v 2 update v3 and v 4 each for the second time v 3 update v 4 for the third time
v
v 3 v
v (^4)
T n ( ) 9 T n 3 ----^ n
2 = + + n lg n
Fall 2000
Problem 5, exam 1
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 1, and the number of edges connecting v to u is at most 1.)
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 1. 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 order- ing. 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 dem- onstrated in the graph below. The first time vertex c is encountered, it’s marked;
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.
a c b
Fall 2000
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 exam- ple on which it performs badly is shown below. If k is approximately equal to the
square root of n, the number of paths from one end of the graph to the other is
approximately.
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–1 multiplications, yielding a runtime estimate of O(n^4 ).
Problem 4, final exam
Part a
The lightest edge might not be in a shortest path tree, for example:
Part b
The shortest path might change. Suppose there are two shortest paths, one with k edges and one with k+1 edges. After the increase, the path with k edges is now shorter than the path with k+1 edges.
(n–2)/k levels
n ( n ) ⁄ 2
4 5 1
2 3
root
Fall 2000
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|–1—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 = {Fj} such that Fj = {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 Fj contains the endpoints of an edge, so for each Fj,
whichever of the endpoints is in the cover also hits Fj.
<= Assume F has a hitting set H of size at most k. Since at least one item from each Fj
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.