Midterm Exam #2 - Algorithm Design | CS 1510, Exams of Computer Science

Material Type: Exam; Professor: Pruhs; Class: ALGORITHM DESIGN; Subject: Computer Science; University: University of Pittsburgh; Term: Fall 2003;

Typology: Exams

Pre 2010

Uploaded on 09/02/2009

koofers-user-x5s
koofers-user-x5s 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1510 Midterm 2
Fall 2003
1. (20 points) We consider the problem of computing the longest common subsequence of two
sequences A=a1,...,amand B=b1,...,bn. Let T(i, j) be the length of the longest common
subsequence of a1,...,aiand b1,...,bj.
(a) Write a recursive function to compute T(i, j ) in the naive way. Don’t forget the base
case(s).
(b) Show that if you implement this recursion directly in say the C programming language,
that the program could use time that is exponential in n.
(c) Write iterative array based code to compute T(m, n) that runs in O(n2) time.
(d) Write code to actually find the longest common subsequence from your array.
2. (20 points) Consider the problem where the input is a collection of ntrain trips within
Germany. For the ith trip Tiyou are given the date diof that trip, and the non-discounted
fare fifor that trip. For simplicity we will assume that dates are nonnegative integers. The
German railway system sells a class A Bahncard for YEuros that entitles you to a 50% fare
reduction on all train travel within Germany within Adays of purchase. The German railway
system also sells a class B Bahncard for MEuros that entitles you to a 60% fare reduction
on all train travel within Germany within Bdays of purchase. You can apply at most one
Bahncard discount to a particular trip. The problem is to determine the least you can spend
on your travel.
In this paragraph we give an example. Assume that Y= 500, A= 365, and M= 60 and
B= 30. Further assume, d1= 1, f1= 20 Euros, d2= 40, f2= 200 Euros, d3= 80, f3= 200
Euros, d4= 120, f4= 100 Euros, d5= 200, f5= 200 Euros, and d6= 400, and f6= 600
Euros. Then you might buy a class B Bahncard on day 40, and a class A Bahncard on day
200. This results in a total cost of
20 + 60 + (.4)(200) + 200 + 100 + 500 + (.5)(200 ) + (.5)(600)
Euros.
Give an O(n3) time algorithm for problem. Significant partial credit will be given for any
polynomial time algorithm.
pf2

Partial preview of the text

Download Midterm Exam #2 - Algorithm Design | CS 1510 and more Exams Computer Science in PDF only on Docsity!

CS 1510 Midterm 2 Fall 2003

  1. (20 points) We consider the problem of computing the longest common subsequence of two sequences A = a 1 ,... , am and B = b 1 ,... , bn. Let T (i, j) be the length of the longest common subsequence of a 1 ,... , ai and b 1 ,... , bj.

(a) Write a recursive function to compute T (i, j) in the naive way. Don’t forget the base case(s). (b) Show that if you implement this recursion directly in say the C programming language, that the program could use time that is exponential in n. (c) Write iterative array based code to compute T (m, n) that runs in O(n^2 ) time. (d) Write code to actually find the longest common subsequence from your array.

  1. (20 points) Consider the problem where the input is a collection of n train trips within Germany. For the ith trip Ti you are given the date di of that trip, and the non-discounted fare fi for that trip. For simplicity we will assume that dates are nonnegative integers. The German railway system sells a class A Bahncard for Y Euros that entitles you to a 50% fare reduction on all train travel within Germany within A days of purchase. The German railway system also sells a class B Bahncard for M Euros that entitles you to a 60% fare reduction on all train travel within Germany within B days of purchase. You can apply at most one Bahncard discount to a particular trip. The problem is to determine the least you can spend on your travel. In this paragraph we give an example. Assume that Y = 500, A = 365, and M = 60 and B = 30. Further assume, d 1 = 1, f 1 = 20 Euros, d 2 = 40, f 2 = 200 Euros, d 3 = 80, f 3 = 200 Euros, d 4 = 120, f 4 = 100 Euros, d 5 = 200, f 5 = 200 Euros, and d 6 = 400, and f 6 = 600 Euros. Then you might buy a class B Bahncard on day 40, and a class A Bahncard on day
    1. This results in a total cost of

20 + 60 + (.4)(200) + 200 + 100 + 500 + (.5)(200) + (.5)(600)

Euros. Give an O(n^3 ) time algorithm for problem. Significant partial credit will be given for any polynomial time algorithm.

  1. (20 points) This is an actual problem that arises in video processing, called temporally consistent assignment. You are given two videos, each represented as a sequence of im- ages. Both videos were taken off of the same scene at roughly the same time period. Let X = x 1 , x 2 ,... , xm be one sequence of images and Y = y 1 , y 2 ,... yn be the other sequence of images. The cameras are not synchronized and may run at significantly different speeds. The objective is to assign each image of the X video sequence with its most similar image in the Y video sequence, subject to the constraint that the assignments are consistent in time.In order to determine how similar two images are, as part of the input, you may assume that you are given a table D[i, j] which contains a numeric dissimilarity value between these two images xi and yj. The lower D[i, j] is, the more similar the images are. Do not worry how D[i, j] is computed. An assignment of sequence X to sequence Y is a sequence of m indices A = j 1 , j 2 ,... , jm meaning that, for 1 ≤ i ≤ m, image xi is assigned to image yji. The cost of an assignment is a sum of the dissimilarities between the assigned images. That is, cost(A) =

∑m i=1 D[i, ji]. An assignmentA^ is temporally consistent if^ ji^ ≤^ ji+1, for 1^ ≤^ i < m. In other words, if xi is assigned to some image yji then the next image in the sequence xi+ must be assigned to an image appearing no earlier that yji in the Y image sequence. (This makes sense, as time runs forward for both cameras.) We allow two images of X to be as- signed to the same image of Y. The problem is: given the video sequences X, Y and the cost table D[i, j], compute the minimum cost temporally consistent assignment of X to Y. Consider the following table for m = 3 and n = 6:

D[i, j] j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 1 3 7 4 9 1 2 i = 2 5 4 2 5 8 6 i = 3 6 1 5 8 2 7

If x 1 is assigned to y 1 (that is j 1 = 1) and x 2 is assigned to y 3 (that is j 2 = 3), and x 3 is assigned to y 6 (that is j 3 = 6) then the cost of this assignment is D[1, 1]+D[2, 3]+D[3, 6] = 3+2+2 = 7. Give an O(n^3 ) time algorithm to compute the lowest achievable cost. Significant partial credit will be given for any algorithm with polynomial running time.