comp108 algorithmic foundations divide and conquer
DESCRIPTION
COMP108 Algorithmic Foundations Divide and Conquer. Prudence Wong. Pancake Sorting. Input: Stack of pancakes, each of different sizes Output: Arrange in order of size (smallest on top) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/1.jpg)
Algorithmic FoundationsCOMP108
COMP108Algorithmic FoundationsDivide and Conquer
Prudence Wong
![Page 2: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/2.jpg)
Algorithmic FoundationsCOMP108
Pancake SortingInput: Stack of pancakes, each of different sizesOutput:Arrange in order of size (smallest on top)Action: Slip a flipper under one of the pancakes
and flip over the whole stack above the flipper
finish
4
1
32
41
32
start
![Page 3: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/3.jpg)
Algorithmic FoundationsCOMP108
Triomino PuzzleInput: 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 overlappingIs it do-able?
2n
2n
![Page 4: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/4.jpg)
Algorithmic FoundationsCOMP108
Robozzle - RecursionTask: to program a robot to pick up all stars in a
certain areaCommand: Go straight, Turn Left, Turn
Right
![Page 5: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/5.jpg)
Algorithmic FoundationsCOMP108
Divide and Conquer …
![Page 6: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/6.jpg)
Algorithmic FoundationsCOMP108
6
(Divide & Conquer)
Learning outcomes Understand how divide and conquer works
and able to analyse complexity of divide and conquer methods by solving recurrence
See examples of divide and conquer methods
![Page 7: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/7.jpg)
Algorithmic FoundationsCOMP108
7
(Divide & Conquer)
Divide and ConquerOne of the best-known algorithm design
techniques
Idea: A problem instance is divided into several
smaller instances of the same problem, ideally of about same size
The smaller instances are solved, typically recursively
The solutions for the smaller instances are combined to get a solution to the large instance
![Page 8: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/8.jpg)
Algorithmic FoundationsCOMP108
Merge Sort …
![Page 9: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/9.jpg)
Algorithmic FoundationsCOMP108
9
(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
![Page 10: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/10.jpg)
Algorithmic FoundationsCOMP108
10
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
we want to sort these 8 numbers,divide them into two halves
![Page 11: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/11.jpg)
Algorithmic FoundationsCOMP108
11
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
divide these 4 numbers into
halves
similarly for these 4
![Page 12: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/12.jpg)
Algorithmic FoundationsCOMP108
12
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
further divide each shorter sequence …until we get sequence with only 1 number
![Page 13: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/13.jpg)
Algorithmic FoundationsCOMP108
13
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
merge pairs of single number
into a sequence of 2 sorted numbers
![Page 14: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/14.jpg)
Algorithmic FoundationsCOMP108
14
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
13, 51 10, 64 5, 34 21, 32
then merge again into sequences of 4 sorted numbers
![Page 15: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/15.jpg)
Algorithmic FoundationsCOMP108
15
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
13, 51 10, 64 5, 34 21, 32
10, 13, 51, 64 5, 21, 32, 34
one more merge give the final sorted sequence
![Page 16: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/16.jpg)
Algorithmic FoundationsCOMP108
16
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
13, 51 10, 64 5, 34 21, 32
5, 10, 13, 21, 32, 34, 51, 64
10, 13, 51, 64 5, 21, 32, 34
![Page 17: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/17.jpg)
Algorithmic FoundationsCOMP108
17
(Divide & Conquer)
Summary
Divide dividing a sequence of n numbers into two
smaller sequences is straightforward
Conquer merging two sorted sequences of total
length n can also be done easily, at most n-1 comparisons
![Page 18: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/18.jpg)
Algorithmic FoundationsCOMP108
18
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
To merge two sorted sequences,we keep two pointers, one to each sequence
Result:
Compare the two numbers pointed,copy the smaller one to the result
and advance the corresponding pointer
![Page 19: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/19.jpg)
Algorithmic FoundationsCOMP108
19
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
Then compare again the two numberspointed to by the pointer;
copy the smaller one to the resultand advance that pointer
5, Result:
![Page 20: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/20.jpg)
Algorithmic FoundationsCOMP108
20
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
Repeat the same process …
5, 10, Result:
![Page 21: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/21.jpg)
Algorithmic FoundationsCOMP108
21
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
Again …
5, 10, 13 Result:
![Page 22: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/22.jpg)
Algorithmic FoundationsCOMP108
22
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
and again …
5, 10, 13, 21 Result:
![Page 23: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/23.jpg)
Algorithmic FoundationsCOMP108
23
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
…
5, 10, 13, 21, 32Result:
![Page 24: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/24.jpg)
Algorithmic FoundationsCOMP108
24
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
When we reach the end of one sequence,simply copy the remaining numbers in the other
sequence to the result
5, 10, 13, 21, 32, 34 Result:
![Page 25: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/25.jpg)
Algorithmic FoundationsCOMP108
25
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34
Then we obtain the final sorted sequence
5, 10, 13, 21, 32, 34, 51, 64 Result:
![Page 26: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/26.jpg)
Algorithmic FoundationsCOMP108
26
(Divide & Conquer)
Pseudo codeAlgorithm Mergesort(A[1..n])
if n > 1 then begincopy 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
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
![Page 27: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/27.jpg)
Algorithmic FoundationsCOMP108
27
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
13, 51 10, 64 5, 34 21, 32
5, 10, 13, 21, 32, 34, 51, 64
10, 13, 51, 64 5, 21, 32, 34
MS( )
MS(
MS(
MS(
MS( )
) MS( ) MS( ) MS( )
)MS( ) MS( )MS( ) MS( )MS( ) MS( )MS( )
M( ), M( ),
M( , )
)
![Page 28: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/28.jpg)
Algorithmic FoundationsCOMP108
)
28
(Divide & Conquer)
51, 13, 10, 64, 34, 5, 32, 21
51, 13, 10, 64 34, 5, 32, 21
51, 13 10, 64 34, 5 32, 21
51 13 10 64 34 5 32 21
13, 51 10, 64 5, 34 21, 32
5, 10, 13, 21, 32, 34, 51, 64
10, 13, 51, 64 5, 21, 32, 34
MS( )
MS(
MS(
MS(
MS( )
) MS( ) MS( ) MS( )
)MS( ) MS( )MS( ) MS( )MS( ) MS( )MS( )
M( ), M( ),
M( , )
1
2
3 4
5
6
7 8
9
10
11
12
13 14
15
16
17 18
19
20
21
order of execution
![Page 29: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/29.jpg)
Algorithmic FoundationsCOMP108
29
(Divide & Conquer)
Pseudo codeAlgorithm Merge(B[1..p], C[1..q], A[1..p+q])
set i=1, j=1, k=1
while i<=p and j<=q do
begin
if B[i]C[j] thenset A[k] = B[i] and i = i+1
else set A[k] = C[j] and j = j+1
k = k+1
end
if i==p+1 then copy C[j..q] to A[k..(p+q)]
else copy B[i..p] to A[k..(p+q)]
Algorithm Merge(B[1..p], C[1..q], A[1..p+q])
set i=1, j=1, k=1
while i<=p and j<=q do
begin
if B[i]C[j] thenset A[k] = B[i] and i = i+1
else set A[k] = C[j] and j = j+1
k = k+1
end
if i==p+1 then copy C[j..q] to A[k..(p+q)]
else copy B[i..p] to A[k..(p+q)]
![Page 30: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/30.jpg)
Algorithmic FoundationsCOMP108
30
(Divide & Conquer)
10, 13, 51, 64 5, 21, 32, 34B: C:
p=4 q=4
i j k A[ ]
Before loop 1 1 1 empty
End of 1st iteration 1 2 2 5
End of 2nd
iteration
2 2 3 5, 10
End of 3rd 3 2 4 5, 10, 13
End of 4th 3 3 5 5, 10, 13, 21
End of 5th 3 4 6 5, 10, 13, 21, 32
End of 6th 3 5 7 5, 10, 13, 21, 32, 34
5, 10, 13, 21, 32, 34, 51,
64
![Page 31: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/31.jpg)
Algorithmic FoundationsCOMP108
31
(Divide & Conquer)
Time complexityLet T(n) denote the time complexity of running merge sort on n numbers.
Let T(n) denote the time complexity of running merge sort on n numbers.
1 if n=12T(n/2) + notherwise
T(n) =
We call this formula a recurrence.
A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs.
To solve a recurrence is to derive asymptotic bounds on the solution
![Page 32: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/32.jpg)
Algorithmic FoundationsCOMP108
32
(Divide & Conquer)
Time complexityProve that is O(n log n)
Make a guess: T(n) 2 n log n (We prove by MI)
For the base case when n=2,L.H.S = T(2) = 2T(1) + 2 = 4,R.H.S = 2 2 log 2 = 4L.H.S R.H.S
For the base case when n=2,L.H.S = T(2) = 2T(1) + 2 = 4,R.H.S = 2 2 log 2 = 4L.H.S R.H.S
1 if n=1
2T(n/2) + notherwise
T(n) =
Substitution method
![Page 33: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/33.jpg)
Algorithmic FoundationsCOMP108
33
(Divide & Conquer)
Time complexityProve that is O(n log n)
Make a guess: T(n) 2 n log n (We prove by MI)
Assume true for all n'<n [assume T(n/2) 2 (n/2) log(n/2)]
T(n)= 2T(n/2)+n
2 (2(n/2)xlog(n/2)) + n
= 2 n (log n - 1) + n
= 2 n log n - 2n + n
2 n log n
i.e., T(n) 2 n log n
1 if n=1
2T(n/2) + notherwise
T(n) =
by hypothesis
![Page 34: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/34.jpg)
Algorithmic FoundationsCOMP108
34
(Divide & Conquer)
Example
Guess: T(n) 2 log n
1 if n=1
T(n/2) + 1 otherwiseT(n) =
For the base case when n=2,
L.H.S = T(2) = T(1) + 1 = 2
R.H.S = 2 log 2 = 2
L.H.S R.H.S
![Page 35: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/35.jpg)
Algorithmic FoundationsCOMP108
35
(Divide & Conquer)
Example
Guess: T(n) 2 log n
Assume true for all n' < n [assume T(n/2) 2 x log (n/2)]
T(n) = T(n/2) + 1
2 x log(n/2) + 1 by hypothesis
= 2x(log n – 1) + 1 log(n/2) = log n – log 2
< 2log n
1 if n=1
T(n/2) + 1 otherwiseT(n) =
i.e., T(n) 2 log n
![Page 36: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/36.jpg)
Algorithmic FoundationsCOMP108
36
(Divide & Conquer)
More exampleProve that is O(n)
Guess: T(n) 2n – 1
For the base case when n=1,L.H.S = T(1) = 1R.H.S = 21 - 1 = 1 L.H.S R.H.S
1 if n=1
2T(n/2) + 1otherwise
T(n) =
![Page 37: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/37.jpg)
Algorithmic FoundationsCOMP108
37
(Divide & Conquer)
More exampleProve that is O(n)
Guess: T(n) 2n – 1
Assume true for all n' < n [assume T(n/2) 2(n/2)-
1]
T(n) = 2T(n/2)+1
2 (2(n/2)-1) + 1 by hypothesis
=2n – 2 + 1
=2n - 1i.e., T(n) 2n-1
1 if n=1
2T(n/2) + 1otherwise
T(n) =
![Page 38: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/38.jpg)
Algorithmic FoundationsCOMP108
38
(Divide & Conquer)
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) = 2T(n/2)+1 T(n) is O(n)
T(n) = 2T(n/2)+n T(n) is O(n log n)
![Page 39: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/39.jpg)
Algorithmic FoundationsCOMP108
Tower of Hanoi …
![Page 40: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/40.jpg)
Algorithmic FoundationsCOMP108
40
(Divide & Conquer)
Tower of Hanoi - Initial config
There are three pegs and some discs of different sizes are on Peg A
321
A B C
![Page 41: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/41.jpg)
Algorithmic FoundationsCOMP108
41
(Divide & Conquer)
Tower of Hanoi - Final config
Want to move the discs to Peg C
321
A B C
![Page 42: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/42.jpg)
Algorithmic FoundationsCOMP108
42
(Divide & Conquer)
Tower of Hanoi - Rules
Only 1 disk can be moved at a time
A disc cannot be placed on top of other discs that are smaller than it
32
Target: Use the smallest number of moves
![Page 43: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/43.jpg)
Algorithmic FoundationsCOMP108
43
(Divide & Conquer)
Tower of Hanoi - One disc only
Easy!
1A B C
![Page 44: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/44.jpg)
Algorithmic FoundationsCOMP108
44
(Divide & Conquer)
Tower of Hanoi - One disc only
Easy! Need one move only.
1A B C
![Page 45: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/45.jpg)
Algorithmic FoundationsCOMP108
45
(Divide & Conquer)
Tower of Hanoi - Two discs
We first need to move Disc-2 to C, How?
21
A B C
by moving Disc-1 to B first, then Disc-2 to C
![Page 46: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/46.jpg)
Algorithmic FoundationsCOMP108
46
(Divide & Conquer)
Tower of Hanoi - Two discs
Next?
2A B C
1
Move Disc-1 to C
![Page 47: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/47.jpg)
Algorithmic FoundationsCOMP108
47
(Divide & Conquer)
Tower of Hanoi - Two discs
Done!
21
A B C
![Page 48: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/48.jpg)
Algorithmic FoundationsCOMP108
48
(Divide & Conquer)
Tower of Hanoi - Three discs
We first need to move Disc-3 to C, How? Move Disc-1&2 to B (recursively)
321
A B C
![Page 49: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/49.jpg)
Algorithmic FoundationsCOMP108
49
(Divide & Conquer)
Tower of Hanoi - Three discs
We first need to move Disc-3 to C, How? Move Disc-1&2 to B (recursively)
3 2A B C
1
Then move Disc-3 to C
![Page 50: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/50.jpg)
Algorithmic FoundationsCOMP108
50
(Divide & Conquer)
Tower of Hanoi - Three discs
Only task left: move Disc-1&2 to C (similarly as before)
321
A B C
![Page 51: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/51.jpg)
Algorithmic FoundationsCOMP108
51
(Divide & Conquer)
Tower of Hanoi - Three discs
Only task left: move Disc-1&2 to C (similarly as before)
32A B C1
![Page 52: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/52.jpg)
Algorithmic FoundationsCOMP108
52
(Divide & Conquer)
Tower of Hanoi - Three discs
Done!
321
A B C
![Page 53: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/53.jpg)
Algorithmic FoundationsCOMP108
Tower of HanoiToH(num_disc, source, dest, spare)
begin
if (num_disc > 1) then
ToH(num_disc-1, source, spare, dest)
Move the disc from source to dest
if (num_disc > 1) then
ToH(num_disc-1, spare, dest, source)
end
53
(Divide & Conquer)
invoke by callingToH(3, A, C, B)
![Page 54: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/54.jpg)
Algorithmic FoundationsCOMP108
54
(Divide & Conquer)
ToH(3, A, C, B)
move 1 discfrom A to C
ToH(2, A, B, C) ToH(2, B, C, A)
ToH(1, A, C, B) ToH(1, C, B, A)
move 1 discfrom A to B
move 1 discfrom A to C
move 1 discfrom C to B
ToH(1, B, A, C) ToH(1, A, C, B)
move 1 discfrom B to C
move 1 discfrom B to A
move 1 discfrom A to C
![Page 55: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/55.jpg)
Algorithmic FoundationsCOMP108
55
(Divide & Conquer)
ToH(3, A, C, B)
move 1 discfrom A to C
ToH(2, A, B, C) ToH(2, B, C, A)
ToH(1, A, C, B) ToH(1, C, B, A)
move 1 discfrom A to B
move 1 discfrom A to C
move 1 discfrom C to B
ToH(1, B, A, C) ToH(1, A, C, B)
move 1 discfrom B to C
move 1 discfrom B to A
move 1 discfrom A to C
1
2
3
45
6
7 8
9
10
1112
13
from A to C; from A to B; from C to B;from A to C;
from B to A; from B to C; from A to C;
![Page 56: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/56.jpg)
Algorithmic FoundationsCOMP108
56
(Divide & Conquer)
move n-1 discs from
B to C
Time complexity
T(n) = T(n-1) + 1 +T(n-1)
Let T(n) denote the time complexity of running the Tower of Hanoi algorithm on n discs.
Let T(n) denote the time complexity of running the Tower of Hanoi algorithm on n discs.
1 if n=1
2T(n-1) + 1 otherwise
move n-1 discs from
A to Bmove Disc-n from A to C
T(n) =
![Page 57: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/57.jpg)
Algorithmic FoundationsCOMP108
57
(Divide & Conquer)
Time complexity (2)T(n) = 2T(n-1) + 1
= 2[2T(n-2) + 1] + 1
= 22 T(n-2) + 2 + 1
= 22 [2T(n-3) + 1] + 21 + 20
= 23 T(n-3) + 22 + 21 + 20
…= 2k T(n-k) + 2k-1 + 2k-2 + … + 22 + 21 + 20
…= 2n-1 T(1) + 2n-2 + 2n-3 + … + 22 + 21 + 20
= 2n-1 + 2n-2 + 2n-3 + … + 22 + 21 + 20
= 2n-1
1 if n=1
2T(n-1) + 1otherwise
T(n) =
In Tutorial 2, we prove by MI that20 + 21 + … + 2n-1 = 2n-1
In Tutorial 2, we prove by MI that20 + 21 + … + 2n-1 = 2n-1
i.e., T(n) is O(2n)iterative method
![Page 58: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/58.jpg)
Algorithmic FoundationsCOMP108
58
(Divide & Conquer)
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) = 2T(n/2)+1 T(n) is O(n)
T(n) = 2T(n/2)+n T(n) is O(n log n)
T(n) = 2T(n-1)+1 T(n) is O(2n)
![Page 59: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/59.jpg)
Algorithmic FoundationsCOMP108
Fibonacci number …
![Page 60: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/60.jpg)
Algorithmic FoundationsCOMP108
Fibonacci's Rabbits
60
(Divide & Conquer)
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.
end ofmonth-0
end ofmonth-1
end ofmonth-3
end ofmonth-4
How many at end of
month-5, 6,7and so on?
end ofmonth-2
![Page 61: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/61.jpg)
Algorithmic FoundationsCOMP108
Petals on flowers
61
(Divide & Conquer)
1 petal:white calla lily
2 petals:euphorbia
3 petals:trillium
5 petals:columbine
8 petals:bloodroot
13 petals:black-eyed susan
21 petals:shasta daisy
34 petals:field daisy
Search: Fibonacci Numbers in Nature
![Page 62: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/62.jpg)
Algorithmic FoundationsCOMP108
62
(Divide & Conquer)
Fibonacci numberFibonacci number F(n)
F(n) = 1 if n = 0 or 1F(n-1) + F(n-2) if n > 1
n 0 1 2 3 4 5 6 7 8 9 10
F(n) 1 1 2 3 5 8 13 21 34 55 89
Pseudo code for the recursive algorithm:Algorithm F(n)
if n==0 or n==1 thenreturn 1
elsereturn F(n-1) + F(n-2)
Pseudo code for the recursive algorithm:Algorithm F(n)
if n==0 or n==1 thenreturn 1
elsereturn F(n-1) + F(n-2)
![Page 63: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/63.jpg)
Algorithmic FoundationsCOMP108
63
(Divide & Conquer)
The execution of F(7)
F7
F6
F5
F5
F4 F3
F3
F2
F1 F0
F2
F1 F0
F1
F2
F1 F0
F2
F1 F0
F2
F1 F0
F4 F4
F3
F3
F3
F2
F1 F0
F2
F1 F0
F2
F1 F0
F1
F1 F1
F1
![Page 64: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/64.jpg)
Algorithmic FoundationsCOMP108
64
(Divide & Conquer)
The execution of F(7)
F7
F6
F5
F5
F4 F3
F3
F2
F1 F0
F2
F1 F0
F1
F2
F1 F0
F2
F1 F0
F2
F1 F0
F4 F4
F3
F3
F3
F2
F1 F0
F2
F1 F0
F2
F1 F0
F1
F1 F1
F1
12
3
4
5
6
78
9
10
13
18
27
order of execution(not everything shown)
![Page 65: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/65.jpg)
Algorithmic FoundationsCOMP108
65
(Divide & Conquer)
The execution of F(7)
F7
F6
F5
F5
F4 F3
F3
F2
F1 F0
F2
F1 F0
F1
F2
F1 F0
F2
F1 F0
F2
F1 F0
F4 F4
F3
F3
F3
F2
F1 F0
F2
F1 F0
F2
F1 F0
F1
F1 F1
F1
return value(not everything shown)
1 1
2
3
1
2
5
8
3
5
13 8
21
![Page 66: COMP108 Algorithmic Foundations Divide and Conquer](https://reader036.vdocument.in/reader036/viewer/2022062519/568151ee550346895dc02800/html5/thumbnails/66.jpg)
Algorithmic FoundationsCOMP108
66
(Dynamic Programming)
Time complexity - exponentialf(n) = f(n-1) + f(n-2) + 1
= [f(n-2)+f(n-3)+1] + f(n-2) + 1
> 2 f(n-2)
> 2 [2f(n-2-2)] = 22 f(n-4)
> 22 [2f(n-4-2)] = 23 f(n-6)
> 23 [2f(n-6-2)] = 24 f(n-8)
…
> 2k f(n-2k)If n is even, f(n) > 2n/2 f(0) = 2n/2
If n is odd, f(n) > f(n-1) > 2(n-1)/2
exponential in n
Suppose f(n) denote the
time complexity to compute F(n)