CSE 591 HW Sketch Sample Solutions 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. Problem 1 (a) Any arc that pointing to s, i.e., incoming arcs of s, cannot be needed in T s. Since s is the root of the tree and all arcs have positive length, its distance won t be updated in Dijkstra s algorithm. Hence such arcs will never be used. Now look at the subtree rooted at s in T s. The paths from s to all vertices in this subtree are necessarily shortest paths, so we can eliminate from consideration all arcs that enter any one of the vertices in this subtree, and finalize the distances to each of these vertices. Without knowing anything at all about the arc lengths, it appears that no other arcs can be excluded with certainty in advance (compare the cases when the arcs out of s not appearing in T s have very short length versus the case where they have very long length). (b) Let N s = {v (s, v) A}, N s = {u (s, u) A}. We claim if N s = N s and for all v N s and length(s, v) = length(s, v), then for every vertex other than s and s has the same distance from the root in both trees. Let s recall the procedure of Dijkstras algorithm, the distances of root s neighbors will be updated in the first iteration. Hence, if both s and s have the same neighbor and arcs with same length, the results of first iteration of Dijkstra s algorithm will also be the same. Continuing the procedure, all other vertices end up getting the same distance. (c) We find large classes of examples here, not a characterization. Whenever the shortest paths from s do not go through s and vice versa, the paths should be arc-disjoint. So pick any directed graph in which s can reach all other vertices; remove all arcs into s; compute the minimum arc length M, and the sum of all the arc lengths, B; add a new vertex s and an arc from s to every other vertex of length M/; and add an arc from s to s having length B + 1. Every shortest path from s does not go through s and vice versa, so we have found a very large class of examples. Millions of other answers are possible (and just as good). Problem (a) Let S be set of servers, C be set of clients. Add a new vertex s, and add arcs from s to each vertex in S; each arc has delay 0 (if permitted, otherwise use some small value ε, and add ε to δ). Algorithm 1 run Dijkstra s algorithm using s as source, to produce tree T s. 1
for (each c in C) do if (there is a path from s to c in T s, say P c ) then if P c > δ then output false else output false output true. Assuming there are m edges, the running time of above algorithm is O(m+( S + C ) log ( S + C )) when using min-heap for Dijkstra s algorithm. (b) Two possible solutions. Here is the first. We prove by contradiction. Suppose for one particular client c, our algorithm says there is no such path but the result is wrong. Then there exists a path from certain server s to c, say P s,c such that P s,c δ. However, in our algorithm, at certain iteration we must use s as the source node and compute shortest path tree. By validity of Dijkstra s algorithm (which should be explained for completeness), we must get a path with length no greater than P s,c, hence no greater than δ. This is a contradiction, therefore our algorithm works correctly and there is no such path indeed. Here is the second (sketch only). One could prove that there is a vertex c C for which there is no path from s to c with total delay at most δ as follows. If no path exists from s to c, I would show a single cut with s on one side, c on the other, so that no arc crosses the cut from s s side to c s. If a path P c does exist, say it is (s = x 0, x 1,..., x l 1, x l = c). Suppose that the delays calculated from s (in T s ) are d 0, d 1,..., d l. Consider the cuts C 0,..., C l 1, where C i splits the vertices into one side containing s that contains all vertices whose delay from s is less than d i and all vertices other than x i+1 whose delay is d i ; the other side contains all remaining vertices. Then I verify that the lowest delay edge in the arc cutset for C i is the edge from x i to x i+1. But every path from s to c must cross all of these cuts. Problem 3 (a) First, for each task T i, we create a vertex v i. If T i is precedence requirement of T j, we create an directed arc (v i, v j ). Let the resulting graph be G, with G = n. We claim that all tasks can be done if and only if there is no directed cycle in G, i.e., G is an directed acyclic graph(dag). Sufficiency: there is no directed cycle in G => all tasks can be completed. Proof. We prove by induction on G. Base case: G = 1. It is trivial case and the only task can be done. Induction step: G > 1. Firstly, we want to show there is a vertex, say u, which has in-degree 0, i.e., no arc points to u. Let P the longest directed path in G, we
claim the starting vertex of P is a candidate for u. Suppose not, there is an arc (w, u), if w is on P, we have a directed cycle, contradicting assumption. Otherwise, w is not on P and we get a longer path w > P, also a contradiction. Hence, there exists such vertex u. By the rule we construct G, u s corresponding task, say T u, has no precedence requirement and can be done at the very beginning. Let G = G u, noting that removing one vertex won t create any cycle, hence G is also a DAG. By induction hypothesis, all tasks remaining can be completed, together with T u, we are able to finish all tasks. Necessity: all tasks can be completed => there is no directed cycle in G. Proof. Suppose not, there is a directed cycle C and a schedule that all tasks can be done according to the schedule. Let u be the vertex whose corresponding task, T u, starts first among all vertices in C. Let s look at u s predecessor in C, say v and its corresponding task T v. By the choice of u, T v starts no earlier than T u, but since T v is T us precedence requirement, T u cannot start until T v is done, contradicting T u starts first. Therefore, no such u exists and hence no directed cycle exists. (b) It just asked for an efficient algorithm, not the most efficient. One could modify the idea in Dijkstra s algorithm, but this solution uses a more direct (if less efficient) approach. Algorithm ans = 0 for (each task T i ) do T i.start = 0 while (G is not empty) do Find a vertex u whose in-degree is 0 Say T u be its corresponding assignment. if (T u has no precedence requirement) then T u.start = 0, T u.end = T u.duration else for (each T v that T v is a precedence of T u ) do T u.start = max(t u.start, T v.end) T u.end = T u.start + T u.duration ans = max(ans, T u.end) remove u from G end while output ans The complexity of running time is O(n log n), the outer while loop takes O(n) time. We can use a min-heap to identify u each time, it takes O(log n) time. Then, it takes O(n) time to determine T us start time and remove it from G, hence the asymptotic running time is O(n log n). 3
Next we show the correctness of our algorithm. It suffices to claim every tasks starts at the earliest possible time. Proof. We prove on induction on n. Base step: n = 1. It is trivial case and true. Induction step: n > 1. Let the task which finishes last be T u. First, we show T u is not precedence requirement for any other task. Suppose not, another task, say T v should start after when T u is finished. In this case, T v will finish later than T u, contradicting choice of T u. Hence, it is safe to remove u from G and all other tasks can be complete since no other tasks depend on T u. By induction hypothesis, we make every task start as early as possible by running our algorithm on G u. Then we put u back, if u has no precedence, it can start at time 0. Otherwise, it should start after all its precedence requirements have been finished. And our algorithm checks for the latest time when its precedence completes and we have shown it is the earliest time when its precedence can be done. Hence, u also starts at the earliest possible time and our algorithm must get optimal solution. (c) We already get a schedule in (b) and can use that for (c). Problem 4 W.l.o.g., we assume the input graph is G = (V, E) Generalized Kruskal s algorithm F = Sort all edges by their lengths in nonincreasing order for (each edge (u, v) E) do if (length(u, v) > 0 and (u, v) does not create a cycle in F) then Add (u, v) to F Time complexity of generalized Kruskal s algorithm is O( E log V ) which is the same as normal Kruskal s algorithm. In order to show F is a maximum spanning forest, we need to show two things: (1) F contains no cycle and () the total weight of F is maximum. (1) is naturally true since when picking edges, we always avoid creating a cycle. For (), we can apply the similar argument for minimal spanning tree. Clearly, edges with negative cost should be abandoned. Let F be one maximum forest of G, if F and F are the same, then we are done. Otherwise, there is an edge e F that is not contained in F. Consider F + e, there must be a cycle C. Here we should observe that: (a). Every other edges in C has weight no less than e. It is true by the rule we construct F, edges with longer length are selected first. (b). There is some edge f in C that is not included in F. Let F = F + e f, we have a new forest and have more common edges with F than F. We can continue this process until we get F. Noting that, each time we replace f by e. we do not increase the weight, it is true by (a). Therefore, we have the following 4
relation w(f ) w(f )... w(f ). Since F is one maximum forest, then F must also be a maximum forest. Problem 5 W.l.o.g, we assume input graph is G = (V, E) and V = n. We also suppose that all edges whose weights are not positive (if any) have been removed, because they will never be selected. (a) We may assume the input format is edge list. First, we need four hashset to store A, B, C, D. in order to run Generalized Kruskal s algorithm, we need disjoint set to check whether it will create a cycle by adding some edges. Also, we need an extra list to store selected edges after running Generalized Kruskal s algorithm. (b) As mentioned in 4, the running time Kruskal s algorithm is O( E log V ) = O(m log n). For combining part, there are at most 6 ( n 1) = 3n 6 edges left, plugging in this value, the running time is O((3n 6) log n) which is O(n log n). Since we run the algorithm recursively on the 6 forests, we have T (n) = 6 T ( n)+ O(n log n), applying master theorem, we have T (n) = O(n log 6 ). (c) Yes, it generates a maximum weight spanning forest. We rely on the proof in Problem 4. To the contrary, consider the graph G with the fewest vertices for which there is an edge e so that generalized Kruskal s algorithm must select e for inclusion, but e is not chosen in any of the induced subgraphs. Without loss of generality, e is in the induced subgraph on A B but is not selected in its spanning forest. Then e must be a minimum weight edge on some cycle in the induced subgraph, and therefore is also a minimum weight edge on some cycle in G. As we saw in class (in the case when all weights are distinct), there must therefore be a maximum weight forest of G that does not include e, which contradicts our assumptions about G and e. Hence the method must generate a maximum weight spanning forest. 5