recursive algorithms: selected exercises. copyright © peter cappello2 exercise 30 devise a...
TRANSCRIPT
Recursive Algorithms: Selected Exercises
Copyright © Peter Cappello 2
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
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
Devise an iterative algorithm to find the nth
term of this sequence.
Copyright © Peter Cappello 5
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;}
Exercise 30 continued
Is this program simplerthan the recursive one?
Is it correct for n = 0, 1, 2, 3, 4?
Why are these values important to test?
Copyright © Peter Cappello 6
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.
Exercise 40
Copyright © Peter Cappello 7
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. (I.H.)
3. w + wi-1 is wi. (Defn of Java + operator).
Exercise 40 continued
Copyright © Peter Cappello 2011 8
Exercise 50Sort 3, 5, 7, 8, 1, 9, 2, 4, 6 using the quick sort.
// real version sorts arrays in place, minimizing movement of elementsstatic 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 ) );}
50 continuedSort 3, 5, 7, 8, 1, 9, 2, 4, 6 using the quick
sort.Pivot: 3notLarger : 1 2
Pivot: 1notLarger : Larger: 2Returned: 1 2
Larger: 5 7 8 9 4 6Pivot: 5notLarger : 4Larger: 7 8 9 6
Pivot: 7notLarger : 6Larger: 8 9
Pivot: 8 notLarger : Larger: 9Returned: 8 9
Returned: 6 7 8 9Returned: 4 5 6 7 8 9
Returned: 1 2 3 4 5 6 7 8 9.
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 10
Copyright © Peter Cappello 11
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 12
Gray Codes & the N-Cube
001000
011010
101100
111110
• 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
Gray Codes & the N-Cube
001000
011010
101100
111110
000
001
011
111
101
100
110
010
Copyright © Peter Cappello 14
RGB Colors
Assume we have 2 levels (1-bit) for each of 3 colors: red, green, & blue:
No:
All:
Copyright © Peter Cappello 15
Gray Codes & the N-Cube000
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
1-bit 2-bit 3-bit
0
1
0
1
1
0
0
0
1
1
0
1
1
0
0
0
1
1
0
1
1
0
1
1
0
0
0
0
0
0
1
1
1
1
Copyright © Peter Cappello 18
End
Copyright © Peter Cappello 19
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
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;
}
10 continued
Copyright © Peter Cappello 21
Exercise 20Prove 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 22
Assume we have 22 levels (2-bits) for
each of 3 colors: red, green, & blue:
None:
1/3
2/3
All:
Gray Codes, RGB Colors, & the 3 X 2n Mesh
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
1,0 1,1 1,2 1,3
0,0 0,1 0,2 0,3
Gray code the bitrepresentation of{ 0, 1, 2, 3 }
Find a Hamiltonianpath in this mesh.
Copyright © Peter Cappello 24
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
1 2 3 41
16