Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.

Similar presentations


Presentation on theme: "Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input."— Presentation transcript:

1 Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example: Computing n! Procedure factorial ( n : nonnegative integer) if n= 0 then factorial(n)=1 else factorial(n) := n * factorial(n-1) Take n=4 4! = 4*3! = 4 * (3 *2!) = 4*3*2*(1!) = 4*3*2*1*(0!) = 24

2 Recursive Algorithm (4.4) Example: Computing a n Procedure power (a: nonzero real, n: nonnegative integer) if n= 0 then power(a, n)=1 else power(a, n) := a * power(a, n-1) The input (n) becomes smaller each time and eventually, it reaches the terminal point when n = 0. Take a=3, n=4 3 4 = 3*3 3 = 3*3*3 2 = 3*3*3*3 1 = 3*3*3*3*3 0 = 81

3 Recursive Linear Search Procedure search (i, j, x) if a i = x then location :=i else if i=j then location :=0 else search(i+1, j, x) search (i, j, x) is the procedure that searches for x in the sequence a i, a i+1, …, a j

4 Recursive Algorithm for Fibonacci Numbers Procedure fibonacci (n: nonnegative integer) if n= 0 then fibonacci (0)=0 else if n=1 then fibonacci(1)=1 else fibonacci (n) := fibonacci (n-1) + fibonacci (n-2)

5 Take again n= 4 4 3 2 1 0 1 0 1 2 To fine fibonacci (n), it requires fibonacci(n+1)-1 additions

6 Iterative Algorithm Procedure iterative fibonacci (n: nonnegative integer) if n= 0 then y=0 else begin x=0; y=1; for i=1 to n-1 begin z= x+y; x=y; y=z; end y is the Fibonacci number fibonacci(n)

7 Iterative Algorithm To compute fibonacci (n), it requires n-1 additions. fibonacci (2) requires 2-1 addition ( fibonacci (1) + fibonacci (0) ) fibonacci (3) requires 3-1 additions ….. Iterative algorithm is more efficient than the recursive algorithm. So Why bother with recursive algorithm? 1.Lot easier to implement 2.Specially-designed machines are efficient for recursive algorithm

8 Merge Sort A recursive sorting algorithm Procedure mergesort ( L = a1, …., an) if n > 1 then m = floor (n/2) L1 = a1, …., am L2= a m+1, …., an L = merge(mergesort(L1, mergesort(L2)) {L is sorted into elements in nondecreasing order}

9 Merge Sort Procedure merge ( L1, L2 sorted lists) L= empty list while L1 and L2 are both nonempty begin remove smaller of first element of L1 and L2 from the list and put it at the right end of L if removal of this elemtn makes one list empty, then remove all elements from the other list and append them to L. {L is sorted into elements in increasing order} L1 L2 L

10 8 2 4 6 9 7 10 1 5 3 8 2 4 6 9 7 10 1 5 3 8 2 4 6 9 7 10 1 5 3 8 2 4 7 10 169 7 10 5 3 82 The time complexity if O (n log n) ! (next time).

11 1 2 3 4 5 6 7 8 9 10 2 4 6 8 9 1 3 5 7 10 2 4 8 6 9 1 7 10 3 5 2 8 4 7 10 169 7 10 5 3 82


Download ppt "Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input."

Similar presentations


Ads by Google