Docsity

Design and Analysis of Algorithm - Practice Midterm | CSE 101, Exams of Computer Science

Toggle sidebar
bg1
CSE 101
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 pro cedure proc(I),
which does not change I.
Algorithm(n: positive integer);
1. begin;
2. I←1;
3. While I≤ndo:
4. begin;{while}
5. FOR J= 1 TO bn/Icdo proc(I)
6. I←2∗I.
7. end;{while}
8. end;
Give a time analysis, up to Θ, for this algorithm.
Let Itbe the value of Iafter titertions of the While loop. Since proc(I) doesn’t change I,Idoubles
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/Ittimes, 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 Θ(nlog n).
Correctness proofs Say that a graph is d-dense if every node has at least dedges adjacent to it. The
following algorithm strategy determines whether graph Ghas a non-empty d-dense sub-graph G0. 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)
1. While there is a node xof degree < d do:
2. G←G− {x}.
3. If Gis empty return False, else return True.
We need to prove that, if Ghas a d-dense sub-graph G0, then the above algorithm returns True, and
conversely, that, if the algorithm returns True, then Ghas a ddense subgraph.
Assume that Ghas a d-dense subgraph S⊆G. We prove the algorithm by induction using the following
loop invariant: Let Gtbe the remaining graph after titerations of the while loop. Then S⊆Gt.
Since after 0 loops, G0=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 xbe 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∈Shave deg(y)≥d. Thus, x6∈ 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 titerations, S⊆Gt. Since S6=∅by the definition of d-dense,
Gt6=∅. 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 Gt6=∅. Since the
loop has halted, every node xin Gthas deg(x)≥d. Thus, Gtis a d-dense subgraph of G, which
proves the converse direction.
1
pf3

Partial preview of the text

Download Design and Analysis of Algorithm - Practice Midterm | CSE 101 and more Exams Computer Science in PDF only on Docsity!

CSE 101

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);

  1. begin;
  2. I ← 1;
  3. While I ≤ n do:
  4. begin;{while}
  5. FOR J = 1 TO bn/Ic do proc(I)
  6. I ← 2 ∗ I.
  7. end;{while}
  8. end;

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)

  1. While there is a node x of degree < d do:
  2. G ← G − {x}.
  3. If G is empty return False, else return True.

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:

  1. Initialize an empty set of intervals. Disjoint
  2. Initialize S as the smallest start time of an interval, and F as the finish time of this interval.
  3. While there is an interval Ij that ends after F (F < fj ) do:
  4. IF there is an interval Ij so that sj ≤ F < fj THEN F ← fj.
  5. ELSE do:
  6. Add [S, F ] to Disjoint.
  7. Find the interval Ij with the smallest sj so that F < sj
  8. S ← sj , F ← fj.
  9. Add [S,F] to Disjoint.
  10. Return Disjoint

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:

  • Sort I 1 , ..In by non-decreasing si, using Heapsort.
  • S ← s 1 ; F ← f 1.
  • Disjoint is initially an empty list of intervals.
  • FOR J = 2 to n do:
  • IF sj ≤ F THEN F ← max(F, fj ).
  • ELSE do:
  • Append (S, F ) to Disjoint;
  • S ← sJ ;
  • F ← fJ ;
  • Append (S, F ) to Disjoint.
  • Return Disjoint.

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]]

  1. IF n = 0 return 0.
  2. IF n = 1 return max(0, A[1]).
  3. S 1 ← M axConsSum[A[1..n/2]]
  4. S 2 ← M axConsSum[A[n/2 + 1..n]]
  5. SumLef tF romCenter ← 0