Fin f11
Fin f11
Fall 2011
Final
This test has 14 questions worth a total of 100 points. You have 180 minutes. The exam is closed
book, except that you are allowed to use a one page cheatsheet (8.5-by-11, both sides, in your own
handwriting). No calculators or other electronic devices are permitted. Give your answers and
show your work in the space provided. Write out and sign the Honor Code pledge before
turning in the test.
I pledge my honor that I have not violated the Honor Code during this examination.
Problem Score
0
1
2
3
4
5
6
Sub 1
Problem Score
7
8
9
10
11
12
13
Sub 2
Name:
Login ID:
Precept:
P01
P01A
P02
P02A
P03
P03A
Total
11
11
12:30
12:30
1:30
1:30
Maia Ginsburg
Aman Dhesi
Sasha Koruga
Joey Dodds
Maia Ginsburg
Joey Dodds
PRINCETON UNIVERSITY
0. Miscellaneous. (1 point)
Write your name and Princeton NetID in the space provided on the front of the exam, and
circle your precept number.
memory
1,000
10,000 bytes
8,000
320,000 bytes
64,000
10,240,000 bytes
512,000
327,680,000 bytes
Estimate the memory usage of the program (in bytes) as a function of N and use tilde
notation to simplify your answer.
Hint: recall that logb a = lg a/ lg b.
(b) For each function on the left, give the best matching order of growth of the running time
on the right.
A. log N
D. N 2
B. N
C. N log N
E. 2N
F. N !
PRINCETON UNIVERSITY
Consider the following acyclic digraph. Assume the adjacency lists are in sorted order: for
example, when iterating through the edges pointing from 0, consider the edge 0 1 before
0 6 or 0 7.
(a) Compute the topological order by running the DFS-based algorithm and listing the
vertices in reverse postorder.
2
___
___
___
___
___
___
___
___
___
(b) Run breadth-first search on the digraph, starting from vertex 2. List the vertices in the
order in which they are dequeued from the FIFO queue.
2
___
___
___
___
___
___
___
___
___
Consider the following edge-weighted graph with 9 vertices and 19 edges. Note that the edge
weights are distinct integers between 1 and 19.
1
15
18
14
16
17
12
10
19
11
13
(a) Complete the sequence of edges in the MST in the order that Kruskals algorithm includes
them (by specifying their edge weights).
1
____
____
____
____
____
____
____
____
(b) Complete the sequence of edges in the MST in the order that Prims algorithm includes
them (by specifying their edge weights).
1
____
____
____
____
____
____
____
____
PRINCETON UNIVERSITY
edge
weight
edge
weight
02
6.0
51
12.0
04
6.0
52
1.0
05
17.0
54
3.0
13
17.0
57
25
11.0
27
distTo[]
edgeTo[]
1.0
30
10.0
17.0
51
58
4.0
6.0
52
6.0
60
12.0
0.0
null
30
1.0
61
5.0
3.0
62
7.0
04
3 10
1.0
31
25.0
64
9.0
5.0
10 5
36
13.0
69
4.0
13.0
36
38
9.0
71
7.0
12.0
27
45
3.0
75
11.0
9.0
38
46
4.0
79
6.0
47
3.0
10 1
15.0
null
48
1.0
10 5
2.0
10
3.0
3 10
49
15.0
10 8
7.0
(a) Give the order in which the first 5 vertices were deleted from the priority queue and
relaxed.
(b) Modify the table (above right) to show the values of the edgeTo[] and distTo[] arrays
immediately after the next vertex has been deleted from the priority queue and relaxed.
Circle those values that changed.
count[]
(first)
..
.
count[]
(second)
..
.
count[]
(third)
..
.
a[i]
(fourth)
a[i]
badge
..
.
freed
'a'
blurb
'b'
embed
'c'
basic
'd'
field
'e'
bluff
'f'
not required
dwarf
not required
fudge
'g'
..
.
not required
climb
not required
10
cycle
10
not required
11
bleed
11
not required
12
budge
12
not required
13
crumb
13
not required
14
cubic
14
not required
15
cable
15
not required
16
blend
16
not required
17
cliff
17
not required
18
bread
18
19
cache
19
..
.
..
.
..
.
i
0
PRINCETON UNIVERSITY
A
B
s
10
10
11
4
A
(i)
0
10
(ii)
0
10
(iii)
0
(iv)
9
(v)
10
10
PRINCETON UNIVERSITY
Circle which one or more of the following strings are keys in the TST.
B
BD
CD
FD
JLO
JP
JPEG
JPEGS
JPG
PEG
PEGS
11
A. Unordered array.
B. Ordered array.
Find the string key in the data structure that is the longest prefix of a given
string.
How many string keys in the data structure starts with a given prefix?
12
PRINCETON UNIVERSITY
Identify each statement with the best matching description on the right.
13
(b) Decode each of the following LZW-encoded messages or explain briefly why it is not a
valid LZW-encoded message. (Recall that codeword 80 is reserved to signify end of file.)
encoded message
41 42 43 44 80
A B C D
42 41 4E 82 41 80
42 41 83 80
41 42 81 82 80
41 42 81 83 80
42 41 4E 44 41 4E 41 80
5.5
815
Data Compression
For reference, below is the hexademical-to-ASCII conversion table from the textbook:
E F
SO SI
RS US
. /
> ?
N O
^ _
n o
~
DEL
14
PRINCETON UNIVERSITY
flow
11
3/6
22 / 22
10
10
4/4
capacity
10 / 18
/7
17 / 17
/6
14 / 16
0 / 15
4/5
10
10
13 / 16
16
16
(b) Perform one iteration of the Ford-Fulkerson algorithm, starting from the flow f . Give
the sequence of vertices on the augmenting path.
15
(b) What is the order of growth of the running time of your algorithm in the worst case
as a function of the number of vertices V and the number of edges E? Circle the best
answer.
1
E+V
E log V
E log E
EV
2V
(a) Show that 3Sum linear-time reduces to 3SumVariant. To demonstrate your reduction,
give the 3SumVariant instance that would be constructed to solve the following 3Sum
instance:
a[]
-66
-30
70
99
-33
66
20
50
b[]
c[]
(b) Show that 3SumVariant linear-time reduces to 3Sum. To demonstrate your reduction,
give the 3Sum instance that would be constructed to solve the following 3SumVariant
instance:
b[]
299
700
10
14
-3
-1
c[]
999
19
-4
600
30
20
20
Hint: define M equal to 1 + maximum absolute value of any integer in b[] or c[].
a[]
16