

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!


Practice Midterm: Spring, 2004
Answer all questions. Give informal (at least) proofs for all answers. Grading will be on completeness and logical correctness, as well as correctness. For the data structure question, the efficiency of your solution will also be taken into account.
Analyzing loops-10pts Consider the following iterative algorithm, that uses an Θ(I) time procedure proc(I), which does not change I. Algorithm(n: positive integer);
Give a time analysis, up to Θ, for this algorithm. Let It be the value of I after t itertions of the While loop. Since proc(I) doesn’t change I, I doubles every loop, so It = 2t. So the while loop terminates when It = 2t^ > n, which is when t > log n. Inside the t’th iteration of the while loop, the FOR loop repeats n/It − 1 ≤ bn/Itc ≤ n/It times, each time taking Θ(It) time for a total of between Θ(n − It) and Θ(n) time each iteration. For the first log n − 1 iterations, It ≤ n/2, and both of these are Θ(n) Therefore, the total time is Θ(n log n).
Correctness proofs Say that a graph is d-dense if every node has at least d edges adjacent to it. The following algorithm strategy determines whether graph G has a non-empty d-dense sub-graph G′. After the algorithm, a correctness proof with some gaps is given. Fill in the gaps to form a complete proof. (The gaps are numbered with Roman numerals. On your exam answers, write the phrases that should be in the gaps, in that order.) Blanks are filled in in boldface DenseSubgraph(G: undirected graph, d: positive integer)
We need to prove that, if G has a d-dense sub-graph G′, then the above algorithm returns True, and conversely, that, if the algorithm returns True, then G has a d dense subgraph. Assume that G has a d-dense subgraph S ⊆ G. We prove the algorithm by induction using the following loop invariant: Let Gt be the remaining graph after t iterations of the while loop. Then S ⊆ Gt. Since after 0 loops, G 0 = G, the base case t = 0 is clear. For the induction step, assume the hypothesis holds for t, i.e., that S ⊆ Gt. We will show it holds for t + 1, i.e., that S ⊆ Gt+1. Let x be the node deleted in the t + 1’st iteration. By the definition of the algorithm deg(x)< d. By the definition of d-dense, all nodes y ∈ S have deg(y)≥ d. Thus, x 6 ∈ S, so S ⊆ Gt − {x} = Gt+ 1. Thus, we have proved our goal. Then, by induction, the invariant holds after any number of iterations. In particular, if the loop halts after t iterations, S ⊆ Gt. Since S 6 = ∅ by the definition of d-dense, Gt 6 = ∅. Thus, at this point, the algorithm returns True as we wanted to prove. Conversely, assume the algorithm returns True Then when the loop halts, we have Gt 6 = ∅. Since the loop has halted, every node x in Gt has deg(x)≥ d. Thus, Gt is a d-dense subgraph of G, which proves the converse direction.
Data structures and efficient versions of algorithms 10 pts: Union of intervals: We are given a list of (not necessarily disjoint) intervals of the real line, I 1 = [s 1 , f 1 ]..In = [sn, fn], with each si < fi. We want to express the union of these intervals as a set of disjoint intervals. A strategy to do this is:
Give an efficient implementation of this strategy, specifying pre-processing and data structures used. Give a time analysis for your version. As a preprocessing step, we can sort the set of intervals by si. This takes O(n log n) time using heapsort or mergesort. The smallest start time of an interval is the first start time in the sorted set, and we initialize F to the first finish time. Since F only increases, we only need to move right in the sorted array to keep finding intervals with sj ≤ F or to find the interval Ij with the smallest sj so that F < sj. This leads to the following algorithm:
The first step takes O(n log n) time. The inside of the FOR loop is constant time, so the rest of the algorithm is O(n). Thus, the first step dominates completely and tyranically, so the total time is O(n log n).
Divide-and-Conquer Recurrence: 10 points Consider the following recursive algorithm. Its input is an array of (not necessarily positive) integers. A[1..n] The goal is to find the maximum possible sum of a consecutive sub-sequence A[i..j] of elements of the array , max 1 ≤i≤j≤n
∑k=j k=i A[k]. MaxConsSum[A[1..n]]