Algorithm Pancake PDF
Algorithm Pancake PDF
Algorithm Pancake PDF
Algorithms
and Data Structures
3
2
finish
start
2
3
4
A Flip
flip here
1
4
2
3
1
How?
2
4
2
3
3
1
2
1
3
2
3
4
3
4
Done!
More Pancakes?
2
4
5
1
6
3
Learning outcomes
Binary Search
3 7 11 12
15 19 24 33
24
41 55
19 24 33
24
further reduce by half 19 24
24
41 55
10
A[1]
30 70
A[2]
A[3]
A[4]
A[5]
A[6]
A[7]
110
A[8]
To find 190
A[9]
330 410
A[10]
550
if 6 > 10? No; mid = 8; if 190 == A[8]? No; if 190 < A[8]? Yes
RBS(A, 6, 7, 190)
11
12
A[1]
A[2]
A[3]
A[4]
30 70
A[5]
A[6]
A[7]
110
A[8]
To find 230
A[9]
330 410
Time complexity
A[10]
550
if 1 > 10? No; mid = 5; if 230 == A[5]? No; if 230 < A[5]? No
RBS(A, 6, 10, 230)
T(n) =
if 6 > 10? No; mid = 8; if 230 == A[8]? No; if 230 < A[8]? Yes
RBS(A, 6, 7, 230)
if n=1
T(n/2) + 1
otherwise
13
14
Substitution method
Recurrence
A recurrence is an equation or inequality that
describes a function in terms of its value on smaller
inputs.
E.g.,
T(n) =
1
T(n/2) + 1
if n = 1
if n > 1
T(n) =
if n=1
T(n/2) + 1
otherwise
L.H.S = T(1) = 1
L.H.S
15
(Divide & Conquer)
L.H.S R.H.S
16
(Divide & Conquer)
Triomino Puzzle
Substitution method
T(n) =
if n=1
T(n/2) + 1
otherwise
Is it do-able?
2n
Triomino Puzzle
2n-by-2n chessboard with one missing
square &
many L-shaped tiles of 3 adjacent squares
Question: Cover the chessboard with L-shaped tiles
without overlapping 2n
Is it do-able?
Input:
Input:
Triomino Puzzle
2n-1
2n-1
2n
2n-1
Merge Sort
21
(Divide & Conquer)
Merge sort
using divide and conquer technique
divide the sequence of n numbers into two halves
recursively sort the two halves
merge the two sorted halves into a single sorted
sequence
23
24
34, 5, 32, 21
divide these 4
numbers into
halves
similarly for
these 4
10, 64
51
13
32, 21
25
26
34, 5, 32, 21
10, 64
10
34, 5
51, 13
34, 5, 32, 21
64
merge pairs of
single number
into a sequence
of 2 sorted
numbers
34, 5
34
32, 21
32
21
51, 13
51
10, 64
13
13, 51
34, 5, 32, 21
10
64
10, 64
34, 5
34
32, 21
32
5, 34
21
21, 32
28
13
13, 51
34, 5, 32, 21
10, 64
10
64
10, 64
34, 5
34
32, 21
32
5, 34
21
21, 32
5, 21, 32, 34
51, 13
51
13
13, 51
10, 64
10
34, 5
34
5, 34
32, 21
32
21
21, 32
5, 21, 32, 34
29
Summary
64
10, 64
34, 5, 32, 21
30
(Divide & Conquer)
5, 21, 32, 34
Divide
dividing a sequence of n numbers into two
smaller sequences is straightforward
Result:
Conquer
32
5, 21, 32, 34
Result: 5,
5, 21, 32, 34
Result: 5, 10,
33
34
5, 21, 32, 34
Result: 5, 10, 13
5, 21, 32, 34
and again
35
36
5, 21, 32, 34
5, 21, 32, 34
37
38
Pseudo code
5, 21, 32, 34
Algorithm Mergesort(A[1..n])
if n > 1 then begin
copy A[1..n/2] to B[1..n/2]
copy A[n/2+1..n] to C[1..n/2]
Mergesort(B[1..n/2])
Mergesort(C[1..n/2])
Merge(B, C, A)
end
39
40
MS(34)MS(
5)
MS( 32)MS(21)
M(
10, 64 )
M( 5, 34
,
21, 32 )
5, 21, 32, 34
10, 64 )
M( 5, 34
10
M(
M( 13, 51
20
)
5, 21, 32, 34
42
order(Divide
of execution
& Conquer)
p=4
q=4
21, 32 )
41
Pseudo code
16
12
MS( 34, 5 ) MS( 32, 21 )
13
14 17
18
MS(34)MS( 5)
MS( 32)MS(21)
15
19
5
M( 13, 51
11
)
C: 5, 21, 32, 34
A[ ]
Before loop
empty
5, 10
End of 3rd
5, 10, 13
End of 4th
5, 10, 13, 21
End of 5th
End of 6th
43
44
Time complexity
Time complexity
T(n) =
1
2T(n/2) + n
is O(n log n)
2T(n/2) + n otherwise
if n=1
otherwise
45
46
Time complexity
1 n)
Prove that T(n)
is O(n= log
if n=1
More example
if n=1
2T(n/2) + n otherwise
if n=1
is O(n)
2T(n/2) + 1 otherwise
Guess: T(n) 2n 1
47
48
More example
Prove that
T(n) =
if n=1
is O(n)
2T(n/2) + 1 otherwise
Guess: T(n) 2n 1
Assume true for all n' < n [assume T(n/2) 2(n/2)-1]
T(n) = 2
T(n/2)+1
2 (2
(n/2)-1) + 1
Summary
Depending on the recurrence, we can guess the
order of growth
T(n) = T(n/2)+1
T(n) is O(log n)
T(n) = 2
T(n/2)+1
T(n) is O(n)
T(n) = 2
T(n/2)+n
T(n) is O(n log n)
by hypothesis
= 2n 2 + 1
= 2n - 1
50
Exercise
Prove that
T(n) =
Exercise
if n=1
O(n log n)
4T(n/4) + n otherwise
Prove that
T(n) =
if n=1
O(n log n)
4T(n/4) + n otherwise
51
52
Exercise
Prove that
T(n) =
if n=1
is O(n log n)
4T(n/4) + n otherwise
Tower of Hanoi
Induction step:
T(n)
= 4 x T(n/4) + n
4 x ( (n/4) log (n/4) ) + n
= n log (n/4) + n
= n (log n log 4) + n
= n log n 2n + n < n log n
53
(Divide & Conquer)
1
2
3
A
55
(Divide & Conquer)
1
2
3
C
56
(Divide & Conquer)
Easy!
3
2
1
A
58
1
C
59
(Divide & Conquer)
1
2
A
60
(Divide & Conquer)
Next?
Move Disc-1 to C
Done!
1
B
2
C
61
1
2
C
1
2
3
A
62
63
(Divide & Conquer)
3
A
2
B
1
C
64
(Divide & Conquer)
1
2
B
3
C
65
1
A
2
B
1
2
3
C
66
(Divide & Conquer)
Tower of Hanoi
Done!
3
C
invoke by calling
ToH(3, A, C, B)
67
68
ToH(3, A, C, B)
ToH(3, A, C, B)
1
ToH(2, A, B, C)
ToH(1, A, C, B)
move 1 disc
from A to C
ToH(1, C, B, A)
ToH(2, B, C, A)
ToH(1, B, A, C)
move 1 disc
from A to B
move 1 disc
from A to C
ToH(2, A, B, C)
2
ToH(1, A, C, B)
move 1 disc
from B to C
move 1 disc
from C to B
move 1 disc
from B to A
ToH(1, A, C, B)
3
move 1 disc
from A to C
move 1 disc
from A to C
move 1 disc
from A to C
6
move 1 disc
from C to B
10
T(n) = T(n-1)
+ 1 + T(n-1)
move n-1
move n-1
discs from
discs from
A to B
B to C
move Disc-n
from A to C
T(n) =
if n=1
2T(n-1) + 1
otherwise
71
(Divide & Conquer)
12
ToH(1, A, C, B)
move 1 disc
from B to C
move 1 disc
from B to A
69
ToH(1, B, A, C)
Time complexity
ToH(2, B, C, A)
11
ToH(1, C, B, A)
move 1 disc
from A to B
13
move 1 disc
from A to C
70
(Divide & Conquer)
= 2
T(n-1) + 1
1
if n=1
T(n) =
2T(n-1) + 1 otherwise
T(n-2) + 1] + 1
= 2[2
= 22 T(n-2) + 2 + 1
T(n-3) + 1] + 21 + 20
= 22 [2
= 23 T(n-3) + 22 + 21 + 20
20 + 21 + + 2n-1 = 2n-1
Summary - continued
Depending on the recurrence, we can guess the
order of growth
T(n) = T(n/2)+1
T(n) is O(log n)
T(n) = 2
T(n/2)+1
T(n) = 2
T(n/2)+n
T(n) = 2
T(n-1)+1
T(n) is O(n)
T(n) is O(n log n)
T(n) is O(2n)
Iterative Method
73
(Divide & Conquer)
T(n) = T(n/2) + 1
= [T(n/4) + 1] + 1
= T(n/22) + 2
= [T(n/23) + 1] + 2
= T(n/23) + 3
T(n) =
if n=1
T(n/2) + 1
otherwise
Hence, T(n)
is O(log n)
= T(n/2k) + k
75
(Divide & Conquer)
= T(n/2log n) + log n
= T(1) + log n
= 1 + log n
76
(Divide & Conquer)
if n=1
2T(n/2) + 1 otherwise
Hence,
T(n) is
O(n)
1
if n=1
T(n) = 2
T(n/2) + n
T(n) =
2T(n/2) + n otherwise
= 2[2
T(n/4) + n/2] + n
= 22 T(n/22) + n + n = 22 T(n/22) + 2n
= 22 [2
T(n/23) + n/22] + 2n
= 23 T(n/23) + 3n
Hence, T(n)
is O(n log
n)
2k T(n/2k)
2k-1
2k-2
++
22
+2+1
2k T(n/2k)
+ k
n
2log n T(n/2log n)
2logn-1 +
2logn-2
=
+
++
= n T(1) + n/2 + n/4 + ... + 4 + 2 + 1
= n + n/2 + n/4 + ... + 4 + 2 + 1 = 2n-1
22
+2+1
77
78
Fibonacci's Rabbits
A pair of rabbits, one month old, is too young to reproduce.
Suppose that in their second month, and every month thereafter,
they produce a new pair.
Fibonacci number
end of
month-0
end of
month-1
end of
month-2
end of
month-4
end of
month-3
How many
at end of
month-5, 6,7
and so on? 80
(Divide & Conquer)
Petals on flowers
Fibonacci number
1
F(n-1) + F(n-2)
F(n) =
1 petal:
white calla lily
2 petals:
euphorbia
8 petals:
bloodroot
3 petals:
trillium
13 petals:
black-eyed susan
5 petals:
columbine
21 petals:
shasta daisy
if n = 0 or 1
if n > 1
F(n)
13
21
10
34 55 89
34 petals:
field daisy
82
F7
F6
F7
27
F6
F5
F5
18
F5
F4
F3
F4
F2
F3
F3
F2
F3
F2
F1 F0
F2
F1
F2
F3
F2
F1 F0
F1 F0 F1 F0
F1
F1 F0
F4
F1
83
(Divide & Conquer)
F2
F3
F2
F2
F1
F2
F1 F0
F2
F1
F2
F3
F1 F0
F1 F0 F1 F0
F1
F1 F0
F1 F0 F1 F0
F2
F3
F4
F1
F1
F1 F0
10
F2
F3
F1 F0 F1 F0
F3
13
F1
F2
F4
F2
F1
F4
F5
F1 F0
order of execution84
21
F7
13
F6
= [f(n-2)+f(n-3)+1] + f(n-2) + 1
F5
F5
F4
F3
> 2 f(n-2)
F4
F2
F3
F3
F2
Suppose f(n)
denote the
time
complexity to
compute F(n)
> 2 [2
f(n-2-2)] = 22 f(n-4)
> 22 [2
f(n-4-2)] = 23 f(n-6)
F2
F1
F4
3
F3
2
1
F2
2
F2
F3
F2
F2
F1 F0
F1 F0 F2
F1
F1 F0
> 23 [2
f(n-6-2)] = 24 f(n-8)
F1 F0 F1 F0
F1
F1
F1 F0 F1 F0
> 2k f(n-2k)
1 F1
F1 F0
return value
85
T(n) = 2T(n-1) + 4
= 2[2xT(n-2) + 4] + 4
= 22 T(n-2) + 2x4 + 4
= 22 [2xT(n-3) + 4] + 21x4 + 20x4
= 23 T(n-3) + 22x4 + 21x4 + 20x4
2T(n-1) + 4 if n > 1
if n=1
exponential in
n