Homework 4

CSE 246 Spring 2015
HOMEWORK 4
due Final exam
1. Consider a text with one thousand zeros.
(a) How many character comparisons will the Horspool's algorithm make in searching the
pattern 10010? (Show also the shift table.)
(b) How many character comparisons will the Boyer-Moore algorithm make in searching
the pattern 10010? (Show also the bad symbol and good-suffix tables.)
(c) How many character comparisons will the brute-force string matching algorithm make
in searching the pattern 10010?
2. Consider the following pattern:
123456789098765432111122233344455566677788899900056748392093493029384774589
(a) In the above pattern, search for last six digits of your student ID using Horspool's
algorithm.
(b) Repeat (a) using Boyer-Moore algorithm.
(c) How many character comparisons are needed in each case. Comment on your results.
(d) How many character comparisons are needed when you apply brute-force string
matching algorithm?
3. Apply the dynamic programming algorithm to find all the solutions to the changemaking problem for the denominations 1, 3, 5 and the amount n = 9.
4. Consider the following weighted digraph.
C
2
3
3
7
A
D
5
8
B
3
2
E
5
(a) Solve the all-pairs shortest path problem by applying Floyd's algorithm.
(b) Find the transitive closure by applying Warshall's algorithm.
5. Consider the six-symbol alphabet {A, B, M, R, U, _} with the following occurrence
frequencies in a text made up of these symbols:
Symbol
Frequency
A
0.35
B
0.05
M
0.2
R
0.14
(a) Construct the Huffman code for the above data.
(b) Encode BURA_MARMARA using this coding.
(c) Decode 10101011001110011101011 using this coding.
(d) Find the compression ratio of this coding.
U
0.1
_
0.16
6. (a) Find a tight lower bound class for the problem of finding two closest numbers among n
real numbers x1, x2, ..., xn. Prove that the lower bound you find is tight. (Hint: Use problem
reduction. Tight lower bound of element uniqueness problem is known to be Ω(nlogn).)
(b) Is the trivial lower bound of the above problem tight? Why?
7. Apply the shortest augmenting path algorithm to find a maximum flow and a
minimum cut in the following network:
1
5
2
2
6
5
4
4
3
6
4
8
6
8. (a) Remember that the time complexity of solving the Knapsack Problem using Dynamic
Programming is O(nW) where n is the number of items and W is the capacity. Describe why
we cannot say that the Knapsack Problem is in P. (Same reasoning is in Problem 6 of section
11.3)
(b) Describe the basic steps for proving that the Knapsack Problem is NP-Complete. (You
may not be able to prove yourself, but understand and briefly describe the existing proof.)
(c) Is it possible to find an algorithm that solves the Knapsack Problem in O(nlogW) time in
worst case? Describe your answer.
9. Apply the branch-and-bound algorithm to solve the traveling salesman problem for
the following graph:
10. Solve the following instance of the Knapsack problem by branch and bound algorithm:
Capacity (W) = 16
Item
1
2
3
4
weight
10
7
8
4
value
$100
$63
$56
$12