Docsity

Algorithms and Data Structures Final Exam Solutions (Fall 2012), Summaries of Design and Analysis of Algorithms

Toggle sidebar
bg1
COS 226 Algorithms and Data Structures Fall 2012
Final Exam Solutions
1. Analysis of algorithms.
(a) 400 seconds
(b) ∼4MR
2. Graphs.
(a) The method marked[v] returns true if and only if there is a directed path from sto v.
(b) E+V, as usual for depth-first search.
(c) V(to initialize the marked[] array).
(d) V2. Note that E≤V2since 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
pf3
pf4
pf5

Partial preview of the text

Download Algorithms and Data Structures Final Exam Solutions (Fall 2012) and more Summaries Design and Analysis of Algorithms in PDF only on Docsity!

COS 226 Algorithms and Data Structures Fall 2012

Final Exam Solutions

  1. Analysis of algorithms.

(a) 400 seconds

(b) ∼ 4 M R

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

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

  1. Minimum spanning trees.

(a) 10 20 30 40 50 100

(b) x ≤ 110.

(c) y ≤ 60.

(d) z ≤ 80.

  1. Shortest paths.

(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

  1. Maximum flow.

(a) 25

(b) A → G → B → C → H → I → J

(c) 25 + 3 = 28

(d) {A, B, C, F, G}

(e) 28

  1. String sorting algorithms.

0 3 4 4 2 3 2 2 1

  1. Ternary search tries.

(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

  1. Huffman codes.

(a)

char freq encoding

B 2 01111

F 1 01110

H 3 0110

I? 00

L 5 010

M 15 10

S 15 11

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.

  • 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.
  1. Data compression.

K X M R D S

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.

A. ∼ 1/

B. ∼ 1/

C. ∼ 1/

D. ∼ 1/

E. ∼ 1/

F. ∼ 1/

G. ∼ 1/

H. ∼ 1/

I. ∼ 1/

J. ∼ 1/

K. ∼ 8/

L. ∼ 1/

M. ∼ 1/

N. ∼ 1/

O. ∼ 1/

P. ∼ 1/

Q. ∼ 2/

R. ∼ 1

S. ∼ 3/

T. ∼ 2

U. ∼ 3

V. ∼ 4

W. ∼ 7

X. ∼ 8