Format code in code blocks.
Preconditions: “Suppose you are given a sorted array of \(n\) distinct numbers that has been rotated \(k\) steps, for some unknown integer \(k\) between \(1\) and \(n − 1\).”
rotationCheck(A[1..n])
if (n == 2)
return(1) << only one possible rotation when n = 2 >>
else
m = ceiling(n/2)
if(A[m]>A[n]) << it's in second half >>
k = (m-1)+rotationCheck(A[m..n])
else << it's in first half >>
k = rotationCheck(A[1..m])
return(k)
RotationFinder(A[1...n]):
if n=2 return 1
else:
middle <- floor(n/2)
if A[middle]>A[middle+1]: << we found it (lucky) >>
return middle
if A[middle]>A[1]: << it's in second half >>
return middle + rotationFinder(A[(middle+1)...n])
else: << it's in first half >>
return rotationFinder(A[1...middle])
Recurrence: \(C(n) = C(n/2) + O(1)\). Solution: \(C(n) \in O(\log n)\).
Backtracking algorithms tend to be slow.
Can you place \(n\) queens on an \(n\times n\) chessboard so that none are threatening another?
Schwer ist es übrigens nicht, durch ein methodisches Tatonniren sich diese Gewissheit zu verschaffen, wenn man 1 oder ein paar Stunden daran wenden will.
Gauss says it’s no big deal.
Start with \(r=1\) and \(j = 1\).
Try \(n=4\).
Q[1..n]
is going to contain the locations of the queens. If Q[i] == j
, that means there is a queen on the \(j\)th square of row \(i\).
Preconditions: r
stands for the row we are trying to place a queen on. So we’ve managed to place a queen on rows \(1, \ldots, r-1\) legally when this algorithm is called.
PlaceQueens(Q[1 .. n], r):
if r = n + 1
print Q[1 .. n] << Problem solved! >>
else
for j <- 1 to n
legal <- TRUE
for i <- 1 to r − 1
if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
legal <- False << a previous queen is attacking this square >>
if legal
Q[r] <- j
PlaceQueens(Q[1 .. n], r + 1)
PlaceQueens(Q[1 .. n], r):
if r = n + 1
print Q[1 .. n] << Problem solved! >>
else
for j <- 1 to n
legal <- TRUE
for i <- 1 to r − 1
if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
legal <- False << a previous queen is attacking this square >>
if legal
Q[r] <- j
PlaceQueens(Q[1 .. n], r + 1)
See Figure 2.3.
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
length 1 2 3 4 5 6 7 8 9 10
price 1 5 8 9 10 17 17 20 24 30
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
Andrew | Jordan | Levi | Ethan | Josiah | John | Nathan |
Kevin | Blake | Logan | Grace | Bri | Jack | Trevor |
Kristen | James | Drake | Isaac | Talia | Claire | Graham |
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
length 1 2 3 4 5 6 7 8 9 10
price 1 5 8 9 10 17 17 20 24 30
A five-inch churro sells for 10 cents. Is there a way to cut it into pieces so that the total price of all the pieces will be more? What is the best (largest) total price you can find? What is the worst total price?
Suppose (as inductive hypothesis) that CutChurro(k)
returns the maximum price among all the ways to cut up a \(k\)-inch churro, for \(k\leq n\). Let’s say you cut a 3-inch piece off of an \(n\)-inch churro, leaving yourself with pieces of size \(3\) inches and \(n-3\) inches. You plan to sell the \(3\)-inch piece, but you are open to making more cuts in the \(n-3\)-inch piece. Give an expression for the maximum price you can get for the pieces, among all the ways you can continue to cut the \(n-3\)-inch piece up.
CutChurro(k)
returns the maximum price among all the ways to cut up a \(k\)-inch churro, for \(k\leq n\). Write a for-loop to determine the maximum price among all the ways to cut up an \(n\)-inch churro.