Top Banner

of 32

CSC335-Chapter10

Jun 04, 2018

Download

Documents

frankjamison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 CSC335-Chapter10

    1/32

    CSC-335CSC-335

    ADTs & Data StructuresADTs & Data Structures

    Instructor:Instructor:

    ChristosChristosKolonisKolonis

    (Chapter 10 ADT Ipleentation:(Chapter 10 ADT Ipleentation:

    !ecursion" Al#orith Anal$sis" an%!ecursion" Al#orith Anal$sis" an%Stan%ar% Al#orithsStan%ar% Al#oriths

  • 8/13/2019 CSC335-Chapter10

    2/32

    Chapter ContentsChapter Contents

    10'1 !ecursion10'1 !ecursion10' )*aples o+ !ecursion: To,ers o+ anoi.10' )*aples o+ !ecursion: To,ers o+ anoi.

    /arsin#/arsin#

    10'3 Ipleentin# !ecursion10'3 Ipleentin# !ecursion

    10' Al#orith )++icienc$10' Al#orith )++icienc$

    10'5 Stan%ar% Al#oriths in C10'5 Stan%ar% Al#oriths in C

    10'2 /roin# Al#oriths Correct (4ptional10'2 /roin# Al#oriths Correct (4ptional

  • 8/13/2019 CSC335-Chapter10

    3/32

    Chapter 46ectiesChapter 46ecties

    !eie, recursion $ loo7in# at e*aples!eie, recursion $ loo7in# at e*aples Sho, ho, recursion is ipleente% usin# aSho, ho, recursion is ipleente% usin# a

    run-tie stac7run-tie stac7

    8oo7 at the iportant topic o+ al#orith8oo7 at the iportant topic o+ al#orith

    e++icienc$ an% ho, it is easure%e++icienc$ an% ho, it is easure% Descrie soe o+ the po,er+ul an% use+ulDescrie soe o+ the po,er+ul an% use+ul

    stan%ar% C +unction teplates in ST8stan%ar% C +unction teplates in ST8

    (4ptional Intro%uce rie+l$ the topic o+(4ptional Intro%uce rie+l$ the topic o+al#orith eri+icational#orith eri+ication

  • 8/13/2019 CSC335-Chapter10

    4/32

    10'1 !ecursion10'1 !ecursion

    A +unction is %e+ine% recursiel$ i+ it has theA +unction is %e+ine% recursiel$ i+ it has the+ollo,in# t,o parts+ollo,in# t,o parts

    An anchor or ase caseAn anchor or ase case The +unction is %e+ine% +or one or ore speci+icThe +unction is %e+ine% +or one or ore speci+ic

    alues o+ the paraeter(salues o+ the paraeter(s An in%uctie or recursie caseAn in%uctie or recursie case

    The +unction9s alue +or current paraeter(s isThe +unction9s alue +or current paraeter(s is%e+ine% in ters o+ preiousl$ %e+ine% +unction%e+ine% in ters o+ preiousl$ %e+ine% +unction

    alues an%or paraeter(salues an%or paraeter(s

  • 8/13/2019 CSC335-Chapter10

    5/32

    !ecursie )*aple!ecursie )*aple

    Consi%er a recursie po,er +unctionConsi%er a recursie po,er +unctiondouble power (double x, unsigned n)double power (double x, unsigned n){ if ( n == 0 ){ if ( n == 0 )

    return 1.0; return 1.0;

    else else

    return x * power (x, n-1); } return x * power (x, n-1); } ;hich is the anchor

  • 8/13/2019 CSC335-Chapter10

    6/32

    !ecursie )*aple!ecursie )*aple

    =ote the=ote theresults o+results o+a calla call

    !ecursie!ecursie

    callscalls !esolution!esolution

    o+ theo+ thecallscalls

    Fig 10.2

  • 8/13/2019 CSC335-Chapter10

    7/32

    AA >a%>a%?se o+ !ecursion?se o+ !ecursion

    @ionacci nuers@ionacci nuers1" 1" " 3" 5" " 13" 1" 31" 1" " 3" 5" " 13" 1" 3++

    11B 1" +B 1" +

    ((B 1 +B 1 +

    nnB +B +

    n -(n -( + +

    n -1n -1

    A recursie +unctionA recursie +unctiondouble Fib (unsigned n)double Fib (unsigned n)

    { if (n

  • 8/13/2019 CSC335-Chapter10

    8/32

    AA >a%>a%?se o+ !ecursion?se o+ !ecursion

    ;h$ is this ine++icient

  • 8/13/2019 CSC335-Chapter10

    9/32

    ?ses o+ !ecursion?ses o+ !ecursion

    >inar$ Search>inar$ Search

    SeeSee source co%esource co%e

    =ote results o+=ote results o+recursie callrecursie call

    http://codesampleschapter10.htm/http://codesampleschapter10.htm/http://codesampleschapter10.htm/
  • 8/13/2019 CSC335-Chapter10

    10/32

    0

    ?ses o+ !ecursion?ses o+ !ecursion

    /alin%roe chec7er/alin%roe chec7er A palin%roe has sae alue ,ith characters reerse%A palin%roe has sae alue ,ith characters reerse%

    1331 racecar1331 racecar

    !ecursie al#orith +or an inte#er!ecursie al#orith +or an inte#er I+I+ nu#$igit%s

  • 8/13/2019 CSC335-Chapter10

    11/32

    1

    !ecursion )*aple:!ecursion )*aple:To,ers o+ anoiTo,ers o+ anoi

    !ecursie al#orith especiall$ appropriate +or!ecursie al#orith especiall$ appropriate +orsolution $ recursionsolution $ recursion

    Tas7Tas7 oe %is7s +ro le+t pe# to ri#ht pe#oe %is7s +ro le+t pe# to ri#ht pe#

    ;hen %is7 oe%" ust e place% on a pe#;hen %is7 oe%" ust e place% on a pe#

    4nl$ one %is7 (top %is7 on a pe# oe% at a tie4nl$ one %is7 (top %is7 on a pe# oe% at a tie 8ar#er %is7 a$ neer e place% on a saller %is78ar#er %is7 a$ neer e place% on a saller %is7

  • 8/13/2019 CSC335-Chapter10

    12/32

    2

    !ecursion )*aple:!ecursion )*aple:To,ers o+ anoiTo,ers o+ anoi

    I%enti+$ ase case:I%enti+$ ase case:I+ there is one %is7 oe +ro A to CI+ there is one %is7 oe +ro A to C

    In%uctie solution +or n E 1 %is7sIn%uctie solution +or n E 1 %is7s

    oe topost n 1 %is7s +ro A to >" usin# Coe topost n 1 %is7s +ro A to >" usin# C

    +or teporar$ stora#e+or teporar$ stora#e oe +inal %is7 reainin# on A to Coe +inal %is7 reainin# on A to C

    oe the n 1 %is7 +ro > to C usin# A +oroe the n 1 %is7 +ro > to C usin# A +orteporar$ stora#eteporar$ stora#e

    Fie, co%e +or solution"Fie, co%e +or solution" @i# 10'@i# 10'

    http://codesampleschapter10.htm/http://codesampleschapter10.htm/http://codesampleschapter10.htm/
  • 8/13/2019 CSC335-Chapter10

    13/32

    3

    !ecursion )*aple:!ecursion )*aple:To,ers o+ anoiTo,ers o+ anoi

    =ote the #raphical steps to the solution=ote the #raphical steps to the solution

  • 8/13/2019 CSC335-Chapter10

    14/32

    4

    !ecursion )*aple: /arsin#!ecursion )*aple: /arsin#

    )*aples so +ar are %irect recursion)*aples so +ar are %irect recursion @unction calls itsel+ %irectl$@unction calls itsel+ %irectl$

    In%irect recursion occurs ,henIn%irect recursion occurs ,hen

    A +unction calls other +unctionsA +unction calls other +unctions

    Soe chain o+ +unction calls eentuall$ resultsSoe chain o+ +unction calls eentuall$ resultsin a call to ori#inal +unction a#ainin a call to ori#inal +unction a#ain

    An e*aple o+ this is the prole o+An e*aple o+ this is the prole o+

    processin# arithetic e*pressionsprocessin# arithetic e*pressions

  • 8/13/2019 CSC335-Chapter10

    15/32

    5

    !ecursion )*aple: /arsin#!ecursion )*aple: /arsin#

    /arser is part o+ the copiler/arser is part o+ the copiler

    Input to a copiler isInput to a copiler ischaracterscharacters >ro7en up into eanin#+ul #roups>ro7en up into eanin#+ul #roups

    I%enti+iers" resere% ,or%s" constants" operatorsI%enti+iers" resere% ,or%s" constants" operators

    These units are calle% to7ensThese units are calle% to7ens

    !eco#niGe% $ le*ical anal$Ger!eco#niGe% $ le*ical anal$Ger S$nta* rules applie%S$nta* rules applie%

  • 8/13/2019 CSC335-Chapter10

    16/32

    6

    !ecursion )*aple: /arsin#!ecursion )*aple: /arsin#

    /arser #enerates a/arser #enerates aparse tree usin# theparse tree usin# theto7ens accor%in# toto7ens accor%in# torules elo,:rules elo,:

    An e*pression:An e*pression:ter ter H ter ter H terter ter H ter ter H ter

    A ter:A ter:+actor +actor H +actor +actor H +actor+actor +actor H +actor +actor H +actor

    A +actor:A +actor:( e*pression H letter H %i#it( e*pression H letter H %i#it

    Note the indirect

    recursion

  • 8/13/2019 CSC335-Chapter10

    17/32

    7

    10'3 Ipleentin# !ecursion10'3 Ipleentin# !ecursion

    !ecall section J'" actiation recor% create%!ecall section J'" actiation recor% create%+or +unction call+or +unction call

    Actiation recor%s place% on run-tie stac7Actiation recor%s place% on run-tie stac7

    !ecursie calls #enerate stac7 o+ siilar!ecursie calls #enerate stac7 o+ siilaractiation recor%sactiation recor%s

  • 8/13/2019 CSC335-Chapter10

    18/32

    8

    Ipleentin# !ecursionIpleentin# !ecursion

    ;hen ase case reache% an% successie;hen ase case reache% an% successiecalls resole%calls resole%

    Actiation recor%s are poppe% o++ the stac7Actiation recor%s are poppe% o++ the stac7

  • 8/13/2019 CSC335-Chapter10

    19/32

    9

    10' Al#orith )++icienc$10' Al#orith )++icienc$

    o, %o ,e easure e++icienc$o, %o ,e easure e++icienc$ Space utiliGation aount o+ eor$ reuire%Space utiliGation aount o+ eor$ reuire% Tie reuire% to accoplish the tas7Tie reuire% to accoplish the tas7

    Tie e++icienc$ %epen%s on :Tie e++icienc$ %epen%s on :

    siGe o+ inputsiGe o+ input

    spee% o+ achinespee% o+ achine

    ualit$ o+ source co%eualit$ o+ source co%e

    ualit$ o+ copilerualit$ o+ copiler These vary fro one!"atfor to another

  • 8/13/2019 CSC335-Chapter10

    20/32

    0

    Al#orith )++icienc$Al#orith )++icienc$

    ;e;e cancancount the nuer o+ tiescount the nuer o+ tiesinstructions are e*ecute%instructions are e*ecute%

    This #ies us a easure o+ e++icienc$ o+ anThis #ies us a easure o+ e++icienc$ o+ anal#orithal#orith

    So ,e easure coputin# tie as:So ,e easure coputin# tie as:

    T(nT(n B coputin# tie o+ an al#orith +or input o+ siGe nB coputin# tie o+ an al#orith +or input o+ siGe n B nuer o+ ties the instructions are e*ecute% B nuer o+ ties the instructions are e*ecute%

  • 8/13/2019 CSC335-Chapter10

    21/32

    1

    )*aple: Calculatin# the ean)*aple: Calculatin# the ean

    Tas7Tas7 L ties e*ecute%L ties e*ecute%

    1'1' InitialiGe theInitialiGe the sumsumto 0to 0 11

    '' InitialiGe in%e*InitialiGe in%e* iito 0to 0 11

    3'3' ;hile;hile iiMM nn%o +ollo,in#%o +ollo,in# n1n1

    '' a A%% *NiO to sua A%% *NiO to su nn

    5'5' Increent Increent ii$ 1$ 1 nn

    2'2' !eturn!eturn mean = sum/nmean = sum/n 11

    Total 3n Total 3n

  • 8/13/2019 CSC335-Chapter10

    22/32

    2

    Coputin# Tie 4r%er o+ a#nitu%eCoputin# Tie 4r%er o+ a#nitu%e

    As nuer o+ inputs increasesAs nuer o+ inputs increases T(n B 3n #ro,s at a rate proportional toT(n B 3n #ro,s at a rate proportional to nn

    Thus T(n has the Por%er o+ a#nitu%ePThus T(n has the Por%er o+ a#nitu%eP nn

    TheThe computing timecomputing timeo+ an al#oritho+ an al#orith on inputon inputof size n,of size n,

    T(n)T(n)sai% to haesai% to hae order of magnitude f(n)order of magnitude f(n)""

    ,ritten,ritten T(n) is O(f(n))T(n) is O(f(n)) i+ i+ there is some constant C such thatthere is some constant C such that

    T(nT(n MMCC+(n +or all su++icientl$ lar#e alues o+ n+(n +or all su++icientl$ lar#e alues o+ n

  • 8/13/2019 CSC335-Chapter10

    23/32

    3

    >i# 4h =otation>i# 4h =otation

    Another ,a$ o+ sa$in# this:Another ,a$ o+ sa$in# this:Q TheThe complexitycomplexityo+ the al#orith iso+ the al#orith is4(+(n'4(+(n'

    Q )*aple: @or the ean-Calculation)*aple: @or the ean-Calculation

    Al#orith:Al#orith:

    T(n isT(n is O(n)O(n)

    Q =ote that constants an% ultiplicatie=ote that constants an% ultiplicatie+actors are i#nore%'+actors are i#nore%'

  • 8/13/2019 CSC335-Chapter10

    24/32

    4

    >i# 4h =otation>i# 4h =otation

    +(n is usuall$ siple:+(n is usuall$ siple: n" nn" n((" n" n33" '''" '''

    nn1" lo#1" lo#

    ((nn

    n lo#n lo#((nnlo#lo#

    ((lo#lo#

    ((nn

    =ote #raph=ote #raph

    o+ coono+ cooncoputin# tiescoputin# ties

  • 8/13/2019 CSC335-Chapter10

    25/32

    5

    >i# 4h =otation>i# 4h =otation

    Rraphs o+ coon coputin# tiesRraphs o+ coon coputin# ties

  • 8/13/2019 CSC335-Chapter10

    26/32

    6

    Coon Coputin# Tie @unctionsCoon Coputin# Tie @unctions

    lo#lo#lo#lo#nn lo#lo#nn nn n lo#n lo#nn nn nn33 nn

    ------ 00 11 00 11 11

    0'000'00 11

    1'001'00

    1212

    22

    1212

    1'51'5 33 22 5151 5252

    '00'00 1212 22 5252 0202 2553225532

    '3'3 55 33 120120 1010 3J23J2 2J22J2

    '5'5 22 22 33 0202 2121 1'2J)11'2J)1

    3'003'00 5252 00 2553225532 12JJJ1212JJJ12 1'15J)JJ1'15J)JJ

    3'33'3 1010 1010 100100 105J2105J2 1'0J)01'0J)0 1')301')30

    '3'3 00 105J2105J2 0J1500J150 1'1)11'1)1 1'15)11'15)1 2'J)315252'J)31525

  • 8/13/2019 CSC335-Chapter10

    27/32

    7

    Coputin# in !eal TieCoputin# in !eal Tie

    Suppose each instruction can e %one in 1Suppose each instruction can e %one in 1icrosecon%icrosecon%

    @or n B 52 inputs ho, lon# +or arious +(n@or n B 52 inputs ho, lon# +or arious +(n

    @unction@unction TieTie

    lo#lo#lo#lo#nn 3 icrosecon%s3 icrosecon%s

    8o#8o#nn icrosecon%s icrosecon%s

    nn '5 illisecon%s'5 illisecon%s

    n lo#n lo#nn illisecon%s illisecon%s

    nn 25 illisecon%s25 illisecon%s

    nn33 1J secon%s1J secon%s

    nn 3'J)23'J)2 centuries!!centuries!!

  • 8/13/2019 CSC335-Chapter10

    28/32

    8

    10'5 ST89s Al#oriths10'5 ST89s Al#oriths

    ST8 has a collection o+ ore than 0ST8 has a collection o+ ore than 0#eneric#eneric al#orithsal#oriths'' not eer +unctions o+ ST89s container classesnot eer +unctions o+ ST89s container classes

    %o not access containers %irectl$'%o not access containers %irectl$'

    The$ are stan%-alone +unctionsThe$ are stan%-alone +unctions operate on %ata $ eans o+ iterators 'operate on %ata $ eans o+ iterators '

    a7es it possile to ,or7 ,ith re#ular C-a7es it possile to ,or7 ,ith re#ular C-st$le arra$s as ,ell as containers'st$le arra$s as ,ell as containers'

  • 8/13/2019 CSC335-Chapter10

    29/32

    9

    )*aple o+)*aple o+ sortsortAl#orithAl#orith

    Coes in seeral %i++erent +orsCoes in seeral %i++erent +ors

    4ne uses the4ne uses the

  • 8/13/2019 CSC335-Chapter10

    30/32

    0

    10'2 /roin# Al#oriths Correct10'2 /roin# Al#oriths Correct

    De%uctie proo+ o+ correctness a$ eDe%uctie proo+ o+ correctness a$ ereuire%reuire%

    In sa+et$-critical s$stes ,here lies at ris7In sa+et$-critical s$stes ,here lies at ris7

    ust speci+$ust speci+$

    The P#ienP or precon%itionsThe P#ienP or precon%itions

    The Pto sho,P or post con%itionsThe Pto sho,P or post con%itions

    Pre and AlgorithmPre and Algorithm =>=>PostPost

  • 8/13/2019 CSC335-Chapter10

    31/32

    1

    )*aple: !ecursie /o,er @unction)*aple: !ecursie /o,er @unction

    @unction:@unction:double power (double x, unsigned n)double power (double x, unsigned n){ if ( n == 0 ){ if ( n == 0 )

    return 1.0; return 1.0;

    else else

    return x * power (x, n-1); } return x * power (x, n-1); }

    /recon%ition:/recon%ition: Input consists o+ a real nuerInput consists o+ a real nuer xxan% a nonne#atiean% a nonne#atie

    inte#erinte#er nn

    /ostcon%ition:/ostcon%ition:

    )*ecution o+ +unction terinates)*ecution o+ +unction terinates ;hen it terinates alue returne% is *;hen it terinates alue returne% is *nn

  • 8/13/2019 CSC335-Chapter10

    32/32

    2

    )*aple: !ecursie /o,er @unction)*aple: !ecursie /o,er @unction

    ?se atheatical in%uction on n?se atheatical in%uction on n Sho, postcon%ition +ollo,s i+Sho, postcon%ition +ollo,s i+ n = 0n = 0

    Assue +orAssue +or n = n = " e*ecution terinates an%" e*ecution terinates an%returns correct aluereturns correct alue

    ;hen calle% ,ith;hen calle% ,ith n = " 1n = " 1" in%uctie case" in%uctie case return x * power (x, n ! 1)return x * power (x, n ! 1)is e*ecute%is e*ecute%

    Falue o+Falue o+ n ! 1n ! 1isis

    It +ollo,s that ,ithIt +ollo,s that ,ith n = " 1,n = " 1,returnsreturns x * xx * x))

    ,hich euals,hich euals xx)"1)"1