You are on page 1of 50

DAAmodul

e-1

MODULEI

I
NTRODUCTI
ON
Cont
ent
s

1.Int
roduct ion
1.1.Whati sanAl gor it
hm?
1.2.Al gor ithm Speci fication
1.3.Anal ysisFramewor k
2.PerformanceAnal ysis
2.1.Spacecompl exity
2.2.Ti mecompl exity
3.Asy mptot icNot ations
3.1.Bi g-Ohnot ation
3.2.Omeganot ation
3.3.Thet anot ation
3.4.Li ttl
e- ohnot ati
on
4.Mat hemat ical anal ysisofNon- Recur
siveAl
gor
it
hms
5.Mat hemat ical anal ysisofRecur si
veAlgor
it
hms
6.I
mpor tantPr oblem Ty pes
6.1.Sorting
Searchin
6.2.g
6.3.St ringpr ocessi ng
6.4.Gr aphPr obl ems
6.5.Combi nator i
al Probl ems
7.Fundament al Dat aSt ruct ures
7.1.Li nearDat aSt ruct ures
7.2.Gr aphs
7.3.Tr ees
7.4.Set sandDi ct i
onar ies.

DeptofCSECBI
T, Page1
DAAmodul
e-1

1.I
NTRODUCTI
ON
1.
1Whati
sanAl
gor
it
hm?
I
nfor
mal
Def
ini
ti
on:
AnAl
gor
it
hm i
sanywel
l
-def
inedcomput
ati
onal
procedur
ethatt
akessomev
alue
orsetofv
aluesasi
nputandpr
oducesasetofv
aluesorsomev
alueasout
put
.Thus
al
gor
it
hm i
sasequenceofcomput
ati
onalst
epst
hatt
ransf
ormst
hei
nputi
ntot
he
out
put
.
For
mal
Def
ini
ti
on:
Anal
gor
it
hm i
sasequenceofunambi
guousi
nst
ruct
ionsf
orsol
vi
ngapr
obl
em,
i.
e.,
for
obt
aini
ngar
equi
redout
putf
oranyl
egi
ti
mat
einputi
naf
ini
teamountoft
ime.
Thi
sdef
ini
ti
oncanbei
l
lust
rat
edbyasi
mpl
edi
agr
am:

FI
GURE:
Thenot
ionoft
heal
gor
it
hm

 Therefer
encet
o―i
nst
ruct
ions‖i
nthedef
ini
ti
oni
mpl
i
est
hatt
her
eis
somethi
ngor
someonecapabl
eofunder
standi
ngandf
oll
owi
ngt
hei
nst
ruct
ionsgi
ven.
 Humanbei
ngsandt
hecomput
ersnowaday
sar
eel
ect
roni
cdev
icesbei
ng
i
nvol
vedi
nper
for
mingnumer
iccal
cul
ati
ons.
 Howev
er,t
hatal
thought
hemaj
ori
tyofal
gori
thmsar
eindeedi
ntendedf
or
ev
ent
ualcomput
eri
mpl
ement
ati
on,t
henot
ionofal
gor
it
hm doesnotdependon
suchanassumpt
ion.
I
naddi
ti
on,al
lal
gor
it
hmsshoul
dsat
isf
ythef
oll
owi
ngcr
it
eri
aorpr
oper
ti
es.

1.I
NPUT Zer
oormor
equant
it
iesar
eext
ernal
l
ysuppl
i
ed.

2.OUTPUT Atl
eastonequant
it
yispr
oduced.

3.DEFI
NITENESS Eachi
nst
ruct
ioni
scl
earandunambi
guous.

4.FINI
TENESS I fwetraceoutthei
nstr
uct
ionsofanalgor
it
hm,
thenf
oral
lcases,
theal
gor
it
hm t
erminat
esafterafi
nit
enumberofsteps.

5.EFFECTIVENESS Everyi
nst
ructi
onmustverybasi
csot
hati
tcanbecar
ri
edout
,
i
nprinci
ple,
byaper
sonusi
ngonlypenci
l&paper.

DeptofCSECBI
T, Page2
DAAmodul
e-1

Fundament
alsOfAl
gor
it
hmi
cPr
obl
em Sol
vi
ng
 Al
gor
it
hmsar
epr
ocedur
alsol
uti
onst
opr
obl
ems.Thesesol
uti
onsar
enotanswer
s
butspeci
fi
cinst
ruct
ionsf
orget
ti
nganswer
s.
 Thef
oll
owi
ngdi
agr
am br
ief
lyi
l
lust
rat
est
hesequenceofst
epsonet
ypi
cal
l
ygoes
t
hroughi
ndesi
gni
ngandanal
yzi
nganal
gor
it
hm.
 I
tincl
udest
hef
oll
owi
ngsequenceofst
eps:

Under
standi
ngt
hepr
obl
em

Deci
dingon:Computat
ional
means,
Exactvs.appr
oximatepr
obl
em
sol
vi
ng,datastr
uct
ure(
s),Al
gor
it
hm desi
gntechni
ques.

Desi
gnanal
gor
it
hm
 
Pr
ovecor
rect
ness Anal
yzet
heal
gor
it
hm

 Under
standi
ngt
hepr
obl
em

Befor
edesigni
nganal
gor
it
hm t
hemosti
mpor
tantt
hingi
stounder
standt
he
probl
em gi
ven.

Aski
ngquest
ions,doi
ngaf
ewexampl
esbyhand,
thi
nki
ngaboutspeci
alcases,
et
c.

AnI
nputt
oanal
gor
it
hm speci
fi
esani
nst
anceoft
hepr
obl
em t
heal
gor
it
hm t
hati
tsol
ves.


I
mportantt
ospeci
fyexact
lyt
her
angeofi
nst
ancest
heal
gor
it
hm needst
o
handl
e.El
seit

wi
l
lwor
kcor
rect
lyf
ormaj
ori
tyofi
nput
sbutcr
ashonsome―boundar
y‖v
alue.


Acorrectal
gor
it
hm i
snotonethatwor
ksmostoft
het
ime,
butonet
hatwor
ks
cor
rectl
yforal
ll
egi
ti
mateinput
s.
 Ascer
tai
ningt
hecapabi
l
iti
esofacomput
ati
onaldev
ice

Af
terunder
standi
ngneedt
oascer
tai
nthecapabi
l
iti
esoft
hedev
ice.

Thevastmajor
it
yofalgor
it
hmsinuset odayaresti
ll
dest
inedt
obe
progr
ammedf oracomputercl
osel
yresemblingthevonNeumannmachi
ne—a
computerar
chit
ect
ure.

VonNeumannar
chi
tect
uresar
esequent
ial
andt
heal
gor
it
hmsi
mpl
ement
edon

DeptofCSECBI
T, Page3
DAAmodul
e-1
t
hem ar
ecal
l
edsequent
ial
algor
it
hms.

Al
gor
ithmswhi
chdesi
gnedt
obeexecut
edonpar
all
elcomput
erscal
l
edpar
all
el
al
gor
it
hms.

Forver
ycompl
exal
gor
it
hmsconcentr
ateonamachi
newi
thhi
ghspeedand
morememorywher
eti
meiscr
it
ical
.
 Choosi
ngbet
weenexactandappr
oxi
mat
epr
obl
em sol
vi
ng

Forexactr
esul
t-
>exactal
gor
it
hm

Forappr
oxi
mat
eresul
t-
>appr
oxi
mat
ional
gor
it
hm.

Exampl
esofexactal
gor
it
hms:
Obt
aini
ngsquar
eroot
sfornumber
sandsol
vi
ngnon-

l
i
nearequat
ions.

Anappr
oxi
mat
ional
gor
it
hm canbeapar
tofamor
esophi
sti
cat
edal
gor
it
hm

t
hatsol
vesapr
obl
em exact
ly.
 Deci
dingonappr
opr
iat
edat
ast
ruct
ures

Al
gor
it
hmsmayormaynotdemandi
ngenui
tyi
nrepr
esent
ingt
hei
rinput
s.

I
nput
sar
erepr
esent
edusi
ngv
ari
ousdat
ast
ruct
ures.

Al
gor
it
hm +dat
ast
ruct
ures=pr
ogr
am.

 Al
gor
it
hm Desi
gnTechni
quesandMet
hodsofSpeci
fyi
nganAl
gor
it
hm

An algor
it
hm designtechnique (
or―strat
egy‖or―paradigm‖)isageneral
approachtosolv
ingprobl
emsal gori
thmi
call
ythati
sappli
cabletoavar
iet
yof
probl
emsf r
om di
ffer
entareasofcomputi
ng.

Thediff
erental
gori
thm desi
gntechni
quesare:br
utefor
ceapproach,
divi
deand
conquer,greedy method,decrease and conquer
,dy nami
c programming,
tr
ansfor
m andconquerandbackt r
acki
ng.

Methodsofspeci
fyi
nganalgori
thm:
Usi
ngnat
ural
language,
int
hismet
hod
ambigui
typr
oblem wil
lbet
here.

Thenext2opt
ionsar
e:pseudocodeandf
lowchar
t.

Pseudocode:
mixofnat
ural
andpr
ogr
ammi
ngl
anguage.Mor
epr
eci
set
hanNL

Fl
owchart
:methodofexpr
essi
nganalgori
thm bycoll
ect
ionofconnect
ed
geomet
ri
cshapescont
aini
ngdescr
ipt
ionsofthealgor
it
hm‘ssteps.

Thisr
epr
esent
ati
ont
echni
quehaspr
ovedt
obei
nconv
eni
entf
orl
arge
probl
ems.

DeptofCSECBI
T, Page4
DAAmodul
e-1

Thest
ateoft
hear
tofcomput
inghasnoty
etr
eachedapoi
ntwher
eanal
gor
it
hm‘
s

descr
ipt
ion—bei
tinanat
urall
anguageorpseudocode—canbef
edi
ntoan
el
ect
roni
ccomput
erdi
rect
ly.I
nst
ead,i
tneedst
obeconv
ert
edi
ntoacomput
er
pr
ogr
am wr
it
teni
napar
ti
cul
arcomput
erl
anguage.

Hencepr
ogr
am asyetanot
herwayofspeci
fyi
ngthealgor
it
hm,
alt
houghi
tis
pr
efer
abl
etoconsi
deri
tastheal
gori
thm‘si
mplementati
on.
 Pr
ovi
nganAl
gor
it
hm’
sCor
rect
ness

Af
terspeci
fyi
nganal
gor
it
hm wehav
etopr
ovei
tscor
rect
ness.

Thecorr
ect
nessist
oprovethatt
heal
gori
thm y
iel
dsar
equi
redr
esul
tforev
ery
l
egit
imatei
nputi
nafini
teamountoft
ime.

Forexampl
e,t
hecor
rect
nessofEucl
i
d‘sal
gor
it
hm f
orcomput
ingt
hegr
eat
est
commondi visorstemsfrom thecorr
ect nessoftheequali
tygcd(
m,n)=gcd( n,
m modn) ,t
hesi mpleobserv
ationt
hatt hesecondintegerget
ssmall
eronev ery
i
terat
ionofthealgori
thm,andthefactthatt heal
gor
it
hm stopswhenthesecond
i
ntegerbecomes0.

Forsomeal
gor
ithms,
apr
oofofcor
rect
nessi
squi
teeasy
;forot
her
s,i
tcanbe
qui
tecompl
ex.

Acommont
echni
quef
orpr
ovi
ngcor
rect
nessi
stousemat
hemat
icali
nduct
ion.

Proofbymathemat
ical
induct
ioni
smostappr
opr
iat
eforpr
ovi
ngt
hecor
rect
ness
ofanalgor
it
hm.
 Anal
yzi
nganal
gor
it
hm

Af
tercor
rect
ness,
eff
ici
encyhast
obeest
imat
ed.

Ti
meef
fi
ciencyandspaceef
fi
ciency
.

Ti
me:
howf
astt
heal
gor
it
hm r
uns?

Space:
howmuchext
ramemor
ytheal
gor
it
hm needs?

Si
mpl
i
cit
y:how si
mpl
eri
tiscompar
edt
oexi
sti
ngal
gor
it
hms.Somet
imessi
mpl
er

al
gor
it
hms ar
e al
so mor
e ef
fi
cient t
hanmor
ecompl
i
cat
edal
ter
nat
ives.
Unf
ort
unat
ely
,it
i
snotal
waystr
ue,
inwhi
chcaseaj
udi
cious
compr
omise needst
obemade.

Gener
ali
ty:
Gener
ali
tyoft
hepr
obl
em t
heal
gor
it
hm sol
vesandt
hesetofi
nput
sit
accept
s.

DeptofCSECBI
T, Page5
DAAmodul
e-1

Ifnotsat
isf
iedwit
htheset
hreepr
oper
ti
esi
tisnecessar
ytor
edesi
gnt
heal
gor
it
hm.
 Codethealgori
thm

Wr
it
ingpr
ogr
am byusi
ngpr
ogr
ammi
ngl
anguage.

Select
ionofprogr
ammi
ngl
anguageshoul
dsuppor
tthef
eat
uresment
ionedi
n
thedesignphase.

Progr
am testi
ng:Iftheinputstoalgor
it
hmsbelongtot hespecifi
edsetst
hen
requi
renov er
ifi
cati
on.Butwhi l
eimplement
ingalgor
it
hmsaspr ogramstobe
usedinact
ualappli
cati
ons,iti
srequi
redtopr
ovi
desuchv er
if
icat
ions.

Document
ati
onoft
heal
gor
it
hm i
sal
soi
mpor
tant
.

1.
2Al
gor
it
hm Speci
fi
cat
ion

Al
gor
it
hm canbedescr
ibedi
nthr
eeway
s.
1.Nat
ural
languagel
i
keEngl
i
sh:
Whent
hiswayi
schoosedcar
eshoul
dbet
aken,
we
shoul
densur
ethateach&ev
eryst
atementi
sdef
ini
te.
2.Gr
aphi
crepr
esent
ati
oncal
l
edf
lowchar
t:Thi
smet
hodwi
l
lwor
kwel
lwhent
he
al
gor
it
hm i
ssmal
l
&si
mpl
e.
3.Pseudo-
codeMet
hod:
Int
hismet
hod,
weshoul
dty
pical
l
ydescr
ibeal
gor
it
hmsas
pr
ogr
am,
whi
chr
esembl
espr
ogr
ammi
ngl
anguageconst
ruct
s
Pseudo-
CodeConv
ent
ions:
1.
Comment
sbegi
nwi
th/
/andcont
inueunt
ilt
heendofl
i
ne.
2.
Bocksar
eindi
cat
edwi
thmat
chi
ngbr
aces{
and}
.
Anidenti
fi
erbegi
nswi
thal
ett
er.Thedat
aty
pesofv
ari
abl
esar
enotexpl
i
cit
ly
3.
decl
ared.
4.
Compounddatatypescanbefor
medwi
thr
ecords.Herei
sanexampl
e,
dat
aty
pe–ndata–n;node*l
i
nk;
Node.Recor
d{dataty
pe–1dat
a-1;
...}
Her
eli
nki
sapoi
ntert
other
ecor
dty
penode.I
ndi
vi
dualdat
ait
emsofar
ecor
dcanbe
accessedwi
th→ andper
iod.
5.
Assi
gnmentofv
aluest
ovar
iabl
esi
sdoneusi
ngt
heassi
gnmentst
atement
.
<Var
iabl
e>:
=<expr
essi
on>;
6.
Ther
ear
etwoBool
eanv
aluesTRUEandFALSE.

Logi
cal
Oper
ator
sAND,
OR,
NOT
DeptofCSECBI
T, Page6
DAAmodul
e-1


Rel
ati
onalOper
ator
s<,
<=,
>,
>=,
=,!
=
7.Thef
oll
owi
ngl
oopi
ngst
atement
sar
eempl
oyed.For
,whi
l
eandr
epeat
-unt
il
Whi
l
eLoop:
Whi
l
e<condi
ti
on>do
{<st
atement
-1>...<st
atement
-n>}
ForLoop:
Forv
ari
abl
e:=v
alue-
1tov
alue-
2st
epst
epdo
{<st
atement
-1>...<st
atement
-n>}
r
epeat
-unt
il
:
r
epeat<st
atement
-1>...<st
atement
-n>unt
il
<condi
ti
on>
8.
Acondi
ti
onalst
atementhast
hef
oll
owi
ngf
orms.

I
f<condi
ti
on>t
hen<st
atement
>

I
f<condi
ti
on>t
hen<st
atement
-1>El
se<st
atement
-1>


Case
st
atement
:
Case
{:
<condi
ti
on-
1>:
<st
atement
-1>
...
:<condi
ti
on-
n>:
<st
atement
-n>
:el
se:
<st
atement
-n+1>
}
9.
Inputandout
putar
edoneusi
ngt
hei
nst
ruct
ionsr
ead&wr
it
e.
10.
Ther
eisonl
yonet
ypeofpr
ocedur
e:

Al
gor
it
hm,t
heheadi
ngt
akest
hef
orm,
Algor
it
hm Name(
Par
amet
erl
i
sts)

Asanexampl
e,t
hef
oll
owi
ngal
gor
it
hm f
iel
ds&r
etur
nst
hemaxi
mum of„
n‟gi
vennumber
s:

Al
gor
it
hm Max(
A,n)
/Ai
sanar
rayofsi
zen
{
Resul
t:=A[
1];
DeptofCSECBI
T, Page7
DAAmodul
e-1

f
ori
:
=2t
on
do
ifA[i
]>Resul
tthen
Resul
t:=A[
I]
;
r
etur
nResul
t;
}
I
nthi
salgor
it
hm (
namedMax)
,A&nar
epr
ocedur
epar
amet
ers.Resul
t&i
areLocal
v
ari
abl
es.
1.
3Anal
ysi
sFr
amewor
k

Ther
ear
etwoki
ndsofef
fi
ciency
:
♦Ti
meef
fi
ciency-i
ndi
cat
eshowf
astanal
gor
it
hm i
nquest
ionr
uns.
♦Spaceef
fi
ciency-deal
swi
tht
heext
raspacet
heal
gor
it
hm r
equi
res.

Measur
ingAnI
nputSi
ze
♦ Anal
gor
it
hm'
sef
fi
ci
encyi
sinv
est
igat
edasaf
unct
ionofsomepar
amet
er

n‘i
ndi
cat
ingt
heal
gor
it
hm'
sinputsi
ze.
♦I
nmostcases,
sel
ect
ingsuchapar
amet
eri
squi
test
rai
ght
for
war
d.
♦Forexampl
e,i
twi
l
lbet
hesi
zeoft
hel
i
stf
orpr
obl
emsofsor
ti
ng,
sear
chi
ng,
findi
ng
t
hel
i
st'
ssmal
l
estel
ement
,andmostot
herpr
obl
emsdeal
i
ngwi
thl
i
sts.
♦Fort
hepr
obl
em ofev
aluat
ingapol
ynomi
alp(
x)ofdegr
een,
itwi
l
lbet
hepol
ynomi
al'
s
degr
ee
ort
henumberofi
tscoef
fi
cient
s,whi
chi
slar
gerbyonet
hani
tsdegr
ee.
♦Ther
ear
esi
tuat
ions,
ofcour
se,
wher
ethechoi
ceofapar
amet
eri
ndi
cat
ingani
nput
si
zedoesmat
ter
.
 Exampl
e-comput
ingt
hepr
oductoft
won-
by-
nmat
ri
ces.
Ther
ear
etwo
nat
ural
measur
esofsi
zef
ort
hispr
obl
em.
 Themat
ri
xor
dern.
 Thet
otal
numberofel
ement
sNi
nthemat
ri
cesbei
ngmul
ti
pli
ed.
♦Si
ncet
her
eisasi
mpl
efor
mul
arel
ati
ngt
heset
womeasur
es,wecaneasi
l
yswi
tch
f
rom one t
othe ot
her
,butt
he answeraboutan al
gor
it
hm'
s ef
fi
ciency wi
l
lbe
qual
i
tat
ivel
ydi
ff
erentdependi
ngonwhi
choft
het
womeasur
esweuse.
♦Thechoi
ceofanappr
opr
iat
esi
zemet
ri
ccanbei
nfl
uencedbyoper
ati
onsoft
he
al
gor
it
hm i
nquest
ion.Forexampl
e,howshoul
dwemeasur
eani
nput
'ssi
zef
oraspel
l
-
DeptofCSECBI
T, Page8
DAAmodul
e-1

checki
ngal
gor
it
hm?I
ftheal
gor
it
hm exami
nesi
ndi
vi
dualchar
act
ersofi
tsi
nput
,then
weshoul
dmeasur
ethesi
zebyt
henumberofchar
act
ers;i
fitwor
ksbypr
ocessi
ng
wor
ds,
weshoul
dcountt
hei
rnumberi
nthei
nput
.
♦ Weshoul
dmakeaspeci
alnot
eaboutmeasur
ingsi
zeofi
nput
sforal
gor
it
hms
i
nvol
vi
ngpr
oper
ti
esofnumber
s(e.
g.,
checki
ngwhet
heragi
veni
ntegerni
spr
ime)
.

Uni
tsForMeasur
ingRunTi
me

♦Wecansi
mpl
yusesomest
andar
duni
toft
imemeasur
ement
-asecond,
ami
l
li
second,
andsoon-
tomeasur
ether
unni
ngt
imeofapr
ogr
am i
mpl
ement
ingt
heal
gor
it
hm.
♦Ther
ear
eobv
iousdr
awbackst
osuchanappr
oach.Theyar
e
♦Dependenceont
hespeedofapar
ti
cul
arcomput
er
♦Dependenceont
hequal
i
tyofapr
ogr
am i
mpl
ement
ingt
heal
gor
it
hm
♦Thecompi
l
erusedi
ngener
ati
ngt
hemachi
necode
♦Thedi
ff
icul
tyofcl
ocki
ngt
heact
ual
runni
ngt
imeoft
hepr
ogr
am.
♦Si
ncewear
einneedt
omeasur
eal
gor
it
hm ef
fi
ciency
,weshoul
dhav
eamet
ri
cthat
doesnotdependont
heseext
raneousf
act
ors.
♦Onepossi
bleappr
oachi
stocountt
henumberoft
imeseachoft
heal
gor
it
hm'
s
oper
ati
onsi
sexecut
ed.Thi
sappr
oachi
sbot
hdi
ff
icul
tandunnecessar
y.
♦ Themainobj
ect
ivei
stoi
dent
if
ythemosti
mpor
tantoper
ati
onoft
heal
gor
it
hm,
cal
l
edthe
basi
coper
ati
on,
theoper
ati
oncont
ri
but
ingt
hemostt
othet
otal
runni
ngt
ime,
and
comput
ethenumberoft
imest
hebasi
coper
ati
oni
sexecut
ed.
♦Asar
ule,
iti
snotdi
ff
icul
ttoi
dent
if
ythebasi
coper
ati
onofanal
gor
it
hm.
Fore.
g.,
Thebasi
coper
ati
oni
susual
l
ythemostt
ime-
consumi
ngoper
ati
oni
nthe
al
gor
it
hm‘
sinner
mostl
oop

♦Consi
dert
hef
oll
owi
ngexampl
e:
Letcopbet
heex
ecut
iont
imeofanal
gor
it
hm‘
sbasi
coper
ati
ononapar
ti
cul
ar
comput
er etC(
,andl n)bet
henumberoft
imest
hisoper
ati
onneedst
obeexecut
edf
or
t
hisal
gor
it
hm.Thenwecanest
imat
ether
unni
ngt
imeT(
n)ofapr
ogr
am i
mpl
ement
ing
t
hisal
gor
it
hm ont
hatcomput
erbyt
hef
ormul
a
T(
n)≈copC(
n).
Tot
alnumberofst
epsf
orbasi
coper
ati
onexecut
ion,
C(n)=n
DeptofCSECBI
T, Page9
DAAmodul
e-1

Or
der
sOfGr
owt
h

♦Adi
ff
erencei
nrunni
ngt
imesonsmal
linput
sisnotwhatr
eal
l
ydi
sti
ngui
shesef
fi
cient
al
gor
it
hmsf
rom i
nef
fi
ci
entones.
♦Whenwehav
etocomput
e,f
orexampl
e,t
hegr
eat
estcommondi
vi
soroft
wosmal
l
number
s,i
tisnoti
mmedi
atel
ycl
earhow muchmor
eef
fi
cientEucl
i
d‘sal
gor
it
hm i
s
compar
edt
otheot
hert
woal
gor
it
hmsdi
scussedi
npr
evi
oussect
ionorev
enwhywe
shoul
dcar
ewhi
choft
hem i
sfast
erandbyhowmuch.I
tisonl
ywhenwehav
etof
ind
t
hegr
eat
estcommondi
vi
soroft
wol
argenumber
sthatt
hedi
ff
erencei
nal
gor
it
hm
ef
fi
cienci
esbecomesbot
hcl
earandi
mpor
tant
.
♦Forl
argev
aluesofn,
iti
sthef
unct
ion‘
sor
derofgr
owt
hthatcount
s:l
ookatTabl
e
whi
chcont
ainsv
aluesofaf
ewf
unct
ionspar
ti
cul
arl
yimpor
tantf
oranal
ysi
sof
al
gor
it
hms.

Tabl
e:Val
ues(
someappr
oxi
mat
e)ofsev
eral
funct
ionsi
mpor
tantf
oranal
ysi
sofal
gor
it
hms


Wor
st-
Case,
Best
-Case,
Aver
ageCaseEf
fi
cienci
es

♦Al
gor
it
hm ef
fi
ci
encydependsont
hei
nputsi
zen.Andf
orsomeal
gor
it
hmsef
fi
ciency
notonl
yoni
nputsi
zebutal
soont
hespeci
fi
csofpar
ti
cul
arort
ypeofi
nput
.
♦Wehav
ebest
,wor
st&av
eragecaseef
fi
cienci
es.
Wor
st-
caseef
fi
ciency
:Ef
fi
ciency(
numberoft
imest
hebasi
coper
ati
onwi
l
lbeexecut
ed)
f
ort
hewor
stcasei zen.i
nputofsi .e.Theal
gor
it
hm r
unst
hel
ongestamongal
lpossi
ble
i
nput
sofsi
zen.
Best
-caseef
fi
ciency
:Ef
fi
ciency(
numberoft
imest
hebasi
coper
ati
onwi
l
lbeexecut
ed)

DeptofCSECBI
T, Page
10
DAAmodul
e-1

f
ort
hebestcasei zen.i
nputofsi .e.Theal
gor
it
hm r
unst
hef
ast
estamongal
lpossi
ble
i
nput
sofsi
zen.Av
erage-
caseef
fi
ciency
:Av
eraget
imet
aken(
numberoft
imest
he
basi
coper
ati
onwi
l
lbeexecut
ed)t
osol
veal
lthepossi
blei
nst
ances(
random)oft
he
i
nput
.
NOTE:
Iti
snott
heav
erageofwor
standbestcase.
♦Exampl
e:Sequent
ialsear
ch.Thi
sisast
rai
ght
for
war
dal
gor
it
hm t
hatsear
chesf
ora
gi
veni
tem (
somesear
chkeyK)i
nal
i
stofnel
ement
sbychecki
ngsuccessi
veel
ement
s
oft
hel
i
stunt
ilei
theramat
chwi
tht
hesear
chkeyi
sfoundort
hel
i
sti
sexhaust
ed.
♦ ♦Her
eist
heal
gor
it
hm'
spseudocode,i
nwhi
ch,f
orsi
mpl
i
cit
y,al
i
sti
s
i
mpl
ement
edasanar
ray
.(I
tal
soassumest
hatt
hesecondcondi
ti
onwi
l
lnotbe
checkedi
fthef
ir
stone,whi
chcheckst
hatt
hear
ray
'si
ndexdoesnotexceedi
ts
upperbound,
fai
l
s.)

Cl
ear
ly,
ther
unni
ngt
imeoft
hisal
gor
it
hm canbequi
tedi
ff
erentf
ort
hesamel
i
st
si
zen.Wor
stcaseef
fi
ciency
♦Thewor
st-
caseef
fi
ciencyofanal
gor
it
hm i
sit
sef
fi
ciencyf
ort
hewor
st-
casei
nputof
si
zen,whi
chi
sani
nput(
ori
nput
s)ofsi
zenf
orwhi
cht
heal
gor
it
hm r
unst
hel
ongest
amongal
lpossi
blei
nput
soft
hatsi
ze.
♦I
nthewor
stcaset
hei
nputmi
ghtbei
nsuchawayt
hatt
her
ear
enomat
chi
ng
el
ement
sort
hef
ir
stmat
chi
ngel
ementhappenst
obet
hel
astoneont
hel
i
st,i
nthi
s
caset
heal
gor
it
hm makest
hel
argestnumberofkeycompar
isonsamongal
lpossi
ble
i
nput
sofsi
zen:
Cwor
st(
n)=n.

DeptofCSECBI
T, Page
11
DAAmodul
e-1

♦Thewayt
odet
ermi
nei
squi
test
rai
ght
for
war
d
♦ Toanal
yzet
heal
gor
it
hm t
oseewhatki
ndofi
nput
syi
eldt
hel
argestv
alueoft
he
basi
c
oper
ati
on'
scountC(
n)amongal
lpossi
blei
nput
sofsi
zenandt
hencomput
ethi
swor
st-
casev
alueCwor
st(
n).
♦ Thewor st
-caseanal
ysi
spr
ovi
desv
eryi
mpor
tanti
nfor
mat
ionaboutanal
gor
it
hm'
s
eff
ici
ency
byboundi
ngi
tsr
unni
ngt
imef
rom abov
e.I
not
herwor
ds,
itguar
ant
eest
hatf
orany
i
nst
anceofsi
zen,t
her
unni
ngt
imewi
l
lnotexceedCwor
st(
n)i
tsr
unni
ngt
imeont
he
wor
st-
casei
nput
s.
BestcaseEf
fi
ciency
♦ Thebest
-caseef
fi
ciencyofanal
gor
it
hm i
sit
sef
fi
ciencyf
ort
hebest
-casei
nputof
si
zen,whi
chi
sani
nput(
ori
nput
s)ofsi
zenf
orwhi
cht
heal
gor
it
hm r
unst
hef
ast
est
amongal
lpossi
blei
nput
soft
hatsi
ze.
♦Wecananal
yzet
hebestcaseef
fi
ciencyasf
oll
ows.

Fi
rst
,det
ermi
net
heki
ndofi
nput
sforwhi
cht
hecountC(
n)wi
l
lbet
hesmal
l
estamong
al
lpossi
blei
nput
sofsi
zen.(
Not
ethatt
hebestcasedoesnotmeant
hesmal
l
esti
nput
;
i
tmeanst
hei
nputofsi
zenf
orwhi
cht
heal
gor
it
hm r
unst
hef
ast
est
.)
♦Thenascer
tai
nthev
alueofC(
n)ont
hesemostconv
eni
enti
nput
s.
♦Exampl
e-f
orsequent
ial
sear
ch,
best
-casei
nput
swi
l
lbel
i
stsofsi
zenwi
tht
hei
rfi
rst
el
ementequal
toasear
chkey
;accor
dingl
y,Cbest(
n)=1.
♦Theanal
ysi
soft
hebest
-caseef
fi
ciencyi
snotnear
lyasi
mpor
tantast
hatoft
he
wor
st-
caseef
fi
ci
ency
.
♦Buti
tisnotcompl
etel
yusel
ess.Forexampl
e,t
her
eisasor
ti
ngal
gor
it
hm (
inser
ti
on
sor
t)f
or
whi
cht
hebest
-casei
nput
sar
eal
readysor
tedar
ray
sonwhi
cht
heal
gor
it
hm wor
ksv
ery
f
ast
.
♦Thus,suchanal
gor
it
hm mi
ghtwel
lbe t
hemet
hodofchoi
cef
orappl
i
cat
ions
deal
i
ngwith
al
mostsor
tedar
ray
s.And,
ofcour
se,
ift
hebest
-caseef
fi
ciencyofanal
gor
it
hm i
s
unsat
isf
act
ory
,wecani
mmedi
atel
ydi
scar
ditwi
thoutf
urt
heranal
ysi
s.
Av
eragecaseef
fi
ciency
♦I
tyi
eldst
hei
nfor
mat
ionaboutanal
gor
it
hm aboutanal
gor
it
hm‗
sbehav
iourona
DeptofCSECBI
T, Page
12
DAAmodul
e-1

―t
ypi
cal
andr
andom i
nput
.
♦Toanal
yzet
heal
gor
it
hm'
sav
erage-
caseef
fi
ciency
,wemustmakesomeassumpt
ions
aboutpossi
blei
nput
sofsi
zen.
♦Thei
nvest
igat
ionoft
heav
eragecaseef
fi
ciencyi
sconsi
der
abl
ymor
edi
ff
icul
tthan
i
nvest
igat
ionoft
hewor
stcaseandbestcaseef
fi
ciency
.
♦I
tinv
olv
esdi
vi
dingal
linst
ancesofsi
zen.
int
osev
eralcl
assessot
hatf
oreach
i
nst
anceoft
hecl
asst
henumberoft
imest
heal
gor
it
hm'
sbasi
coper
ati
oni
sexecut
edi
s
t
hesame.
♦Thenapr
obabil
i
tydist
ri
buti
onofi
nput
sneedstobeobtai
nedorassumedsothatthe
expect
edv
alueofthebasi
coper
ati
on'
scountcant
henbederi
ved.Theav
eragenumber
ofkeycompar
isonsCavg(
n)canbecomput
edasf
oll
ows,
♦Letusconsi
deragai
nsequent
ial
sear
ch.Thest
andar
d
assumpt
ionsar
e,♦Letpbet
hepr
obabi
l
ityofsuccessf
ulsear
ch.
t
♦I
nthecaseofasuccessf
ulsear
ch,
thepr
obabi
l
ityoft
hef
ir
stmat
choccur
ri
ngi
nthei
h
posi
ti
onoft
hel
i
sti
sp/
nforev
eryi
,wher
e1<=i
<=nandt
henumberofcompar
isons
madebyt
heal
gor
it
hm i
nsuchasi
tuat
ioni
sobv
iousl
yi.
♦I
nthecaseofanunsuccessf
ulsear
ch,
thenumberofcompar
isonsi
snwi
tht
he
pr
obabi
l
ityofsuchasear
chbei
ng(
1-p)
.Ther
efor
e,

Exampl
e,i
fp=1(
i.
e.,
thesear
chmustbesuccessf
ul)
,theav
eragenumberofkey
compar
isonsmadebysequent
ial
sear
chi
s(n+1)/
2.
♦I
fp=0(
i.
e.,
thesear
chmustbeunsuccessf
ul)
,theav
eragenumberofkey
compar
isonswi
l
lbenbecauset
heal
gor
it
hm wi
l
linspectal
lnel
ement
sonal
lsuch
i
nput
s.
DeptofCSECBI
T, Page
13
DAAmodul
e-1

Recapi
tul
ati
onoft
heAnal
ysi
sFr
amewor
k
1 Bothti
meandspaceef
fi
cienci
esar
emeasur
edasf
unct
ionsoft
heal
gor
it
hm‘
s
i
nputsi
ze.
2 Ti
meef
fi
ciencyi
smeasur
edbycount
ingt
henumberoft
imest
heal
gor
it
hm‘
sbasi
c
oper
ati
oni
sexecut
ed.Spaceef
fi
ciencyi
smeasur
edbycount
ingt
henumberof
ext
ramemor
yuni
tsconsumedbyt
heal
gor
it
hm.
3 Theef
fi
cienci
esofsomeal
gor
it
hmsmaydi
ff
ersi
gni
fi
cant
lyf
ori
nput
soft
hesame
si
ze.Forsuchal
gor
it
hms,weneedt
odi
sti
ngui
shbet
weent
hewor
st-
case,av
erage-
case,
andbest
-caseef
fi
cienci
es.
4 Theframewor
k‘spr
imar
yint
erestl
i
esi
ntheor
derofgr
owt
hoft
heal
gor
it
hm‘
s
runni
ngti
me
(
ext
ramemor
yuni
tsconsumed)asi
tsi
nputsi
zegoest
oinf
ini
ty.

2.PERFORMANCEANALYSI
S
2.
1Spacecompl
exi
ty

 Tot
alamountofcomput
ermemor
yrequi
redbyanal
gor
it
hm t
ocompl
etei
ts
execut
ioni
scal
l
edasspacecompl
exi
tyoft
hatal
gor
it
hm.

 TheSpacer
equi
redbyanal
gor
it
hm i
sthesum off
oll
owi
ngcomponent
s

Afixedpartt
hati
sindependentoftheinputandout
put
.Thi
sincl
udesmemor
y
spaceforcodes,
vari
abl
es,constantsandsoon.

Avar
iabl
epartthatdependsontheinput
,outputandr
ecur
sionst
ack.(Wecal
l
t
heseparametersasinst
ancecharact
eri
sti
cs)
 Spacer
equi
rementS(
P)ofanal
gor
ihm P,
t

S(
P)=c+Sp

eci
wher saconstantdependsont
hefi
xedpart
,Spi
sthei
nst
ancechar
act
eri
sti
cs
Exampl
e-1:Consi
derfol
lowingal
gor
it
hm abc(
)
Al
gor
it
hm abc(
a,b,
c)
{
r
etur
na+b++*
c+(
a+b-
c)/
(a+b)+4.
0;
}

DeptofCSECBI
T, Page
14
DAAmodul
e-1

Herefi
xedcomponentdependsont
hesi
zeofa,
bandc.Al
soi
nst
ancechar
act
eri
sti
cs
Sp=0

Exampl
e-2:
Letusconsi
dert
heal
gor
it
hm t
ofi
ndsum ofar
ray
.
Fort
heal
gori
thm gi
venher
ethepr
obl
em i
nst
ancesar
echar
act
erzedbyn,t
i henumber
ofel
ement
stobesummed.Thespaceneededbya[]dependsonn.Sot hespace
compl
exi
tycanbewr
it
t Ssum(
enas; n)≥(
n+3)nf
ora[]
,Oneeachf
orn,
iand

2.
2Ti
meCompl
exi
ty

 Thet
imeT(
p)t
akenbyapr
ogr
am Pi
sthesum oft
hecompi
l
eti
meandt
her
un
t
ime(
execut
iont
ime)

Thecompi
let
imedoesnotdependontheinst
ancecharact
eri
sti
cs.Alsowemay
assumet
hatacompil
edprogr
am wil
lberunsever
alti
meswi t
houtrecompi
lat
ion.

Ther
unt
imei
sdenot
edbyt
p(i
nst
ancechar
act
eri
sti
cs)
.

 Usuall
y,theexecuti
ontimeorr un-
ti
meoft hepr ogr
am i
sr ef
eredasi
tst
ime
complexi
tydenotedbytp(inst
ancecharacter
ist
ics)
.Thisi
sthesum oft
het
ime
takentoexecut
ealli
nst
ruct
ionsint
heprogram.

Exact runti
me t
ask,ast henumberofi nstructi
on
est
imat i
on is acompl ex executed
i
sdependentont heinputdata. dif
fer
enti
nstr
ucti
onswil
ltakedifferentt imeto
Also execute.
Sofort he oftheti
me
esti
mat ion complexi
ty wecountonl
ythenumberofpr ogr am st eps.
progra il ooseldefine assynt
acti
cal
lyorsemant i
cal l
ymeani ng
Am st
eps y d segmentof
th progr a hasan thatisindependentofi nstance
e m thatexecuti
on t
ime char acteristi
cs.
Forexampl
ecommenthaszer
ost
eps;assi
gnmentst
atementhasonest
epandi
n
ani
ter
ati
vest
atementsuchast
hef
or,whi
l
e&r
epeat
-unt
ilst
atement
s,t
hest
ep

DeptofCSECBI
T, Page
15
DAAmodul
e-1

counti
scount
edf
ort
hecont
rol
par
toft
hest
atement
 Wecandet
ermi
net
hest
epsneededbyapr
ogr
am t
osol
veapar
ti
cul
arpr
obl
em
i
nst
ancei
ntwoway
s

Inthef i
rstmethodwei nt
roduceanew v ari
ablecountt
otheprogram whi
chis
i
niti
ali
zed to zer
o.We also int
roduce st
atementst oi
ncrementcountbyan
appropri
ate amountint
ot he program.So when each ti
me ori
ginalpr
ogram
executes,hecountal
t soincrementedbythestepcount
.

Example-
1:Consi
dert
heal
gor
ihm Sum()
t .Af
tert
hei
ntr
oduct
ionoft
hecountt
he
progr
am wil
lbeasfol
l
ows.

Fr
om t
heabov
ewecanest
imat
ethati
nvocat
ionofSum()execut
est
otal
numberof2n+3st
eps.

Exampl
e-2:
Consi
dert
heal
gor
it
hm t
hatcomput
esSum ofnnumber
srecur
siv
ely


Whenanal
yzi
ngar
ecur
siv
epr
ogr
am f
ori
tsst
epcount
,weof
tenobt
ain
DeptofCSECBI
T, Page
16
DAAmodul
e-1
ar
ecur
siv
efor
mul
afort
hest
epcount
,forexampl
e


Theser ecursi
veformulasareref
err
edtoasr ecurrencer
elati
ons.Onewayof
solvi
ngi stomaker epeat
edsubsti
tut
ionsforeachoccurrenceofthefunct
ion
tRSum ontheright
-handsideunti
lal
lsuchoccurrencesdi
sappear




So,
thest
epcountf
orRSum i
s2n+2

Thesecondmethodtodet
erminet
hestepcountofanal
gor
it
hm ist
obuilda
tabl
einwhichweli
stt
hetotal
numberofst
epscontri
but
esbyeachstat
ement.

Fir
stdeter
minet
henumberofst
epsperexecut
ion(
s/e)oft
hest
atementand
thetot
al
numberoft
imes(
ie.
,fr
equency
)eachst
atementi
sexecut
ed.

Bycombini
ngthesetwoquant
it
ies,t
hetot
alcont
ri
but
ionofal
lst
atement
s,
thest
epcountfort
heenti
real
gorit
hm i
sobtai
ned.

Exampl
e1:
Consi
dert
heal
gor
ihm sum()
t .

Tabl
e:St
ept
abl
efort
heSum(
)al
gor
it
hm

DeptofCSECBI
T, Page
17
DAAmodul
e-1

Exampl
e2:
Consi
dert
heal
gor
it
hm t
hatcomput
esSum ofnnumber
srecur
siv
ely

Tabl
e:St
ept
abl
efort
heRSum(
)al
gor
it
hm

3.ASYMPTOTI
CNOTATI
ONS

The ef
fi
ciency anal
ysi
sfr
amewor
k concent
rat
es on t
he or
derofgr
owt
h ofan
al
gor
it
hm‘
sbasi
coper
ati
oncountast
hepr
inci
pal
indi
cat
oroft
heal
gor
it
hm‘
sef
fi
ciency
.
Tocompar
eandr
anksuchor
der
sofgr
owt
h,comput
ersci
ent
ist
suset
hreenot
ati
ons:
O(
bigoh)
,Ω(
bigomega)
,and
Θ(
bigt
het
a).
I
nfor
mal
Int
roduct
ion
I
nfor
mal
l
y,O(
g(n)
)ist
hesetofal
lfunct
ionswi
thal
owerorsameor
derof
gr hasg(
owt n)(
towi
thi
naconst
antmul
ti
ple,asngoest
oinf
ini
ty)
.Thus,t
ogi
veaf
ew
exampl
es,
thef
oll
owi
ngasser
ti
onsar
eal
ltr
ue:

I
ndeed,
thef
ir
stt
wof
unct
ionsar
eli
nearandhencehav
eal
oweror
derofgr
owt
hthang(
n)
2 2
=n ,
whi
l
ethel
astonei
squadr
ati
candhencehast
hesameor
derofgr hasn .Ont
owt he
ot
herhand,
DeptofCSECBI
T, Page
18
DAAmodul
e-1

3 3
I
ndeed,
thef
unct
ionsn and0.
00001n ar
ebot
hcubi
candhencehav
eahi
gheror
derof
2 4
gr
owt
hthann ,
andsohast
hef
our
th-
degr
eepol
ynomi
aln +n+1.
Thesecondnot
ati Ω(
on, g(
n))
,st
andsf
ort
hesetofal
lfunct
ionswi
thahi
gherorsame
or
derofgr hasg(
owt n)(
towi
thi
naconst
antmul
ti
pl asngoest
e, oinf
ini
ty)
.
Forexampl
e,

Fi
nal
l
y,Θ(
g(n)
)ist
hesetofal
lfunct
ionst
hathav
ethesameor
derofgr hasg(
owt n).

Thedef
ini
ti
oni
sil
l
ust
rat
edi
nthef
oll
owi
ngf
igur
e.

DeptofCSECBI
T, Page
19
DAAmodul
e-1

Thedef
ini
ti
oni
sil
l
ust
rat
edi
nthef
oll
owi
ngf
igur
e.

Exampl
es:
Ref
ercl
assnot
es

Usef
ulpr
oper
tyi
nvol
vi
ngt
heasy
mpt
oti
cnot
ati
ons

DeptofCSECBI
T, Page
20
DAAmodul
e-1

Usingt
hef ormaldef
init
ionsoftheasy
mpt ot
icnotat
ions,
wecanprov
et hei
rgeneral
pr
opert
ies.Thefol
lowingproper
ty,i
npart
icul
ar,i
susefuli
nanal
yzi
ngalgori
thmsthat
compr i
setwoconsecuti
vel
yexecutedpar
ts

DeptofCSECBI
T, Page
21
DAAmodul
e-1

Basi
cef
fi
ciencycl
asses
Thet
imeef
fi
ci
enci
esofal
argenumberofal
gor
it
hmsf
all
int
oonl
yaf
ewcl
asses.

DeptofCSECBI
T, Page
22
DAAmodul
e-1

DeptofCSECBI
T, Page
23
DAAmodul
e-1

4.MATHEMATI
CALANALYSI
SOFNON-
RECURSI
VEALGORI
THMS

Gener
alpl
anf
oranal
yzi
ngef
fi
ciencyofnon-
recur
siv
eal
gor
it
hms:

1.
Deci
deonpar
amet
erni
ndi
cat
ingt
hei
nputsi
zeoft
heal
gor
it
hm.
2.
Ident
if
yal
gor
it
hm‘
sbasi
coper
ati
on.
3.Checkwhet
hert
henumberoft
imest
hebasi
coper
ati
oni
sexecut
eddependsonl
yon
t
hei
nputsi
zen.I
fital
sodependsont
het
ypeofi
nput
,theni
nvest
igat
ewor
st,
aver
age,
andbestcaseef
fi
ciencysepar
atel
y.
4.Setupsummat
ionf
orC(
n)r
efl
ect
ingt
henumberoft
imest
heal
gor
it
hm‘
sbasi
c
oper
ati
oni
sexecut
ed.
5.Si
mpl
i
fysummat
ionusi
ngst
andar
dfor
mul
as(
Ref
ercl
assnot
esf
orf
ormul
asused
f
orsi
mpl
i
fyi
ng)
.

Exampl
e:Fi
ndi
ngt
hel
argestel
ementi
nagi
venar
ray

Anal
ysi
s:

DeptofCSECBI
T, Page
24
DAAmodul
e-1

1.
Inputsi
ze:
thenumberofel
ement
s=n(
sizeoft
hear
ray
)
2.
Twooper
ati
onscanbeconsi
der
edt
obeasbasi
coper
ati
oni
.e.
,
a)
Compar
ison:
:
A[i
]>maxv
al.
b)
Assi
gnment:
:max
val
←A[
i]
.
Heret
hecompar
isonst
atementi
sconsi
der
edt
obet
hebasi
coper
ati
onoft
he
al
gori
thm.
3.Nobest
,wor
st,
aver
agecases-becauset
henumberofcompar
isonswi
l
lbesamef
or
al
lar
ray
sofsi
zenandi
tisnotdependentont
ypeofi
nput
.
4.LetC(
n)denot
esnumberofcompar
isons:Al
gor
it
hm makesonecompar
isonon
eachexecut
ionoft
hel
oop,whi
chi
srepeat
edf
oreachv
alueoft
hel
oop‘
svar
iabl
ei
wi
thi
nthebound
bet
ween1andn–1.

DeptofCSECBI
T, Page
25
DAAmodul
e-1

Exampl
e:El
ementuni
quenesspr
obl
em-
Checkswhet
hert
heel
ement
sint
hear
rayar
e
di
sti
nctornot
.

Anal
ysi
s
1.
Inputsi
ze:
numberofel
ement
s=n(
sizeoft
hear
ray
)
2.
Basi
coper
ati
on:Compar
ison
3.
Best
,wor
st,
aver
agecasesexi
sts

•Ar
raywi
thnoequal
element
s
•Ar
raywi
thl
astt
woel
ement
sar
etheonl
ypai
rofequal
element
s
4.LetC(n)denot
esnumberofcompar
isonsi
nwor
stcase:
Algor
it
hm makesone
compari
sonfor
eachr
epet
it
ionoft
hei
nner
mostl
oopi
.e.
,foreachv
alueoft
hel
oop‘
svar
iabl
ejbet
ween
i
tsl
i
mit
si+1andn–1;andt
hisi
srepeat
edf
oreachv
alueoft
heout
erl
oopi
.e,f
or
eachv
alueoft
hel
oop‘
svar
iabl
eibet
weeni
tsl
i
mis0andn–2.
t

DeptofCSECBI
T, Page
26
DAAmodul
e-1

5.
Simpl
i
fyt
hesummat
ionusi
ngst
andar
dfor
mul
asasf
oll
ows:

Exampl
e3:Gi
v won×nmat
ent rcesAandB,f
i indt
het
imeef
fi
ciencyoft
hedef
ini
ti
on-
basedal
gor
it
hm f
orcomput
ingt
hei oductC=AB.Bydef
rpr ini
ton,Ci
i sann×nmat
ri
x
whoseel
ement
sar
ecomput
edast
hescal
ar(
dot
)pr
oduct
soft
her
owsofmat
rxAand
i
t
he col
umns of mat
ri
x B:

DeptofCSECBI
T, Page
27
DAAmodul
e-1

Anal
ysi
s:

1.
Wemeasur
eani
nput
‘ssi
zebymat
ri dern.
xor
2.Ther
ear
etwoar
it
hmet
icaloper
ati
onsi
nthei
nner
mostl
oopher
e—mul
ti
pli
cat
ion
andaddi
ti
on—t
hat
,inpr
inci
ple,cancompet
efordesi
gnat
ionast
heal
gor
it
hm‘
sbasi
c
oper
ati
on.
Act
ual
l
y,wedonothav
etochoosebet
weent
hem,becauseoneachr
epet
it
ionoft
he
i
nner
mostl
oopeachoft
het
woi
sexecut
edexact
lyonce.Sobycount
ingonewe
aut
omat
ical
l
ycountt
heot
her
.Hence,
weconsi
dermul
ti
pli
cat
ionast
hebasi
coper
ati
on.
3.Letussetupasum f
ort
het
otalnumberofmul
ti
pli
cat
ionsM(
n)execut
edbyt
he
al
gor
it
hm.(
Sincet
hiscountdependsonl
yont
hesi
zeoft
hei
nputmat
ri
ces,wedonot
hav
etoi
nvest
igat
ethewor
st-
case,
aver
age-
case,
andbest
-caseef
fi
cienci
essepar
atel
y.
)
4.Obv
iousl
y,t
her
eisj
ustonemul
ti
pli
cat
ionexecut
edoneachr
epet
it
ionoft
he

DeptofCSECBI
T, Page
28
DAAmodul
e-1
al
gor
it
hm‘
s
i
nner
mostloop,whi
chi
sgovernedbythevar
i ekr
abl angingfrom t
hel
owerbound0to
heupperboundn−1.Ther
t efore,
thenumberofmult
ipl
icati
onsmadeforever
ypai
rof
specif
icval
uesofvari esi
abl andj i
s

Now,
wecancomput
ethi
ssum byusi
ngf
ormul
a(S1)andr
ule(
R1)
.St
art
ingwi
tht
he
i mostsum,
nner whi
chi
sequal
ton,
weget

Exampl
e4:
Thef
oll
owi
ngal
gor
it
hm f
indst
henumberofbi
nar
ydi
git
sint
hebi
nar
y
r
epr
esent
ati
onofaposi
ti
vedeci
mal
int
eger

DeptofCSECBI
T, Page
29
DAAmodul
e-1


Fir
st,noti
cethatt
hemostf
requent
lyexecutedoperat
ionher
eisnotinsi
dethewhi
le
l
oopbutr athert
hecompar
isonn>1t hatdeter
mineswhethert
heloop‘sbodywi
ll
be
executed.

Sincet
henumberoft i
mesthecompari
sonwil
lbeexecut
edislar
gert
hanthe
numberofrepet
it
ionsoft
heloop‘
sbodybyexact
ly1,t
hechoicei
snotthat
i
mpor t
ant
.

Amor esi
gni
fi
cantf
eatur
eofthi
sexamplei
sthefactthatt
heloopvar
iablet
akeson
onl
yaf ewval
uesbetweeni
tslowerandupperl
i
mi t
s;ther
efor
e,wehav etousean
al
ter
nati
veway
ofcomput
ingt
henumberoft
imest
hel
oopi
sexecut
ed.Si
ncet
hev
alueofni
s
abouthal
vedoneachr
epet
it
ionoft
hel
oop,
theanswershoul og2n.
dbeaboutl

Theexactf
ormul
afort
henumberoft
imest
hecompar
isonn>1wi
l
lbeexecut
edi
s
act
uall
y

t
henumberofbi
tsi
nthebi
nar
yrepr
esent
atonofn.
i

5.MATHEMATI
CALANALYSI
S(TI
MEEFFI
CIENCY)OFRECURSI
VE
ALGORI
THMS
Gener
alpl
anf
oranal
yzi
ngef
fi
ciencyofr
ecur
siv
eal
gor
it
hms:
1.
Deci
deonpar
amet
erni
ndi
cat
ingi
nputsi
ze
2.
Ident
if
yal
gor
it
hm‘
sbasi
coper
ati
on
3.Checkwhet
hert
henumberoft
imest
hebasi
coper
ati
oni
sexecut
eddependsonl
y
ont
hei
nputsi
zen.I
fital
sodependsont
het
ypeofi
nput
,inv
est
igat
ewor
st,av
erage,
andbestcaseef
fi
ciencysepar
atel
y.
4.Setuprecur
rencer
elat
ion,
wit
hanappr
opr
iat
eini
ti
alcondi
ti
on,
fort
henumberof
ti
mesthe
al
gor
it
hm‘
sbasi
coper
ati
oni
sexecut
ed.
5.Sol
vet
her
ecur
rence.

Exampl
e1:
Fact
ori
alf
unct
ion

DeptofCSECBI
T, Page
30
DAAmodul
e-1

Def
ini
ti n!=1*2*…*
on: (n-
1)*nf
orn≥1and0!=1
Recur
siv
edef
ini
tonofn!
i :F(
n)=F(
n-1)*nf
orn≥1and
F(
0)=1

Anal
ysi
s:
1.
Inputsi
ze:
giv
ennumber=n
2.
Basi
coper
ati
on:
mul
ti
pli
cat
ion
3.
Nobest
,wor
st,
aver
agecases.
4. n)denot
LetM( esnumberofmul
ti
pli
cat
ions

5.
Sol
vet
her
ecur
rencer
elat
ionusi
ngbackwar
dsubst
it
uti
onmet
hod:
M(
n)=M(
n-1)+1 subst
it
uteM(
n-1)
=M(
n-2)+1

+2
=(
M(n-
2)+1)+1 =M(
n-2)subst
it
ute M(
n-2)
=M(
n-3)+1
=(
M(n-
3)+1)+2 =M(
n-3)+3

Thegener
alf
ormul
afort
heabov
epat
ter
nforsome‗
i‘i
sasf
oll
ows:
=M(
n-i
)+i
Byt
aki
ngt
headv
ant
ageoft
hei
nit
ial
condi
ti
ongi
veni
.e.
,
M(0)
=0,
wenowsubst
it
utei
=n
i
nthepat
ter
nsf
ormul
atogett
heul
ti
mat
eresul
toft
hebackwar
dsubst
it
uti
ons.
=M(
n-n)
+n

DeptofCSECBI
T, Page
31
DAAmodul
e-1

=M(
0)+n
M(
n)=n

Thenumberofmul
ti
pil
cat
ionst
ocomput
ethef
act
ori
alof‗
n‘i
snwher
ethet
ime
compl
exi
tyi
sli
near
.

Exampl
e2:
TowerofHanoi
Probl
em

I
nthi
spuzzl endi
e,wehav sksofdi
ff
erentsi
zest
hatcansl
i
deont
oanyoft
hreepegs.
I
nit
ial
l
y,al
lthedi
sksar
eont
hef
ir
stpegi
nor
derofsi
ze,
thel
argestont
hebot
tom and
t
hesmal
l
estont
op.Thegoali
stomov
eal
lthedi
skst
othet
hir
dpeg,
usi
ngt
hesecond
oneasanauxi
l
iar
y,i
fnecessar
y.Wecanmov
eonl
yonedi
skatat
ime,andi
tis
f
orbi
ddent
opl
aceal
argerdi
skont
opofasmal
l
erone.

Thepr
obl
em hasanel
egantr
ecur
siv
esol
uti
on,whi
chi
sil
l
ust
rat
edi
nFi
gur
e.

en>1di
Tomov sksf
rom peg1t
opeg3(
wit
hpeg2asauxi
l
iar
y):
 wefir
stmover
ecur
siv
elyn−1di
sksf
rom peg1t
opeg2(
wit
hpeg3as
auxi
l
iary
).
 Thenmov
ethel
argestdi
ski
.e.
,nt
hdi
skdi
rect
lyf
rom peg1t
opeg3.
 And,
final
l
y,mov
erecur
siv
elyn−1di
sksf
rom peg2t
opeg3(
usi
ngpeg1
asauxi
l
iar
y).

Ofcour
se,fn=1,
i wesi
mpl
ymov
ethesi
ngl
edi
skdi
rect
lyf
rom t
hesour
cepegt
o
t
hedesti
nat
ionpeg.

Anal
ysi
s:
Letusappl
ythegener
alpl
anout
li
nedabov
etot
heTowerofHanoi
probl
em.
DeptofCSECBI
T, Page
32
DAAmodul
e-1

1. sksni
Thenumberofdi stheobv
iouschoi
cef
ort
hei
nput
‘ssi
zei
ndi
cat
or.
2.
Mov
ingonedi
skast
heal
gor
it
hm‘
sbasi
coper
ati
on.
3.Cl
ear
ly,
t esM(
henumberofmov n)dependsonnonl
y,andwegett
hef
oll
owi
ng
r
ecur
renceequat
ionf
ori
t:
n)=M(
M( n-1) n-
+1+M( 1)f
orn>1,

n)=2M(
M( n-1)+1,
M(1)=1and
Thei
nit
ial
condi
ti
oni
s:
M(
1)=1f
orn=1
5.Sol
vet
her
ecur
rencer
elat
ionusi
ngbackwar
dsubst
it
uti
onmet
hod

M(
n)=2M(
n-1)+1 sub.M(
n−1)=2M(
n−2)+1

2(
2M(
n-2)+1)+1=2^
2*M(
n-2)+2^
1+2^
0 sub.M(
n−2)=2M(
n−3)+1

=2^
2*(
2M(
n-3)+1)+2^
1+2^
0
=2^
3*M(
n-3)+2^
2+2^
1+2^
0
=…

Si
ncet
hei
nit
ial
condi
ti
oni
sspeci
fi orn=1,
edf whi
chi
sachi
ev ori
edf =n−1,
weget
t
hef
oll
owi
ngf
ormul
afort
hesol
uti
ont
orecur
rence

Thus,wehav
eanexponent
ialal
gor
it
hm,whi
chwi
l
lrunf
oranuni
magi
nabl
ylongt
ime
ev
enf
ormoder
atev
aluesofn.
Thi
sisnotduet
othef
actt
hatt
hispar
ti
cul
aral
gor
it
hm
i
spoor
;inf
act
,iti
snotdi
ff
icul
ttopr
ovet
hatt
hisi
sthemostef
fi
ciental
gor
it
hm
possi
blef
ort
hispr
obl
em.I
tist
hepr
obl
em‘
sint
ri
nsi
cdi
ff
icul
tyt
hatmakesi
tso
comput
ati
onal
l
yhar
d.

EXAMPLE3Ar
ecur
siv
ever
sionoft
heal
gor
it
hm t
hatcomput
esnumberofbi
nar
ydi
git
s
nn‘
i sbi
nar
yrepr
esent
ati
on.

DeptofCSECBI
T, Page
33
DAAmodul
e-1


Letussetuparecurr
enceandani
nit
ial
condi
ti
onf
ort
henumberofaddi
tonsA(
i n)
madebyt heal
gori
thm.

Thenumberofaddi
ti
onsmadei
ncomput
ing ,pl
usonemor
eaddi
ti
oni
smadeby
t
heal
gor
it
hm t
oincr
easet
her
etur
nedv
alueby1.Thi
sleadst
other
ecur
rence


Sincetherecur
siv
ecalsendwhenni
l sequal
to1andt
her
ear
enoaddi
ti
onsmade
then,t
heinit
ial
condi
ti
onis
A(
1)=0.f
orn=1

Thepresenceof_n/
2_int
hefunct
ion‘
sargumentmakesthemet
hodofbackwar
d
subst
it
utionsst
umbleonv uesofnt
al hatarenotpower
sof2.

Therefore,
thestandar
dappr
oacht
osolv
ingsucharecurr
encei
stosolveitonl
yfor
k
n=2 andt hentakeadv
ant
ageofthet
heor
em cal
l
edt hesmoothnessrul
e,which
cl
aimst hatunder
k
v
erybr
oadassumpt
ionst
heor
derofgr
owt
hobser
v orn=2 gi
edf vesacor
rect
answerabouttheorderofgr
owt
hforal
lv uesofn.(
al Alt
ernat
ivel
y,af
terget
ti
nga
sol
uti
onforpowersof
2,
wecansomet
imesf
ine-
tunet
hissol
uti
ont
ogetaf
ormul
aval
i
dforanar
bit
rar
y
n.
)
 k
Appl
yi
ngt
hesamet
other
ecur
rence,
forn=2whi
cht
akest
hef
orm

DeptofCSECBI
T, Page
34
DAAmodul
e-1

6.I
MPORTANTPROBLEM TYPES
Themosti
mpor
tantpr
obl
em t
ypes:
 Sor
ti
ng

 Sear
chi
ng

 St
ri
ngpr
ocessi
ng

 Gr
aphpr
obl
ems

 Combi
nat
ori
alpr
obl
ems
6.
1Sor
ti
ng
 Thesor
ti
ngpr
obl
em i
stor
ear
ranget
hei
temsofagi
venl
i
sti
nnondecr
easi
ng or
der
.

 Asapr
act
icalmat
ter
,weusual
l
yneedt
osor
tli
stsofnumber
s,char
act
ersf
rom an
al
phabet
,char
act
er st
ri
ngs,and,most i
mpor
tant
,recor
ds si
mil
ar t
othose
mai
ntai
nedbyschool
sabout
t
hei
rst
udent
s,l
i
brar
iesaboutt
hei
rhol
dings,andcompani
esaboutt
hei
rempl
oyees.

 I
nthecaseofr
ecor
ds,weneedt
ochooseapi
eceofi
nfor
mat
iont
ogui
desor
ti
ng.
Forexampl
e,wecanchooset
osor
tst
udentr
ecor
dsi
nal
phabet
icalor
derofnames
orbyst
udentnumberorbyst
udentgr
ade-
poi
ntav
erage.Suchaspeci
all
ychosen
pi
eceofi
nfor
mat
ioni edakey
scal
l .

 Al
thoughsomeal
gor
it
hmsar
eindeedbet
tert
hanot
her
s,t
her
eisnoal
gor
it
hm t
hat
woul
dbet
hebestsol
uti
oni
nal
lsi
tuat
ions.

DeptofCSECBI
T, Page
35
DAAmodul
e-1

 Someoft
heal
gor
it
hmsar
esi
mpl
ebutr
elat
ivel
ysl
ow,whi
l
eot
her
sar
efast
erbut
mor
ecompl
ex;somewor
kbet
teronr
andoml
yor
der
edi
nput
s,whi
l
eot
her
sdo
bet
teronal
most
-sor
tedl
i
sts;somear
esui
tabl
eonl
yforl
i
stsr
esi
dingi
nthef
ast
memor
y,whi
l
eot
her
scanbeadapt
edf
orsor
ti
ngl
argef
il
esst
oredonadi
sk;
andso
on.

 Twopr
oper
ti
esofsor
ti
ngal
gor
it
hms:

A sor
ti
ngal
gor
it
hm i
scal
l
edst
abl
eifi
tpr
eser
vest
her
elat
iveor
derofanyt
wo
equal
el
ement
sini
tsi
nput
.Inot
herwor
ds,i
fani
nputl
i
stcont
ainst
woequalel
ement
sin
posi
ti
onsi
andj
wher
ei<j
,theni
nthesor
tedl
i
stt
heyhav
etobei
nposi
ti
onsìandj̀
r
espect
ivel
y,sucht
hatì<j̀

Thesecondnot ablefeatur
eofasor t
ingal
gori
thm i
stheamountofextramemory
theal
gori
thm requir
es.Analgor
ithm i
ssai
dtobein-pl
acei
fitdoesnotr
equi
reext
ra
memory,except,possibl
y,f
orafewmemor yuni
ts.

6.
2Sear
chi
ng
 Thesear
chi
ngpr
obl
em deal
swi
thf
indi
ngagi
venv
alue, edasear
cal
l chkey
,ina
gi
venset
(
oramul
ti
set
,whi
chper
mit
ssev
eral
element
stohav
ethesamev
alue)
.

 Ther
ear
epl
ent
yofsear
chi
ngal
gor
it
hmst
ochoosef
rom.Theyr
angef
rom t
he
st
rai
ght
for
war
dsequent
ialsear
cht
oaspect
acul
arl
yef
fi
cientbutl
i
mit
edbi
nar
y
sear
chandal
gor
it
hmsbasedonr
epr
esent
ingt
heunder
lyi
ngseti
nadi
ff
erentf
orm
mor
econduci
vet
osear
chi
ng.

 Thelatt
eralgor
it
hmsar
eofpar
ti
cul
ari
mpor
tancef
orr
eal
-wor
ldappl
i
cat
ions
becausethey
ar
eindi
spensabl
eforst
ori
ngandr
etr
iev
ingi
nfor
mat
ionf
rom l
argedat
abases.
 Forsear
chi
ng,t
oo,
ther
eisnosi
ngl
eal
gor
it
hm t
hatf
it
sal
lsi
tuat
ionsbest
.

 Someal
gor
it
hmswor
kfast
ert
hanot
her
sbutr
equi
remor
ememor
y;somear
ever
y
f
astbutappl
i
cabl
eonl
ytosor
tedar
ray
s;andsoon.

6.
3St
ri
ngPr
ocessi
ng

DeptofCSECBI
T, Page
36
DAAmodul
e-1

 Ast
ri
ngi
sasequenceofchar
act
ersf
rom anal
phabet
.

 St
ri
ngsofpar
ti
cul
ari
nter
estar
etextst
ri
ngs,whi
chcompr
isel
ett
ers,number
s,and
speci
alchar
act
ers;
bitst
ri
ngs,
whi
chcompr
isezer
osandones;
andgenesequences,
whi
chcanbemodel
edbyst
ri
ngsofchar
act
ersf
rom t
hef
our
-char
act
eral
phabet
{
A,C,
G,T}
.

 Onepar
ti
cul
arpr
obl
em sear
chi
ngf
oragi
venwor
dinat
ext
—hasat
tr
act
edspeci
al
at
tent
ionf
rom r
esear
cher
sref
err oasst
edt ri
ngmat
chi
ng.

6.
4Gr
aphPr
obl
ems

 Oneoft
heol
destandmosti
nter
est
ingar
easi
nal
gor
it
hmi
csi
sgr
aphal
gor
it
hms.

 I
nfor
mal
l
y,agr
aphcanbet
houghtofasacol
l
ect
ionofpoi
ntscal
l
edv
ert
ices,
some
ofwhi
char
econnect
edbyl
i
nesegment
scal
l
ededges.

 Gr
aphsar
eani
nter
est
ingsubj
ectt
ost
udy
,forbot
htheor
eti
cal
andpr
act
ical
reasons.
Gr
aphs can be used f
ormodel
i
ng a wi
de v
ari
ety ofappl
i
cat
ions,i
ncl
udi
ng
t
ranspor
tat
ion,communi
cat
ion,soci
alandeconomi
cnet
wor
ks,pr
ojectschedul
i
ng,
andgames.

 Basi
cgr
aphal
gor
it
hmsi
ncl
udegr
aph-
tr
aver
salal
gor
it
hms(
how canoner
eachal
l
t
hepoi
ntsi
nanet
wor
k?)
,shor
test
-pat
hal
gor
it
hms(
whati
sthebestr
out
ebet
ween
t
woci
ti
es?
),andt
opol
ogi
calsor
ti
ngf
orgr
aphswi
thdi
rect
ededges(
isasetof
cour
seswi
tht
hei
rpr
erequi
sit
esconsi
stentorsel
f-
cont
radi
ctor
y?)
.

 Somegr
aphpr
obl
emsar
ecomput
ati
onal
l
yver
yhar
d;t
hemostwel
l
-known
exampl
esar
ethet
rav
eli
ngsal
esmanpr
obl
em andt
hegr
aph-
col
ori
ngpr
obl
em.

 Thet
rav
eli
ngsal
esmanpr
obl
em (
TSP)i
sthepr
obl
em off
indi
ngt
heshor
testt
our
t
hrough n ci
ti
es t
hatv
isi
ts ev
ery ci
ty exact
ly once.I
n addi
ti
on t
o obv
ious
appl
i
cat
ionsi
nvol
vi
ngr
out
epl
anni
ng,
itar
isesi
nsuchmoder
nappl
i
cat
ionsasci
rcui
t
boar
dandVLSIchi
pfabr
icat
ion,
X-r
aycr
yst
all
ogr
aphy
,andgenet
icengi
neer
ing.

 Thegr
aph-
col
ori
ngpr
obl
em seekst
oassi
gnt
hesmal
l
estnumberofcol
orst
othe
v
ert
icesofagr
aphsot
hatnot
woadj
acentv
ert
icesar
ethesamecol
or.Thi
s
pr
obl
em ar
isesi
nsev
eralappl
i
cat
ions,suchasev
entschedul
i
ng:i
ftheev
ent
sar
e

DeptofCSECBI
T, Page
37
DAAmodul
e-1

r
epr
esent
ed by v
ert
ices t
hatar
e connect
ed by an edge i
fand onl
yift
he
cor
respondi
ngev
ent
scannotbeschedul
edatt
hesamet
ime,
asol
uti
ont
othegr
aph
-
col
ori
ngpr
obl
em y
iel
dsanopt
imal
schedul
e.

6.
5Combi
nat
ori
alPr
obl
ems

 Gener
all
yspeaki
ng,
combi
nat
ori
alpr
obl
emsar
ethemostdi
ff
icul
tpr
obl
emsi
n
comput
ing,
from bot
hat
heor
eti
cal
andpr
act
ical
standpoi
nt.

 Thei
rdi
ff
icul
tyst
emsf
rom t
hef
oll
owi
ngf
act
s.

Fi
rst
,thenumberofcombinat
ori
alobj
ectsty
pical
l
ygr owsext
remel
yfastwi
th
aproblem‘ssi
ze,reachi
ng uni
maginabl
emagni tudesevenformoderat
e-
si
zed
i
nst
ances.

Second,
ther
earenoknownal
gor
it
hmsforsol
vi
ngmostsuchpr
obl
ems
exact
lyi
nanaccept
abl
eamountofti
me.

7.
FUNDAMENTALDATASTRUCTURES
 Si
ncethev
astmaj
ori
tyofal
gor
it
hmsofi
nter
estoper
ateondat
a,par
ti
cul
ar
waysof
or
gani
zi
ngdat
apl
ayacr
it
ical
rol
eint
hedesi
gnandanal
ysi
sofal
gor
it
hms.
 Adatast
ruct
urecanbedef
inedasapar
ti
cul
arschemeofor
gani
zi
ngr
elat
eddat
a
i
tems.

7.
1Li
nearDat
aSt
ruct
ures
 Thet
womosti
mpor
tantel
ement
arydat
ast
ruct
uresar
ethear
rayandt
hel
i
nkedl
i
st.
 A(
one-
dimensi
onal
)ar
rayi
sasequenceofni
temsoft
hesamedat
aty
pet
hatar
e
st
oredcont
iguousl
yincomput
ermemor
yandmadeaccessi
blebyspeci
fyi
nga
v
alueoft
hear
ray
‘si
ndex.

Fi
gur
e:Ar
r nel
ayof ement
s.

DeptofCSECBI
T, Page
38
DAAmodul
e-1

 Al
i
nkedl
i
sti
sasequenceofzer
oormor
eel
ement
scal
l
ednodes,
eachcont
aini
ng
t
woki
ndsofi
nfor
mat
ion:
somedat
aandoneormor
eli
nkscal
l
edpoi
nter
stoot
her
nodesoft
hel
i
nkedl
i
st.
 I
nasi
ngl
yli
nkedl
i
st,
eachnodeexceptt
hel
astonecont
ainsasi
ngl
epoi
ntert
o
t
henextel
ement
.

Fi
gur
e:Si
ngl
yli
nkedl
i nel
stof ement
s.

 Anot
herext
ensi
oni
sthest
ruct
urecal
l
edt
hedoubl
yli
nkedl
i
st,
inwhi
chev
erynode,
exceptt
hef
ir
standt
hel
ast
,cont
ainspoi
nter
stobot
hit
ssuccessorandi
ts
pr
edecessor
.

Fi
gur
e:Doubl
yli
nkedl
i nel
stof ement
s.
 Al
i
sti
saf
ini
tesequenceofdat
ait
ems,
i.
e.,
acol
l
ect
ionofdat
ait
emsar
rangedi
na
cer
tai
nli
nearor
der
.
aresear
chi
ng
The basic operat
ions per
for
med on t
his dat
a st
ruct
ure for
,
inser
ti
ng,anddel
eti
nganelement
.
 Twospeci
alt
ypesofl
i
sts,
stacksandqueues,
arepar
ti
cul
arl
yimpor
tant
.

Ast
acki
sal
i
sti
nwhi
chi
nser
ti
onsanddel
eti
onscanbedoneonl
yatt
heend.Thi
sendi
s

cal
l
edt
het
opbecauseast
acki
susual
l
yvi
sual
i
zednothor
izont
all
ybut
v
ert
ical
l
y—aki
ntoast
ackofpl
ateswhose―oper
ati
ons‖i
tmi
micsv
erycl
osel
y.
 Aqueue,
ont
heot
herhand,
isal
i
stf
rom whi
chel
ement
sar
edel
etedf
rom oneend
oft
hest
ruct
ure,cal
l
ed t
hef
ront(
thi
soper
ati
oni
scal
l
ed dequeue)
,and new
el
ement
sar
eaddedt
otheot
herend,cal
l
edt
her
ear(
thi
soper
ati
oni
scal
l
ed
enqueue)
.Consequent
ly,a queue oper
ates i
n a ―f
ir
st-
in–f
ir
st-
out
‖(FI
FO)
f
ashi
on—aki
ntoaqueueofcust
omer
sser
vedbyasi
ngl
etel
l
eri
nabank.Queues
DeptofCSECBI
T, Page
39
DAAmodul
e-1

al
sohav
emanyi
mpor
tantappl
i
cat
ions,i
ncl
udi
ngsev
eralal
gor
it
hmsf
orgr
aph
pr
obl
ems.
 Manyi
mpor
tantappl
i
cat
ionsr
equi
resel
ect
ionofani
tem oft
hehi
ghestpr
ior
it
y
amongady
nami
cal
l
ychangi
ngsetofcandi
dat
es.
 Adat
ast
ruct
uret
hatseekst
osat
isf
ytheneedsofsuchappl
i
cat
ionsi
scal
l
eda
pr
ior
it
yqueue.Apr
ior
it
yqueuei
sacol
l
ect
ionofdat
ait
emsf
rom at
otal
l
yor
der
ed
uni
ver
se(
most
oft
en,i
ntegerorr
ealnumber
s).Thepr
inci
paloper
ati
onson a
pr
ior
it
yqueuear
efi
ndi
ngi
tsl
argestel
ement
,del
eti
ngi
tsl
argestel
ement
,and
addi
nganewel
ement
.

7.
2Gr
aphs
 Agraphisinf
ormal
l
ythoughtofasacol
l
ect
ionofpoi
ntsi
nthepl
anecal
l
ed
―verti
ces‖or
nodes,
‖someoft
hem connect
edbyl
i
nesegment
scal
l
ed―edges‖or―ar
cs.

 AgraphGi
scal
l
edundi
rect
edi
fev
eryedgei
niti
sundi
rect
ed.Agr
aphwhoseev
ery
edge
i
sdi
rect
edi
scal
l
eddi
rect
ed.Di
rect
edgr
aphsar
eal
socal
l
eddi
graphs.
 Thegr
aphdepi
ctedi
nFi
gur
e(a)hassi
xver
ti
cesandsev
enundi
rect
ed
edges:
V={
a,b,
c,d,
e,f}
,E={
(a,
c),
(a,
d),
(b,
c),
(b,
f),
(c,
e),
(d,
e),
(e,
f)
}.Thedi
graphdepi
ctedi
nFi
gur
e1.
6bhassi
xver
ti
cesandei
ght
di
rect
ededges:
V={
a,b,
c,d,
e,f}
,E={
(a,
c),
(b,
c),
(b,
f),
(c,
e),
(d,
a),
(d,
e),
(e,
c),
(e,
f)}
.

Fi
gur
e:(
a)Undi
rect
edgr
aph.(
b)Di
graph.

Gr
aphRepr
esent
ati
ons-Gr
aphsf
orcomput
eral
gor
it
hmsar
eusual
l
yrepr
esent
edi
n
oneof
twoway
s:headj
t acencymat
ri
xandadj
acencyl
i
sts.


Theadjacencymat
ri
xofagraphwit
hnv er
ti
cesisannxnbooleanmatri
xwith
onerowandonecolumnforeachofthegr
aph‘
sv er
ti
ces,i
nwhicht
heelement
th th th
i
nthei r owandt
hej columnisequalto1iftherei
sanedgefrom t
hei
th
v
ert
ext
othej v
ert
ex,
andequal
to0i
fther
eisnosuchedge.
DeptofCSECBI
T, Page
40
DAAmodul
e-1


Theadjacencylist
sofagr aphoradigraphi
sacollect
ionofli
nkedli
sts,one
foreachv er
tex,
thatcontai
nallt
heverti
cesadj
acenttothel
ist
‘sver
tex(i
.e.
,al
l
thevert
icesconnectedtoitbyanedge).

Fi
gur
e:(
a)Adj
acencymat
ri
xand(
b)adj
acencyl
i
stsoft
hegr
aphi
nFi
gur
e(a)

Wei
ght
edGr
aphs

Aweight
edgraph(orweight
eddigr
aph)i
sagraph(
ordi
graph)wit
hnumber
s
assi
gnedtoi
tsedges.Thesenumbersar edwei
ecal
l ght
sorcosts.

Anint
erestinsuchgr
aphsismot i
vatedbynumerousr eal
-worl
dapplicati
ons,
suchasf i
ndi
ngtheshor
testpathbetweent wopoint
si nat r
ansport
ationor
communicati
onnet
wor
korthetrav
eli
ngsalesmanprobl
em mentionedearli
er.

Bothpr
inci
palr
epr
esentat
ionsofagr
aphcanbeeasi
l
yadopt
edt
o
accommodatewei
ghtedgraphs.

I
fawei
ght
edgr
aphi
srepr
esent
edbyi
tsadj
acencymat
ri
x,t
heni
tsel
ement
A[i
,j]wil
lsi
mpl
ycontai
ntheweightoftheedgefr
om thei
thtothej
thv
ert
ex
i
fthereissuchanedgeandaspeci alsymbol,e.
g.∞,i
, fther
eisnosuch
edge.Suchamatr
ixi
scall
edtheweightmatri
xorcostmatr
ix.

Thisappr
oachi
sil
l
ust
rat
edi
nbel
owf
igur
e(b)f
ort
hewei
ght
edgr
aphi
n
fi
gure(a)
.
(
Forsomeappl
i
cat
ions,
iti
smor
econv
eni
entt
oput0‘
sont
hemai
n
di
agonal
oft
headj
acencymat
ri
x.)
DeptofCSECBI
T, Page
41
DAAmodul
e-1

Adjacencyl
ist
sforaweight
edgraphhavet
oincl
udeinthei
rnodesnotonly
thenameofanadj acentvert
exbutalsot
heweightofthecorrespondi
ng
edge(Figur
ec).

Fi
gur
e:(
a)Wei
ght
edgr
aph.(
b)I
tswei
ghtmat
ri
x.(
c)I
tsadj
acencyl
i
sts.

Pat
hsandCy
cles

Amongthemanypropert
iesofgr
aphs,twoar
eimpor
tantf
oragr
eatnumberof
appl
i
cat
i connect
ons: ivi
tyandacy
cli
cit
y.

Bot
har
ebasedont
henot
ionofapat
h.

Apathfrom verexut
t over
texvofagraphGcanbedef
inedasasequenceof
adj
acent(connectedbyanedge)ver
ti
cesthatst
art
swi
thuandendswithv.

I
fallver
ti
cesofapatharedisti
nct
,thepathissaidtobesimple.Thelengt
hofa
pat
histhetot
alnumberofv
erti
cesi
nt hev
ertexsequencedef
ini
ngthepathminus1,
whi
chisthesameasthenumberofedgesinthepath.

Forexampl a,
e, c,b,fi
sasimplepat
hoflengt
h3f om at
r ofinthegraphi
nbelow
fi
gure(a)
,whereasa,c,
e,c,
b,fisapat
h(notsimpl
e)ofl
engt
h5f om at
r of.


I
nt hecaseofadi rect
edgr
aph,wear eusual
l
yi nt
erestedindir
ectedpaths.A
dir
ectedpathisasequenceofv er
ti
cesinwhichev eryconsecuti
vepairofthe
verti
cesisconnect
edbyanedgedir
ectedfr
om thevertexli
stedf
ir
sttothevertex
l
istednext.

e,a,
Forexampl c,e,
fisadi
rect
edpat
hfom at
r ofi
nthegr
aphi
nfi
gur
e(b)
.

DeptofCSECBI
T, Page
42
DAAmodul
e-1


Agraphi
ssai
dtobeconnect
edi
fforev
erypai
rofi
tsv
ert
icesuandvt
her
eisapat
h
f
rom u
ov
t .I
fwemakeamodel
ofaconnect
edgr
aphbyconnect
ingsomebal
l
s
r
epr
esent
ingt
hegr
aph‘
sver
ti
ceswi
thst
ri
ngsr
epr
esent
ingt
heedges,
itwi
l
lbea
si
ngl
epi
ece.

I
fagraphisnotconnect
ed,suchamodel
will
consistofsev
eral
connect
edpi
eces
t
hatarecal
ledconnect
edcomponent
softhegraph.

For
mal
l
y,a connect
ed componenti
s a maxi
mal(
notexpandabl
e byi
ncl
udi
ng
anotherver
texandanedge)connect edsubgraph2ofagivengraph.Forexampl
e,
thegraphsinfol
lowingf
igures(a)i
sconnected,whereast
hegraphinf i
gur
e(b)i
s
not,
becausether
ei snopath,f
orexample,
from atof.


Thegraphinfi
gure(c)hastwoconnect
edcomponent
swi
thv
ert
i a,
ces{ b,
c,d,
e}
and{f
,g,h,
i},
respect
ivel
y.

Acycleisapathofaposit
ivel
engt
hthatst
artsandendsatthesamev ert
exand
doesnottr
aver
sethesameedgemorethanonce.Forexampl
e,f,
h,i
,g,
fi sacycl
e
i
nthegraphinf
igur
e(c).

Agr
aphwi
thnocy
clesi
ssai
dtobeacy
cli
c.
DeptofCSECBI
T, Page
43
DAAmodul
e-1

7.
3Tr
ees

Atr
ee(mor
eaccur
atel
y,af
reet
ree)i
saconnect
edacy
cli
cgr
aphasi
nfi
gur
e(a)
bel
ow.

Agraphthathasnocycl
esbutisnotnecessari
lyconnect
edi edaf
scal
l orest
:each
ofi
tsconnectedcomponent
sisatreeasinfigure(b)bel
ow

Treeshav
esev
eral
impor
tantpr
oper
ti
esot
hergr
aphsdonothav
e.I
npar
ti
cul
ar,
the
number
ofedgesi
nat
reei
sal
way
sonel
esst
hant
henumberofi
tsv
ert
ices:
|E|
=|V|
−1.


Root
edTr
ees

Anotherver
yimport
antproper
tyoftr
eesist
hefactthatf
orev
erytwovert
icesi
na
tr
ee,ther
ealwaysexi
stsexact
lyonesimpl
epat
hf r
om oneofthesev
erti
cestothe
other
.

Thispr
oper
tymakesi
tpossi
bletosel
ectanar
bit
rar
yver
texi
naf
reet
reeand
consi
deri her
tast ootoft
heso-cal
ledroot
edt
ree.

Arootedt r
eeisusuall
ydepi
ctedbyplacingit
srootonthetop(
lev
el0oft
hetr
ee),
t
hev ert
icesadjacentt
otherootbel
owi t(l
evel1)
,thev
erti
cest
woedgesapar
tfr
om
t
herootst i
ll
bel
ow(
lev
el2)
,andsoon.Fi
gur
ebel
owpr
esent
ssuchat
ransf
ormat
ionf
rom af
ree
t
reet
oa\
root
edt
ree.

DeptofCSECBI
T, Page
44
DAAmodul
e-1

Fi
gur
e:(
a)Fr
eet
ree.(
b)I
tst
ransf
ormat
ioni
ntoar
oot
edt
ree.

Foranyver
texvinatreeT,all
thev
ert
icesont
hesi
mpl
epat
hfr
om t
her
oott
othat
ver
texar
ecalledancest
orsofv.

Thevert
exit
selfi
susual
lyconsi
deredi
tsownancestor;
thesetofancest
orst
hat
excl
udesthevert
exi
tsel
fisref
err
edtoasthesetofproperancest
ors.

If(
u,v)isthelastedgeofthesi
mpl
epathf
rom t
her
oottover
texv(andu≠v),ui
s
sai
dt obet heparentofvandvi edachi
scal
l l
dofu;ver
ti
cesthathav
ethesame
parentar
esaidtobesibli
ngs.

Avert
exwit
hnochi
l
dreni edal
scal
l eaf;
aver
texwi
thatl
eastonechi
l
discal
l
ed
par
ental
.

Allt
hev er
ti
cesforwhichaver
texvisanancestorar
esai
dtobedescendant
sofv
;
theproperdescendant
sexcl
udethever
texv
itsel
f.

Allt
hedescendant
sofaver
texvwi t
hal
ltheedgesconnect
ingt
hem f
ormt
he
subtr
eeofTrootedatt
hatv
ertex.

Thus,f
orthet r
eeinFigur
e(b)abov
e,t
herootofthet r
eei sa;vert
icesd,g,
f,h,andI
arel
eaves,andv er
tcesa,
i b,e,
andcarepar
ental
;thepar entofbisa; t
hechil
drenof
barecandg; t
hesiblngsofbar
i edande;andthev ert
icesoft hesubtr
eerootedat
bare{b,c,
g, h,i
}.

Thedept
hofav
erexvi
t sthel
engt
hoft
hesi
mpl
epat
hfr
om t
her ov
oott .

Thehei
ghtofat
reei
sthel
engt
hoft
hel
ongestsi
mpl
epat
hfr
om t
her
oott
oal
eaf
.

Forexampl
e,t
hedept
hofv
erexcoft
t het
reei
nFi
gur
e(b)abov
eis2,
andt
hehei
ght
ofthetr
eei
s3.

DeptofCSECBI
T, Page
45
DAAmodul
e-1

Thus,ifwecounttr
eelevel
stopdownstart
ingwi
th0f orther
oot‘
slev
el,t
hedept
h
ofav ert
exi ssi
mpl
yit
sleveli
nthet
ree,andthet
ree‘
sheighti
sthemaximum l
evel
ofi
tsv er
tices

Or
der
edTr
ees

Anor
der
edt
reei
sar
oot
edt
reei
nwhi
chal
lthechi
l
drenofeachv
ert
exar
eor
der
ed.

I
tisconv
eni
entt
oassumet
hati
nat
ree‘
sdi
agr
am,al
lthechi
l
drenar
eor
der
edl
eftt
ori
ght
.


Abinaryt
reecanbedef i
nedasanorderedtr
eeinwhicheveryver
texhasnomore
t
hantwochildrenandeachchi
ldi
sdesi
gnatedasei
theral
eftchi
l
dorar i
ghtchi
l
dof
i
tsparent
;abinaryt
reemayalsobeempty.

Anexampl
eofabi
nar
ytr
eei
sgi
veni
nFi
gur
e(a)bel
ow.


Thebi
nar
ytr
eewi
thi
tsr
ootatt
hel
eft(
ri
ght
)chi
l
dofav
ert
exi
nabi
nar
ytr
eei
s
cal
ledtheleft(r
ight
)subtr
eeofthatver
tex
.Sinceleftandrightsubt
reesarebinar
y
tr
eesaswel l
,abinar
ytreecanalsobedefi
nedrecursiv
ely
.Thismakesi tpossi
blet
o
solvemanyproblemsinvolv
ingbinar
ytr
eesbyrecursi
vealgori
thms.

I
nFigur
e(b)
,somenumber
sar
eassi
gnedt
over
ti
cesoft
hebi
nar
ytr
eei
nFi
gur
e(a)
above.

Notet
hatanumberassi
gnedt oeachparent
alv
ert
exisl
argerthanallt
henumbers
i
nitsl
eftsubtr
eeandsmallert
hanallt
henumbersinit
srightsubt
ree.Sucht
rees
ar
ecaledbi
l nar
ysear
cht
rees.

DeptofCSECBI
T, Page
46
DAAmodul
e-1

A bi
nar
ytr
eei
susual
l
yimpl
ement
edf
orcomput
ingpur
posesbyacol
l
ect
ionof
nodescorrespondingtovert
icesofthet
ree.Eachnodecontai
nssomei nf
ormation
associ
atedwiththev er
tex(i
tsnameorsomev al
ueassi
gnedtoit
)andtwopoi nt
ers
tothenodesrepresenti
ngtheleftchi
l
dandrightchi
l
dofthever
tex,r
espect
ivel
y.

Figur
ebelowi
l
lust
rat
essuchani
mpl
ement
ati
onf
ort
hebi
nar
ysear
cht
reei
nFi
gur
e
(b)abov
e.

Fi
gur
e:St
andar
dimpl
ement
ati
onoft
hebi
nar
ysear
cht
reei
nFi
gur
e(b)abov
e


Acomput err
epresent
ati
onofanarbi
tr
aryor
deredtr
eecanbedonebysi
mply
provi
dingaparent
alvert
exwi
ththenumberofpoint
ersequal
tot
henumberofi
ts
chil
dren.

Thisr
epr
esent
ati
onmaypr ov
etobei
nconv
eni
enti
fthenumberofchi
l
drenv
ari
es
widel
yamongthenodes.

Wecanav
oidt
hisi
nconv
eni
encebyusi
ngnodeswi
thj
ustt
wopoi
nter
s,aswedi
d
f
orbinar
ytrees.Here,however
,theleftpoi
nterwillpointtot hef
ir
stchil
dofthe
v
ert
ex,and t he r
ightpoint
erwi l
lpointtoi ts nextsi bl
ing.Accor
dingl
y,t
his
r
epr
esentat
ioniscall
edthefi
rst
chi
ld–nextsi
bli
ngrepresentat
ion.

Thus,al
lthesi
bli
ngsofav
ert
exar
eli
nkedv
iat
henodes‘
rightpoi
nter
sinasi
ngl
y
l
inked

DeptofCSECBI
T, Page
47
DAAmodul
e-1

l
i
st,
wit
hthef
ir
stel
ementoft
hel
i
stpoi
ntedt
obyt
hel
eftpoi
nteroft
hei
rpar
ent
.

Fi
gure(a)bel
owi l
l
ustrat
esthi
sr epresent
ati
onforthetr
eei nFigur
e(b)aboveI
tis
notdi
ff
icul
ttoseet hatthi
srepresentati
oneff
ecti
velyt
ransfor
msanor deredt
ree
i
ntoabinar
ytreesaidtobeassociat
edwi tht
heorderedt
ree.

Wegett
hisrepr
esent
ati
onby―r
otat
ing‖t
hepoi
nter
sabout45degr
eescl
ockwi
se
(
seeFi
gureb)

Fi
gur
e:(
a)Fir
stchi
l
d–nextsi
bli
ngr
epr
esent
ati
onoft
het
reei
nFi
gur
e1.
11b.(
b)I
tsbi
nar
y
tr
eer
epr
esent
ati
on.

7.
4Set
sandDi
cti
onar
ies

Thenot
ionofasetpl
aysacent r
alr
oleinmathematcs.Asetcanbedescr
i ibedasan
unor
der
edcoll
ect
ion(possi
blyempty)ofdi
sti
ncti
temscal
ledelement
softheset.

Aspecif
icseti
sdefi
nedei
therbyanexpli
citl
i
sti
ngofit
sel
ements(e.
g.S={
, 2,3,5,
7})orbyspeci
fyi
ngapr
opert
ythatal
ltheset‘
selement
sandonl
ytheymustsat
isfy
(e.
g.S={
, n:ni
sapri
menumbersmallerthan10}
).

Themosti
mpor
tantsetoper
ati
onsar
e:checki
ngmember
shi
pofagi
veni
tem i
na
gi
venset
;fi
ndi
ngtheuni
onoftwoset
s,whichcompr
isesallt
heelement
sineit
her
orbot
hofthem;andfi
ndi
ngthei
nter
sect
ionoftwosets,whichcompri
sesal
lthe

DeptofCSECBI
T, Page
48
DAAmodul
e-1

commonel
ement
sint
heset
s.

Set
scanbei
mpl
ement
edi
ncomput
erappl
i
cat
ionsi
ntwoway
s.Thef
ir
stconsi
der
s
onl
yset st
hataresubset
sofsomel gesetU,
ar call heuni
edt ver
salset
.IfsetUhasn
el
ement s,
thenanysubsetSofUcanberepresent
edbyabi
tstri
ngofsizen,cal
l
ed
abitv ect
or,i
nwhi chthei
thelementi
s1i fandonlyi hei
ft thelementofU i s
i
ncludedinsetS.

Thus,
tocont
inuewi
thourexampl
e,fU={
i 1,2,
3,4,
5,6,
7,8,
9},t
henS={
2,3,
5,7}
i
sr epresent
edbyt hebitst
ri
ng011010100.Thi
swayofrepr
esent
ingset
smakesi t
possibletoimplementthestandar
dsetoperat
ionsv
eryf
ast
,butattheexpenseof
potenti
all
yusingalargeamountofstor
age.

Thesecondandmor
ecommonwayt
orepr
esentasetf
orcomput
ingpur
posesi
sto
uset heli
ststructur
et oindi
cat etheset ‘
selements.Ofcourse,thisopt i
on,too,is
feasibl
eonlyforfini
teset s;fortunatel
y,unli
kemathematics,t
hisist hekindofset s
mostcomput erappl icati
ons need.Not e,however,the two pr i
ncipalpoints of
disti
ncti
onbetweenset sandl ists.Fir
st,asetcannotcontainidenti
calel ements;a
l
istcan.Thi sr equirementf oruni queness i
s sometimes circumv ented byt he
i
nt r
oductonofamul
i t
iset,orbag,anunor deredcoll
ecti
onofi t
emst hatarenot
necessaril
ydi
stinct.

Second,
aseti
sanunorderedcoll
ect
ionofi
tems;
ther
efor
e,changi
ngt
heor
derof
i
tselement
sdoesnotchangetheset
.

Ali
st,defi
nedasanorder
edcollecti
onofitems,isexact
lyt
heopposit
e.Thi
sisan
i
mpor t
anttheor
eti
cal
dist
inct
ion,butfor
tunatel
yiti
snotimpor
tantformany
appl
icati
ons.

Iti
salsoworthmenti
oningthati
faseti
srepresentedbyali
st,
dependi
ngont
he
appli
cat
ionatand,
itmightbeworthmai
ntaini
ngt heli
sti
nasortedor
der.

Incomput i
ng,
theoperat
ionsweneedt
operf
ormforasetoramul
ti
setmostoft
en
aresearchi
ngforagiv
eni t
em,addi
nganewi
tem,
anddeleti
ngani
tem f
rom t
he
coll
ect
ion.

Adatastruct
urethati
mplement
sthesethr
eeoperat
ionsi
scall
edthedict
ionar
y.
Notet
herelat
ionshi
pbet
weenthi
sdatast
ruct
ureandthepr
obl
em ofsear
chi
ng,we
ar
edeali
nghere
wi
thsear
chi
ng i
na dy
nami
c cont
ext
. Consequent
ly,
an
DeptofCSECBI
T, Page
49
DAAmodul
e-1

ef
fi
cient i
mpl
ement
ati
on of a

di
cti
onar
yhast
ost
ri
keacompr
omi
sebet
weent
heef
fi
ciencyofsear
chi
ngandt
he
ef
fi
cienci
esoft
heot
hert
wooper
ati
ons.Ther
ear
equi
teaf
ewway
sadi
cti
onar
ycan
bei
mpl
ement
ed.Theyr
angef
rom anunsophi
sti
cat
eduseofar
ray
s(sor
tedornot
)
t
omuchmor
esophi
sti
cat
edt
echni
quessuchashashi
ngandbal
ancedsear
cht
rees

A numberofappl
i
cat
ionsi
ncomput
ingr
equi
ready
nami
cpar
ti
tonofsome n-
i elementset
i
ntoacol
l
ect
ionofdi
sj
ointsubset
s.

Af
terbei
ngi
nit
ial
i
zedasacol
l
ect
i none-
onof elementsubset
s,t
hecol
l
ect
ioni
s
subj
ect
edt
oasequenceofi
nter
mixeduni
onandsear
choper
ati
ons.Thi
spr
obl
em
i
scal
l hesetuni
edt onpr
obl
em.

DeptofCSECBI
T, Page
50

You might also like