CS 1501
Algorithm Implementation
Exam 2
SOLUTIONS
|
Problem |
Possible Pts |
Pts Received |
|
1 |
20 |
|
|
2 |
10 |
|
|
3 |
12 |
|
|
4 |
12 |
|
|
5 |
10 |
|
|
6 |
10 |
|
|
7 |
8 |
|
|
8 |
10 |
|
|
9 |
8 |
|
|
Total |
100 |
|
For all questions, be sure to show your work. Answers without work will not receive full
credit.
1)
(20
points – 2 points each) Fill in the Blanks. Complete the
statements with the MOST APPROPRIATE
word(s) and/or phrase(s).
a) The sum 1 + 2 + 3 + … + N evaluates to _________N(N+1)/2___________________.
b) If a program's run-time can be
modeled by the function 8N5/2
+ 2N2(3lgN + 6N) , the program's Theta
runtime growth rate is _________Theta(N3)_____.
c) The Boyer-Moore string matching algorithm can do as few as Theta
(______N/M_________________)
comparisons for an unsuccessful search
of a pattern of length M within a string of length N.
d) Collisions can
be avoided in a hash table as long
as the table size, M, is greater
than or equal to ___the size of the key space___________.
e) A block code
containing K bits can represent up
to ________2K_______________
distinct characters.
f)
The Unix compress
implementation of LZW tries to use as few bits as possible in the codewords that
it outputs, so as to maximize compression.
However, it still allows for a large number of
distinct codewords. It accomplishes both
of these goals by _____starting with 9 bits and increasing the number of bits as
needed_________________________________________.
g) Run-time
analysis of recursive algorithms is
typically done via ____recurrence relations________.
h) An undirected graph
with V vertices has a minimum of _____0_________________ edges and a maximum of ____(V-1)(V)/2__________
edges.
i)
PFS on a graph with V vertices and E edges runs in time ______Theta([V+E]lgV)_____ with an adjacency
list and in time ____Theta(V2)_____________ with an adjacency
matrix.
j)
A sequence of N Inserts followed by
2)
(10
points – 2 points each) Indicate if each of the following
statements is TRUE or FALSE. For FALSE
statements, INDICATE WHY THEY ARE FALSE.
a) The "branch and bound" technique
reduces worst case run-times for
search algorithms from exponential to
polynomial run-times. False – in the
worst case they are still exponential
b)
DLBs are an
improvement over regular multiway tries because they have faster search times. False – it improves in terms of memory useage
c) If
someone comes up with a factoring
algorithm that is 1,000,000 times
faster than current factoring algorithms, RSA will no longer be a useful
encryption scheme. False – the improvement is not asymptotic, so
RSA could still be used with larger key sizes.
d) NP-Complete problems are problems that are
known to require exponential run-times. False – it is believed but has not been proved.
e)
If I discover a true
polynomial algorithm that solves the Traveling Salesman Problem, I will
have proved that P = NP. True
3) (12
points – 4 + 4 + 4) Consider RSA encryption
a) Show
(using pictures and explanation in detail) how an RSA Envelope (or digital envelope) works, and why it is used.
See
Powerpoint slides
b) Creating new RSA keys
requires generating large random prime
integers. Explain the general
approach discussed in lecture for generating large random prime numbers, and
how its run-time can be determined (note: since this is only a general
algorithm, a specific overall run-time is not required).
Consider
the pseudcode below:
Generate Large Random Integer X
using a good random number generator
While (!isPrime(X))
Generate Large Random
Integer X
The
overall run-time can be determined as follows:
(Expected number of iterations of
the loop) * (Time required to test primality)
Note
that the Miller-Rabin Witness algorithm is an efficient way of testing
primality, but it was not required in this answer.
c) I'd like to send my friend a
message such that 1) Only he can read it
(no one else) and such that 2) He can
verify that I was the sender and that it wasn't tampered with. Explain how I could do this using RSA. Assume that any RSA keys can be authenticated
to their owners.
I
need to encrypt and digitally sign my message.
Assume my keys are EM and DM and my friend's keys
are EF and DF. I
can solve this problem in the following way:
Encrypt the message with my friend's
public key, EF
Digitally sign the resulting message
with my private key, DM
Send the signed message to my friend
Upon
receiving the message, my friend will
Verify the signature using my public
key, EM
Decrypt the message using his
private key, DF
4) (10 points – 6 + 4) Consider the simple,
naïve algorithm (using a loop) we discussed in lecture for raising an N-bit
integer to an integer power (i.e. XY for N-bit integers X and
Y).
a) Write a Java method to calculate this value, using the BigInteger
class. Your method should return a new BigInteger that is the current
BigInteger raised to the argument BigInteger power. Use the header below:
public BigInteger pow(BigInteger
Y) //
Return this raised to the Y
{ // power
BigInteger ans = new BigInteger("1");
BigInteger
count = new BigInteger("1");
while (count.compareTo(Y) <= 0)
{
ans = ans.multiply(this);
count = count.add(BigInteger.ONE);
}
return ans;
}
Note
that this method is assuming access to the BigInteger class. In reality, we would have to write it with
two arguments.
b) Assuming the Gradeschool algorithm is used for
multiplication, state and thoroughly
justify the Theta run-time for exponentiation using the code you
wrote in part a) above in terms of N.
The loop in the
method iterates Y times. At each iteration, a multiplication must be done. Since Gradeschool requires Theta(N2)
time, the total run-time will be YN2. Since Y is an N-bit integer, it can have a
value of up to ~2N in the worst case. Thus, the total run-time is Theta(N22N).
5) (10 points) An array
representation of a min-heap data structure is shown below. Draw the resulting arrays after the operations Insert(28) followed
by DeleteMin(). For
full credit show the array as it would look after EACH operation.
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
|
|
15 |
30 |
20 |
35 |
60 |
55 |
25 |
45 |
50 |
80 |
|
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
|
15 |
28 |
20 |
35 |
30 |
55 |
25 |
45 |
50 |
80 |
60 |
6) (16
points – 8 + 8) Consider the graph below with the edge
weights shown. Assume the vertices are
stored in alphabetical order, using an adjacency matrix.

a)
Complete
the table below, as it would look after a Depth-First Search Spanning
Tree (starting from vertex A) were created for the graph. val[] is the DFS visit order for the vertex, and dad[] is the parent vertex
in the DFS tree. Show your work above or
in the space below the table for partial credit.
|
|
A |
B |
C |
D |
E |
F |
G |
H |
I |
|
val |
1 |
2 |
3 |
4 |
8 |
6 |
9 |
7 |
5 |
|
dad |
- |
A (1) |
B (2) |
C (3) |
H (8) |
I (9) |
E (5) |
F (6) |
D (4) |
b)
Complete
the table below, as it would look after a Minimum Spanning Tree
(starting from vertex A) were created for the graph, assuming Prim's Algorithm
is used. val[] is the edge weight associated with the vertex, and dad[] is the parent vertex in the MST. Show your work above or in the space below
the table for partial credit.
|
|
A |
B |
C |
D |
E |
F |
G |
H |
I |
|
val |
0 |
2 |
5 |
9 |
3 |
6 |
7 |
4 |
8 |
|
dad |
0 |
A (1) |
B (2) |
C (3) |
H (8) |
C (3) |
E (5) |
F (6) |
D (4) |
7) (8 points)
Consider the weighted graph below. The numbers are the edge capacities. S is the source vertex and
T is the sink vertex.

![]()
Using the BREADTH FIRST SEARCH implementation of the Ford-Fulkerson
algorithm, show EACH AUGMENTING PATH generated (in
the correct order that the paths are generated), the amount of flow for
each path, and the Maximum Flow for the graph.
Assume that an adjacency matrix implementation is used, and that the
vertices are listed in the adjacency matrix in the following order: S, A, B, C, D, E, T. To ensure partial credit, be sure to SHOW
YOUR WORK.
SADT: 60
SBDT: 40
SCDAET: 60
-----------------------
TOTAL: 160
Note that the last
path has a backward edge in it. See
notes and text for more information.
8)
(8
points) Consider
the 2-OPT local search algorithm for
the Traveling Salesman Problem. Consider the complete graph below and an
initial tour of the graph ABCDEA of
weight 25.

Show all of the neighbors of this tour, indicate the neighbor that is chosen
and show the resulting tour. Note that this is just ONE ITERATION
of the 2-OPT algorithm. Show your work.
Neighbors possible:
OLD EDGES NEW EDGES DIFFERENCE
AB, CD AC, BD -- 3
AB, DE AD, BE -- 3
BC, DE BD, CE -- 2
BC, EA BE, CA -- 4
CD, EA CE, DA -- 2
The best improvement
of –4 results from the 4th neighbor shown above. This results in the
new tour: ABEDCA with length 21.
9)
(8 points – 4 + 4) Consider the size and store arrays below, as discussed in
lecture
|
index |
1 |
2 |
3 |
4 |
5 |
6 |
|
size |
3 |
10 |
5 |
6 |
4 |
14 |
|
index |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
|
store |
- |
- |
1 |
5 |
3 |
4 |
5 |
3 |
4 |
2 |
4 |
5 |
a) Fill the values into the store
array as it would look after a dynamic programming solution to the Subset Sum
problem were found for M = 12. Show any work below.
b) Assuming that there are N items in the size array, what is the
run-time of this algorithm? Why is this run-time only considered to be "pseudo" polynomial?
Explain.
Based
on the code for the dynamic programming subset sum solution, the run-time is Theta(NM). This is
pseudo-polynomial because there is no limit on the value for the bound, M. Thus, M could be exponential relative to N,
and the overall run-time could therefore also be exponential.