Presentation is loading. Please wait.

Presentation is loading. Please wait.

APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1.

Similar presentations


Presentation on theme: "APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1."— Presentation transcript:

1 APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1

2 Applications of Recursion  Binary search  Hanoi tower Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-2

3 Binary Search  Given an array A, search for a specific element  Linear search for(int i=0; i<size; i++) { if(A[i]….) { …. }  Time complexity: O(n) Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-3

4 Binary Search (continued)  Linear search is used for unsorted array  For sorted array, we can use binary search  Given array A[0..size] A[0] ≤ A[1] ≤ A[2] ≤ A[3] ≤ … ≤ A[size] Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-4

5 Binary Search Algorithm  To search A[lo] through A[hi] do the following found = false; mid = approximate midpoint between lo and hi If(key==a[mid]) found = true; else if(key<A[mid]) { search A[lo] through A[mid-1]; } else // key > A[mid] { Search A[mid+1] through A[hi] } Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-5

6 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

7 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

8 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

9 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33.

10 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

11 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

12 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

13 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

14 Binary Search Demo (1)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 33

15 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

16 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

17 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

18 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

19 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

20 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

21 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

22 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40

23 Binary Search Demo (2)  Algorithm maintains a[lo]  value  a[hi]  Ex. Binary search for 40 terminate search (lo>hi)

24 Complexity of Binary Search  O(Log 2 (n)) Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-24 nlog(n) 103 1007 1,00010 1,000,00020 1,000,000,00030

25 The Towers of Hanoi A Stack-based Application  GIVEN: three poles and a set of discs on the first pole, discs of different sizes, the smallest discs at the top  GOAL: move all the discs from the left pole to the right one.  CONDITIONS  Only one disc may be moved at a time.  A disc can be placed either on an empty pole or on top of a larger disc.

26 Towers of Hanoi

27

28

29

30

31

32

33

34 Data Structure: Stack void Push(element) element Pop() Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-34

35 Stack: Animation  http://www.cosc.canterbury.ac.nz/mukun dan/dsal/StackAppl.html http://www.cosc.canterbury.ac.nz/mukun dan/dsal/StackAppl.html Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-35

36 Recursive Solution void hanoi (int N, Stack left, Stack middle, Stack right) { int value; if( N == 1) { value = left.pop(); right.push(value); } else { hanoi(N-1, left, right, middle); value = left.pop(); right.push(value); hanoi(N-1,middle, left, right); }

37 Is the End of the World Approaching?  Problem complexity 2 n  64 gold discs  Given 1 move a second  600,000,000,000 years until the end of the world

38 Online-Animation  http://www.cut-the- knot.org/recurrence/hanoi.shtml http://www.cut-the- knot.org/recurrence/hanoi.shtml 38


Download ppt "APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1."

Similar presentations


Ads by Google