0% found this document useful (0 votes)
7 views

2a Recursive Functions I

Uploaded by

ssmukherjee2013
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2a Recursive Functions I

Uploaded by

ssmukherjee2013
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Recursive

Functions I
Sample Algorithm 1
Input: An array, list, containing n elements
Output: Sorted Array in ascending order
Associated Structure: None
Begin Proc
Repeat for i = 1 to N Step 1
Set current := i
Repeat for location = i to N step 1
If list [location] < list [current]
// min element identified
Set current := location
End if
End Repeat
If ( i <> current)// if swap required
swap ( list [i], list [current] )
End if
End Repeat
End
2
Growth Factors for Algorithm 1

 Contains2 nested loops


 Each running for n elements
 Growth Factor: O(n2)

3
Sample Procedure 2

Begin MergeSort (arr, first, last)


If first < last then
Set middle = ( first + last ) / 2
MergeSort (list, first, middle)
MergeSort (list, middle + 1, last)
MergeLists (list, first, middle, middle + 1, last)
End if
End

4
Growth Factor for MergeSort

 Recursive Procedure
 Starts withn elements
 Reduces array by half in each recursion
 Makes two recursive calls each time
 Processes all elements once each time

5
Analysis of Recursive Procedures

 Mathematical model of Merge Sort:


T(n) = 2T(n/2) + cn
 T(n)
stands for operations carried out
by MergeSort with n elements:
• Procedure calls itself twice, each time
with n/2 elements
• Also calls MergeList, which performs c*n
number of operations, c stands for steps
in the procedure
6
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

7
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

T(n/2) T(n/2) c(n/2+n/2)

8
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

T(n/2) T(n/2) c(n/2+n/2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4*n/4)

9
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

T(n/2) T(n/2) c(n/2+n/2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4*n/4)

T(n/8) c(8*n/8)

10
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

T(n/2) T(n/2) c(n/2+n/2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4*n/4)

T(n/8) c(8*n/8)

. . . . . . . . . . .

T(1)
11
Recursion Tree
T(n) = 2T(n/2) + cn

T(n) cn

T(n/2) T(n/2) c(n/2+n/2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4*n/4)

T(n/8) c(8*n/8)

. . . . . . . . . . .

T(1) c(n*1)
12
Analysis of Recursive Tree

 Total Number of operations:


T(n) = c × n × (number of levels in tree)

13
Analysis of Recursive Tree

 Total Number of operations:


T(n) = c × n × (number of levels in tree)
 Assuming, n = 2h, where h is any integer
constant and T(1) = 1
 Number of levels in tree = h
 T(n) = c × n × h = c × n × log n

14
Analysis of Recursive Tree

 Total Number of operations:


T(n) = c × n × (number of levels in tree)
 Assuming, n = 2h, where h is any integer
constant and T(1) = 1
 Number of levels in tree = h
 T(n) = c × n × h = c × n × log n
 T(n) = O(n log n)
This result is true for all values of n
15
Recursion Tree 2
T(n) = 2T(n/2) + cn2

T(n) cn2

16
Recursion Tree 2
T(n) = 2T(n/2) + cn2

T(n) cn2

T(n/2) T(n/2) c((n/2)2+(n/2)2)

17
Recursion Tree 2
T(n) = 2T(n/2) + cn2

T(n) cn2

T(n/2) T(n/2) c((n/2)2+(n/2)2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4(n/4)2)

18
Recursion Tree 2
T(n) = 2T(n/2) + cn2

T(n) cn2

T(n/2) T(n/2) c((n/2)2+(n/2)2)

T(n/4) T(n/4) T(n/4) T(n/4) c(4(n/4)2)

T(n/8) c(8(n/8)2)

19
Analysis of Recursive Tree 2

 Total Number of operations:


T(n) = c (n2 + n2/2 + n2/4 + n2/8 + …)
 Thisis a decreasing series in n2
 Hence, T(n) = O(n2)

20
Recursion Tree 3
T(n) = 4T(n/2) + cn

T(n) cn

21
Recursion Tree 3
T(n) = 4T(n/2) + cn

T(n) cn

T(n/2) c(4*(n/2))

22
Recursion Tree 3
T(n) = 4T(n/2) + cn

T(n) cn

T(n/2) c(4*(n/2))

. . . c(16*(n/4))
T(n/4)

T(n/8) . . . . . . c(64*(n/8))

. . . . . . . . . . .
T(1) c(?)
23
Analysis of Recursive Tree 3

 Total Number of operations:


T(n) = c (n + 4n/2 + 16n/4 + 64n/8 + …)
= c(1 + 4n/2 +(4)2n/(2)2 +(4)3n/(2)3+
..)
 Thisis an increasing series and the last
member will be the largest number in
the series

24
Analysis of Recursive Tree 3

 Total Number of operations:


T(n) = c (n + 4n/2 + 16n/4 + 64n/8 + …)
= c(1 + 4n/2 +(4)2n/(2)2 +(4)3n/(2)3+
..)
 Assuming n = 2h, the last member of the
series will be c.4h.(n/2h)
= c4h = c4(log2n)= cn(log24)
= cn2
25
Analysis of Recursive Tree 3

 Total Number of operations:


T(n) = c (n + 4n/2 + 16n/4 + 64n/8 + …)
= c(1 + 4n/2 +(4)2n/(2)2 +(4)3n/(2)3+
..)
 Assuming n = 2h, the last member of the
series will be c.4h.(n/2h)
= c4h = c4(log2n)= cn(log24)
= cn2
 Hence, T(n) = O(n2)
26
The End

27

You might also like