



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
The u.c. Berkeley cs170 midterm 2 algorithms exam held on april 1, 2004. The exam covers various topics related to algorithms, including dijkstra's algorithm, prim's algorithm, kruskal's algorithm, minimum spanning trees, shortest paths, longest paths, and dynamic programming. The exam consists of multiple-choice questions and is designed to test the students' understanding of these concepts.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




U.C. Berkeley – CS170: Algorithms Midterm 2 Lecturers: Umesh Vazirani & Christos Papadimitriou April 1, 2004
Answer all questions. Read them carefully first. Be precise and concise. The number of points indicate the amount of time (in minutes) each problem is worth spending. Write in the space provided, and use the back of the page for scratch. Good Luck!
total
(20 points)
In the graph shown above: (a) In what order are the vertices deleted from the priority queue in Dijkstra’s algorithm for the shortest path? (Start node: A.)
(b) In Prim’s algorithm for the minimum spanning tree? (Start node: A.)
(c) In what order are the edges added in Kruskal’s algorithm?
(d) show the union-find trees at the end of Kruskal’s algorithm (in case of a tie in rank, the lexicographically first node becomes root).
(15 points) Given an array of integers a 1 ,a 2 ,…,an, positive and negative, such as -1, 3, 2, -7, 4, 2, -2, 3, -1, you want to find the largest sum of contiguous integers; in this case it would be 4 + 2 – 2 + 3 = 7.
We can accomplish this by (you guessed it!) dynamic programming. For each i = 0,…,n define maxsum[i] to be the value of the largest sum seen so far. We also need maxsuff[i] to be the largest sum of a suffix ending at ai. (In the array above, maxsum[4] = 5, maxsuff[4] = 0).
Fill in the blanks in the dynamic programming algorithm:
(a) initialize: maxsum[0] = maxsuff[0] =
(b) iteration: for i = 0, …, n-1, maxsum[i+1] =
maxsuff[i+1] =
(c) What other data structure do you need in order to recover in the end the beginning and end of the maximum contiguous sum?
(d) What is the running time of the algorithm?