HW 3 Finsol
HW 3 Finsol
Note: So far we have seen selection sort, bubble sort, and insertion sort. All three of these take O n2
time. But there are O(n lg n) time sorting algorithm, e.g., mergesort, which we shall learn later in the
course. In these home work problems, if you need sorting, you may assume that you have an O(n lg n)
sorting algorithm at hand.
1. (10 points) A firm wants to determine the highest floor of its n-story headquarters from which a gadget
can fall without breaking. The firm has two identical gadgets to experiment with. If one of them gets
broken, it cannot be repaired, and the experiment will have to be completed with the remaining gadget.
Design as efficient (as few droppings as you need) an algorithm as you can to solve this problem.
[Solution]
We could start at the first floor and check every floor following it upwards. Then we do not need two
gadgets. The best-case is when gadget breaks at first floor. The worst-case is when the gadget first
breaks at the n-th floor. No other method’s best-case is better than this one. But we are usually more
concerned about the worst-case (average-case is more important but it is hard to analyze). So, we shall
try to improve the worst-case as we have more than one gadget.
In order to minimize the number of required drops, we should make big jumps. But when the first gadget
breaks, we fall back to linear search for the “jump” number of floors in the worst-case. So, we should
hit an optimal trade-off between number of jumps and jump-length. Say, jump length is j. Then in the
worst-case we need to make nj jumps and following that we have to perform linear search of length j.
√ √
The optimal point is when nj = j or j = n. So, we drop the first gadget from the b nc-th floor and if
√
it√doesn’t break, move to the 2 · b nc-th floor, and so on. When the first gadget√breaks, we have to do
b nc drops with the second gadget in the worst-case. So, the algorithm is in O( n).
2. (10 points) Apply topological sorting using Depth First Search on the following directed graph. Show
m n o p
q r s
t u v w
x y z
the full stack with push and pop indices for each element, DFS forest (with various types of edges), and
the final topological order of the vertices.
[Solution]
Figure 2 shows the DFS forest and the full stack. The topological order is the reverse of the pop-order:
hp, n, o, s, m, r, y, v, x, w, z, u, q, ti
3. (10 points) There are two types of professional wrestlers: “babyfaces” (“good guys”) and “heels” (“bad
guys”). Between any pair of professional wrestlers, there may or may not be a rivalry. Suppose we have
n professional wrestlers and we have a list of r pairs of wrestlers for which there are rivalries. Give an
O(n + r)-time algorithm that determines whether it is possible to designate some of the wrestlers as
babyfaces and the remainder as heels such that each rivalry is between a babyface and a heel. If it is
possible to perform such a designation, your algorithm should produce it. Explain why do you think
your algorithm’s time efficiency is O(n + r)? [Hint: Think in terms of graphs.]
COSC 3100, Spring Homework 3, Page 2 of 7 Due on February 28, 2012
[Solution] Represent the problem as a graph, where each vertex represents a wrestler and each edge
represents a rivalry. The graph will have n vertices and r edges.
Perform as many BFS’s as are needed to visit all n vertices. Assign the first wrestler to be a good guy
and then assign all its immediate neighbors to be bad guys, and so on. If a wrestler is assigned to be a
good guy (or bad guy), but one of its neighbors has already been assigned to be a good guy (or bad guy),
report that a desired designation is not possible. The solution has BFS’s complexity, which is O(n + r).
4. (10 points) One can model a maze by having a vertex for a starting point, a finishing point, dead ends,
and all the points in the maze where more than one path can be taken, and then connecting the vertices
according to the paths in the maze.
(a) Construct such a graph for the maze in Figure 2.
Figure 3: Maze
[Solution] Figure 4 shows the numbering and the graph for the maze.
(b) Which traversal–DFS or BFS–would you use if you found yourself in a maze and why?
COSC 3100, Spring Homework 3, Page 3 of 7 Due on February 28, 2012
Figure 4: Maze
[Solution]
DFS or BFS, either one is efficient.
5. (10 points) Generate all permutations of {3, 5, 6, 9} by
(a) the bottom-up minimal-change algorithm.
(b) the Johnson-Trotter algorithm (with arrows and the greatest mobile element marked).
(c) the lexicographic-order algorithm.
Solution
(a) 3, 35, 53, 356, 365, 635, 653, 563, 536, 3569, 3596, 3956, 9356, 9365, 3965, 3695, 3659, 6359, 6395, 6935, 9635, 9653,
6953, 6593, 6539, 5639, 5693, 5963, 9563, 9536, 5936, 5396, 5369
←
−← −←−← − ← −←−← −← − ←−← −←−← − ← −←−← −←− →−← −←−← − ←−→ −←−← − ←−← −→−← − ←−← −←−→− ← −←−← −←− ← −←−←−← −
(b) 3 5 6 9 , 3 5 9 6 , 3 9 5 6 , 9 3 5 6 , 9 3 6 5 , 3 9 6 5 , 3 6 9 5 , 3 6 5 9 , 6 3 5 9 , 6 3 9 5 ,
←
−← −←−← − ← −←−← −← − →
−→ −← −← − →−→ −← −←− →−← −→−← − →−← −←−→ − ←−→ −←−← − ←−→−← −←− ← −←−→ −← − ← −←−→
−← −
6 9 3 5, 9 6 3 5, 9 6 5 3, 6 9 5 3, 6 5 9 3, 6 5 3 9, 5 6 3 9, 5 6 9 3, 5 9 6 3, 9 5 6 3,
→
−← −←−→− ← −→
−← −→− ← −← −→
−→ − ← −←−→−→ −
9 5 3 6, 5 9 3 6, 5 3 9 6, 5 3 6 9
(c) 3 5 6 9 , 3 5 9 6, 3 6 5 9 ,3 6 9 5, 3 9 5 6 , 3 9 6 5, 5 3 6 9 , 5 3 9 6, 5 6 3 9 ,
i i+1=j i i+1 j i i+1=j i i+1=j i i+1=j i i+1 j i i+1=j i i+1 j i i+1=j
56 9 3, 5 9 3 6 , 5 9 6 3, 6 3 5 9 , 6 3 9 5, 6 5 3 9 ,6 5 9 3, 6 9 3 5 ,
i i+1=j i i+1=j i i+1 j i i+1=j i i+1 j i i+1=j i i+1=j i i+1=j
6 9 5 3, 9 3 5 6 , 9 3 6 5, 9 5 3 6 ,9 5 6 3, 9 6 3 5 ,9 6 5 3
i i+1=j i i+1=j i i+1 j i i+1=j i i+1=j i i+1=j
6. (10 points) Consider the pseudocode of the algorithm for generating permutations in the listing Algo-
rithm 1:
(a) Trace the algorithm by hand for n = 2, 3, and 4. [Hint: Try to reuse the work you did for n = 2
while you are working for n = 3 . . . ]
[Solution]
You will find that when n is odd, after P ermute(n) finishes, the array remains the same as it was
at the beginning of the call P ermute(n). And when n is even, say P ermute(n) started with an
array h1, 2, 3, . . . , n − 1, ni, then at the end the array becomes hn − 1, 1, 2, . . . , n − 2, ni, it actually
cyclically right shifts the first n − 1 positions.
(b) Prove the correctness of P ermute(n). [Hint: Prove that if Permute(n − 1) works correctly,
Permute(n) must also work correctly.]
[Solution]
COSC 3100, Spring Homework 3, Page 4 of 7 Due on February 28, 2012
Algorithm 1 Permute(n)
1: {Input: A positive integer n and a global array A[1..n]}
2: {Output: All permutations of elements of A}
3: if n ← 1 then
4: print A
5: else
6: for i ← 1 to n do
7: P ermute(n − 1)
8: if n is odd then
9: swap A[1] and A[n]
10: else
11: swap A[i] and A[n]
12: end if
13: end for
14: end if
If P ermute(n−1) works properly, i.e., if it prints all (n−1)! permutations correctly, then P ermute(n)
in its for loop will make n! prints. If we can prove that all these prints are distinct, then P ermute(n)
also must work correctly.
When n is even, it makes n calls to P ermute(n − 1) in its for loop. As n is even, n − 1 must be odd.
So after each call to P ermute(n − 1) the array is left as it was at the beginning of that call and
then occurs the “swap A[i] and A[n]”. If we consider the n-th element of the array to be sitting
idle during the first call to P ermute(n − 1), then this swap following the end of P ermute(n − 1)
now picks the first element which remains idle during this second call to P ermute(n − 1), and then
the second element is chosen to remain idle during the third P ermute(n − 1) call, and so on. Thus
during each of this n calls to P ermute(n − 1) inside the for loop of P ermute(n) a distinct element
remains idle. So, all permutations must be distinct.
Now, if n is odd, n − 1 is even, thus the observation in part(a) asserts that an array h1, 2, 3, . . . , n −
1, ni turns into hn − 1, 1, 2, . . . , n − 2, ni after the end of the first call to P ermute(n − 1) and then
“swap A[1] and A[n]” picks a different element as the idle one for each of the remaining iterations.
Thus again, all the permutations must be distinct.
(c) What is the time efficiency of P ermute(n)? [Hint: The pseudocode Pn gives you the recurrence
relation (number of swaps), solve it. You may need the formula e ≈ i=0 i!1 for large n.]
[Solution]
The recurrence for the number of swaps is as follows,
(
n [T (n − 1) + 1] if n > 1
T (n) =
0 if n = 1
COSC 3100, Spring Homework 3, Page 5 of 7 Due on February 28, 2012
T (n) = nT (n − 1) + n
= n[(n − 1)T (n − 2) + (n − 1)] + n
= n(n − 1)T (n − 2) + n(n − 1) + n
= n(n − 1)(n − 2) . . . [n − (n − 1)]T ([n − (n − 1)]) + n(n − 1)(n − 2) . . . [n − (n − 1)] +
n(n − 1)(n − 2) . . . [n − (n − 2)] + . . . + n(n − 1) + n
n! n! n!
= n! + n! + + + ... +
2! 3! (n − 1)!
1 1 1
= n! 1 + 1 + + + . . . +
2! 3! (n − 1)!
i=n
!
X1 1
= n! −
i=0
i! n!
1
≈ n!(e − 0) When n is big is small
n!
Thus T (n) ∈ Θ(n!).
7. (10 points) Consider the following algorithm for searching in a sorted array A[0..n − 1]. If n = 1,
simply compare the search key K with the single element of the array; otherwise, search recursively by
comparing K with A b n3 c , and if K is larger, compare it with A b 2n
3 c to determine in which third of
the array to continue search.
(a) Set up a recurrence for the number of key comparisons in the worst-case. You may assume that
n = 3k .
[Solution]
The recurrence relation is,
(
n
T 3 +2 if n > 1
T (n) =
1 if n = 1
n
T (n) = T + 2 + 2 + ... + 2
3k | {z }
k 20 s
9. (10 points) Suppose you are given an array A of n sorted numbers that has been circularly shifted k
positions to the right. For example, h35, 42, 5, 15, 27, 29i is a sorted array that has been circularly shifted
k = 2 positions, while h27, 29, 35, 42, 5, 15i has been shifted k = 4 positions.
(a) Suppose you know what k is. Give an as-efficient-as-you-can (AEAYC) algorithm to find the largest
number in A. Express the algorithm in pseudocode.
[Solution] Algorithm 3 gives the pseudocode.
(b) Suppose you do not know what k is. Give an AEAYC algorithm to find the largest number in A.
Express the algorithm in pseudocode.
[Solution] Algorithm 4 gives the pseudocode.
10. (10 points) Given a set S of n integers and an integer t, give an O nk−1 lg n algorithm to test whether
k of the integers in S add up to t. Express the algorithm in pseudocode.
[Solution]
Let us see what we can do when k = 3. We need a O n2 lg n algorithm. Sort the array first with a
O(n lg n) sorting algorithm. The n2 inside the O() indicates a nested loop and the lg n indicates one
COSC 3100, Spring Homework 3, Page 7 of 7 Due on February 28, 2012
binary search for each of these n2 iterations. Binary search needs a key, K. Inside the second loop, we
could subtract the sum of two numbers from t, to obtain K. We shall generalize this idea for any k > 1.
Lines 17–32 maintain the indexing of the implicit k −1 nested loops (for the nk−1 part of the complexity).