COL 70 : Assignment 1 solutions 1(a ( n T (n = T + bn log n ( n = T + b n ( n log + bn log n ( n = T + b n ( n 8 log + b n log ( n + bn log n = bn log n + b n log n + bn log n +... log n terms = nb [(log n + 1 log n + 1 ( log 1 log n +... + log 1 bn [log n 1] = O(n log n + log ] +... 1(b T (n = at (n 1 + bn c = a [at (n + b(n 1 c ] + bn c = a T (n + ab(n 1 c + bn c = a [at (n 3 + b(n c ] + ab(n 1 c + bn c n = a i b(n i c i=0 = bn c n i=0 a i Thus, if 0 a < 1, T (n = O(n c. if a = 1, T (n = O(n c+1. if a > 1, T (n = O(a n. 7 Assume that there exists a linear time algorithm Select(S, r that partitions the array S into two parts around the rth smallest element and returns this element with rank r. Input: Set S = {x 1,..., x n }, k= number of partitions (1 < k < n Output: A set P = {y 0, y 1,..., y k } (y i S and y i < y i+1 that partitions S into k partitions S i such that S i = {x j y i 1 < x j y i }, (1 i k, S i = n/k or n/k + 1 1
Algorithm Divide(S, x /* x is a power of */ m Select(S, S /; S 1 {y y S, y m}; S {y y S, y > m}; if x = then /* Concatenate partitions S 1 and S */ return S 1. S ; else S L Divide(S 1, x/; S R Divide(S, x/; return S L. S R ; Algorithm 1: Divide k l such that l 1 < k l ; S Divide(S, k ; /* if k = k then return points of each S i */ P ; size 0; r = n%k; splitcount n k + 1; for i in 1... k do if size + S i splitcount then P P Select(S i, splitcount size; if r < 0 then splitcount splitcount + n k ; else splitcount splitcount + n k + 1; r = r 1; size size + S i ; return P ; Running Time: Select takes time linear in the size of its input. Let T (n, x be the time taken by the Divide algorithm on an input of n elements and x partitions. Then and T (n, x = T (n 1, x/ + T (n n 1, x/ + cn T (n, = cn Solving this recurrence using the Recurrence Tree, we have O(log k levels where each level has an overall cost of O(n. Therefore, the total time for the Divide algorithm is O(n log k = O(n log k. In the for loop, we have at most k calls to Select with an overall cost of O(n. Therefore, the total cost is O(n log k. Proof of correctness: Since k > k n/k < n/k. Therefore, each of the k points returned must be in exactly one of the k partitions obtained from the Divide algorithm. The rest of the proof follows from the correctness of the Select algorithm. We also ensure that there are r partitions with size n/k + 1 and k r partitions of size n/k where r = n%k. 11 To prove that the subgraph returned by the Djikstra or (Bellman Ford algorithm is a directed tree, rooted at source with n 1 edges. Directed : Since both Djikstra and Bellman Ford return single source shortest paths, the resulting graph must be directed, with all directed paths starting from the source. Tree rooted at source s
1. indegree(s = 0 : The weight of the shortest path to source s is initialized to 0 in both Djikstra and Bellman Ford algorithms. In a graph with non-negative edges, there is no path with weight less than 0. Therefore, in both algorithms, no relaxation can reduce the label of s, and indegree(s = 0 in the resulting graph.. v V \{s} such that v is reachable from s, indegree(v = 1 : Say there is a node u such that indegree(u =. This implies there are two paths from s to u, say p 1 and p. Let the predecessors of u in p 1 and p be y 1 and y, respectively. Since both p 1 and p are shortest paths, it must be that weight(p 1 = weight(p = w. Say that p 1 is discovered first, then the label of u is δ(u = w. When the edge (y, u is subsequently relaxed, the new label is w, which is not less than δ(u, hence the edge (y, u cannot be a part of the resulting graph, resulting in a contradiction. 3. Output graph has no cycles: Let there be cycle in the graph, and let u be a node in this cycle. We have two cases, (i s is not a part of this cycle, and (ii s is a part of the cycle. In case (i, indegree(u =, and in case (ii indegree(s = 1, both resulting in contradictions. Therefore, there are no cycles in the output graph. n 1 edges : Since the output graph is a directed graph with n nodes where the indegree of all nodes but one is equal to 1, the total number of edges is n 1. 1 Input: A directed acyclic graph, G(V, E, source s. Output : Single source shortest paths from s. Algorithm Topological Sort of the vertices in G. Initialization v V \{s}, δ(v = δ(s = 0 For u V in topological order, relax edges outgoing from u. v V, (u, v E, δ(v = min(δ(v, δ(u + w(u, v Proof of Correctness Claim: When the edges outgoing from u are relaxed, the shortest path to u has already been discovered. We prove the above using induction. Base case: For all nodes u with topological order less than that of s, there is no path from u to s, and for s, weight of the shortest path is equal to 0. Inductive step: Let the topological order of u be k. Assume that the shortest paths to all nodes having topological order less than k has been discovered. Let v be the predecessor of u in the shortest path from s to u. Topological-order(v < Topological-order(u = k. Therefore, shortest path from s to v has been discovered before relaxing edge (v, u and when edge (v, u is relaxed, the shortest path to u is discovered. Analysis. Topological sort involves DFS, which is O( V + E. Initialization of δ(u u V is O( V. Relaxing each edge at most once requires O( E. Therefore, total running time is O( V + E. 13 (i A is the n n adjacency matrix of a graph. Suppose that B i,j is more than the path weight of the shortest path of at most edges between vertex i and vertex j. Definition of B i.j is as follows B i,j = min 1 k n {a i,k + a k,j } The shortest path from i to j must go via some vertex k such that 1 k n. So we have a contradiction since the definition of B i,j covers all such intermediate vertices. Therefore B i,j is the path weight of the shortest path of at most edges between vertex i and vertex j. (ii We will prove by induction. Base Case: For l = 1, A is the adjacency matrix itself and thus gives the shortest paths of length at most 1 edge. 3
Induction Hypothesis: Suppose B = A A...l times stores the shortest path distance with at most l edges between i and j, be true for all l < k. We have to prove that the hypothesis is true for l = k. B = (A A...l 1times A The first part is the matrix that represents the shortest path distances between vertices with at most l 1 edges. By definition of of the operation B is the matrix representing the shortest path distances between vertices with at most l edges.
(iii A fast algotithm to compute A l Input: The matrix A and l Output: A l Algorithm: temp = A P =n n empty matrix while l > 0 : if l mod == 1 : P = P temp temp =temp temp l = l/ return P Running Time: O(n 3 logl since each operation takes O(n 3 time and there are O(logl such operations. 5