



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 solutions to the final exam of the Algorithms and Data Structures course offered at the university during the Fall 2012 semester. It covers various topics such as analysis of algorithms, graphs, minimum spanning trees, shortest paths, maximum flow, string sorting algorithms, ternary search tries, Huffman codes, data compression, and algorithm design. The solutions include step-by-step explanations and visual aids.
Typology: Summaries
1 / 6
This page cannot be seen from the preview
Don't miss anything!




COS 226 Algorithms and Data Structures Fall 2012
(a) 400 seconds
(b) ∼ 4 M R
(a) The method marked[v] returns true if and only if there is a directed path from s to v.
(b) E + V , as usual for depth-first search.
(c) V (to initialize the marked[] array).
(d) V 2. Note that E ≤ V 2 since there are no parallel edges.
(a) reverse postorder: 0 1 6 5 2 8 9 4 3 7
(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.
(a) 0 1 5 4
(b) x = 8. 0
(c) y > 12 .0. (We also accepted y ≥ 12 .0.)
(d) vertex 2
(e) v (^) distTo[] edgeTo[]
3 20.0 2 → 3 6 35.0 2 → 6
(a) 25
(b) A → G → B → C → H → I → J
(c) 25 + 3 = 28
(d) {A, B, C, F, G}
(e) 28
0 3 4 4 2 3 2 2 1
(a) A (7), CAA (5), CGA (4), CGCA (11), TA (8), TGT (12), TT (9)
(b)
Final, Fall 2012
A
G C
A A
A A
A
T
A T
G
T
G
T
C
A
C
7
5 13
4 11
3
8 9
17
12
T
T 0
99 A
(a)
Final, Fall 2012
M
0
I
L
(^0 )
B
H
0
1
1
S
0 1
1
0
F
0
1
(b) 6 ≤ f req(I) ≤ 15.
8 255 Run-length coding with 8-bit counts for best case inputs of^ N^ bits. The best case is an alternating sequence of 255 0s and 255 1s. Each sequence of 255 0s (or 255 1s) is encoded with 8 bits (11111111).
8 1 Run-length coding with 8-bit counts for worst-case inputs of^ N^ bits. The worst case is an alternating sequence of 0s and 1s. Each bit is encoded with 8 bits (00000001).
1 8 Huffman coding for best-case inputs of^ N^ characters. The best case is when one character occurs 100% of the time (or all but a constant number of times), in which case it is encoded using 1 bit.
8 8 The worst case is when each of the^256 characters occurs with equal frequency. In this case, each character is encoded using 8 bits.
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 already has 2^12 = 4096 entries. 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 way up to 212 − 256 = 3840 As when the table gets full. After this point, each sequence of 3840 consecutive As is encoded using only 12 bits.
12 8 LZW coding for worst-case inputs of^ N^ characters using with 12-bit codewords. Recall: no new codewords are added to the table if the table already has 2^12 = 4096 entries. The worst case is when the codeword table gets filled up with useless codewords and then the rest of the message cannot take advantage of any of the added codewords. An input of 3840 As followed by N − 3840 Bs would have this property. In this case, each B requires a 12 -bit codeword.