0% found this document useful (0 votes)
52 views6 pages

Fin f12 Sol

The document contains solutions to problems from a final exam for an algorithms and data structures course. It includes solutions to problems related to analysis of algorithms, graphs, graph search, minimum spanning trees, shortest paths, maximum flow, string sorting, tries, substring search, regular expressions, Huffman codes, data compression, and algorithm design. The solutions provide concise answers and explanations to questions asked in each problem area.

Uploaded by

Neda'a Hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views6 pages

Fin f12 Sol

The document contains solutions to problems from a final exam for an algorithms and data structures course. It includes solutions to problems related to analysis of algorithms, graphs, graph search, minimum spanning trees, shortest paths, maximum flow, string sorting, tries, substring search, regular expressions, Huffman codes, data compression, and algorithm design. The solutions provide concise answers and explanations to questions asked in each problem area.

Uploaded by

Neda'a Hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COS 226 Algorithms and Data Structures Fall 2012

Final Exam Solutions

1. Analysis of algorithms.

(a) 400 seconds

(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.

(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.

3. Graph search.

(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

4. Minimum spanning trees.

(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

(c) y > 12.0. (We also accepted y ≥ 12.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

7. String sorting algorithms.


034423221

8. Ternary search tries.

(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

10. Boyer-Moore substring search.

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

11. Regular expressions.


0→3 2 → 10 3→4 3→9 5→6 6→5 6→7 9→3
0 1 2 3 4 5 6 7 8 9 10 11

(
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.

(a) Final, Fall 2012


char freq encoding
B 2 01111 0 1

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

(b) 6 ≤ f req(I) ≤ 15.


• Since I is not touched until after merging L with {B, F, H}, f req(I) ≥ f req(L) = 5
and f req(I) ≥ f req({B, F, H}) = 2 + 1 + 3 = 6.
• Since I is merged with {B, F, H, L} instead of M or S, f req(I) ≤ f req(M ) = 15
and f req(I) ≤ f req(S) = 15.

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.

(a) Here is an elegant solution:


• For each string s, form its L circular suffixes and suffix sort them (using LSD radix
sort). Use the lexicographically first sorted suffix as its fingerprint. Two strings are
cyclic rotations of one another if and only if they have the same fingerprint.
• Sort the N fingerprints (using LSD radix sort) and check adjacent fingerprints for
equality. If any two are equal, then output yes; otherwise output no.
(b) The order of growth of the running time is N L2 .
• Explicitly forming the L circular suffixes of a string takes L2 time and space. Sorting
the L suffixes (each of length L) them takes L2 time using LSD radix sort. Doing
this for each string takes a total of N L2 time.
• Sorting the N fingerprints (each of length L) takes N L time using LSD radix sort.
Checking for adjacent entries that are equal also takes N L time.

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.

Here is an alternate N L2 time solution:

• 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

(b) (i) and (iii)

You might also like