Design and Analysis of Algorithms
Design and Analysis of Algorithms
Algorithms
Analysis of Recursive Algorithms
Analyzing the Time Efficiency of
Recursive Algorithms
*.* 2
Recursive Function
• Compute the factorial • ALGORITHM F(n)
function F(n) = n! for an
arbitrary nonnegative • //Input: A nonnegative
integer n. Since integer n
n! = 1 . . . . . (n − • //Output: The value of
1) . n n!
= (n − 1)! . n for n ≥ 1 • if n = 0 return 1
and 0! = 1 by
definition,
• else return F(n − 1) ∗
n
we can compute F(n) =
F(n − 1) . n with the
following recursive
*.* algorithm. 3
Factorial 𝑇 𝑛 = 𝑇 𝑛 − 1 + 1
• 𝑇𝑛 𝑛−1 + • By Substitution
=𝑇 1
+1 • 𝑇 𝑛 =𝑇𝑛−2 +1+1
• 𝑇𝑛−1 =𝑇𝑛−2
• 𝑇𝑛−2 =𝑇𝑛−3 +1 • 𝑇 𝑛 =𝑇𝑛−3 +3
• 𝑇𝑛−3 =𝑇𝑛−4 +1 • 𝑇 𝑛 =𝑇𝑛−4 +4
• 𝑇𝑛−4 =𝑇𝑛−5 +1 • 𝑇 𝑛 =𝑇𝑛−5 +5
• …
• 𝑇 𝑛 =𝑇𝑛−𝑖 +𝑖
*.* 4
Factorial 𝑇 𝑛 = 𝑇 𝑛 − 1 + 1
*.* 5
Digits
• The following algorithm • ALGORITHM BinRec(n)
finds the number of binary
digits in the binary • //Input: A positive decimal
representation of a positive integer n
decimal integer. • //Output: The number of
binary digits in n’s binary
representation
• if
• n = 1 return 1
• else
• return BinRec(n/2) + 1
*.* 6
Complexity
• BinRec(n) • 𝑇(𝑛) = 𝑇
𝑛
+1
2
• if � �
• n = 1 return 1
• 𝑇 �
=𝑇 �
+1
• else • 𝑇 2 =𝑇 4 +1
• return BinRec(n/2) + 1 � �
• 𝑇 � =𝑇 � +1
𝑛
• 𝑇 4 =𝑇 8
32
+1
� 𝑛
*.* 7
�
16
Complexity
• 𝑇(𝑛) = 𝑇
𝑛
+1
• By Substitution
2
𝑛
� � • 𝑇(𝑛) = 𝑇 +2
• 𝑇 �
=𝑇 �
+1 4
�
• 𝑇𝑛 = �𝑇 + 3
• 𝑇 2 =𝑇 4 +1
� � • 𝑇𝑛 = 8𝑇 + 4
• 𝑇 � =𝑇 � +1
𝑛
𝑛 • 𝑇𝑛 =𝑇 +5
• 𝑇 4 =𝑇 8 +1 16
� 𝑛32 • …
𝑛
� 𝑛
16 • 𝑇𝑛 =𝑇
𝑖
+ log 𝑖
32
8
*.* 8
𝑛
16
Complexity
𝑛 𝑛
• 𝑇 𝑛 =𝑇 𝑖
+ log 𝑖 • 𝑇 𝑛 =𝑇 𝑛
+
log 𝑛
• For 𝑇
𝑛
=𝑇 1 • 𝑇 𝑛 = 1 + log 𝑛
𝑖
• 𝑇 𝑛 = log 𝑛 + 1 =
• 𝑖=𝑛 Θ(log 𝑛)
*.* 9
Merge Sort Algorithm
MergeSort(A, i, j) 𝑛
• 𝑇𝑛 =
2
+𝑛
if j > i then 2𝑇
�
mid ← (i + j)/2
• 𝑇 𝑛/2 = �
+ 𝑛/2
2𝑇
MergeSort(A, i, • 𝑇 𝑛/4 = 4 + 𝑛/4
mid ) 2𝑇 𝑛
• 𝑇 𝑛/8 = 8 + 𝑛/8
MergeSort(A, mid + 1, j )
2𝑇 𝑛𝑛 𝑛
Merge(A, i, mid, j ) + 16
32
• 𝑇 𝑛/16 = 16
2𝑇
*.* 10
Telescoping Sum
• 𝑇𝑛
�
+𝑛
• Sum of equations
�
= 2𝑇 𝑛 𝑛 𝑛
� • 𝑇 𝑛 + 2𝑇 + 4𝑇 + 8𝑇 +
• 2𝑇 𝑛/2 = 2𝑇
2
�
+ 2 ∗ 𝑛/2 𝑛
2
�
4
�
8
⋯ + 2𝑇 𝑛 = 2𝑇 �
+ 4𝑇 �
+
2
• 4𝑇 𝑛/4 = 2𝑇 4 + 4 ∗ 𝑛/4 𝑛
𝑛
+ 𝑛 + 𝑛4 +
�
8𝑇 +⋯+ 2
𝑛 𝑇+ ⋯ + 𝑛
𝑛
• 8𝑇 𝑛/8 � + 8 ∗ 𝑛/8 𝑛
8 2 𝑛
= 2𝑇 • 𝑇 𝑛 = 𝑛𝑇 1 + 𝑛 + 𝑛 + 𝑛 + ⋯
𝑛
• 16𝑇 𝑛/16 8 𝑛
+ 16 ∗ 16 +𝑛
= 2𝑇 𝑛
32 • 𝑇𝑛 = 𝑛 log 𝑛
• … 𝑛 16 𝑛 𝑛
𝑛 � = 𝑛𝑇 +2( )
2 𝑇 𝑛
• 𝑛
� �
�
2
*.* 2 11
Iterative Substitution
𝑛 • By Substitution
• 𝑇𝑛 =
2
+𝑛
𝑛
2𝑇 • 𝑇𝑛 = 4𝑇 𝑛
4 +𝑛+2
�
• 𝑇 𝑛/2 = �
+ 𝑛/2 �
2𝑇 • 𝑇𝑛 = 8𝑇
�
+𝑛+𝑛+𝑛
• 𝑇 𝑛/4 = 4 + 𝑛/4 𝑛
• 𝑇𝑛 = 8 + 4𝑛
2𝑇 𝑛 16𝑇
• 𝑇 𝑛/8 = 8 + 𝑛/8
16
+ 5𝑛
𝑛
2𝑇 𝑛𝑛 • 𝑇𝑛 =
𝑛 32𝑇 32
• 𝑇 𝑛/16 = + 16 𝑛
+ log 𝑖
1632 • … 𝑖
2𝑇 (𝑛)
*.*
• 𝑇𝑛 = 𝑖𝑇 12
Iterative Substitution
𝑛 𝑛
• 𝑇𝑛 =
𝑖
+ log 𝑖 • 𝑇 𝑛 = 𝑖𝑇 +
𝑖𝑇 (𝑛) 𝑖
=𝑇1 log 𝑖
• For 𝑇 𝑛/𝑖 (𝑛)
• 𝑇 𝑛 = 𝑛𝑇 1 +
• 𝑖=𝑛 log 𝑛
• (𝑛)
𝑇 𝑛 = 𝑛 + 𝑛 log 𝑛
• 𝑇 𝑛 = 𝑛 log 𝑛 + 𝑛 =
Θ(𝑛 log
𝑛)
*.* 13
Quick Sort
Algorithm
QuickSort(A, left, right) { • Best Case
if (right > left) then { • 𝑇𝑛
�
= 2𝑇� + 𝑛
pivot = Partition(A,
left, right); • Worst Case
2
QuickSort(A, left, pivot- • 𝑇𝑛 =𝑇 𝑛 −1 +
1); 𝑇 1 +𝑛 ≅T n−1
pivot+1,
+n
Q uickSort(A
, right);
}
}
*.* 14
Tower of Hanoi
puzzle
• The game starts by having few discs stacked in
increasing order of size. The number of discs can
vary, but there are only three pegs.
*.* 15
Tower of Hanoi
puzzle
*.* 16
Tower of Hanoi
puzzle
Recursive Solution for the Tower of Hanoi with algorithm
*.* 17
Tower of Hanoi
puzzle
• M(n) = M(n − 1) + 1+ M(n − 1) for n > 1.
• With the obvious initial condition M(1) = 1, we have
the following recurrence relation for the number of
moves M(n):
• M(n) = 2M(n − 1) + 1 for n > 1,
• M(1) = 1.
• We solve this recurrence by the same method of
backward substitutions:
*.* 18
Tower of Hanoi
puzzle
• 𝑇 𝑛 = 2𝑇 𝑛 − 1 + 1
• 𝑇 𝑛 = 2𝑛
*.* 19