Fin f14
Fin f14
Final
This test has 14 questions worth a total of 100 points. You have 180 minutes. The exam is closed book, with the
exception of a one page cheatsheet. No calculators or other electronic devices are permitted. Write out and sign
the Honor Code pledge just before turning in the test.
This exam is preprocessed by computer. Please use a pen; if you use a pencil, be sure to write
darkly. Do not write any answers outside of the designated frames. And do not write on the corners.
“I pledge my honor that I have not violated the Honor Code during this examination.”
Name:
netID:
Room:
Total
0. Initialization (2 points)
In the space provided on the front of the exam, write your name and Princeton netID; fill in your precept number;
write the name of the room in which you are taking the exam; and write and sign the honor code.
8
1 7
4 5
0
6
3
2
(a) Starting from vertex 0, run a depth-first search of the digraph, and list the vertices in reverse postorder.
(b) Starting from vertex 0, run a depth-first search of the digraph, and list the vertices in preorder.
2. Analysis of Algorithms (5 points)
For each code fragment on the left, check the best matching order of growth of the running time. You may use an
answer more than once or not at all.
int x = 0 , i ;
for ( i = 0; i < N ; i ++)
x += f2 ( N );
int x = 1, i, j;
for ( i = 0; i < N ; i ++)
for ( j = 1; j < R ; j ++)
x = x * j;
int x = 0 , i , j ;
for ( i = 1; i <= N ; i ++)
for ( j = 1; j <= N + R ; j += i )
x += j ;
3. String Sorting Algorithms (7 points)
The column on the left is the original input of 24 strings to be sorted; the column on the right are the strings
in sorted order; the other 7 columns are the contents at some intermediate step during one of the 3 radix sorting
algorithms listed below.
Match up each column with the corresponding sorting algorithm. You may use a number more than once.
Hint: think about algorithm invariants; do not trace code.
0 4
(1) LSD radix sort (3) 3-way radix quicksort (no shuffle)
(4) Sorted
4. Substring Search (8 points)
(a) Consider the Knuth-Morris-Pratt DFA for the following string of length 8:
C A C A C B C B
Complete the first row of the table.
0 1 2 3 4 5 6 7
A
B 0 0 0 0 0 6 0 8
C 1 1 3 1 5 1 7 1
(b) Suppose that you run the Boyer-Moore algorithm (the basic version considered in the textbook and lecture)
to search for the pattern
M Y F A T H E
in the text
Y B R O T H E R TFinal,
H A T FFall
A T H2014
E R W A S M Y F A T H E R T
Give the trace of the algorithm in the grid below, circling the characters in the pattern that get compared
with characters in the text.
Y B R O T H E R T H A T F A T H E R W A S M Y F A T H E R T
M Y F A T H E
Y B R O T H E R T H A T F A T H E R W A S M Y F A T H E R T
M Y F A T H E
5. Minimum Spanning Tree Algorithms (6 points)
Each of the figures below represents a partial spanning tree. Determine whether it could possibly be obtained from
(a prematurely stopped) Prim’s algorithm, (a prematurely stopped) Kruskal’s algorithm, both or neither.
13 17 6 1
15 14 9 13
10 8
3 6
3 1
4 1 2
7 12 11
13 17 6 1
15 14 9 13
10 8
3 6
3 1
4 1 2
7 12 11
13 17 6 1
15 14 9 13
10 8
3 6
3 1
4 1 2
7 12 11
13 17 6 1
15 14 9 13
10 8
3 6
3 1
4 1 2
7 12 11
13 17 6 1
15 14 9 13
10 8
3 6
3 1
4 1 2
7 12 11
13 17 6 1
Final, Fall 2014
flow capacity
2 3 ?/
6/6 /
16 7/7 13 0/7 /
12 0 / 10 7 1 / 10
/
0
F 6 / 10 G 15 / 15 H 15 / 19 I 12 / 17 J
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
(c) Starting from the flow f , perform one iteration of the Ford-Fulkerson algorithm. List the sequence of vertices
on the augmenting path.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
A B C D E F G H I J
7. Properties of Algorithms (9 points)
Check whether each of the following statements are True or False.
(a) Shortest paths. Consider an edge-weighted digraph G with distinct and positive edge weights, a source
vertex s, and a destination vertex t. Assume that G contains at least 3 vertices, has no parallel edges or self
loops, and that every vertex is reachable from s.
True False
Any shortest s → t path must include the lightest edge.
Any shortest s → t path must include the second lightest edge.
Any shortest s → t path must exclude the heaviest edge.
The shortest s → t path is unique.
(b) Minimum spanning trees. Consider an edge-weighted graph G with distinct and positive edge weights.
Assume that G contains at least 3 vertices, has no parallel edges or self loops, and is connected.
True False
Any MST must include the lightest edge.
Any MST must include the second lightest edge.
Any MST must exclude the heaviest edge.
The MST is unique.
R A - T
N D U S
C E G I H M L O
T A S R -
U D
L O H M N G I
C E
T - R A
S U H M
N D C E G I L O
R A T
C E - S N D U
H M G I L O
9. LZW Compression (5 points)
What is the result of compressing the following string of length 15 using LZW compression?
B B B B B B C A B B C B B B C
Assume the original encoding table consists of all 7-bit ASCII characters and uses 8-bit codewords. Recall that
codeword 80 is reserved to signify end of file.
42 80
ght is useful for reference. 1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
A D D B D B C A
COS 226 FINAL, FALL 2014 2 COS 226 FINAL, FALL 2014 7
C A D D A B C C
public void add(String fragment) add the DNA fragment to the collection
public int prefixCount(String p) number of DNA fragments that start with prefix p
Here is an example:
Give a crisp and concise English description of your data structure. Your answer will be graded on correctness,96
efficiency, and clarity.
(a) Declare the instance variables for your FragmentCollection data type. You may use nested data types.
public class FragmentCollection {
}
(b) Briefly describe how to implement each of the operations, using either prose or code.
• public void add(String fragment):
(c) What is the order of growth of prefixCount(p) as a function of the number N of keys added, the length W
of the prefix p, the alphabet size R, and the number M of fragments that match the given prefix p?
NR
1 log N N W W + log N W +M N + W log R WR
WR
12. Reductions (13 points)
Consider the following two graph-processing problems:
• Shortest-Path. Given an edge-weighted digraph G with nonnegative edge weights, a source vertex s and
a destination vertex t, find a shortest path from s to t.
u 2 v
weight
8
99
1
source s 5 w 6 t destination
(c) Determine whether each of following statements can be infered from the fact that Shortest-Path and
Shortest-Teleport-Path linear-time reduces to one another. For simplicity, assume E ≥ V .
Yes No
If there exists an E log log E algorithm for Shortest-Teleport-Path, then there exists an
E log log E algorithm for Shortest-Path.
If there exists an E log log E algorithm for Shortest-Path, then there exists an E log log E
algorithm for Shortest-Teleport-Path.
If there does not exist a linear-time algorithm for Shortest-Path, then there does not exists
a linear-time algorithm for Shortest-Teleport-Path.
If there does not exist a linear-time algorithm for Shortest-Teleport-Path, then there
does not exists a linear-time algorithm for Shortest-Path.
13. Problem Identification (9 points)
You are applying for a job at a new software technology company. Your interviewer asks you to identify the
following tasks as either possible (with algorithms and data structures introduced in this course), impossible, or an
open research problem.
Given an edge-weighted digraph in which all edge weights are either 1 or 2 and
two vertices s and t, find a shortest path from s to t in time proportional to
E+V.
Given an edge-weighted DAG with positive edge weights and two vertices s and
t, find a path from s to t that maximizes the product of the weights of the edges
participating in the path in time proportional to E + V .
Given an edge-weighted graph with positive edge weights, find a spanning tree
that maximizes the product of the weights of the edges participating in the span-
ning tree in time proportional to E + V .
Given an edge-weighted graph with positive edge weights and two distinguished
vertices s and t, find a simple path (no repeated vertices) between s and t that
maximizes the sum of the weights of the edges participating in the path in time
proportional to E V .
Given a flow network and a mincut in that flow network, find a maxflow in time
proportional to E + V .
Given an array of N strings over the DNA alphabet {A, C, T, G}, determine
whether all N strings are distinct in time linear in the number of characters in
the input.
Given an array a of N 64-bit integers, determine whether there are two indices
i and j such that ai + aj = 0 in time proportional to N .