Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

• Divide and Conquer approach

• Recursion
Example
Asymptotic analysis for recurrence relation
 Recursive Tree Method
 Substitution Method,
 Master theorem
• Merge Sort Analysis
 Recursive Tree Method
 Substitution Method,
 Master Theorem and

13/03/2023 Dr.S.Abinaya-BCSE202L 1
Divide and Conquer approach

• Divide the problem into a number of subproblems that are smaller


instances of the same problem.
• Conquer the subproblems by solving them recursively. If the
subproblem sizes are small enough, however, just solve the
subproblems in a straightforward manner.
• Combine the solutions to the subproblems into the solution for the
original problem.

13/03/2023 Dr.S.Abinaya-BCSE202L 2
Recursion
• Any function which calls itself is
called recursive.
• The case, where the function does not
recur, is called the base case.
• The case, where the function calls
itself to perform a subtask, is referred
to as the recursive case.

13/03/2023 Dr.S.Abinaya-BCSE202L 3
Recursion
• Analysis of recursive algorithms
• Asymptotic analysis for recurrence relation
 Recursion Tree Method
 Substitution Method,
 Master Theorem and

13/03/2023 Dr.S.Abinaya-BCSE202L 4
Recursion Tree Method
• A recursion tree models the costs (time) of a recursive execution of an
algorithm.
• The recursion-tree method promotes intuition, however.
• The recursion tree method is good for generating guesses for the substitution
method

13/03/2023 Dr.S.Abinaya-BCSE202L 5
Example: Recursion
//Display the elements disp(5)
#include<stdio.h> Recursion Tree
int disp(int num)
{
5 disp(4)
if(num>0)
{
printf("%d\n",num);
disp(num-1); 4 disp(3)
}
}
int main() 3 disp(2)
{
int x;
2 disp(1)
printf("Enter the number:");
scanf("%d",&x);
The whole block executed for n+1 calls
disp(x);
T(n) = n+1 1 disp(0)
return 0; = O(n)
}

13/03/2023 Dr.S.Abinaya-BCSE202L 6
Substitution method
•The most general method:
1. Guess the form of the solution.
2. Verify by induction.
3. Solve for constants

13/03/2023 Dr.S.Abinaya-BCSE202L 7
Example: Substitution method
int disp(int num) T(n) = T(n-1) + 1 , T(n-1) = ?

{ T(n-1) = T(n-1-1) + 1
1
if(num>0) = T(n-2) + 1

Sub T(n-1) in T(n)


{ 1
T(n-1) T(n) = [T(n-2)+1] + 1
printf("%d\n",num);
= T(n-2)+2 , now T(n-2) = ?
disp(num-1);
T(n-2) = T(n-2-1) + 1 (by substituting n-2 in T(n) above)
} = T(n-3) + 1 T(n) = T(n-1) + 1
} Sub T(n-2) in T(n) = T(n-2) + 2
= T(n-3) + 3
T(n) = T(n) = T(n-1)+2
T(n) = [T(n-3)+1]+2
= T(n-1) +1
= T(n-3)+3 = T(n-k) + k

13/03/2023 Dr.S.Abinaya-BCSE202L 8
Substitution method- Contd.,
T(n) =

T(n) = T(n-1) + 1 n–k=0


= T(n-2) + 2 n=k
= T(n-3) + 3 Sub k = n in T(n-k) + k

= T(n – n) + n
= T(n-k) + k = T(0) + n
=1 + n
= O(n)

13/03/2023 Dr.S.Abinaya-BCSE202L 9
Example : Factorial
int find_fact(int n)
{
//Factorial of 0 is 1
if(n==0) //Base Condition
return(1);

//Function calling itself: recursion


return(n*find_fact(n-1)); //Recursive condition
}

13/03/2023 Dr.S.Abinaya-BCSE202L 10
Solve it Yourself?
Recurrence relation – Factorial?

13/03/2023 Dr.S.Abinaya-BCSE202L 11
The master method
•The mastermethod appliestorecurrencesof theform
• T(n)= aT(n/b)+f(n) ,
wherea1,b>1,and f is asymptotically positive

Check the following recurrence relation can be solved using master method?
×

T (n) = 4T (n / 2) + n 

13/03/2023 Dr.S.Abinaya-BCSE202L 12
Merge Sort Analysis

13/03/2023 Dr.S.Abinaya-BCSE202L 13
Merge Sort
• The operation of sorting is closely related to process of merging
• Merge Sort is a divide and conquer algorithm
• It is based on the idea of breaking down a list into several sub-lists until each
sub list consists of a single element
• Merging those sub lists in a manner that results into a sorted list

13/03/2023 Dr.S.Abinaya-BCSE202L 14
BCSE202L - DSA

Module 1
Algorithm Analysis
Dr Hasmath Farhana
Assistant Professor

hasmath.farhana@vit.ac.in
Algorithm
MERGE-SORT A[1 . . n] MergeSort(A, l, h )
1. If n = 1, done.
if(l < h)
2. Recursively sort A[ 1 . . n/2 ] and
A[ n/2+1 . . n ] .
mid = l+h /2; (floor division)
3. “Merge” the 2 sorted lists MergeSort(A, l, mid)
MergeSort(A, mid+1, h)
1 2 3 4 5 6 7 8 Merge(A, l, mid, h)

l h

mid = 1+8 /2 = 4.5 = 4


13/03/2023 Dr.S.Abinaya-BCSE202L 16
Recurrence Relation
MergeSort(A, l, h )
if(l < h) 1

mid = l+h /2; (floor division) 1

MergeSort(A, l, mid) T(n/2)

MergeSort(A, mid+1, h) T(n/2)

Merge(A, l, mid, h) ?

T(n) = 2T(n/2) + ? (Time to Merge)


Time to divide

13/03/2023 Dr.S.Abinaya-BCSE202L 17
Merge subroutine
Merge(A, lb, mid, ub){ 1 2 3 4 5 6 7 8
i = lb Original 1 13 16 24 12 15 27 38
j = mid + 1
lb ub
k = lb
mid
while(i<=mid && j<=ub) { =4
if(a[i]<=a[j]) {
b[k]=a[i]; i++; }
else {
b[k]=a[j]; j++; }
k++; }
if (i>mid){
while(j<=ub){
b[k]=a[j]; j++; k++; }
else {
while(i<=mid){
b[k]=a[i]; i++; k++; }
}
13/03/2023 Dr.S.Abinaya-BCSE202L 18
Recurrence Relation
MergeSort(A, l, h )
if(l < h) 1

mid = l+h /2; (floor division) 1

MergeSort(A, l, mid) T(n/2)

MergeSort(A, mid+1, h) T(n/2)

Merge(A, l, mid, h) O(n)

T(n) = 2T(n/2) + O(n)


Time to divide

T(n) = 2T(n/2) + cn
13/03/2023 Dr.S.Abinaya-BCSE202L 19
Recurrence for merge sort
(1) if n = 1;
T(n) =
2T(n/2) + (n) if n >
1.

13/03/2023 Dr.S.Abinaya-BCSE202L 20
Recursion Tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

13/03/2023 Dr.S.Abinaya-BCSE202L 21
Substitution method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant

1 if n = 1;
T(n) =
2T(n/2) + n if n > 1.

13/03/2023 Dr.S.Abinaya-BCSE202L 22
Master Theorem

03/13/2023 Dr.S.Abinaya-BCSE202L 23
Master Method

13/03/2023 Dr.S.Abinaya-BCSE202L 24
Master Method

13/03/2023 Dr.S.Abinaya-BCSE202L 25
The Master Method

13/03/2023 Dr.S.Abinaya-BCSE202L 26
Merge-Sort Tree
 An execution of merge-sort is depicted by a binary tree
– each node represents a recursive call of merge-sort and stores
 unsorted sequence before the execution and its partition
 sorted sequence at the end of the execution
– the root is the initial call
– the leaves are calls on subsequences of size 0 or 1

7 2  9 4  2 4 7 9

7  2  2 7 9  4  4 9

77 22 99 44


13/03/2023 Dr.S.Abinaya-BCSE202L 27
Execution Example
 Partition
7 2 9 43 8 6 1

13/03/2023 Dr.S.Abinaya-BCSE202L 28
Execution Example (cont.)

 Recursive call, partition


7 2 9 43 8 6 1

7 29 4

13/03/2023 Dr.S.Abinaya-BCSE202L 29
Execution Example (cont.)
 Recursive call, partition
7 2 9 43 8 6 1

7 29 4

72

13/03/2023 Dr.S.Abinaya-BCSE202L 30
Execution Example (cont.)
 Recursive call, base case
7 2 9 43 8 6 1

7 29 4

72

77

13/03/2023 Dr.S.Abinaya-BCSE202L 31
Execution Example (cont.)
 Recursive call, base case
7 2 9 43 8 6 1

7 29 4

72

77 22

13/03/2023 Dr.S.Abinaya-BCSE202L 32
Execution Example (cont.)

 Merge
7 2 9 43 8 6 1

7 29 4

722 7

77 22

13/03/2023 Dr.S.Abinaya-BCSE202L 33
Execution Example (cont.)
 Recursive call, …, base case, merge
7 2 9 43 8 6 1

7 29 4

722 7 9 4  4 9

77 22 99 44

13/03/2023 Dr.S.Abinaya-BCSE202L 34
Execution Example (cont.)
 Merge
7 2 9 43 8 6 1

7 29 4 2 4 7 9

722 7 9 4  4 9

77 22 99 44

13/03/2023 Dr.S.Abinaya-BCSE202L 35
Execution Example (cont.)
 Recursive call, …, merge, merge
7 2 9 43 8 6 1

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

13/03/2023 Dr.S.Abinaya-BCSE202L 36
Execution Example (cont.)

 Merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

13/03/2023 Dr.S.Abinaya-BCSE202L 37

You might also like