0% found this document useful (0 votes)
171 views

Fin f11

This exam tests students on algorithms and data structures concepts covered in COS 226. It consists of 14 questions worth a total of 100 points over 180 minutes. Students are allowed one page of handwritten notes as a cheat sheet. They must write their name, NetID, and precept number on the exam and sign an honor pledge before turning it in.

Uploaded by

btitty
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)
171 views

Fin f11

This exam tests students on algorithms and data structures concepts covered in COS 226. It consists of 14 questions worth a total of 100 points over 180 minutes. Students are allowed one page of handwritten notes as a cheat sheet. They must write their name, NetID, and precept number on the exam and sign an honor pledge before turning it in.

Uploaded by

btitty
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/ 16

COS 226

Algorithms and Data Structures

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.

1. Analysis of algorithms. (10 points)


(a) Suppose that you collect the following memory usage data for a program as a function
of the input size N .
N

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.

COS 226 FINAL, FALL 2011

(b) For each function on the left, give the best matching order of growth of the running time
on the right.

public static int f1(int N) {


int x = 0;
for (int i = 0; i < N; i++)
x++;
return x;
}

A. log N

public static int f2(int N) {


int x = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
x++;
return x;
}

D. N 2

public static int f3(int N) {


if (N == 0) return 1;
int x = 0;
for (int i = 0; i < N; i++)
x += f3(N-1);
return x;
}

public static int f4(int N) {


if (N == 0) return 0;
return f4(N/2) + f1(N) + f4(N/2);
}

public static int f5(int N) {


int x = 0;
for (int i = N; i > 0; i = i/2)
x += f1(i);
return x;
}

public static int f6(int N) {


if (N == 0) return 1;
return f6(N-1) + f6(N-1);
}

public static int f7(int N) {


if (N == 1) return 0;
return 1 + f7(N/2);
}

B. N
C. N log N

E. 2N
F. N !

PRINCETON UNIVERSITY

2. Graph search. (8 points)

Topological sort, Fall 2011

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
___

___

___

___

___

___

___

___

___

COS 226 FINAL, FALL 2011

3. Minimum spanning trees. Minimum


(8 points)

Spanning Tree, Fall 2011

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

4. Shortest paths. (8 points)


Suppose that you are running Dijkstras algorithm on the edge-weighted digraph (below left),
starting from a source vertex s. The table (below right) gives the edgeTo[] and distTo[]
values immediately after vertex 2 has been deleted from the priority queue and relaxed.

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.

COS 226 FINAL, FALL 2011

5. String sorting. (6 points)


Consider the first call to key-indexed counting when running LSD string sort on the input
array a[] of 20 strings. Recall that key-indexed counting is comprised of four loops. Give the
contents of the integer array count[] after each of the first three loops (for indices between
'a' and 'g'); then, give the contents of the string array (for the indices 05 and 1819) after
the fourth loop.

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

6. Substring search. (6 points)


Below is a partially-completed Knuth-Morris-Pratt DFA for a string s of length 11 over the
alphabet { A , B }. Reconstruct the DFA and s in the space below.

A
B
s

10

10

11

4
A

COS 226 FINAL, FALL 2011

7. Regular expressions. (5 points)


You have been promoted to COS 226 grader. Circle each NFA below that could have been
constructed by the RE-to-NFA algorithm from the textbook. Otherwise, explain one mistake
in each invalid NFA.
The match transitions are drawn with solid lines; the -transitions are drawn with dotted
Final, Fall 2011
lines.

(i)
0

10

(ii)
0

10

(iii)
0

Final, Fall 2011

(iv)
9

(v)
10

10

PRINCETON UNIVERSITY

Final, Fall 2011


8. Ternary search tries. (7 points)
Consider the following ternary search trie, with string keys and integer values.

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

COS 226 FINAL, FALL 2011

11

9. String symbol table implementation. (7 points)


For each of the operations on the left, list which one or more of the symbol table implementations on the right can be used to efficiently implement it. By efficient, we mean L log N
or better on typical ASCII strings (in random order) of average length L, where N is the
number of keys in the data structure.

Find the value associated with a given


string key in the data structure.

A. Unordered array.
B. Ordered array.

Associate a value with a string key.


C. Red-black BST.

Delete a string key (and its associated


value) from the data structure.

Find the smallest string key in the data


structure.

Find the smallest string key in the data


structure that is greater than or equal
to a given string.

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?

D. Separate-chaining hash table.


E. Ternary search trie.

12

PRINCETON UNIVERSITY

Final, Fall 2011


10. Data compression. (10 points)
(a) Consider the following Huffman trie of a message over the 5-character alphabet {A, B, C, D, E}:

Identify each statement with the best matching description on the right.

The frequency of A is strictly less than the frequency of B.

A. True for all78messages.


B. False for all messages.

The frequency of C is greater than or equal to


the frequency of A.

The frequency of D is strictly greater than the


frequency of A.

The frequency of D is greater than or equal to


that of A, B, and C combined.

The frequency of E is strictly less than that of


A, B, and C combined.

C. Depends on the message.

COS 226 FINAL, FALL 2011

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

decoded message or brief explanation of why invalid

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:

I encoding. When you HexDump a bit0 1 2 3 4 5 6 7 8 9 A B C D


m that contains ASCII-encoded charac- 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR
he table at right is useful for reference. 1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS
n a two digit hex number, use the first 2 SP ! " # $ % & ( ) * + , igit as a row index and the second hex 3 0 1 2 3 4 5 6 7 8 9 : ; < =
as a column index to find the character 4 @ A B C D E F G H I J K L M
t encodes. For example, 31 encodes the 5 P Q R S T U V W X Y Z [ \ ]
1, 4A encodes the letter J, and so forth.
6 ` a b c d e f g h i j k l m
table is for 7-bit ASCII, so the first hex 7 p q r s t u v w x y z { | }
must be 7 or less. Hex numbers starting
Hexadecimal-to-ASCII conversion table
0 and 1 (and the numbers 20 and 7F)
spond to non-printing control characMany of the control characters are left over from the days when physical devices

E F
SO SI
RS US

. /
> ?
N O
^ _
n o
~

DEL

Final, Fall 2011

14

PRINCETON UNIVERSITY

11. Maximum flow. (8 points)


Consider the following st-flow network and feasible flow f .

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

(a) What is the value of the flow f ?

(b) Perform one iteration of the Ford-Fulkerson algorithm, starting from the flow f . Give
the sequence of vertices on the augmenting path.

(c) What is the value of the maximum flow?

(d) List the vertices on the s side of the minimum cut.

(e) What is the capacity of the minimum cut?

COS 226 FINAL, FALL 2011

15

12. Algorithm design. (8 points)


Given an edge-weighted graph G and an edge e, design a linear-time algorithm to determine
whether e appears in an MST of G. For simplicity, assume that G is connected and that all
edge weights are distinct.
Note: Since your algorithm must take linear time in the worst case, you cannot afford to
compute the MST itself.

(a) Describe your algorithm in the space below.

(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

13. Reductions. (8 points)


Consider the following two related problems:
3Sum. Given an integer array a[], are there three indices i, j, and k (not necessarily
distinct) such that a[i] + a[j] + a[k] == 0 ?
3SumVariant. Given two integer arrays b[] and c[], are there three indices i, j, and
k (not necessarily distinct) such that b[i] + b[j] == c[k] ?

(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

You might also like