Basic idea:
for i<-1 to n
sing verse i
for j<-1 to n
sing verse j
for k<-1 to n
sing verse k
for i ← 1 to n
Sing “And I was like”
for j ← i down to 1
Sing “Baby, Baby, Baby, oooh,”
for k ← j down to 1
Sing "Baby, Baby, Baby, oooh,"
if i > 1
Sing “Thought you'd always be mine”
else Sing “Mine”
for i <- 1 to n:
for j <- 1 to n:
for k <- n down to 1:
Sing "On the j'th bookshelf in the i'th bookstore
on route to Kentucky, there were k Bibles
left to distribute. The clerk sent a lad off
to preach with one Bible."
if not the last verse:
Sing "So..."
Sing the twelve days of Christmas but for every type of gift, sing BottlesOfBeer(i) with i equal to how many gifts there are (e.g., instead of singing “5 golden rings”, sing BottlesOfBeer(5)). Twelve days of Christmas is \(\Theta(n^2)\), and bottles of beer is \(\Theta(n)\) so it ends up being \(\Theta(n^3)\).
Not pseudocode, but clearly defines an algorithm.
Estimate. Don’t try to count things exactly.
2a. \(O(\log n)\): The number of syllables in \(n\) is asymptotically equivalent to the number of digits in \(n\), which is \(O(\log n)\).
2b. \(O(n \log n)\): For \(i = 1 \ldots n\), singing \(i\) requires \(O(\log n)\) syllables, and \(i\) gets sung \(O(n)\) times.
2c. \(O(n^2 \log n)\): (similar to 2b)
This does not specify an algorithm:
… merge sort the numbers in the array, and then iterate through the array and check if the sum of the number and the number after it, and lastly print the result.
Acceptable answer, if not great:
SumFromArr(intArray, x)
for each element a in intArray
for each of the other elements b
add a to b
if sum is x, return true
return false
Running time is \(\Theta(n^2)\)
\(\Theta(n \log n)\) examples:
findSumInArr(arr, x):
arr = mergeSort(arr)
leftPointer <- 0
rightPointer <- (length of arr) - 1
while (leftPointer < rightPointer) {
if ((arr[leftPointer] + arr[rightPointer]) == x)
return 1
else if ((arr[leftPointer] + arr[rightPointer]) < x)
leftPointer <- leftPointer + 1
else
rightPointer <- rightPointer - 1
}
findsum(array arr, int x) :
mergeSort arr
for a in arr:
binary search arr for (x - a)
if (x - a) is found
return true;
return false
Your only task is to simplify the original problem, or to solve it directly when simplification is either unnecessary or impossible; the Recursion Fairy will solve all the simpler subproblems for you, using Methods That Are None Of Your Business…
The Recursion Fairy is the inductive hypothesis.
PeasantMultiply(X, Y):
if X = 0
return 0
else
x <- floor(X/2)
y <- Y + Y
prod <- PeasantMultiply(x, y) # Recursion Fairy does correctly
if X is odd
prod <- prod + Y
return prod
Strong inductive hypothesis:
PeasantMultiply(x,y)
works for any x
less than X
.
Hanoi(n, src, dst, tmp):
if n > 0
Hanoi(n − 1, src, tmp, dst)
move disk n from src to dst
Hanoi(n − 1, tmp, dst, src)
Hanoi(n, src, dst, tmp): # T(n) moves
if n > 0
Hanoi(n − 1, src, tmp, dst) # T(n-1) moves
move disk n from src to dst # 1 move
Hanoi(n − 1, tmp, dst, src) # T(n-1) moves
\[ T(n) = \left\{\begin{array}{ll} 0 & \mbox{if } n=0 \\ 2T(n-1) +1 & \mbox{if } n>0 \end{array} \right. \]
Suppose that \(T(n)\) is given by the recurrence relation:
\[ T(n) = \left\{\begin{array}{ll} 0 & \mbox{if } n=0 \\ 2T(n-1) +1 & \mbox{if } n>0 \end{array} \right. \]
Prove that \(T(n) = 2^n - 1\) for all \(n\geq 0\).
\[\begin{align} T(n) &= 2T(n-1) + 1 && \mbox{by the recurrence relation} \\ &= 2(2^{n-1} - 1) + 1 && \mbox{by inductive hypothesis} \\ &= 2^n - 1 \end{align}\]
Solution for \(n=5\):
11111 -> 11110 -> 11010 -> 11011 -> 11001 -> 11000 -> 01000 -> 01001
-> 01011 -> 01010 -> 01110 -> 01111 -> 01101 -> 01100 -> 00100
-> 00101 -> 00111 -> 00110 -> 00010 -> 00011 -> 00001 -> 00000
Consider the case \(n = 3\). Draw a graph whose nodes are the 8 possible states of the puzzle, and draw a directed edge between two states if there is a legal move between the states. Is there a directed path from 111 to 000? Is there more than one path?
Describe a recursive algorithm for solving this puzzle. Your input is the number of rings \(n\); your algorithm should print a reduced sequence of moves that solves the puzzle. For example, given the integer 5 as input, your algorithm should print the sequence 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1.
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 |
---|---|---|---|---|---|
Jack | Nathan | Logan | Levi | Blake | Graham |
Trevor | Bri | Talia | Andrew | Claire | Timothy |
Kevin | Jordan | Josiah | Isaac | James | Grace |
John | Kristen | Drake | Ethan |