Recursive Algorithms: Selected Exercises 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 2 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 3 Devise an iterative algorithm to find the nth term of this sequence. Copyright © Peter Cappello 4 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 5 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 6 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. 1. concat returns w + concat( w, i - 1 ). 2. concat( w, i – 1 ) is wi-1. 3. w + wi-1 is wi. Copyright © Peter Cappello (I.H.) (Defn of Java + operator). 7 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 8 Wikipedia entry for Quicksort Copyright © Peter Cappello 9 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: 5 7 8 9 4 6 Pivot: 5 notLarger : 4 Larger: 7 8 9 6 Pivot: 7 notLarger : 6 Larger: 8 9 Pivot: 8 notLarger : Larger: 9 Returned: 8 9 Returned: 6 7 8 9 Returned: 4 5 6 7 8 9 Returned: 1 2 3 4 5 6 7 8 9. Copyright © Peter Cappello 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 ) ); } 10 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: 1. 00 2. 01 3. 11 4. 10 Copyright © Peter Cappello 11 Gray Codes & the N-Cube 110 010 111 011 100 000 101 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 12 Gray Codes & the N-Cube 000 110 111 001 011 010 011 100 000 111 101 001 101 100 110 010 Copyright © Peter Cappello 13 RGB Colors Assume we have 2 levels (1-bit) for each of 3 colors: red, green, & blue: No: All: Copyright © Peter Cappello 14 Gray Codes & the N-Cube 000 001 011 111 101 100 110 010 Copyright © Peter Cappello 15 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 16 1-bit 2-bit 3-bit 0 0 0 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 0 Copyright © Peter Cappello 1 0 1 0 17 End Copyright © Peter Cappello 18 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 19 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 20 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: 1. The first element, first, is removed: a has n – 1 elements. 2. maxRest is correct maximum of remaining elements. (I.H.) 3. maximum returns maximum of { first, maxRest }. (last statement) Copyright © Peter Cappello 21 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: Level 0 1/3 Level 1 2/3 Level 2 All: Level 3 Copyright © Peter Cappello 22 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 1,0 1,1 1,2 1,3 Gray code the bit representation of { 0, 1, 2, 3 } Find a Hamiltonian path in this mesh. 0,0 0,1 0,2 0,3 Copyright © Peter Cappello 23 Confining ourselves to 22-levels of red & blue . . . A 2 X 22 Mesh 16 15 14 13 9 10 11 12 8 7 6 5 16 1 1 2 3 4 Copyright © Peter Cappello 24