Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Algorithms: Selected Exercises

Similar presentations


Presentation on theme: "Recursive Algorithms: Selected Exercises"— Presentation transcript:

1 Recursive Algorithms: Selected Exercises

2 Copyright © Peter Cappello
Exercise 30 Devise a recursive algorithm to find the nth term of the sequence defined by: a0 = 1, a1 = 2 an = an-1 an-2, for n = 2, 3, 4, … Copyright © Peter Cappello

3 Copyright © Peter Cappello
Exercise 30 Solution Devise a recursive algorithm to find the nth term of the sequence defined by: a0 = 1, a1 = 2 an = an-1 an-2, for n = 2, 3, 4, … int a( int n ) { assert n >= 0; return ( n <= 1) ? n + 1 : a( n - 1) * a( n - 2 ); } Copyright © Peter Cappello

4 Copyright © Peter Cappello
Devise an iterative algorithm to find the nth term of this sequence. Copyright © Peter Cappello

5 Copyright © Peter Cappello
Exercise 30 continued int a( int n ) { assert n >= 0; if ( n <= 1 ) return n + 1; int an = 2, an1 = 2, an2; for ( int i = 3; i <= n; i++ ) an2 = an1; an1 = an; an = an1 * an2; } return an; Is this program simpler than the recursive one? Is it correct for n = 0, 1, 2, 3, 4? Why are these values important to test? Copyright © Peter Cappello

6 Copyright © Peter Cappello
Exercise 40 Give a recursive algorithm to find string wi, the concatenation of i copies of bit string w. // In real life, use StringBuffer String concat( String w, int i ) { assert i >= 0 && w != null; if ( i == 0 ) return ""; return w + concat( w, i - 1 ); } Prove that the algorithm is correct. Copyright © Peter Cappello

7 Copyright © Peter Cappello
Exercise 40 continued Basis i = 0: The String “” = w0 is returned (The “if” clause) Inductive hypothesis: concat returns the correct value for i -1. Inductive step: Show concat returns the correct value for i. concat returns w + concat( w, i - 1 ). concat( w, i – 1 ) is wi (I.H.) w + wi-1 is wi (Defn of Java + operator). Copyright © Peter Cappello

8 Copyright © Peter Cappello 2011
Exercise 50 Sort 3, 5, 7, 8, 1, 9, 2, 4, 6 using the quick sort. // real version sorts arrays in place, minimizing movement of elements static List<Integer> quicksort( List<Integer> list ) { assert list != null if ( list.size() <= 1 ) return list; int pivot = list.remove( 0 ); List notLarger = new LinkedList(); List larger = new LinkedList(); while ( ! list.isEmpty() ) { int element = list.remove( 0 ); if ( element <= pivot ) notLarger .add( element ); else larger.add( element ); } List<Integer> sortedList = quicksort( notLarger ); sortedList.add( pivot ); return sortedList.addAll( quicksort( larger ) ); Copyright © Peter Cappello 2011

9 Copyright © Peter Cappello
Wikipedia entry for Quicksort Copyright © Peter Cappello

10 Copyright © Peter Cappello
50 continued Sort 3, 5, 7, 8, 1, 9, 2, 4, 6 using the quick sort. Pivot: 3 notLarger : 1 2 Pivot: 1 notLarger : Larger: 2 Returned: 1 2 Larger: Pivot: 5 notLarger : 4 Larger: Pivot: 7 notLarger : 6 Larger: 8 9 Pivot: 8 Larger: 9 Returned: 8 9 Returned: Returned: Returned: static List<Integer> quicksort( List<Integer> list ) { assert list != null if ( list.size() <= 1 ) return list; int pivot = list.remove( 0 ); List notLarger = new LinkedList(); List larger = new LinkedList(); while ( ! list.isEmpty() ) { int element = list.remove( 0 ); if ( element <= pivot ) notLarger .add( element ); else larger.add( element ); } List<Integer> sortedList = quicksort( notLarger ); sortedList.add( pivot ); return sortedList.addAll( quicksort( larger ) ); Copyright © Peter Cappello

11 Copyright © Peter Cappello
Gray Codes Wolfram MathWorld reference Wikipedia Gray code: A sequence of numbers where adjacent numbers differ in a single digit by 1. For numbers 1, 2, 3, and 4, as bit strings, 00, 01, 10, 11, one gray code is: 00 01 11 10 Copyright © Peter Cappello

12 Copyright © Peter Cappello
Gray Codes & the N-Cube 110 111 010 011 100 101 000 001 A path that visits each vertex exactly once is Hamiltonian. There is a 1-to-1 correspondence between Hamiltonian paths in the n-cube and n-bit gray codes. Copyright © Peter Cappello

13 Copyright © Peter Cappello
Gray Codes & the N-Cube 000 001 011 111 101 100 110 010 110 111 010 011 100 101 000 001 Copyright © Peter Cappello

14 Copyright © Peter Cappello
RGB Colors Assume we have 2 levels (1-bit) for each of 3 colors: red, green, & blue: No: All: Copyright © Peter Cappello

15 Copyright © Peter Cappello
Gray Codes & the N-Cube 000 001 011 111 101 100 110 010 Copyright © Peter Cappello

16 Enumerate a Cyclic Gray Code
A gray code is cyclic when its last value differs from its first value by 1 in a single digit. (See previous slide.) Give a recursive algorithm for enumerating the 2n n-bit binary numbers as a cyclic gray code: List<BitString> enumerateGrayCode( int n ) { // your algorithm goes here } Copyright © Peter Cappello

17 Copyright © Peter Cappello
1-bit 2-bit 3-bit 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Copyright © Peter Cappello

18 Copyright © Peter Cappello
End Copyright © Peter Cappello

19 Copyright © Peter Cappello
Exercise 10 Give a recursive algorithm for finding the maximum of a finite set of integers, using the fact that the maximum of n integers is the larger of: the last integer in the list the maximum of the first n – 1 integers in the list. Copyright © Peter Cappello

20 Copyright © Peter Cappello
10 continued int maximum( LinkedList<Integer> a ) { assert a != null && !a.isEmpty(); int first = a.removeFirst(); if ( a.isEmpty() ) return first; int maxRest = maximum( a ); // necessary? return ( first < maxRest ) ? maxRest : first; } Copyright © Peter Cappello

21 Copyright © Peter Cappello
Exercise 20 Prove that the algorithm that you devised in Exercise 10 is correct. Basis n = 1: If argument a has only 1 element, its value is returned. (statement in “if” clause) Inductive hypothesis: maximum is correct for Lists of size < n. Induction step: maximum is given an argument with n elements: The first element, first, is removed: a has n – 1 elements. maxRest is correct maximum of remaining elements. (I.H.) maximum returns maximum of { first, maxRest }. (last statement) Copyright © Peter Cappello

22 Gray Codes, RGB Colors, & the 3 X 2n Mesh
Assume we have 22 levels (2-bits) for each of 3 colors: red, green, & blue: None: 1/3 2/3 All: Level 0 Level 1 Level 2 Level 3 Copyright © Peter Cappello

23 Confining ourselves to 22-levels of red & blue . . . A 2 X 22 Mesh
3,0 3,1 3,2 3,3 2,0 2,1 2,2 2,3 Gray code the bit representation of { 0, 1, 2, 3 } Find a Hamiltonian path in this mesh. 1,0 1,1 1,2 1,3 0,0 0,1 0,2 0,3 Copyright © Peter Cappello

24 Confining ourselves to 22-levels of red & blue . . . A 2 X 22 Mesh
16 15 14 13 16 9 10 11 12 8 7 6 5 1 2 3 4 1 Copyright © Peter Cappello


Download ppt "Recursive Algorithms: Selected Exercises"

Similar presentations


Ads by Google