You are on page 1of 25

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 ) Is this program simpler
return n + 1; than the recursive one?
int an = 2, an1 = 2, an2;
for ( int i = 3; i <= n; i++ ) Is it correct for
{ n = 0, 1, 2, 3, 4?
an2 = an1;
an1 = an; Why are these values
an = an1 * an2; important to test?
}
return an;
}

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. (I.H.)

3. w + wi-1 is wi. (Defn of Java + operator).

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


Quicksort in Haskell
qsort :: Ord a => [a] [a]
qsort [ ] =[]
qsort (x : xs ) = qsort smaller ++ [x] ++ qsort larger
where
smaller = [ a | a <- xs, a <= x ]
larger = [ b | b <- xs, b > x ]

Copyright Peter Cappello 2015 10


Wikipedia entry for Quicksort

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
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
Gray Codes & the N-Cube
000
001
110 111
011
010 011 111
101
100 101
100
000 001
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-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


1-bit 2-bit 3-bit

0 0 0 0 0 0

1 0 1 0 0 1

1 1 0 1 1

1 0 0 1 0
1 1 0
1 1 1
1 0 1
1 0 0

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


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


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 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: Level 0
1/3 Level 1
2/3
Level 2
All:
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 }
1,0 1,1 1,2 1,3
Find a Hamiltonian
path in this mesh.
0,0 0,1 0,2 0,3
Copyright Peter Cappello 24
Confining ourselves to 22-levels of
red & blue . . . A 22 X 22 Mesh

16 15 14 13 16

9 10 11 12

8 7 6 5

1
1 2 3 4
Copyright Peter Cappello 25

You might also like