Sie sind auf Seite 1von 8

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Recursion Recursion: A tool for Algorithm Designer


Atul Gupta

A programming tool for implementing Divide and Conquer Approach of Problem Solving Infinite computations by finite statements Recursive computations/functions call themselves A recursive function must have
Expression for sub division Termination condition

An Example: Factorial
int factorial(n) if n == 1, return 1 else return (n factorial(n-1) end factorial

int factorial(n) if n == 1, return 1 else return (n factorial(n-1) end factorial int factorial(n-1) if n == 1, return 1 else return ((n-1) factorial(n-2) end factorial int factorial(n-2) if n == 1, return 1 else return ((n-2) factorial(n-3) end factorial

How Recursion works?

int factorial(2) if n == 1, return 1 int factorial(1) else if n == 1, return (2 factorial(1) return 1 end factorial else return (n factorial(0) end factorial

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Divide and Conquer Approach


Divide the problem into sub problems Conquer each sub problem autonomously Combine the solutions of the sub problems

Merge Sort
5 2 4 7 1 3 9 6 5 2 4 7 5 2 5 2 4 4 7 7 1 1 3 9 6 1 3 3 9 9 6 6

Merge Sort
1 2 3 4 5 6 7 9 2 4 5 7 2 5 5 2 4 4 7 7 1 1 3 6 9 1 3 3 9 6 9 6

Algorithm: Merge Sort


merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)
merge (A, p, q, r) 1 n1 q - p + 1 2 n2 r - q 3 create arrays L[n1] and R[n2] 4 for (i 1; i<= n1, i++) 5 L[i] A[p + i - 1] 6 for (j 1; i<= n2, j++) 7 R[j] A[q + j] 8 i1 9 j1 10 for (k p; k<= r ; k++) do 11 if L[i] R[j] 12 then A[k] L[i] 13 ii+1 14 else A[k] R[j] 15 jj+1

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Merge-Sort Tree

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Execution Example
Partition

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

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

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

the root is the initial call the leaves are calls on subsequences of size 1

7 2 9 4 2 4 7 9

7 2 9 4 2 4 7 9

3 8 6 1 1 3 8 6

7 2 2 7

9 4 4 9

7 2 2 7

9 4 4 9

3 8 3 8

6 1 1 6

77

22

99

44
9

77

22

99

44
Merge Sort

33

88

66

11
10

Execution (cont.)
Recursive call, partition

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Execution (cont.)
Recursive call, partition

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

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

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

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

7 2 2 7

9 4 4 9

3 8 3 8

6 1 1 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

77

22

99

44
Merge Sort

33

88

66

11
11

77

22

99

44
Merge Sort

33

88

66

11
12

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Execution (cont.)
Recursive call, base case

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Execution (cont.)
Recursive call, base case

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

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

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

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

77

22

99

44
Merge Sort

33

88

66

11
13

77

22

99

44
Merge Sort

33

88

66

11
14

Execution (cont.)
Merge

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Execution (cont.)

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Recursive call, , base case, merge


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

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

77

22

99

44
Merge Sort

33

88

66

11
15

77

22

99

44
Merge Sort

33

88

66

11
16

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Execution (cont.)
Merge

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Execution (cont.)

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Recursive call, , merge, merge


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

7 29 4 2 4 7 9

3 8 6 1 1 3 8 6

7 29 4 2 4 7 9

3 8 6 1 1 3 6 8

722 7

9 4 4 9

3 8 3 8

6 1 1 6

722 7

9 4 4 9

3 8 3 8

6 1 1 6

77

22

99

44
Merge Sort

33

88

66

11
17

77

22

99

44
Merge Sort

33

88

66

11
18

Execution (cont.)
Merge

merge-sort(A, p, r) if p < r then q (p + r)/2 merge-sort (A, p, q) merge-sort (A, q + 1, r) merge (A, p, q, r)

Analysis of Merge-Sort
The height h of the merge-sort tree is O(log n)
at each recursive call we divide in half the sequence,

The overall amount or work done at the nodes of depth i is O(n)


7 2 9 43 8 6 1 1 2 3 4 6 7 8 9 we partition and merge 2i sequences of size n/2i we make 2i+1 recursive calls

Thus, the total running time of merge-sort is O(n log n)


7 29 4 2 4 7 9 3 8 6 1 1 3 6 8 depth #seqs 0 722 7 9 4 4 9 3 8 3 8 6 1 1 6 1 i 77 22 99 44
Merge Sort

size n n/2 n/2i


Merge Sort 20

1 2 2i

33

88

66

11
19

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Recursive Algorithms: Analysis


Recurrence (or Recurrence Equation)
(1) T(n) = aT(n/b) + D(n) + C(n) otherwise if n <= c

Merge Sort: Algorithmic Analysis


Recurrence for merge sort
(1) T(n) = 2T(n/2) + (n) for n > 1 if n = 1

Parts
Solution for smallest subdivision (Terminating condition) Subdivision relation (aT(n/b)) Division efforts (D(n)) Combining effort (C(n))

A Sequence
0,1,1,2,3,5,8,13,21,34, ,

Fibonacci Sequence

F30 > 1000000 F100 = 21 digit number Fn = 20.694n

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

An Algorithm to find nth Fibonacci


function fib1(n) if (n = 0) return 0 if (n = 1) return 1 return fib1(n-1) + fib1(n-2) T(n) = T(n -1) + T(n - 2) + 3 for n > 1 Whenever we have an algorithm, there are three questions we always ask about it:
1. Is it correct? 2. How much time does it take, as a function of n? 3. And can we do better?

So, What is the problem with Fib1()?

T(200) = ?

Nth Fibonacci: Another Solution


function fib2(n) if n = 0 return 0 if n = 1 return 1 create an array f[0 ..n] f[0] = 0, f[1] = 1 for i = 2 to n do f[i] = f[i-1] + f[i-2] return f[n]

Binary Search
BinarySearchIterative(x, A) { left = 0; right = length(A) 1; while (left <= right) { mid = (left + right) / 2 if (A[mid] == x) return mid; else if (A[mid] > value) right = mid - 1 else left = mid + 1 } return (-1) // not found }

T(200) = ? T(200,000) = ?

BinarySearchRecursive(x, A,left,right) { If (left > right) return (-1) // not found else { mid = (left + right) / 2 if (A[mid] == x) return mid; else if (A[mid] > value) return BinarySearchRecursive(x,A,left,mid-1); else return BinarySearchRecursive(x,A,mid+1,right); } }

Atul Gupta

CS 306 DAA 2012, IIITDM Jabalpur

8/24/2012

Tail Recursion vs. Augmented Recursion


Tail Recursion
Functions ending in a recursive call Does not build deferred operations Similar to iteration gcd(x, y) { if (y == 0) return x; else return gcd(y, x % y); }

Recursion vs. iteration


Q: Is the recursive version usually faster? A: No -- it's usually slower (due to the overhead of maintaining the stack) Q: Does the recursive version usually use less memory? A: No -- it usually uses more memory (for the stack). Q: Then why use recursion?? A: Sometimes it is much simpler to write the recursive version (for instance, trees operations are good examples...) Recursion can be replaced by iteration Problems whose solutions are inherently recursive

Augmented Recursion
builds up deferred operations Inefficient Iteration is preferred

int fact(int n) { if (n == 1) return 1; else return n * fact(n - 1); }

Solving Recurrence
Substitution Method Recursion-tree Method Master Method

Recursion: Summary
A way of thinking about problems. Recursion is a tool to solve problems using Divide and Conquer approach Divide the problem into sub problems, Solving sub problems recursively, and combining the solutions Tail Recursion is a preferred form of Recursion Recursive solutions many be inefficient Related to mathematical induction.

Atul Gupta

Das könnte Ihnen auch gefallen