Selected Exercises Copyright Peter Cappello 2 Exercise 30 Devise a recursive algorithm to find the n th term of the sequence defined by a 0 1 a 1 2 a n a n1 ID: 532523
Download Presentation The PPT/PDF document "Recursive Algorithms:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Recursive Algorithms: Selected ExercisesSlide2
Copyright © Peter Cappello
2
Exercise 30
Devise a recursive
algorithm
to find the
n
th
term of the sequence defined by:
a
0
= 1,
a
1
= 2
a
n
=
a
n-1
a
n-2
, for
n
= 2, 3, 4, …Slide3
Copyright © Peter Cappello
3
Exercise 30 Solution
Devise a recursive
algorithm
to find the
n
th
term of the sequence defined by:
a
0
= 1,
a
1
= 2
a
n
=
a
n-1
a
n-2
, for
n
= 2, 3, 4, …
int
a
( int
n
)
{
assert
n
>= 0;
return (
n
<= 1) ?
n
+ 1 :
a
(
n
- 1) *
a
(
n
- 2 );
}Slide4
Copyright © Peter Cappello
4
Devise an
iterative
algorithm to find the
n
th term of this sequence.Slide5
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
simpler
than the recursive one?
Is it
correct
for
n
= 0, 1, 2, 3,
4
?
Why are these values important to test?Slide6
Copyright © Peter Cappello
6
Give a recursive
algorithm
to find string
w
i
, 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 40Slide7
Copyright © Peter Cappello
7
Basis
i = 0
:
The String “” =
w
0
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-1. (I.H.) w + w
i-1 is wi
.
(Defn of Java + operator)
.
Exercise 40 continuedSlide8
Copyright © Peter Cappello 2011
8
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 ) );
}Slide9
50 continuedSort 3, 5, 7, 8, 1, 9, 2, 4, 6 using the quick sort.
Pivot: 3
notLarger : 1 2Pivot: 1notLarger : Larger: 2Returned: 1 2Larger: 5 7 8 9 4 6Pivot: 5
notLarger : 4Larger: 7 8 9 6Pivot: 7notLarger : 6Larger: 8 9 Pivot: 8 notLarger : Larger: 9 Returned: 8 9Returned: 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
9Slide10
Quicksort in Haskellqsort :: 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 201510Slide11
Copyright © Peter Cappello
11
Wikipedia entry for QuicksortSlide12
Copyright © Peter Cappello
12
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:00011110Slide13
Copyright © Peter Cappello
13
Gray Codes & the N-Cube
001
000
011
010
101
100
111
110
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.Slide14
Copyright © Peter Cappello
14
Gray Codes & the N-Cube
001
000
011
010
101
100
111
110
000
001
011
111
101
100
110
010Slide15
Copyright © Peter Cappello
15
RGB Colors
Assume we have
2
levels (1-bit) for each of
3
colors:
red
,
green
, &
blue
:
No:
All:Slide16
Copyright © Peter Cappello
16
Gray Codes & the N-Cube
000
001
011
111
101
100
110
010Slide17
Copyright © Peter Cappello
17
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
2
n
n
-bit binary numbers as a cyclic gray code:List<BitString> enumerateGrayCode( int n ){ // your algorithm goes here}Slide18
Copyright © Peter Cappello
18
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
1Slide19
Copyright © Peter Cappello
19
EndSlide20
Copyright © Peter Cappello
20
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.Slide21
Copyright © Peter Cappello
21
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 continuedSlide22
Copyright © Peter Cappello
22
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)Slide23
Copyright © Peter Cappello
23
Assume we have
2
2
levels (
2
-bits) for each of
3
colors:
red
,
green
,
&
blue
:
None:1/32/3 All:Gray Codes, RGB Colors, & the 3 X 2n
Mesh
Level 0
Level 1
Level 2
Level 3Slide24
Copyright © Peter Cappello
24
Confining ourselves to 2
2
-levels of
red
&
blue
. . . A 2 X 2
2
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 bit
representation of
{ 0, 1, 2, 3 }
Find a Hamiltonian
path in this mesh.Slide25
Copyright © Peter Cappello
25
Confining ourselves to 2
2
-levels of
red
&
blue
. . . A
2
2
X 2
2
Mesh
16
15
14
13
9
10
11
12
8
7
6
5
1
2
3
4
1
16