

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Aub finals for programming algorithms and data structures dated 2013-2014, with solutions
Typology: Exams
Uploaded on 05/14/2023
10 documents
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Summer 2013 – 14 Final Examination Monday July 14, Bliss 105, 8:00 – 10:00 a.m.
Instructions. This quiz is scored out of 100. This quiz is open book and open notes: you can use the textbook, notes you have taken in class, and your homework solutions. Show your work, as partial credit will be given. You may use any algorithm that was covered in class. Just give the name of the algorithm and the chapter or page number in the book where the algorithm is given. If taken from your lecture notes, just say “lecture notes.” For questions that require you to write an algorithm, you must prove that your algorithm has the required running time.
Please draw a horizontal line to separate each answer from the next. Best of luck!
Problem 1 (20 points).
Give the running time of the following, expressed only in terms of n. You may express this as Θ(f (n)), for suitable f (n).
{n > 0 } i := 1; while i ≤ n^3 j := 1; while j ≤ i j := j + 1; endwhile i := i + 1; endwhile
Sample solution. Running time given by the following summation, by putting the limits of the loop as the limits of the sum: ∑n^3
i=
∑^ i
j=
the summand is 1 since inner loop body has constant running time. We could also have used a constant c. The cost of i := i + 1 is ignored since its lower order.
The above simplifies to ∑n^3
i=
i
which is Θ(n^6 ).
Problem 2 (20 points).
Part a (10 points). Given an LLRB, suppose that we swap the colors of all the edges. Is the result, in general, a valid LLRB? Explain.
Sample solution. No, can have red-red violations
Part b (10 points). Is it possible to have a LLRB with 1, 000 , 000 nodes and no red edges? Explain.
Sample solution. An LLRB with no red links must be compete, due to the black balance condition, and so it must have 2n^ − 1 nodes for some n. 1, 000 , 000 is not of this form, so an LLRB with 1, 000 , 000 nodes must have some red links.
Problem 3 (20 points).
Part a (10 points). Give an algorithm to count the number of nodes in each connected component of an undirected graph. Must run in time O(V + E).
Sample solution. Run the conected components algortihm in the slides. Create a counts array, which is indexed by the component number. Traverse the cc array, and increment counts[i] for each node v with cc[v] = i.
Part b (10 points). Give an algorithm to determine if a connected undirected graph G contains an articulation point. An articulation point is a node whose removal would break G into two disconnected components.
Must run in time O(V (V + E)).
Sample solution. For each node v, do the following. Delete v and run the connected compo- nents algorithm and count the number of components. If count = 2 then v is an articulation point.
Problem 4 (20 points).
Let G be a weighted directed graph and suppose that G contains an edge from u to v swth weight 0. Suppose now that we merge the nodes u and v. Will the shortest path lengths in G remain the same, in general, or will they change. Explain.
Sample solution. No, since this can create new paths in G.