CSC-335 ADT's & Data Structures (Chapter 10 - ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms)
CSC-335 ADT's & Data Structures (Chapter 10 - ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms)
Chapter Contents
10'1 !ecursion 10'( )*a ples o+ !ecursion: To,ers o+ -anoi. /arsin# 10'3 I ple entin# !ecursion 10'0 Al#orith )++icienc$ 10'5 Stan%ar% Al#orith s in C11 10'2 /ro3in# Al#orith s Correct (4ptional&
Chapter 456ecti3es
!e3ie, recursion 5$ loo7in# at e*a ples Sho, ho, recursion is i ple ente% usin# a run-ti e stac7 8oo7 at the i portant topic o+ al#orith e++icienc$ an% ho, it is easure% Descri5e so e o+ the po,er+ul an% use+ul stan%ar% C11 +unction te plates in ST8 (4ptional& Intro%uce 5rie+l$ the topic o+ al#orith 3eri+ication
10'1 !ecursion
A +unction is %e+ine% recursi3el$ i+ it has the +ollo,in# t,o parts An anchor or 5ase case
The +unction is %e+ine% +or one or 3alues o+ the para eter(s& ore speci+ic
);
?ses o+ !ecursion
>inar$ Search
See source co%e =ote results o+ recursi3e call
?ses o+ !ecursion
/alin%ro e chec7er
A palin%ro e has sa e 3alue ,ith characters re3erse% 1(303(1 racecar
!ecursi3e al#orith
+or an inte#er
I+ nu#$igit%s <= 1 return true )lse chec7 +irst an% last %i#its nu#&10nu#$igits-1 an% nu# ' 10
i+ the$ %o not atch return +alse
I+ the$ atch" chec7 ore %i#its Appl$ al#orith recursi3el$ to: nu# ' 10nu#$igits-1 an% nu#$igits -
10
Tas7
Do3e %is7s +ro le+t pe# to ri#ht pe# ;hen %is7 o3e%" ust 5e place% on a pe# 4nl$ one %is7 (top %is7 on a pe#& o3e% at a ti e 8ar#er %is7 a$ ne3er 5e place% on a s aller %is7
11
!ecursion )*a ple: To,ers o+ -anoi I%enti+$ 5ase case: I+ there is one %is7 o3e +ro A to C In%ucti3e solution +or n E 1 %is7s
Do3e top ost n 1 %is7s +ro A to >" usin# C +or te porar$ stora#e Do3e +inal %is7 re ainin# on A to C Do3e the n 1 %is7 +ro > to C usin# A +or te porar$ stora#e
12
!ecursion )*a ple: To,ers o+ -anoi =ote the #raphical steps to the solution
13
14
15
An e*pression: ter 1 ter H ter ter H ter A ter : +actor I +actor H +actor : +actor H +actor A +actor: ( e*pression & H letter H %i#it
16
17
18
10'0 Al#orith
-o, %o ,e
)++icienc$
easure e++icienc$
Space utiliGation a ount o+ e or$ reKuire% Ti e reKuire% to acco plish the tas7
Ti e e++icienc$ %epen%s on :
siGe o+ input spee% o+ achine Kualit$ o+ source co%e Kualit$ o+ co piler
These Thesevary varyfro fro one one !"atfor !"atfor to toanother another
19
Al#orith
)++icienc$
So ,e
T(n& B co putin# ti e o+ an al#orith +or input o+ siGe n B nu 5er o+ ti es the instructions are e*ecute%
20
InitialiGe the sum to 0 InitialiGe in%e* i to 0 ;hile i M n %o +ollo,in# a& A%% *NiO to su 5& Incre ent i 5$ 1 !eturn mean = sum/n Total
1 1 n11 n n 1 3n 1 0
21
Thus T(n& has the Por%er o+ a#nitu%eP n The computing time o+ an al#orith on input of size n,
T(n) sai% to ha3e order of magnitude f(n)" ,ritten T(n) is O(f(n))
22
>i# 4h =otation
Another ,a$ o+ sa$in# this: Q The complexity o+ the al#orith 4(+(n&&' Q is
ultiplicati3e
23
>i# 4h =otation
+(n& is usuall$ si ple: n" n(" n3" ''' (n 1" lo#(n n lo#(n lo#(lo#(n =ote #raph o+ co on co putin# ti es
24
>i# 4h =otation
Rraphs o+ co on co putin# ti es
25
Co
on Co putin# Ti e @unctions
lo#(lo#(n --0'00 1'00 1'5A ('00 ('3( ('5A 3'00 3'3( 0'3(
lo#(n 0 1 ( 3 0 5 2 A 10 (0
26
Co putin# in !eal Ti e
Suppose each instruction can 5e %one in 1 icrosecon% @or n B (52 inputs ho, lon# +or 3arious +(n&
@unction lo#(lo#(n 8o#(n n n lo#(n n( n3 (n 3 A '(5 ( 25 Ti e icrosecon%s icrosecon%s illisecon%s illisecon%s illisecon%s 1J secon%s 3'J1)20 centuries!!
27
not e 5er +unctions o+ ST89s container classes %o not access containers %irectl$'
28
The na e o+ a 5oolean +unction to 5e use% +or a less than See @i#' 10'S
29
a$ 5e
30
/recon%ition:
Input consists o+ a real nu 5er x an% a nonne#ati3e inte#er n
/ostcon%ition:
)*ecution o+ +unction ter inates ;hen it ter inates 3alue returne% is *n
31
Assu e +or n = )" e*ecution ter inates an% returns correct 3alue
;hen calle% ,ith n = ) " 1" in%ucti3e case return x * power (x, n ! 1) is e*ecute% Falue o+ n ! 1 is ) It +ollo,s that ,ith n = ) " 1, returns x * x) ,hich eKuals x)"1
32