Fin f12 Sol
Fin f12 Sol
1. Analysis of algorithms.
(b) ∼ 4M R
2. Graphs.
(a) The method marked[v] returns true if and only if there is a directed path from s to v.
3. Graph search.
(b) preorder: 0 1 6 2 7 8 3 9 4 5
(a) 10 20 30 40 50 100
(b) x ≤ 110.
(c) y ≤ 60.
(d) z ≤ 80.
5. Shortest paths.
(a) 0 1 5 4
(b) x = 8.0
1
(d) vertex 2
(e)
v distTo[] edgeTo[]
3 20.0 2→3
6 35.0 2→6
6. Maximum flow.
(a) 25
(b) A → G → B → C → H → I → J
(c) 25 + 3 = 28
(d) {A, B, C, F, G}
(e) 28
(a) A (7), CAA (5), CGA (4), CGCA (11), TA (8), TGT (12), TT (9)
Final, Fall 2012
(b)
C
7 A A T 3
G G C
5 A A 13 C 8 A A T 9
4 A A 11 T 17 T G
T 0 T 12
99 A
2
9. Knuth-Morris-Pratt substring search.
ABCABABABCAA
0 1 2 3 4 5 6 7 8 9 10 11
A 1 1 1 4 1 6 1 8 1 1 11 12
B 0 2 0 0 5 0 7 0 9 0 0 5
C 0 0 3 0 0 3 0 3 0 10 0 0
s A B C A Final,
B A Fall
B 2012
A B C A A
M E N D E R O F R O A D S W I T H T H E A I D O F T H E
I D O F T H E
I D O F T H E
I D O F T H E
I D O F T H E
I D O F T H E
I D O F T H E
Final, Fall 2012
(
The B 8 → 9,| 9 → 10,
edges ( and 10C → 11 are
D the remaining
* A -transitions
) *
edges. ) is a
Here
drawing of the NFA.
0 1 2 3 4 5 6 7 8 9 10 11
( B | ( C D * A ) * )
3
12. Huffman codes.
F 1 01110
H 3 0110 0 1 0 1
I ? 00 I M S
L 5 010 0 1
M 15 10 L
S 15 11 0 1
H
0 1
F B
4
13. Data compression.
KXMRDS
8
255 Run-length coding with 8-bit counts for best case inputs of N bits. A. ∼ 1/4096
The best case is an alternating sequence of 255 0s and 255 1s. Each B. ∼ 1/3840
sequence of 255 0s (or 255 1s) is encoded with 8 bits (11111111).
C. ∼ 1/2731
8
Run-length coding with 8-bit counts for worst-case inputs of N bits. D. ∼ 1/2560
1
The worst case is an alternating sequence of 0s and 1s. Each bit is E. ∼ 1/320
encoded with 8 bits (00000001).
F. ∼ 1/256
1
G. ∼ 1/255
8 Huffman coding for best-case inputs of N characters.
The best case is when one character occurs 100% of the time (or all H. ∼ 1/128
but a constant number of times), in which case it is encoded using I. ∼ 1/127
1 bit.
J. ∼ 1/32
8
The worst case is when each of the 256 characters occurs with equal K. ∼ 8/255
8
frequency. In this case, each character is encoded using 8 bits. L. ∼ 1/16
M. ∼ 1/8
12
8×3840 LZW coding for best-case inputs of N characters using 12-bit code-
words. Recall: no new codewords are added to the table if the table N. ∼ 1/7
already has 212 = 4096 entries. O. ∼ 1/4
The best case is one 8-bit character, say A, repeated N times. The
table contains 12-bit codewords for A, AA, AAA, and so on, all the P. ∼ 1/2
way up to 212 − 256 = 3840 As when the table gets full. After this Q. ∼ 2/3
point, each sequence of 3840 consecutive As is encoded using only 12
bits. R. ∼ 1
S. ∼ 3/2
12
8 LZW coding for worst-case inputs of N characters using with 12-bit T. ∼ 2
codewords. Recall: no new codewords are added to the table if the
table already has 212 = 4096 entries. U. ∼ 3
The worst case is when the codeword table gets filled up with useless V. ∼ 4
codewords and then the rest of the message cannot take advantage
of any of the added codewords. An input of 3840 As followed by W. ∼ 7
N − 3840 Bs would have this property. In this case, each B requires
a 12-bit codeword. X. ∼ 8
5
14. Algorithm design.
The running time can be improved to N L in the worst case by implicitly forming the L circular
suffixes of a string and using a linear-time suffix sorting algorithm to compute its fingerprint.
• Explicitly (or implicitly) form the L circular suffixes of each of the N strings and put
all N L of them into an array.
• Sort the N L strings using LSD radix sort.
• Check for adjacent entries that both are equal and are circular suffixes of different
original strings. The latter check is necessary if one of the original strings happens to
be a nontrivial cyclic rotation of itself, such as stackstack.
15. Reductions.
(a) We need to create a graph G0 such that its longest cycle corresponds to a longest s-t
Final,
path in G. The intuition is thatFalladding
2012 Longest
an edge Path
between s and t turns any s-t path of
0
length k in G into a cycle of length k + 1 in G . This doesn’t quite work because there
3
might be a cycle in G that is slonger than1 the length of2 the longest s-t path.
Instead of adding an edge between s and t, we add a new path (with new vertices) be-
tween s and t of length V . Now, s-t paths in G of length k ≥ 1 are in 1-1 correspondence
with cycles in G0 of length kt + V . Thus,
4
finding the
5
longest cycle
6
in G0 provides the
longest s-t path in G.
s 1 2 3
G'
t 4 5 6