Upload
spandana-russell
View
226
Download
0
Embed Size (px)
Citation preview
8/18/2019 18 Recursive Definitions
1/57
11
Recursive DefinitionsRecursive Definitions
and Structural Inductionand Structural InductionCS/APMA 202CS/APMA 202
Rosen section 3.4Rosen section 3.4 Aaron Bloomfield Aaron Bloomfield
8/18/2019 18 Recursive Definitions
2/57
2
RecursionRecursion
Recursion means definin somet!in"Recursion means definin somet!in"
suc! as a function" in terms of itself suc! as a function" in terms of itself #or e$am%le" let#or e$am%le" let f f && x x ' (' ( x x ))
*e can define*e can define f f && x x ' as' as f f && x x ' (' ( x x + f&+ f& x x ,-',-'
8/18/2019 18 Recursive Definitions
3/57
3
Recursion e$am%leRecursion e$am%le
Rosen" section 3.4" uestion -Rosen" section 3.4" uestion - #ind#ind f f &-'"&-'" f f &2'"&2'" f f &3'" and&3'" and f f &4'" !ere&4'" !ere f f &0' ( -&0' ( -
a'a' etet f f &&nn1-' (1-' ( f f &&nn' 1 2' 1 2
f f &-' (&-' ( f f &0' 1 2 ( - 1 2 ( 3&0' 1 2 ( - 1 2 ( 3f f &2' (&2' ( f f &-' 1 2 ( 3 1 2 ( &-' 1 2 ( 3 1 2 (
f f &3' (&3' ( f f &2' 1 2 ( 1 2 ( &2' 1 2 ( 1 2 (
f f &4' (&4' ( f f &3' 1 2 ( 1 2 ( &3' 1 2 ( 1 2 (
5'5' etet f f &&nn1-' ( 31-' ( 3f f &&nn''f f &-' ( 3 +&-' ( 3 + f f &0' ( 3+- ( 3&0' ( 3+- ( 3
f f &2' ( 3 +&2' ( 3 + f f &-' ( 3+3 ( &-' ( 3+3 (
f f &3' ( 3 +&3' ( 3 + f f &2' ( 3+ ( 2&2' ( 3+ ( 2
f f &4' ( 3 +&4' ( 3 + f f &3' ( 3+2 ( 6-&3' ( 3+2 ( 6-
8/18/2019 18 Recursive Definitions
4/57
4
Recursion e$am%leRecursion e$am%le
Rosen" section 3.4" uestion -Rosen" section 3.4" uestion - #ind#ind f f &-'"&-'" f f &2'"&2'" f f &3'" and&3'" and f f &4'" !ere&4'" !ere f f &0' ( -&0' ( -
c'c' etet f f &&nn1-' ( 21-' ( 2f f &&nn''
f f &-' ( 2&-' ( 2f f &0'&0' ( 2( 2-- ( 2( 2f f &2' ( 2&2' ( 2f f &-'&-' ( 2( 222 ( 4( 4
f f &3' ( 2&3' ( 2f f &2'&2' ( 2( 244 ( -7( -7
f f &4' ( 2&4' ( 2f f &3'&3' ( 2( 2-7-7 ( 737( 737
d'd' etet f f &&nn1-' (1-' ( f f &&nn''22
11 f f &&nn' 1 -' 1 -f f &-' (&-' ( f f &0'&0'22 11 f f &0' 1 - ( -&0' 1 - ( -22 1 - 1 - ( 31 - 1 - ( 3
f f &2' (&2' ( f f &-'&-'22 11 f f &0' 1 - ( 3&0' 1 - ( 322 1 3 1 - ( -31 3 1 - ( -3
f f &3' (&3' ( f f &2'&2'22 11 f f &0' 1 - ( -3&0' 1 - ( -322 1 -3 1 - ( -631 -3 1 - ( -63
f f &4' (&4' ( f f &3'&3'22 11 f f &0' 1 - ( -63&0' 1 - ( -6322 1 -63 1 - ( 33731 -63 1 - ( 3373
8/18/2019 18 Recursive Definitions
5/57
#ractals#ractals
A fractal is a %attern t!at uses recursion A fractal is a %attern t!at uses recursion 8!e %attern itself re%eats indefinitel98!e %attern itself re%eats indefinitel9
8/18/2019 18 Recursive Definitions
6/57
66
FractalsFractals
8/18/2019 18 Recursive Definitions
7/57
#i5onacci seuence#i5onacci seuence
Definition of t!e #i5onacci seuenceDefinition of t!e #i5onacci seuence
:on,recursive;:on,recursive;
Recursive;Recursive; F F &&nn' (' ( F F &&nn,-' 1,-' 1 F F &&nn,2',2'
or;or; F F &&nn1-' (1-' ( F F &&nn' 1' 1 F F &&nn,-',-'
Must ala9s s%ecif9 5ase case&s')Must ala9s s%ecif9 5ase case&s') F F &-' ( -"&-' ( -" F F &2' ( -&2' ( -
:ote t!at some ill use:ote t!at some ill use F F &0' ( -"&0' ( -" F F &-' ( -&-' ( -
( ) ( )n
nn
n F
25
5151)(
⋅
−−+=
8/18/2019 18 Recursive Definitions
8/57
6
#i5onacci seuence in
8/18/2019 18 Recursive Definitions
9/57
Recursion definitionRecursion definition
#rom =8!e >ac?er@s Dictionar9;#rom =8!e >ac?er@s Dictionar9;
recursionrecursion nn. See. See recursionrecursion. See also. See alsotail recursiontail recursion..
8/18/2019 18 Recursive Definitions
10/57
-0
Bad recursive definitionsBad recursive definitions
Consider;Consider; f f &0' ( -&0' ( -
f f &&nn' ( - 1' ( - 1 f f &&nn,2',2'
*!at is*!at is f f &-'&-'
Consider;Consider; f f &0' ( -&0' ( -
f f &n' ( -1&n' ( -1f f &,n'&,n'
*!at is f&-'*!at is f&-'
8/18/2019 18 Recursive Definitions
11/57
--
Definin sets via recursionDefinin sets via recursion
Same to %arts;Same to %arts; Base case &or 5asis ste%'Base case &or 5asis ste%'
Recursive ste%Recursive ste%
$am%le; t!e set of %ositive inteers$am%le; t!e set of %ositive inteers
Basis ste%Basis ste%; -; - ∈∈ SS Recursive ste%Recursive ste%; if; if x x ∈∈ SS" t!en" t!en x x 1-1- ∈∈ SS
8/18/2019 18 Recursive Definitions
12/57
-2
Definin sets via recursionDefinin sets via recursion
Rosen" section 3.4" uestion 24; ive recursiveRosen" section 3.4" uestion 24; ive recursive
definitions for;definitions for;
a'a' 8!e set of odd %ositive inteers8!e set of odd %ositive inteers
-- ∈∈ SS IfIf x x ∈∈ SS" t!en" t!en x x 1212 ∈∈ SS
5'5' 8!e set of %ositive inteer %oers of 38!e set of %ositive inteer %oers of 3 33 ∈∈ SS
IfIf x x ∈∈ SS" t!en 3+" t!en 3+ x x ∈∈ SSc'c' 8!e set of %ol9nomials it! inteer coefficients8!e set of %ol9nomials it! inteer coefficients
00 ∈∈ SS
IfIf p p&& x x '' ∈∈ SS" t!en" t!en p p&& x x ' 1 c$' 1 c$nn ∈∈ SS c c ∈∈ ZZ"" nn ∈∈ ZZ andand nn ≥ 0≥ 0
8/18/2019 18 Recursive Definitions
13/57
-3
Definin strins via recursionDefinin strins via recursion
8erminolo98erminolo9 λλ is t!e em%t9 strin; =is t!e em%t9 strin; =
ΣΣ is t!e set of all letters; a" 5" c" E" F Gis t!e set of all letters; a" 5" c" E" F G
8!e set of letters can c!ane de%endin on t!e %ro5lem8!e set of letters can c!ane de%endin on t!e %ro5lem
*e can define a set of strins*e can define a set of strins ΣΣ+ as follos+ as follos Base ste%;Base ste%; λλ ∈∈ ΣΣ++
IfIf w w ∈∈ ΣΣ+ and+ and x x ∈∈ ΣΣ" t!en $" t!en $ ∈∈ ΣΣ++
8!us"8!us" ΣΣ+ s t!e set of all t!e %ossi5le strins t!at can+ s t!e set of all t!e %ossi5le strins t!at can
5e enerated it! t!e al%!a5et5e enerated it! t!e al%!a5et
Is t!is counta5l9 infinite or uncounta5l9 infiniteIs t!is counta5l9 infinite or uncounta5l9 infinite
8/18/2019 18 Recursive Definitions
14/57
-4
Definin strins via recursionDefinin strins via recursion
etet ΣΣ ( 0" - G( 0" - G
8!us"8!us" ΣΣ+ is t!e set of all 5inar9 num5ers+ is t!e set of all 5inar9 num5ers
Hr all 5inar9 strinsHr all 5inar9 strins Hr all %ossi5le com%uter filesHr all %ossi5le com%uter files
8/18/2019 18 Recursive Definitions
15/57
-
Strin lent! via recursionStrin lent! via recursion
>o to define strin lent! recursivel9>o to define strin lent! recursivel9 Basis ste%;Basis ste%; l l &&λλ' ( 0' ( 0 Recursive ste%;Recursive ste%; l l &&wx wx ' (' ( l l &&w w ' 1 - if' 1 - if w w ∈∈ ΣΣ+ and+ and x x
∈∈ ΣΣ
$am%le;$am%le; l l &=aaa'&=aaa' l l &=aaa' (&=aaa' ( l l &=aa' 1 -&=aa' 1 -
l l &=aa' (&=aa' ( l l &=a' 1 -&=a' 1 - l l &=a' (&=a' ( l l &=' 1 -&=' 1 - l l &=' ( 0&=' ( 0 Result; 3Result; 3
8/18/2019 18 Recursive Definitions
16/57
8/18/2019 18 Recursive Definitions
17/57
-
Strins via recursion e$am%leStrins via recursion e$am%le
Rosen" section 3.4" uestion 36; ive aRosen" section 3.4" uestion 36; ive a
recursive definition for t!e set of strin t!atrecursive definition for t!e set of strin t!at
are %alindromesare %alindromes *e ill define set*e ill define set P P " !ic! is t!e set of all" !ic! is t!e set of all
%alindromes%alindromes
Basis ste%Basis ste%;; λλ ∈∈ P P Second 5asis ste%;Second 5asis ste%; x x ∈∈ P P !en!en x x ∈∈ ΣΣ
Recursive ste%Recursive ste%;; xpx xpx ∈∈ P P ifif x x ∈∈ ΣΣ andand p p ∈∈ P P
8/18/2019 18 Recursive Definitions
18/57
-6
Strins and induction e$am%leStrins and induction e$am%le
8!is reuires structural induction" !ic!8!is reuires structural induction" !ic!
ill 5e covered later in t!is slide setill 5e covered later in t!is slide set
8/18/2019 18 Recursive Definitions
19/57
-
Recursion %rosRecursion %ros
as9 to %roramas9 to %roram
as9 to understandas9 to understand
8/18/2019 18 Recursive Definitions
20/57
20
Recursion consRecursion cons
Consider t!e recursive #i5onacci enerator Consider t!e recursive #i5onacci enerator
>o man9 recursive calls does it ma?e>o man9 recursive calls does it ma?e F F &-'; -&-'; - F F &2'; -&2'; -
F F &3'; 3&3'; 3 F F &4'; &4'; F F &'; &'; F F &-0'; -0&-0'; -0 F F &20'; -3"2&20'; -3"2 F F &30'; -"774"0&30'; -"774"0 F F &40'; 204"776"30&40'; 204"776"30 F F &0'; 2"-2"36"04&0'; 2"-2"36"04 F F &-00'; 06"44"77"36"23"630"-4&-00'; 06"44"77"36"23"630"-4 ≈≈ + -0 + -02020
At - 5illion recursive calls %er second &enerous'" t!is ould ta?e over At - 5illion recursive calls %er second &enerous'" t!is ould ta?e over22"000 9ears22"000 9ears
But t!at ould also ta?e ell over -0But t!at ould also ta?e ell over -0-2-2 5 of memor9)5 of memor9)
8/18/2019 18 Recursive Definitions
21/57
2-
8rees8rees
Rooted trees;Rooted trees; A ra%! containin nodes and edes A ra%! containin nodes and edes
Cannot contain a c9cle)Cannot contain a c9cle)
C9cle not alloed in a tree
8/18/2019 18 Recursive Definitions
22/57
22
Rooted treesRooted trees
Recursive definition;Recursive definition; Basis ste%Basis ste%; A sinle verte$; A sinle verte$ r r is a rooted treeis a rooted tree
Recursive ste%Recursive ste%;;
etet T T --"" T T 22" E"" E" T T nn 5e rooted trees5e rooted trees#orm a ne tree it! a ne root#orm a ne tree it! a ne root r r t!at contains an edet!at contains an ede
to t!e root of eac! of t!e treesto t!e root of eac! of t!e trees T T --"" T T 22" E"" E" T T nn
8/18/2019 18 Recursive Definitions
23/57
23
&$tended' Binar9 trees&$tended' Binar9 trees
Recursive definitionRecursive definition Basis ste%Basis ste%;; 8!e em%t9 set is an e$tended 5inar9 tree8!e em%t9 set is an e$tended 5inar9 tree
Recursive ste%Recursive ste%;;
etet T T --" and" and T T 22 5e e$tended 5inar9 trees5e e$tended 5inar9 trees#orm a ne tree it! a ne root#orm a ne tree it! a ne root r r
#orm a ne tree suc! t!at#orm a ne tree suc! t!at T T -- is t!e left su5tree"is t!e left su5tree"
andand T T 22 is t!e ri!t su5treeis t!e ri!t su5tree
8/18/2019 18 Recursive Definitions
24/57
24
#ull 5inar9 trees#ull 5inar9 trees
Recursive definitionRecursive definition Basis ste%;Basis ste%; A full 5inar9 tree consistin onl9 of t!e verte$ A full 5inar9 tree consistin onl9 of t!e verte$ r r
Recursive ste%Recursive ste%;;
etet T T --" and" and T T 22 5e e$tended 5inar9 trees5e e$tended 5inar9 trees
#orm a ne tree it! a ne root#orm a ne tree it! a ne root r r
#orm a ne tree#orm a ne tree T T suc! t!atsuc! t!at T T -- is t!e left su5tree"is t!e left su5tree"
andand T T 22 is t!e ri!t su5treeis t!e ri!t su5tree
8!is is denoted 598!is is denoted 59 T T (( T T --JJT T 22
:ote t!e onl9 difference 5eteen a reular:ote t!e onl9 difference 5eteen a reular
5inar9 tree and a full one is t!e 5asis ste%5inar9 tree and a full one is t!e 5asis ste%
8/18/2019 18 Recursive Definitions
25/57
2
Binar9 tree !ei!tBinar9 tree !ei!t
hh&&T T ' denotes t!e !ei!t of tree' denotes t!e !ei!t of tree T T
Recursive definition;Recursive definition; Basis ste%Basis ste%; 8!e !ei!t of a tree it!; 8!e !ei!t of a tree it!
onl9 one nodeonl9 one node r r is 0is 0 Recursive ste%Recursive ste%;;
etet T T -- andand T T 22 5e 5inar9 trees5e 5inar9 trees
8!e 5inar9 tree8!e 5inar9 tree T T (( T T --JJT T
22 !as !ei!t!as !ei!t
hh&&T T ' ( - 1 ma$ &' ( - 1 ma$ & hh&&T T --'"'" hh&&T T 22' '' '
8!is definition can 5e eneraliFed to8!is definition can 5e eneraliFed tonon,5inar9 treesnon,5inar9 trees
8/18/2019 18 Recursive Definitions
26/57
27
Binar9 tree siFeBinar9 tree siFe
nn&&T T ' denotes t!e num5er of vertices in tree' denotes t!e num5er of vertices in tree T T
Recursive definition;Recursive definition; Basis ste%Basis ste%; 8!e num5er of vertices of an; 8!e num5er of vertices of an
em%t9 tree is 0em%t9 tree is 0 Basis ste%Basis ste%; 8!e num5er of vertices of a tree; 8!e num5er of vertices of a tree
it! onl9 one nodeit! onl9 one node r r is -is - Recursive ste%Recursive ste%;;
etet T T -- andand T T 22 5e 5inar9 trees5e 5inar9 trees
8!e num5er of vertices in 5inar9 tree8!e num5er of vertices in 5inar9 tree T T (( T T --JJT T 22 is;is;
n&n&T T ' ( - 1' ( - 1 nn&&T T --' 1' 1 nn&&T T 22''
8!is definition can 5e eneraliFed to non,8!is definition can 5e eneraliFed to non,5inar9 trees5inar9 trees
8/18/2019 18 Recursive Definitions
27/57
2727
A bit of humor: ComputerA bit of humor: Computer
terminologyterminology
8/18/2019 18 Recursive Definitions
28/57
26
Recursion vs. inductionRecursion vs. induction
Consider t!e recursive definition forConsider t!e recursive definition for
factorial;factorial;
f f &0' ( -&0' ( -
f f &n' ( n +&n' ( n + f f &n,-'&n,-'
Sort of li?e inductionSort of li?e induction
Base case
8!e =ste%
8/18/2019 18 Recursive Definitions
29/57
2
Recursion vs. inductionRecursion vs. induction
Rosen" section 3.4" e$am%le &%ae 272'Rosen" section 3.4" e$am%le &%ae 272'
Consider t!e set of all inteers t!at are multi%lesConsider t!e set of all inteers t!at are multi%les
of 3of 3 3" 7" " -2" -" E G 3" 7" " -2" -" E G
x x KK x x ( 3( 3k k andand k k ∈∈ ZZ11 GG
Recursive definition;Recursive definition; Basis ste%Basis ste%; 3; 3 ∈∈ SS
Recursive ste%Recursive ste%; If; If x x ∈∈ SS andand y y ∈∈ SS" t!en" t!en x x 11y y ∈∈ SS
8/18/2019 18 Recursive Definitions
30/57
30
Recursion vs. inductionRecursion vs. induction
Proof via induction; %rove t!atProof via induction; %rove t!at SS contains all t!e inteerscontains all t!e inteerst!at are divisi5le 59 3t!at are divisi5le 59 3 etet A A 5e t!e set of all ints divisi5le 59 35e t!e set of all ints divisi5le 59 3 S!o t!atS!o t!at SS (( A A
8o %arts;8o %arts; S!o t!atS!o t!at SS ⊆⊆ A A
etet P P &&nn' ( 3' ( 3nn ∈∈ SS
Base caseBase case;; P P &-' ( 3+-&-' ( 3+- ∈∈ SS B9 t!e 5asis ste% of t!e recursive definitionB9 t!e 5asis ste% of t!e recursive definition
Inductive !9%ot!esisInductive !9%ot!esis; assume; assume P P &&k k ' ( 3+' ( 3+k k ∈∈ SS is trueis true
Inductive ste%Inductive ste%; s!o t!at; s!o t!at P P &&k k 1-' ( 3+&1-' ( 3+&k k 1-' is true1-' is true 3+&3+&k k 1-' ( 31-' ( 3k k 1313 33k k ∈∈ SS 59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis 33 ∈∈ SS 59 t!e 5ase case59 t!e 5ase case 8!us" 38!us" 3k k 1313 ∈∈ SS 59 t!e recursive definition59 t!e recursive definition
S!o t!atS!o t!at A A ⊆⊆ SSDone in t!e te$t" %ae 27 ¬ re%roduced !ere'Done in t!e te$t" %ae 27 ¬ re%roduced !ere'
8/18/2019 18 Recursive Definitions
31/57
3-
*!at did e Lust do*!at did e Lust do
:otice !at e did;:otice !at e did; S!oed t!e 5ase caseS!oed t!e 5ase case
Assumed t!e inductive !9%ot!esis Assumed t!e inductive !9%ot!esis
#or t!e inductive ste%" e;#or t!e inductive ste%" e;
S!oed t!at eac! of t!e =%arts ere inS!oed t!at eac! of t!e =%arts ere in SS 8!e %arts 5ein 38!e %arts 5ein 3k k and 3and 3
S!oed t!at since 5ot! %arts ere inS!oed t!at since 5ot! %arts ere in SS" 59 t!e" 59 t!erecursive definition" t!e com5ination of t!ose %artsrecursive definition" t!e com5ination of t!ose %arts
is inis in SS i.e." 3i.e." 3k k 1313 ∈∈ SS
8!is is called8!is is called structural inductionstructural induction
8/18/2019 18 Recursive Definitions
32/57
8/18/2019 18 Recursive Definitions
33/57
34
8ree structural induction e$am%le8ree structural induction e$am%le
Rosen" section 3.4" uestion 43Rosen" section 3.4" uestion 43
S!o t!atS!o t!at nn&&T T '' ≥≥ 22hh&&T T ' 1 -' 1 -
Basis ste%Basis ste%; et; et T T 5e t!e full 5inar95e t!e full 5inar9
tree of Lust one nodetree of Lust one node r r hh&&T T ' ( 0' ( 0
nn&&T T ' ( -' ( - nn&&T T '' ≥≥ 22hh&&T T ' 1 -' 1 -
-- ≥≥ 2+0 1 -2+0 1 -
-- ≥≥ --
8/18/2019 18 Recursive Definitions
34/57
3
8ree structural induction e$am%le8ree structural induction e$am%le
S!o t!atS!o t!at nn&&T T '' ≥≥ 22hh&&T T ' 1 -' 1 -
Inductive !9%ot!esisInductive !9%ot!esis;; etet T T -- andand T T 22 5e full 5inar9 trees5e full 5inar9 trees
Assume t!at Assume t!at nn&&T T 11'' ≥≥ 22hh&&T T 11' 1 - for some tree' 1 - for some tree T T 11 Assume t!at Assume t!at nn&&T T 2 2 '' ≥≥ 22hh&&T T 2 2 ' 1 - for some tree' 1 - for some tree T T 2 2
Recursive ste%Recursive ste%;; etet T T (( T T -- JJ T T 22>ere t!e J o%erator means creatin a ne tree it! a root note>ere t!e J o%erator means creatin a ne tree it! a root note r r and su5treesand su5treesT T -- andand T T 22
:e element is:e element is T T B9 t!e definition of !ei!t and siFe" e ?no;B9 t!e definition of !ei!t and siFe" e ?no;
nn&&T T ' ( - 1' ( - 1 nn&&T T --' 1' 1 nn&&T T 22''
hh&&T T ' ( - 1 ma$ &' ( - 1 ma$ & hh&&T T --'"'" hh&&T T 22' '' ' 8!erefore;8!erefore;
nn&&T T ' ( - 1' ( - 1 nn&&T T --' 1' 1 nn&&T T 22''
≥≥ - 1 2- 1 2hh&&T T --' 1 - 1 2' 1 - 1 2hh&&T T 22' 1 -' 1 -
≥≥ - 1 2+ma$ &- 1 2+ma$ & hh&&T T --'"'" hh&&T T 22' '' ' t!e sum of to non,ne N@s is at leastt!e sum of to non,ne N@s is at least
as lare as t!e larer of t!e toas lare as t!e larer of t!e to
( - 1 2+( - 1 2+hh&&T T '' 8!us"8!us" nn&&T T '' ≥≥ 22hh&&T T ' 1 -' 1 -
8/18/2019 18 Recursive Definitions
35/57
37
Strin structural induction e$am%leStrin structural induction e$am%le
Rosen" section 3.4" uestion 32Rosen" section 3.4" uestion 32
Part &a'; ive t!e definition forPart &a'; ive t!e definition for ones(s)ones(s)" !ic! counts t!e" !ic! counts t!enum5er of ones in a 5it strinnum5er of ones in a 5it strin ss
etet ΣΣ ( 0" - G( 0" - G
Basis ste%Basis ste%;; onesones&&λλ'' == 00
Recursive ste%Recursive ste%;; ones(wx) = ones(w) + x ones(wx) = ones(w) + x *!ere*!ere x x ∈∈ ΣΣ andand w w ∈∈ ΣΣ++ :ote t!at:ote t!at x x is a 5it; eit!er 0 or -is a 5it; eit!er 0 or -
8/18/2019 18 Recursive Definitions
36/57
3
Strin structural induction e$am%leStrin structural induction e$am%le
Part &5'; se structural induction to %rove t!atPart &5'; se structural induction to %rove t!at ones(st) = ones(s) +ones(st) = ones(s) +ones(t)ones(t)
Basis ste%Basis ste%;; t t (( λλ ones (sones (sJJλ λ ) = ones(s) = ones(s)+0 = ones(s) + ones( ) = ones(s) = ones(s)+0 = ones(s) + ones( λ λ ) )
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume ones(s∙t) = ones(s) + ones(t)ones(s∙t) = ones(s) + ones(t)
Recursive ste%Recursive ste%; *ant to s!o t!at; *ant to s!o t!at ones(s∙t∙x) = ones(s) + ones(t∙x)ones(s∙t∙x) = ones(s) + ones(t∙x) *!ere*!ere ss"" tt ∈∈ ΣΣ+ and+ and x x ∈∈ ΣΣ :e element is:e element is ones(s∙t∙x)ones(s∙t∙x) ones (s∙t∙x) = ones ((s∙t)∙x))ones (s∙t∙x) = ones ((s∙t)∙x)) 59 associativit9 of concatenation59 associativit9 of concatenation = x+ones(s∙t)= x+ones(s∙t) 59 recursive definition59 recursive definition = x + ones(s) + ones(t)= x + ones(s) + ones(t) 59 inductive !9%ot!esis59 inductive !9%ot!esis = ones(s) + (x + ones(t))= ones(s) + (x + ones(t)) 59 commutativit9 and assoc. of 159 commutativit9 and assoc. of 1 = ones(s) + ones(t∙x)= ones(s) + ones(t∙x) 59 recursive definition59 recursive definition Proven)Proven)
8/18/2019 18 Recursive Definitions
37/57
8/18/2019 18 Recursive Definitions
38/57
8/18/2019 18 Recursive Definitions
39/57
40
Induction met!ods com%aredInduction met!ods com%ared
WeakWeakmathematicalmathematical
StrongStrongMathematicalMathematical StructuralStructural
Used for Used for suall9suall9
formulaeformulaesuall9 formulae notsuall9 formulae not
%rova5le via%rova5le viamat!ematical inductionmat!ematical induction
Hnl9 t!ins definedHnl9 t!ins definedvia recursionvia recursion
AssumptionAssumption Assume P&?' Assume P&?' Assume P&-'" P&2'" E" Assume P&-'" P&2'" E"P&?'P&?'
Assume statement is Assume statement istrue for some OoldOtrue for some OoldO
elementselements
What toWhat to
proveprove8rue for8rue for
P&?1-'P&?1-' 8rue for P&?1-'8rue for P&?1-'
Statement is true forStatement is true forsome OneO elementssome OneO elements
created it! OoldOcreated it! OoldOelementselements
Step 1Step 1calledcalled
Base caseBase case Base caseBase case Basis ste%Basis ste%
Step 3Step 3
calledcalled
Inductive ste%Inductive ste% Inductive ste%Inductive ste% Recursive ste%Recursive ste%
8/18/2019 18 Recursive Definitions
40/57
4-
Induction t9%es com%aredInduction t9%es com%ared
S!o t!atS!o t!at F F &&nn' 2' 2nn *!ere*!ere F F &&nn' is t!e' is t!e nnt!t! #i5onacci num5er #i5onacci num5er Actuall9 Actuall9 F F &&nn' 2' 20.3+0.3+nn" 5ut e on@t %rove t!at !ere" 5ut e on@t %rove t!at !ere
#i5onacci definition;#i5onacci definition; Basis ste%Basis ste%;; F F &-' ( - and&-' ( - and F F &2' ( -&2' ( - Recursive ste%Recursive ste%;; F F &&nn' (' ( F F &&nn,-' 1,-' 1 F F &&nn,2',2'
Base case &or 5asis ste%'Base case &or 5asis ste%'; S!o true for; S!o true for F F &-'&-'andand F F &2'&2' #&-' ( - 2#&-' ( - 2-- ( 2( 2 #&2' ( - 2#&2' ( - 222 ( 4( 4
8/18/2019 18 Recursive Definitions
41/57
42
Qia ea? mat!ematical inductionQia ea? mat!ematical induction
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume F F &&k k ' 2' 2k k
Inductive ste%Inductive ste%; Prove; Prove F F &&k k 1-' 21-' 2k k 1-1- F F &&k k 1-' (1-' ( F F &&k k ' 1' 1 F F &&k k ,-',-'
*e ?no*e ?no F F &&k k ' 2' 2k k
59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis ac! term is less t!an t!e ne$t" t!ereforeac! term is less t!an t!e ne$t" t!erefore
F F &&k k ' ' F F &&k k ,-',-'8!us"8!us" F F &&k k ,-' ,-' F F &&k k ' 2' 2k k
8!erefore"8!erefore" F F &&k k 1-' (1-' ( F F &&k k ' 1' 1 F F &&k k ,-' 2,-' 2k k 1 21 2k k ( 2( 2k k 1-1-
Proven)Proven)
8/18/2019 18 Recursive Definitions
42/57
43
Qia stron mat!ematical inductionQia stron mat!ematical induction
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume F F &-' 2&-' 2--""
F F &2' 2&2' 222" E"" E" F F &&k-k--' 2-' 2k k ,-,-"" F F &&k k ' 2' 2k k
Inductive ste%Inductive ste%; Prove; Prove F F &&k k 1-' 21-' 2k k 1-1-
F F &&k k 1-' (1-' ( F F &&k k ' 1' 1 F F &&k k ,-',-'
*e ?no*e ?no F F &&k k ' 2' 2k k 59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis
*e ?no*e ?no F F &&k k ,-' 2,-' 2k k ,-,- 59 t!e inductive59 t!e inductive
!9%ot!esis!9%ot!esis 8!erefore"8!erefore" F F &&k k ' 1' 1 F F &&k k ,-' 2,-' 2k k 1 21 2k k ,-,- 2 2k k 1-1-
Proven)Proven)
8/18/2019 18 Recursive Definitions
43/57
44
Qia structural inductionQia structural induction
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume F F &&nn' 2' 2nn
Recursive ste%Recursive ste%;; S!o true for =ne element;S!o true for =ne element; F F &&nn1-'1-' *e ?no*e ?no F F &&nn' 2' 2nn 59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis ac! term is less t!an t!e ne$t" t!ereforeac! term is less t!an t!e ne$t" t!erefore
F F &&nn' ' F F &&nn,-',-'
8!us"8!us"
F F
&&nn
,-' ,-'
F F
&&nn
' 2' 2nn
8!erefore"8!erefore" F F &&nn' 1' 1 F F &&nn,-' 2,-' 2nn 1 21 2nn ( 2( 2nn1-1-
Proven)Proven)
8/18/2019 18 Recursive Definitions
44/57
4
Anot!er a9 via structural induction Anot!er a9 via structural induction
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume F F &&nn' 2' 2nn andandF F &&nn,-' 2,-' 2nn,-,- 8!e difference !ere is e are usin to =old8!e difference !ere is e are usin to =old
elements versus one" as in t!e last slideelements versus one" as in t!e last slide
Recursive ste%Recursive ste%;; S!o true for =ne element;S!o true for =ne element; F F &&nn1-'1-' F F &&nn1-' (1-' ( F F &&nn' 1' 1 F F &&nn,-',-'
*e ?no*e ?no
F F
&&nn
' 2' 2nn
59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis
*e ?no*e ?no F F &&nn,-' 2,-' 2nn,-,- 59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis 8!erefore"8!erefore" F F &&nn' 1' 1 F F &&nn,-' 2,-' 2k k 1 21 2k k ,-,- 2 2k k 1-1-
Proven)Proven)
8/18/2019 18 Recursive Definitions
45/57
47
But ait)But ait)
In t!is e$am%le" t!e structural induction %roofIn t!is e$am%le" t!e structural induction %roof
as essentiall9 t!e same as t!e ea? or stronas essentiall9 t!e same as t!e ea? or stron
mat!ematical induction %roof mat!ematical induction %roof
It@s !ard to find an e$am%le t!at or?s ell for all ofIt@s !ard to find an e$am%le t!at or?s ell for all oft!e induction t9%est!e induction t9%es
Structural induction ill or? on some recursiveStructural induction ill or? on some recursive
%ro5lems !ic! ea? or stron mat!ematical%ro5lems !ic! ea? or stron mat!ematicalinduction ill notinduction ill not 8rees" strins" etc.8rees" strins" etc.
8/18/2019 18 Recursive Definitions
46/57
8/18/2019 18 Recursive Definitions
47/57
46
Section 3.4" uestion 6Section 3.4" uestion 6
ive t!e recursive definition of t!e folloin seuencesive t!e recursive definition of t!e folloin seuences :ote t!at man9 ansers are %ossi5le):ote t!at man9 ansers are %ossi5le)
a)a) aann ( 4( 4nn 2 2 8erms; 2" 7" -0" -4" -7" etc.8erms; 2" 7" -0" -4" -7" etc. aa11 ( 2( 2 aann (( aan-1n-1 1 41 4
)) aann ( - 1 &,-'( - 1 &,-'nn 8erms; 0" 2" 0" 2" 0" 2" etc.8erms; 0" 2" 0" 2" 0" 2" etc. aa11 ( 0"( 0" aa2 2 ( 2( 2 aann (( aan-2 n-2
c)c) aann (( nn&&nn1-'1-' 8erms; 2" 7" -2" 20" 30" 42" etc.8erms; 2" 7" -2" 20" 30" 42" etc. aa11 ( 2( 2 aann (( aan-1n-1 1 2+n1 2+n
!)!) aann (( nn22 8erms; -" 4" " -7" 2" 37" 4" etc.8erms; -" 4" " -7" 2" 37" 4" etc. aa11 ( -( -
aann (( aan-1n-1 1 21 2nn , -, -
8/18/2019 18 Recursive Definitions
48/57
4
Section 3.4" uestion -2Section 3.4" uestion -2
S!o t!atS!o t!at f f --22 11 f f 2222 11 f f 3322 1 E 11 E 1 f f nn22 (( f f nnf f nn1-1-
Base caseBase case;; nn ( -( - f f --22 (( f f 11f f 2 2 --22 ( -+-( -+-
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume f f --22 11 f f 2222 11 f f 3322 1 E 11 E 1 f f k k 22 (( f f k k f f k k 1-1-
Inductive ste%Inductive ste%; Prove; Prove f f --22 11 f f 2222 11 f f 3322 1 E 11 E 1 f f k k 22 11 f f k+1k+122 (( f f k+1k+1f f k k 1212
8/18/2019 18 Recursive Definitions
49/57
0
Section 3.4" uestion -2Section 3.4" uestion -2
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume f f --22 11 f f 2222 11 f f 3322 1 E 11 E 1 f f k k 22 (( f f k k f f k k 1-1-
Inductive ste%Inductive ste%; Prove; Prove f f --22 11 f f 2222 11 f f 3322 1 E 11 E 1 f f k k 22 11 f f k+1k+122 (( f f k+1k+1f f k k 1212
f f k k f f k k 1-1- 11 f f k+1k+122 (( f f k+1k+1f f k k 1212
f f k k f f k k 1-1- 11 f f k+1k+122 (( f f k+1k+1 &&f f k k 11 f f k k 1-1-''
f f k k f f k k 1-1- 11 f f k+1k+122 (( f f k k f f k k 1-1- 11 f f k+1k+122
8/18/2019 18 Recursive Definitions
50/57
-
Section 3.4" uestion -3Section 3.4" uestion -3
S!o t!atS!o t!at f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22nn,-,- (( f f 22nn
Base caseBase case;; nn ( -( - f f -- (( f f 2+-2+- - ( -- ( -
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume
f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22k k ,-,- (( f f 22k k
Inductive ste%Inductive ste%; Prove; Prove f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22k k ,-,- 11 f f 2&2&k k 1-',-1-',- (( f f 2&2&k k 1-'1-' f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22k k ,-,- 11 f f 22k k 1-1- (( f f 22k k 1212
8/18/2019 18 Recursive Definitions
51/57
2
Section 3.4" uestion -3Section 3.4" uestion -3
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22k k ,-,- (( f f 22k k
Inductive ste%Inductive ste%; Prove; Prove f f -- 11 f f 22 11 f f 33 1 E 11 E 1 f f 22k k ,-,- 11 f f 22k k 1-1- (( f f 22k k 1212
f f 22k k 11 f f 22k k 1-1- (( f f 22k k 1212
8rue 59 definition of8rue 59 definition of f f 22k k 1212
8/18/2019 18 Recursive Definitions
52/57
3
Section 3.4" uestion 22Section 3.4" uestion 22
S!o t!at t!e setS!o t!at t!e set SS defined 59defined 59 Basis ste%Basis ste%; -; - ∈∈ SS Recursive ste%Recursive ste%;; ss 11 t t ∈∈ SS !en!en ss ∈∈ SS andand t t ∈∈ SS
is t!e set of %ositive inteers;is t!e set of %ositive inteers; ZZ11 ( -" 2" 3" E G( -" 2" 3" E G
:ote t!e &some!at recursive' definition of t!e %ositive inteers;:ote t!e &some!at recursive' definition of t!e %ositive inteers; - is a %ositive inteer - is a %ositive inteer #or an9 ar5itrar9#or an9 ar5itrar9 nn t!at is a %ositive inteer"t!at is a %ositive inteer" nn1- is also a %ositive inteer 1- is also a %ositive inteer
Proof 59 structural inductionProof 59 structural inductionBasis ste%Basis ste%; -; - ∈∈ SS and -and - ∈∈ ZZ11
Inductive !9%ot!esisInductive !9%ot!esis; Assume; Assume k k ∈∈ SS
Recursive ste%Recursive ste%; S!o; S!o k k 1-1- ∈∈ SS k k ∈∈ SS 59 t!e inductive !9%ot!esis59 t!e inductive !9%ot!esis -- ∈∈ SS 59 t!e 5ase case59 t!e 5ase case k k 1-1- ∈∈ SS 59 t!e recursive ste% of t!e recursive definition a5ove59 t!e recursive ste% of t!e recursive definition a5ove
8/18/2019 18 Recursive Definitions
53/57
4
Section 3.4" uestion 3Section 3.4" uestion 3
ive a recursive definition of t!e reversal of aive a recursive definition of t!e reversal of astrinstrin
Basis ste%Basis ste%;; λλRR
(( λλ :ote t!at t!e su%erscri%ted R means reversal of a:ote t!at t!e su%erscri%ted R means reversal of a
strinstrin
Recursive ste%Recursive ste%; Consider a strin; Consider a strin w w ∈∈ ΣΣ++
ReriteRerite w w asas "y "y !ere!ere " " ∈∈ ΣΣ+ and+ and yy ∈∈ ΣΣ" " is t!e firstis t!e first nn,- c!aracters in,- c!aracters in w w
y y is t!e last c!aracter inis t!e last c!aracter in w w
w w RR (( y y &&" " RR''
Parent!eses are for our 5enefitParent!eses are for our 5enefit
8/18/2019 18 Recursive Definitions
54/57
55
Quick survey Quick survey
I felt I understood the material in thisI felt I understood the material in this
slide set…slide set…
a)a) Very well Very well
b)b) With some review, I’ll be goodWith some review, I’ll be good
c)c) Not reallyNot really
d)d) Not at allNot at all
8/18/2019 18 Recursive Definitions
55/57
56
Quick survey Quick survey
The pace of the lecture for thisThe pace of the lecture for this
slide set was…slide set was…
a)a) astast
b)b) !bout right !bout right
c)c) ! little slow ! little slow
d)d) Too slowToo slow
8/18/2019 18 Recursive Definitions
56/57
57
Quick survey Quick survey
"ow interesting was the material in"ow interesting was the material in
this slide set# $e honest%this slide set# $e honest%
a)a) Wow% That was &'''''' cool%Wow% That was &'''''' cool%
b)b) &omewhat interesting&omewhat interesting
c)c) (ather borting(ather borting
d)d) ********************
8/18/2019 18 Recursive Definitions
57/57
Today’s demotivators Today’s demotivators