Sie sind auf Seite 1von 85

LAB MANUAL OF

ALGORITHM ANALYSIS AND DESIGN LAB


ETCS-254

Maharaja Agrasen Institute of Technology, PSP area,
Sector , !ohini, Ne" #elhi $$%%&'
( Affiliate) to *uru *o+in) Singh In)ra,rastha Uni-ersity,
.ash/ere *ate, Ne" #elhi 0
INDEX OF THE CONTENTS
1. Introdut!on to t"# A$%or!t"& An'$()!) 'nd D#)!%n.
2. *$'t+or& u)#d !n t"# L',.
-. H'rd.'r# '/'!$',$# !n t"# $',.
4. L!)t o+ 0r't!'$) 1 ') 0#r )($$',u) 0r#)r!,#d ,( G.G.S.I.*.23
1*r't!'$) 'r# d!/!d#d !nto )#/#n )#t!on).3
5. For&'t o+ t"# $', r#ord to ,# 0r#0'r#d ,( t"# )tud#nt).
4. M'r5!n% )"#&# +or t"# 0r't!'$ #6'&.
7. St#0) to ,# +o$$o.#d 1+or #'" 0r't!'$3.
8. S'&0$# 0ro%r'&).
9. L!)t o+ /!/' :u#)t!on).
1;. L!)t o+ 'd/'n#d 0r't!'$).
11. St#0) to ,# +o$$o.#d +or 'd/'n#d 0r't!'$).
INTROD2CTION TO ALGORITHM ANALYSIS AND DESIGN
LAB
An algorith/, na/e) after the ninth century scholar A+u 1afar Muha//a) I+n
Musu Al2.ho"ari3/i, An algorith/ is a set of rules for carrying out calculation
either +y han) or on a /achine4
$4 Algorith/ic is a +ranch of co/,uter science that consists of )esigning an)
analy3ing co/,uter algorith/s The 5)esign6 ,ertain to
i4 The )escri,tion of algorith/ at an a+stract le-el +y /eans of a ,seu)o
language, an)
ii4 Proof of correctness that is, the algorith/ sol-es the gi-en ,ro+le/ in
all cases4
4 The 5analysis6 )eals "ith ,erfor/ance e-aluation (co/,le7ity analysis04
The co/,le7ity of an algorith/ is a function g(n0 that gi-es the u,,er +oun) of the
nu/+er of o,eration (or running ti/e0 ,erfor/e) +y an algorith/ "hen the in,ut
si3e is n4
There are t"o inter,retations of u,,er +oun)4
<or)t-')# Co&0$#6!t(
The running ti/e for any gi-en si3e in,ut "ill +e lo"er than the u,,er
+oun) e7ce,t ,ossi+ly for so/e -alues of the in,ut "here the /a7i/u/ is
reache)4
A/#r'%#-')# Co&0$#6!t(
The running ti/e for any gi-en si3e in,ut "ill +e the a-erage nu/+er of
o,erations o-er all ,ro+le/ instances for a gi-en si3e4
An algorith/ has to sol-e a ,ro+le/4 An algorith/ic ,ro+le/ is s,ecifie) +y
)escri+ing the set of instances it /ust "or8 on an) "hat )esire) ,ro,erties the
out,ut /ust ha-e4
9e nee) so/e "ay to e7,ress the se:uence of ste,s co/,rising an algorith/4 In
or)er of increasing ,recision, "e ha-e ;nglish, ,seu)oco)e, an) real ,rogra//ing
languages4 Unfortunately, ease of e7,ression /o-es in the re-erse or)er4 In the
/anual to )escri+e the i)eas of an algorith/ ,seu)oco)es, algorith/s an) functios
are use)4
In the algorith/ analysis an) )esign la+ -arious stratgies such as #i-i)e an)
con:uer techin:ue , gree)y techni:ue an) )yna/ic ,rogra//ing techni:ues are
)one4 Many sorting algorith/s are i/,le/ente) to analy3e the ti/e
co/,le7ities4String /atching algorith/s, gra,hs an) s,anning tree algorith/s are
i/,le/ente) so as to a+le to un)erstan) the a,,lications of -arious )esign
stratgies4
*LATFORM 2SED IN THE LAB
Linu7 (also 8no"n as *NU<Linu70 is a Uni72li8e co/,uter o,erating syste/4 It is
one of the /ost ,ro/inent e7a/,les of o,en source )e-elo,/ent an) free
soft"are= its un)erlying source co)e is a-aila+le for anyone to use, /o)ify, an)
re)istri+ute freely4
SIM*LE LIN2X COMMANDS
!n!t Allo"s to change the ser-er +oot u, on a s,ecific run le-el
Most co//on use> init '
This is a useful co//an), "hen for instance a ser-ers fails to i)entify
-i)eo ty,e, an) en)s u, )ro,,ing to the non2gra,hical +oot2u, /o)e
(also calle) runle-el ?04
The ser-er runle-els rely on scri,ts to +asically start u, a ser-er "ith
s,ecific ,rocesses an) tools u,on +ootu,4 !unle-el ' is the )efault
gra,hical runle-el for Linu7 ser-ers4 But so/eti/es you get stuc8 in a
)ifferent /o)e an) nee) to force a le-el4 For those rare cases, the init
co//an) is a si/,le "ay to force the /o)e "ithout ha-ing to e)it the
initta+ file4
d This co//an) is use) to change the )irectory an) using this co//an) "ill
change the location to "hat e-er )irectory is s,ecifie)
c) hello
"ill change to the )irectory na/e) hello locate) insi)e the current )irectory
c) <ho/e<ga/es
"ill change to the )irectory calle) ga/es "ithin the ho/e )irectory4
Any )irectory can +e s,ecifie) on the Linu7 syste/ an) change to that )irectory
fro/ any other )irectory4 There are of course a -ariety of s"itches associate)
"ith the c) co//an) +ut generally it is use) ,retty /uch as it is4
R& re/o-es <)eletes )irectories an) files
Most co//on use> r/ 2r na/e (re,lace na/e of the file or )irectory 0
The 2r o,tion forces the co//an) to also a,,ly to each su+)irectory
"ithin the )irectory4 For instance to )elete the entire contents of the )irectory
7 "hich inclu)es )irectories y an) 3 this co//an) "ill )o it in one :uic8
,rocess4 That is /uch /ore useful than trying to use the r/)ir co//an) after
)eleting files@ Instea) use of r/ 2r co//an) "ill sa-e ti/e an) effort4
0 The c, co//an) co,ies files4 A file can +e co,ie) in the current )irectory or
can +e co,ie) to another )irectory4
0 &(+!$#."t&$ ="o&#="#$0=&(n#.n'&#."t&$
This "ill co,y the file calle) /yfile4ht/l in the current )irectory to the
)irectory <ho/e<hel,< an) call it /yne"na/e4ht/l4
Si/,ly ,ut the c, co//an) has the for/at of
0 +!$#1 +!$#2 9ith file$ +eing the na/e (inclu)ing the ,ath if nee)e)0 of the file
+eing co,ie) an) file is the na/e (inclu)ing the ,ath if nee)e)0 of the ne" file
+eing create)4
The c, co//an) the original file re/ains in ,lace4
d!r The d!r co//an) is si/ilar to the $) co//an) only "ith less a-aila+le
s"itches (only a+out '% co/,are) to a+out &% for $)04 By using the d!r
co//an) a list of the contents in the current )irectory liste) in colu/ns can +e
seen4
Ty,e &'n d!r to see /ore a+out the d!r co//an)4
+!nd The +!nd co//an) is use) to fin) files an) or fol)ers "ithin a Linu7 syste/4
To fin) a file using the +!nd co//an)
+!nd =u)r=,!n -n'&# +!$#n'&#
can +e ty,e)4 This "ill search insi)e the <usr<+in )irectory (an) any su+
)irectories "ithin the <usr<+in )irectory0 for the file na/e) filena/e4 To search
the entire filing syste/ inclu)ing any /ounte) )ri-es the co//an) use) is
+!nd = -n'&# +!$#n'&#
an) the +!nd co//an) "ill search e-ery file syste/ +eginning in the root
)irectory4
The +!nd co//an) can also +e use) to fin) co//an) to fin) files +y )ate an)
the +!nd co//an) ha,,ily un)erstan) "il) characters such as > an) ?
$) The ls co//an) lists the contents of a )irectory4 In its si/,le for/ ty,ing just $)
at the co//an) ,ro/,t "ill gi-e a listing for the )irectory currently in use4 The
$) co//an) can also gi-e listings of other )irectories "ithout ha-ing to go to
those )irectories for e7a/,le ty,ing $) =d#/=,!n "ill )is,lay the listing for the
)irectory <)e-<+in 4 The $) co//an) can also +e use) to list s,ecific files +y
ty,ing $) +!$#n'&# this "ill )is,lay the file filena/e (of course you can use any
file na/e here04 The $) co//an) can also han)le "il) characters such as the A
an) B 4 For e7a/,le $) '> "ill list all files starting "ith lo"er case a $) @'AA>
"ill list files starting "ith either lo"er or u,,er case a (a or A re/e/+er linu7 is
case sensiti-e0 or $) '? "ill list all t"o character file na/es +eginning "ith
lo"er case a 4 There are /any s"itches (o-er C%0 associate) "ith the ls
co//an) that ,erfor/ s,ecific functions4 So/e of the /ore co//on s"itches
are liste) here4
$) -' This "ill list all file inclu)ing those +eginning "ith theD4D that "oul)
nor/ally +e hi))en fro/ -ie"4
$) -$ This gi-es a long listing sho"ing file attri+utes an) file ,er/issions4
$) -) 9ill )is,lay the listing sho"ing the si3e of each file roun)e) u, to the
nearest 8ilo+yte4
$) -S This "ill list the files accor)ing to file si3e4
$) -C *i-es the listing )is,lay in colu/ns4
$) -F *i-es a sy/+ol ne7t to each file in the listing sho"ing the file ty,e4
The < /eans it is a )irectory, the A /eans an e7ecuta+le file, the E /eans a
sy/+olic lin84
$) -r *i-es the listing in re-erse or)er4
$) -R This gi-es a recursi-e listing of all )irectories +elo" that "here the
co//an) "as issue)4
$) -t Lists the )irectory accor)ing to ti/e sta/,s4
S"itches can +e co/+ine) to ,ro)uce any out,ut )esire)4
e4g4
$) -$'
This "ill list all the files in long for/at sho"ing full file )etails4
&5d!r The /8)ir co//an) is use) to create a ne" )irectory4
&5d!r &(d!r
This "ill /a8e a )irectory (actually a su+ )irectory0 "ithin the current
)irectory calle) /y)ir4

&/ The /- co//an) /o-es files fro/ one location to another4 9ith the /-
co//an) the file "ill +e /o-e) an) no longer e7ist in its for/er location
,rior to the /-4 The /- co//an) can also +e use) to rena/e files4 The files
can +e /o-e) "ithin the current )irectory or another )irectory4
0 &(+!$#."t&$ ="o&#="#$0=&(n#.n'&#."t&$
This "ill /o-e the file calle) /yfile4ht/l in the current )irectory to the
)irectory <ho/e<hel,< an) call it /yne"na/e4ht/l4
The /- co//an) has the for/at of
&/ +!$#1 +!$#2
9ith file$ +eing the na/e (inclu)ing the ,ath if nee)e)0 of the file +eing
/o-e) an) file is the na/e (inclu)ing the ,ath if nee)e)0 of the ne" file
+eing create)4
r& The r/ co//an) is use) to )elete files4 So/e -ery ,o"erful s"itches can
+e use) "ith the r/ co//an)4To chec8 the &'n r& file +efore ,lacing e7tra
s"itches on the r/ co//an)4
r& &(+!$#
This "ill )elete the file calle) /y)ir4 To )elete a file in another )irectory for
e7a/,le r& ="o&#="#$$o=%ood,(#."t& "ill )elete the file na/e) goo)+ye4ht/
in the )irectory <ho/e<hello<4
So/e of the co//on s"itches for the r& co//an) are
F4 r& -! this o,erates the r/ co//an) in interacti-e /o)e /eaning it
,ro/,ts +efore )eleting a file4 This gi-es a secon) chance to say no )o not
)elete the file or yes )elete the file4 Linu7 is /erciless an) once so/ething is
)elete) it is gone for goo) so the -! flag (s"itch0 is a goo) one to get into the
ha+it of using4
C4 r& B+ "ill force +y,assing any safeguar)s that /ay +e in ,lace such as
,ro/,ting4 Again this co//an) is han)y to 8no" +ut care shoul) +e ta8en
"ith its use4
&4 r& -r "ill )elete e-ery file an) su+ )irectory +elo" that in "hich the
co//an) "as gi-en4 This co//an) has to +e use) "ith care as no ,ro/,t
"ill +e gi-en in /ost linu7 syste/s an) it "ill /ean instant goo) +ye to
your files if /isuse)4
r&d!r The r/)ir co//an) is use) to )elete a )irectory4
r&d!r &(d!r
This "ill )elete the )irectory (actually a su+ )irectory0 calle) /y)ir4
Ho. to <r!t#C Co&0!$# 'nd Run ' S!&0$# C
*ro%r'& On L!nu6 S()t#&
$4At the co//an) line, ,ic8 a )irectory to sa-e ,rogra/ an)
enter>
-i first,rog4c
Note
All G source co)e files /ust ha-e a 4c file e7tension4
All GHH source co)e files /ust ha-e 4c,, file e7tension4
4;nter the follo"ing ,rogra/>
Iinclu)e Jst)io4hK
int /ain(0
L
int in)e7=
for (in)e7 M %= in)e7 J C= in)e7 M in)e7 H $0
,rintf (NOello 9orl)@PnN0=
return %=
Q
?4 Press GtrlHO to sa-e the file an) GtrlHR to e7it4
S4 ;nter>
gcc 2o /y,rog first,rog4c
444to create an e7ecuta+le calle) /y,rog fro/ your source co)e
(first,rog4c04
OereDs a )etaile) )iscussion of the line a+o-e>
gcc (*NU G Go/,iler0 is ,asse)444
4442o "hich /eans gi-e the e7ecuta+le the na/e that follo"s (i4e4
/y,rog0444
444an) the ,rogra/ to co/,ile (referre) to as the Nsource co)eN0 is
first,rog4c4
'4To run the ,rogra/, enter>
4</y,rog
HARD<ARE ADAILABLE IN THE LAB
GPU OGL LIntel GPU (P2IT ?4%*O34OT0
'$ MB !AM<
&% *B O##<
Intel &F' *LG M4B4
On Boar) soun) U ?# *ra,hics Gar)
Lan car) 8ey +oar)
Mouse
G#!9 #ri-e
$'VV Golor Monitor
UPS
Printer #ot Matri7 Printer , $ Laser1et Printer$$F%
Soft"are GHH ,Linu7
LIST OF *RACTICALS1AS *ER GGSI* 2NIDERSITY
SYLLAB2S 3
L',or'tor( N'&#E A$%or!t"& An'$()!) And D#)!%n
Cour)# Cod# E ETCS 254
SORTING ALGORITHMSE
1. To Analy3e ti/e co/,le7ity of Insertion sort4
2. To Analy3e ti/e co/,le7ity of Wuic8 sort4
-. To Analy3e ti/e co/,le7ity of Merge sort4
DYNAMIC *ROGRAMMINGE
4. To I/,le/ent Largest Go//on Su+se:uence4
5. To I/,le/ent O,ti/al Binary Search Tree4
4. To I/,le/ent Matri7 Ghain Multi,lication4
DIDIDE AND CONF2ER TECHNIF2EE
7. To I/,le/ent StrassenVs /atri7 /ulti,lication Algorith/4
GREEDY ALGORITHMGSE
8. To i/,le/ent .na,sac8 Pro+le/4
9. To i/,le/ent Acti-ity Selection Pro+le/4
GRA*HSE
1;.To i/,le/ent #ij8straVs Algorith/4
11. To i/,le/ent 9arshallVs Algorith/4
12.To i/,le/ent Bell/an For)Vs Algorith/4
1-.To i/,le/ent #e,th First Search Algorith/4
14.To i/,le/ent Brea)th First Search Algorith/4
STRING MATCHING ALGORITHMSE
15.To i/,le/ent NaX-e String Matching Algorith/4
14.To i/,le/ent !a+in .ar, String Matching Algorith/
S*ANNING TREESE
17.To i/,le/ent Pri/Vs Algorith/4
18.To i/,le/ent .rus8alVs Algorith/4
FORMAT OF THE LAB RECORDS TO BE *RE*ARED BY THE
ST2DENTS
The stu)ents are re:uire) to /aintain the la+ recor)s as ,er the
instructions>
$4 All the recor) files shoul) ha-e a co-er ,age as ,er the for/at4
4 All the recor) files shoul) ha-e an in)e7 as ,er the for/at4
?4 All the recor)s shoul) ha-e the follo"ing >
I4 #ate
II4 Ai/
III4 Algorith/ Or The Proce)ure to +e follo"e)4
IT4 Progra/
T4 Out,ut
TI4 Ti-a :uestions after each section of ,rogra/s4
MARHING SCHEME
FOR THE
*RACTICAL EXAMINATION
There "ill +e t"o ,ractical e7a/s in each se/ester4
Internal Practical ;7a/
;7ternal Practical ;7a/
INTERNAL *RACTICAL EXAMINATION
It is ta8en +y the concerne) lecturer of the +atch4
MARHING SCHEME FOR INTERNAL EXAM IS>
Total Mar8s> S%
#i-ision of S% /ar8s is as follo"s
$4 !egularity> '
Perfor/ing ,rogra/ in each turn of the la+
Atten)ance of the la+
File
4 Ti-a Toice> $%
?4 Presentation> '
NOTE EFor the regularity, /ar8s are a"ar)e) to the stu)ent out of $% for
each e7,eri/ent ,erfor/e) in the la+ an) at the en) the a-erage /ar8s are
gi-en out of '4
EXTERNAL *RACTICAL EXAMINATION
It is ta8en +y the concerne) lecturer of the +atch an) +y an e7ternal e7a/iner4
In this e7a/ stu)ent nee)s to ,erfor/ the e7,eri/ent allotte) at the ti/e of the
e7a/ination, a sheet "ill +e gi-en to the stu)ent in "hich so/e )etails as8e) +y
the e7a/iner nee)s to +e "ritten an) at the last -i-a "ill +e ta8en +y the
e7ternal e7a/iner4
MARHING SCHEME FOR THIS EXAM IS>
Total Mar8s> F%
#i-ision of F% /ar8s is as follo"s
$4 Sheet fille) +y the stu)ent> $'
4 Ti-a Toice> %
?4 ;7,eri/ent ,erfor/ance> $'
S4 File su+/itte)> $%
NOTEE
Internal /ar8s H ;7ternal /ar8s M Total /ar8s gi-en to the stu)ents
(S% /ar8s0 (F% /ar8s0 ($%% /ar8s0
;7,eri/ents gi-en to ,erfor/ can +e fro/ any section of the la+4
STE*S TO BE TAHEN IM*LEMENT *ROGRAMMS IN
ALGORITHM ANALYSIS AND DESIGN
The ,rogra//s to +e )one in the la+ are )i-i)e) in se-en sections4
The first section is a+out co/,le7ities of -arious sorting algorith/s4 Best, A-erage
an) 9orst case co,le7ities of insertion ,:uic8 an) /erge sort techni:ues are
co/,are) +y ,lotting gra,h for -aring in,ut si3es an) ti/e re:uire) +y the
,articular algorith/4
The secon) section is a+out )yna/ic ,rogar//ing techni:ue4#yna/ic
,rogra//ing sol-es ,ro+le/s +y co/+ining the solution of su+
,ro+le/s4 It is only a,,lica+le "hen su+ ,ro+le/s are not in)e,en)ent,
that is, they share su+ su+2Pro+le/s4 ;ach ti/e a ne" su+ ,ro+le/ is
sol-e), its solution is store) such that other su+ ,ro+le/s sharing the
store) su+ ,ro+le/ can use the store) -alue instea) of )oing a
recalculation, there+y sa-ing "or8 co/,are) to a,,lying the )i-i)e2an)2
con:uer ,rinci,le on the sa/e ,ro+le/ "hich "oul) ha-e recalculate)
e-erything4

The thir) section is a+out )i-i)e an) con:uer techni:ue4 To use )i-i)e an) con:uer
as an algorith/ )esign techni:ue,the ,ro+le/ /ust +e )i-i)e) into t"o s/aller
su+,ro+le/s, sol-e each of the/ recursi-ely, an) then /el) the t"o ,artial
solutions into one solution to the full ,ro+le/4 9hene-er the /erging ta8es less
ti/e than sol-ing the t"o su+,ro+le/s, "e get an efficient algorith/4 Mergesort is
the classic e7a/,le of a )i-i)e2an)2con:uer algorith/4 It ta8es only linear ti/e to
/erge t"o sorte) lists of n< ele/ents each of "hich "as o+taine) in ti/e4
#i-i)e an) con:uer is a )esign techni:ue "ith /any i/,ortant algorith/s to its
cre)it, inclu)ing /ergesort, the fast Fourier transfor/, an) StrassenDs /atri7
/ulti,lication algorith/4
The fourth section is a+out *ree)y strategy of )esigning algorith/s4 In gree)y
strategy algorith/ al"ays ta8es the +est i//e)iate, or local, solution "hile fin)ing
an ans"er4 *ree)y algorith/s fin) the o-erall, or glo+ally, o,ti/al solution for
so/e o,ti/i3ation ,ro+le/s, +ut /ay fin) less2than2o,ti/al solutions for so/e
instances of other ,ro+le/s4
The fifth section is relate) to gra,hs4 A gra,h is a 8in) of )ata structure , that
consists of a set of no)es an) a set of e)ges that esta+lish relationshi,s
(connections0 +et"een the no)es4
The si7th section is a+out string /atching algorith/s 4The ,ro+le/ of string
/atching is a ,re-alent an) i/,ortant ,ro+le/ in co/,uter science to)ay4 The
,ro+le/ is to search for a ,attern string, pat[1..m], in a te7t string txt[1..n]4
Usually n>>m, an) txt /ight +e -ery long in)ee), although this is not
necessarily so4
The se-enth section is a+out s,anning tree4 One a,,lication of s,anning tree coul)
+e a ca+le TT co/,any laying ca+le to a ne" neigh+orhoo)4 If it is constraine) to
+ury the ca+le only along certain ,aths, then there "oul) +e a gra,h re,resenting
"hich ,oints are connecte) +y those ,aths4 So/e of those ,aths /ight +e /ore
e7,ensi-e, +ecause they are longer, or re:uire the ca+le to +e +urie) )ee,er= these
,aths "oul) +e re,resente) +y e)ges "ith larger "eights4 A s,anning tree for that
gra,h "oul) +e a su+set of those ,aths that has no cycles +ut still connects to e-ery
house4 A /ini/u/ s,anning tree or /ini/u/ "eight s,anning tree is a s,anning
tree "ith "eight less than or e:ual to the "eight of e-ery other s,anning tree4 More
generally, any un)irecte) gra,h has a /ini/u/ s,anning forest4
SECTION I
ANALSIS OF
SORTING TECHNIF2ES
ANALYSIS OF SORTING ALGORITHMS
B#)tC <or)tC 'nd A/#r'%#-C')#
The worst case complexity of the algorith/ is the function )efine) +y the
/a7i/u/ nu/+er of ste,s ta8en on any instance of si3e n4
The best case complexity of the algorith/ is the function )efine) +y the /ini/u/
nu/+er of ste,s ta8en on any instance of si3e n4

The average-case complexity of the algorith/ is the function )efine) +y an
a-erage nu/+er of ste,s ta8en on any instance of si3e n4
;ach of these co/,le7ities )efines a nu/erical function 2 ti/e -s4 si3e4
INSERTION SORT
Insertion sort is a si/,le sorting algorith/ that is relati-ely efficient for s/all lists
an) /ostly2sorte) lists, an) often is use) as ,art of /ore so,histicate) algorith/s4
It "or8s +y ta8ing ele/ents fro/ the list one +y one an) inserting the/ in their
correct ,osition into ne" sorte) list4 In arrays, the ne" list an) the re/aining
ele/ents can share the arrayDs s,ace, +ut insertion is e7,ensi-e, re:uiring shifting
all follo"ing ele/ents o-er +y one4 The insertion sort "or8s just li8e its na/e
suggests 2 it inserts each ite/ into its ,ro,er ,lace in the final list4 The si/,lest
i/,le/entation of this re:uires t"o list structures 2 the source list an) the list into
"hich sorte) ite/s are inserte)4 To sa-e /e/ory, /ost i/,le/entations use an in2
,lace sort that "or8s +y /o-ing the current ite/ ,ast the alrea)y sorte) ite/s an)
re,eate)ly s"a,,ing it "ith the ,rece)ing ite/ until it is in ,lace4 Shell sort a
-ariant of insertion sort that is /ore efficient for larger lists4
Analysis of Insertion Sort
Gount the nu/+er of ti/es each line of ,seu)oco)e "ill +e e7ecute)4
Line InsertionSort(A0 IInst4 I;7ec4
$ for j>M to len4 of A )o c$ n
8ey>MAYjZ c n2$
? <A ,ut AYjZ into AY$44j2$Z A< c?M% <
S i>Mj2$ cS n2$
'
"hile )o
c' tj
F AYiH$Z>M AYiZ cF
C i >M i2$ cC
& AYiH$Z>M8ey c& n2$
The +or state/ent is e7ecute) (n2$0H$ ti/es4

9ithin the +or state/ent, N8ey>MAYjZN is e7ecute) n2$ ti/es4
Ste,s ', F, C are har)er to count4
Let the nu/+er of ele/ents that ha-e to +e sli)e right to insert the jth ite/4
Ste, ' is e7ecute) ti/es4
Ste, F is 4
A)) u, the e7ecute) instructions for all ,seu)oco)e lines to get the run2ti/e of the
algorith/>
9hat are the B They )e,en) on the ,articular in,ut4
Best Gase
If itDs alrea)y sorte), all Ds are $4
Oence, the +est case ti/e is
"here C an) D are constants4
9orst Gase
If the in,ut is sorte) in descending or)er, "e "ill ha-e to sli)e all of the alrea)y2
sorte) ele/ents, so , an) ste, ' is e7ecute)
MERGE SORT
Merge sort ta8es a)-antage of the ease of /erging alrea)y sorte) lists into a ne"
sorte) list4 It starts +y co/,aring e-ery t"o ele/ents (i4e4 $ "ith , then ? "ith S4440
an) s"a,,ing the/ if the first shoul) co/e after the secon)4 It then /erges each of
the resulting lists of t"o into lists of four, then /erges those lists of four, an) so
on= until at last t"o lists are /erge) into the final sorte) list4 Of the algorith/s
)escri+e) here, this is the first that scales "ell to -ery large lists4
Merge sort "or8s as follo"s>
$4 #i-i)e the unsorte) list into t"o su+ lists of a+out half the si3e
4 Sort each of the t"o su+ lists
?4 Merge the t"o sorte) su+ lists +ac8 into one sorte) list4

Pseu)oco)e for /ergesort
/ergesort(/0
-ar list left, right
if length(/0 [ $
return /
else
/i))le M length(/0 <
for each 7 in / u, to /i))le
a)) 7 to left
for each 7 in / after /i))le
a)) 7 to right
left M /ergesort(left0
right M /ergesort(right0
result M /erge(left, right0
return result
There are se-eral -ariants for the /erge(0 function, the si/,lest -ariant coul) loo8
li8e this>
Pseu)oco)e for /erge
/erge(left,right0
-ar list result
"hile length(left0 K % an) length(right0 K %
if first(left0 [ first(right0
a,,en) first(left0 to result
left M rest(left0
else
a,,en) first(right0 to result
right M rest(right0
if length(left0 K %
a,,en) left to result
if length(right0 K %
a,,en) right to result
return result
ANALYSIS
The straightfor"ar) -ersion of function /erge re:uires at /ost n ste,s (n ste,s
for co,ying the se:uence to the inter/e)iate array +, an) at /ost n ste,s for
co,ying it +ac8 to array a04 The ti/e co/,le7ity of /ergesort is therefore
T(n0 n H T(n<0 an)
T($0 M %
The solution of this recursion yiel)s
T(n0 n log(n0 O(n log(n00
Thus, the Mergesort algorith/ is o,ti/al, since the lo"er +oun) for the sorting
,ro+le/ of \(n log(n00 is attaine)4
In the /ore efficient -ariant, function /erge re:uires at /ost $4'n ste,s (n< ste,s
for co,ying the first half of the se:uence to the inter/e)iate array +, n< ste,s for
co,ying it +ac8 to array a, an) at /ost n/ ste,s for ,rocessing the secon) half04
This yiel)s a running ti/e of /ergesort of at /ost $4'n log(n0 ste,s4 Algorith/
Mergesort has a ti/e co/,le7ity of ](n log(n00 "hich is o,ti/al4
A )ra"+ac8 of Mergesort is that it nee)s an a))itional s,ace of ](n0 for the
te/,orary array +.

M;!*; SO!T T!;;
F2ICH SORT
Wuic8sort is a )i-i)e an) con:uer algorith/ "hich relies on a ,artition o,eration>
to ,artition an array,an ele/ent, calle) a ,i-ot is choosen,all s/aller ele/ents are
/o-e) +efore the ,i-ot, an) all greater ele/ents are /o-e) after it4 This can +e
)one efficiently in linear ti/e an) in2,lace4 Then recursi-ely sorting can +e )one
for the lesser an) greater su+lists4 ;fficient i/,le/entations of :uic8sort ("ith in2
,lace ,artitioning0 are ty,ically unsta+le sorts an) so/e"hat co/,le7, +ut are
a/ong the fastest sorting algorith/s in ,ractice4 Together "ith its /o)est O(log n0
s,ace usage, this /a8es :uic8sort one of the /ost ,o,ular sorting algorith/s,
a-aila+le in /any stan)ar) li+raries4 The /ost co/,le7 issue in :uic8sort is
choosing a goo) ,i-ot ele/ent= consistently ,oor choices of ,i-ots can result in
)rastically slo"er (O(n00 ,erfor/ance, +ut if at each ste, "e choose the /e)ian
as the ,i-ot then it "or8s in O(n log n04
Wuic8sort sorts +y e/,loying a )i-i)e an) con:uer strategy to )i-i)e a list into t"o
su+2lists4
Pic8 an ele/ent, calle) a ,i-ot, fro/ the list4
!eor)er the list so that all ele/ents "hich are less than ,i-ot co/e +efore the
,i-ot an) so that all ele/ents greater than the ,i-ot co/e after it (e:ual -alues can
go either "ay04 After this ,artitioning, the ,i-ot is in its final ,osition4 This is
calle) the ,artition o,eration4
!ecursi-ely sort the su+2list of lesser ele/ents an) the su+2list of greater ele/ents4
Pseu)oco)e For ,artition(a, left, right, ,i-otIn)e70
,i-otTalue >M aY,i-otIn)e7Z
s"a,(aY,i-otIn)e7Z, aYrightZ0 << Mo-e ,i-ot to en)
storeIn)e7 >M left
for i fro/ left to right2$
if aYiZ [ ,i-otTalue
s"a,(aYstoreIn)e7Z, aYiZ0
storeIn)e7 >M storeIn)e7 H $
s"a,(aYrightZ, aYstoreIn)e7Z0 << Mo-e ,i-ot to its final ,lace
return storeIn)e7
Pseu)oco)e For :uic8sort(a, left, right0
if right K left
select a ,i-ot -alue aY,i-otIn)e7Z
,i-otNe"In)e7 >M ,artition(a, left, right, ,i-otIn)e70
:uic8sort(a, left, ,i-otNe"In)e72$0
:uic8sort(a, ,i-otNe"In)e7H$, right0
ANALYSIS
The ,artition routine e7a/ines e-ery ite/ in the array at /ost once, so co/,le7ity
is clearly O1n34
Usually, the ,artition routine "ill )i-i)e the ,ro+le/ into t"o roughly e:ual si3e)
,artitions4 9e 8no" that "e can )i-i)e n ite/s in half $o%
2
n ti/es4
This /a8es :uic8sort a O1n$o%n3 algorith/ 2 e:ui-alent to hea,sort4
SECTION II
DYNAMIC *ROGRAMING
TECHNIF2E
DYNAMIC *ROGRAMMING
#yna/ic Progra//ing is a techni:ue for co/,uting recurrence relations
efficiently +y sorting ,artial results4 #yna/ic ,rogra//ing is a techni:ue for
efficiently co/,uting recurrences +y storing ,artial results4
Once )yna/ic ,rogra//ing is un)erstoo), it is usually easier to rein-ent certain
algorith/s 4 A )yna/ic ,rogra//ing solution has three co/,onents>
$4 For/ulate the ans"er as a recurrence relation or recursi-e algorith/4
4 Sho" that the nu/+er of )ifferent instances of your recurrence is +oun)e)
+y a ,olyno/ial4
?4 S,ecify an or)er of e-aluation for the recurrence so you al"ays ha-e "hat
you nee)4
LONGEST COMMON S2BSEF2ENCE *ROBLEM
The $on%#)t o&&on )u,)#:u#n# 0ro,$#& (LGS0 is fin)ing a longest se:uence
"hich is a su+se:uence of all se:uences in a set of se:uences (often just t"o04 The
,ro+le/ is so/eti/es )efine) to +e fin)ing all longest co//on su+se:uences4
It shoul) not +e confuse) "ith the longest co//on su+string ,ro+le/ (a su+string
is necessarily a contiguous ,art04
Solution for two sequences
*i-en the se:uences an)
Oere H )enotes concatenation, an) /a7D gi-es the longest se:uence4
Since this ,ro+le/ has an o,ti/al su+structure ,ro,erty, it can +e sol-e) +y
)yna/ic ,rogra//ing4
The rationale for this recurrence is that, if the last character of t"o se:uences are
e:ual, they /ust +e ,art of the LGS4 A larger LGS can ne-er +e o+taine) +y
/atching x
m
to y
j
"here j J n, an) -ice -ersa4 To fin) all the longest co//on
su+se:uences, the LGS shoul) +e )enote) as a set of se:uences, an) Dmax shoul)
return +oth solutions if they are e:ually long4
COM*2TING THE LENGTH OF THE LCS
The +elo" function ta8es as in,ut se:uences RY$44/Z an) ^Y$44nZ co/,utes the
LGS +et"een RY$44iZ an) ^Y$44jZ for all $ [ i [ / an) $ [ j [ n, an) stores it in
GYi,jZ4 GY/,nZ "ill contain the length of the LGS of R an) ^4
+unt!on LGS(RY$44/Z, ^Y$44nZ0
G M array(%44/, %44n0
+or i >M $44/
+or j >M $44n
!+ RYiZ M ^YjZ
GYi,jZ >M GYi2$,j2$Z H $
#$)#>
GYi,jZ >M /a7(GYi,j2$Z, GYi2$,jZ0
r#turn G
The follo"ing function +ac8trac8s the choices ta8en "hen co/,uting the C ta+le4
If the last characters in the ,refi7es are e:ual, they /ust +e in an LGS4 If not, chec8
"hat ga-e the largest LGS of 8ee,ing x
i
an) y
j
, an) /a8e the sa/e choice4 1ust
choose one if they "ere e:ually long4
Gall the function "ith i=m an) j=n4
+unt!on +ac8Trac8(GY%44/,%44nZ, RY$44/Z, ^Y$44nZ, i, j0
!+ i M % or j M %
r#turn NN
#$)# !+ RYiZ M ^YjZ
r#turn +ac8Trac8(G, R, ^, i2$, j2$0 H RYiZ
#$)#
!+ GYi,j2$Z K GYi2$,jZ
r#turn +ac8Trac8(G, R, ^, i, j2$0
#$)#
r#turn +ac8Trac8(G, R, ^, i2$, j0
If choosing x
i
an) y
j
"oul) gi-e an e:ually long result, +oth resulting su+se:uences
shoul) +e sho"n4 This is returne) as a set +y this function4 Notice that this function
is not ,olyno/inal, as it /ight +ranch in al/ost e-ery ste, if the strings are si/ilar4
+unt!on +ac8Trac8All(GY%44/,%44nZ, RY$44/Z, ^Y$44nZ, i, j0
!+ i M % or j M %
r#turn LQ
#$)# !+ RYiZ M ^YjZ>
r#turn L_ H RYi2$Z +or '$$ _ !n +ac8Trac8All(G, R, ^, i2$, j2$0Q
#$)#>
! >M LQ
!+ GYi,j2$Z ` GYi2$,jZ>
! >M ! +ac8Trac8All(G, R, ^, i, j2$0
!+ GYi2$,jZ ` GYi,j2$Z>
! >M ! +ac8Trac8All(G, R, ^, i2$, j0
r#turn !
OPTIMAL BINARY SEARCH TREES
A +inary search tree is a tree "here the 8ey -alues are store) in the internal no)es,
the e7ternal no)es (lea-es0 are null no)es, an) the 8eys are or)ere)
le7icogra,hically4i4e4 for each internal no)e all the 8eys in the left su+tree are less
than the 8eys in the no)e, an) all the 8eys in the right su+tree are greater4
9hen "e 8no" the ,ro+a+ilities of searching each one of the 8eys, it is :uite easy
to co/,ute the e7,ecte) cost of accessing the tree4 An OBST is a BST "hich has
/ini/al e7,ecte) cost4
E6'&0$#E
.ey 2' $ & C $? $
Pro+a+ilities $<& $<? $<$F $<? $<S $<
The e7,ectation2-alue of a search is>
ItDs clear that this tree is not o,ti/al4 2 It is easy to see that if the $ is closer to the
root, gi-en its high ,ro+a+ility, the tree "ill ha-e a lo"er e7,ecte) cost4
CRITERION FOR AN O*TIMAL TREE

;ach o,ti/al +inary search tree is co/,ose) of a root an) (at /ost0 t"o o,ti/al
su+ trees, the left an) the right4 The criterion for o,ti/ality gi-es a )yna/ic
,rogra//ing algorith/4 For the root (an) each no)e in turn0 there are n
,ossi+ilities to select one -alue 4Once this choice is /a)e, the set of 8eys "hich go
into the left su+ tree an) right su+ tree is co/,letely )efine), +ecause the tree is
le7icogra,hically or)ere)4 The left an) right su+ trees are no" constructe)
recursi-ely (o,ti/ally04 This gi-es the recursi-e )efinition of the o,ti/u/ cost>
Let )enote the ,ro+a+ility of accessing 8ey , let )enote the su/ of the
,ro+a+ilities fro/ to
The e7,lanation of the for/ula is easy once "e see that the first ter/ corres,on)s
to the left su+ tree, "hich is one le-el lo"er than the root, the secon) ter/
corres,on)s to the root an) the ? to the right su+ tree4 ;-ery cost is /ulti,lie) +y
its ,ro+a+ility4 For si/,licity "e set an) so si/,lifies to
4 This ,roce)ure is e7,onential if a,,lie) )irectly4 Oo"e-er, the
o,ti/al trees are only constructe) o-er contiguous sets of 8eys, an) there are at
/ost )ifferent sets of contiguous 8eys4
In this case the o,ti/al cost of a su+ tree in a /atri7 T 4The Matri72entry "ill
contain the cost of an o,ti/al su+ tree constructe) "ith the 8eys to
The /atri7 is fille) )iagonal +y )iagonal4 It is custo/ary to fill the /atri7 "ith
that a lot of /ulti,lications an) )i-isions can +e sa-e)4 Let
then
An o,ti/al tree "ith one no)e is just the no)e itself (no other choice0, so the
)iagonal of is easy to fill> 4
The cost of the is in ( in our e7a/,le0 An) you can see, that it is
,ractical, not to "or8 "ith the ,ro+a+ilities, +ut "ith the fre:uencies (i4e the
,ro+a+ilities ti/es the least co//on /ulti,le of their )eno/inators0 to a-oi)
fractions as /atri72entries4
MATRIX CHAIN M2LTI*LICATION
P!OBL;M> Multi,lying a Se:uence of Matrices 4 Su,,ose a long se:uence of
/atrices 4 has to +e /ulti,lie)
Multi,lying an /atri7 +y a /atri7 (using the co//on algorith/0 ta8es
/ulti,lications4
In /atri7 /ulti,lication it is +etter to a-oi) +ig inter/e)iate /atrices, an) since
/atri7 /ulti,lication is associative, "e can ,arenthesise ho"e-er "e "ant4
Matri7 /ulti,lication is not communitive, so the or)er of the /atrices can not +e
,er/ute) "ithout changing the result4
;7a/,le
Gonsi)er , "here A is , is , C is , an) D is 4
There are three ,ossi+le ,arenthesi3ations>
The or)er /a8es a +ig )ifference in real co/,utation4 Let !(i,j0 +e the minimum
nu/+er of /ulti,lications necessary to co/,ute 4
The 8ey o+ser-ations are
The outer/ost ,arentheses ,artition the chain of /atricies (i,j0 at so/e "4
The o,ti/al ,arenthesi3ation or)er has o,ti/al or)ering on either si)e of "4
A recurrence for this is>
If there are n /atrices, there are n#$ )i/ensions4
A )irect recursi-e i/,le/entation of this "ill +e e7,onential, since there is a lot of
)u,licate) "or8 as in the Fi+onacci recurrence4
#i-i)e2an)2con:uer is see/s efficient +ecause there is no o-erla,, +ut 444
There are only su+strings +et"een $ an) n4 Thus it re:uires only s,ace to
store the o,ti/al cost for each of the/4
All the ,ossi+ilities can +e re,resente) in a triangle /atri74 9e can also store the
-alue of " in another triangle /atri7 to reconstruct to or)er of the o,ti/al
,arenthesisation4
The )iagonal /o-es u, to the right as the co/,utation ,rogresses4 On each
ele/ent of the "th )iagonal aj2ia M "4
Pseu)oco)e Matri7Or)er
for iM$ to n )o !Yi, jZM%
for diagonalM$ to n-$
for iM$ to n-diagonal )o
jMiHdiagonal

faster(i,j0M"
return Ym($, n0Z
Pseu)oco)e Sho"Or)er(i, j0
if (iMj0 "rite ( 0
else
"Mfactor(i, j0
"rite bb(DD
Sho"Or)er(i, "0
"rite bbADD
Sho"Or)er ("H$, j0
"rite bb0DD
SECTION III
DIDIDE AND CONF2ER
TECHNIF2E
DIDIDE AND CONF2ER TECHNIF2E
#i-i)e an) con:uer "as a successful /ilitary strategy long +efore it +eca/e an
algorith/ )esign ,ara)ig/4 *enerals o+ser-e) that it "as easier to )efeat one ar/y
of '%,%%% /en, follo"e) +y another ar/y of '%,%%% /en than it "as to +eat a
single $%%,%%% /an ar/y4 Thus the "ise general "oul) attac8 so as to )i-i)e the
ene/y ar/y into t"o forces an) then /o, u, one after the other4
To use )i-i)e an) con:uer as an algorith/ )esign techni:ue, "e /ust )i-i)e the
,ro+le/ into t"o s/aller su+,ro+le/s, sol-e each of the/ recursi-ely, an) then
/el) the t"o ,artial solutions into one solution to the full ,ro+le/4 9hene-er the
/erging ta8es less ti/e than sol-ing the t"o su+,ro+le/s, "e get an efficient
algorith/4
#i-i)e an) con:uer is a )esign techni:ue "ith /any i/,ortant algorith/s to its
cre)it, inclu)ing /ergesort, the fast Fourier transfor/, an) StrassenDs /atri7
/ulti,lication algorith/4
STRASSEN ALGORITHM
In the /athe/atical )isci,line of linear alge+ra, t"# Str'))#n '$%or!t"&, na/e)
after Tol8er Strassen, is an algorith/ use) for /atri7 /ulti,lication4 It is
asy/,totically faster than the stan)ar) /atri7 /ulti,lication algorith/, +ut slo"er
than the fastest 8no"n algorith/4
Algorith
Let A, +e t"o s:uare /atrices o-er a fiel) %4 9e "ant to calculate the /atri7
,ro)uct C as
If the /atrices A, are not of ty,e
n
7
n
"e fill the /issing ro"s an) colu/ns
"ith 3eros4
9e ,artition A, an) C into e:ually si3e) +loc8 /atrices
"ith
then
9ith this construction "e ha-e not re)uce) the nu/+er of /ulti,lications4 9e still
nee) & /ulti,lications to calculate the C
i,j
/atrices, the sa/e nu/+er of
/ulti,lications "e nee) "hen using stan)ar) /atri7 /ulti,lication4
No" co/es the i/,ortant ,art4 9e )efine ne" /atrices
"hich are then use) to e7,ress the C
i,j
in ter/s of !
8
4 Because of our )efinition of
the !
8
"e can eli/inate one /atri7 /ulti,lication an) re)uce the nu/+er of
/ulti,lications to C (one /ulti,lications for each !
8
0 an) e7,ress the C
i,j
as
9e iterate this )i-ision ,rocess n2ti/es until the su+/atrices )egenerate into
nu/+ers4
Practical i/,le/entations of StrassenDs algorith/ s"itch to stan)ar) /etho)s of
/atri7 /ulti,lication for s/all enough su+/atrices, for "hich they are /ore
efficient= the o-erhea) of StrassenDs algorith/ i/,lies that these Ns/all enoughN
su+/atrices are actually :uite large, "ell into thousan)s of ele/ents4
ANALYSIS OF STRASSEN ALGORITHM
The stan)ar) /atri7 /ulti,lications ta8es
/ulti,lications of the ele/ents in the fiel) %4 9e ignore the a))itions nee)e)
+ecause, )e,en)ing on %, they can +e /uch faster than the /ulti,lications in
co/,uter i/,le/entations, es,ecially if the si3es of the /atri7 entries e7cee) the
"or) si3e of the /achine4
9ith the Strassen algorith/ "e can re)uce the nu/+er of /ulti,lications to
4
The re)uction in the nu/+er of /ulti,lications ho"e-er co/es at the ,rice at a
so/e"hat re)uce) nu/eric sta+ility4
SECTION ID
GREEDY TECHNIF2E
GREEDY ALGORITHMS
*ree)y Algorith/ "or8s +y /a8ing the )ecision that see/s /ost ,ro/ising at any
/o/ent= it ne-er reconsi)ers this )ecision, "hate-er situation /ay arise later4 They
ta8e )ecisions on the +asis of infor/ation at han) "ithout "orrying a+out the
effect these )ecisions /ay ha-e in the future4 They are easy to in-ent, easy to
i/,le/ent an) /ost of the ti/e :uite efficient4 Many ,ro+le/s cannot +e sol-e)
correctly +y gree)y a,,roach4 *ree)y algorith/s are use) to sol-e o,ti/i3ation
,ro+le/s4
C"'r't#r!)t!) 'nd F#'tur#) o+ *ro,$#&) )o$/#d ,( Gr##d( A$%or!t"&)
To construct the solution in an o,ti/al "ay, Algorith/ /aintains t"o sets4 One
contains chosen ite/s an) the other contains rejecte) ite/s4
The gree)y algorith/ consists of four function4
$4 A function that chec8s "hether chosen set of ite/s ,ro-i)e a solution4
4 A function that chec8s the feasi+ility of a set4
?4 The selection function tells "hich of the can)i)ates is the /ost ,ro/ising4
S4 An o+jecti-e function, "hich )oes not a,,ear e7,licitly, gi-es the -alue of a
solution4

Strutur# Gr##d( A$%or!t"&
Initially the set of chosen ite/s is e/,ty i4e4, solution set4
At each ste,
o ite/ "ill +e a))e) in a solution set +y using selection function4
o IF the set "oul) no longer +e feasi+le
reject ite/s un)er consi)eration (an) is ne-er consi)er again04
o ;LS; IF set is still feasi+le TO;N
a)) the current ite/4
A feasi+le set (of can)i)ates0 is ,ro/ising if it can +e e7ten)e) to ,ro)uce not
/erely a solution, +ut an o,ti/al solution to the ,ro+le/4 In ,articular, the
e/,ty set is al"ays ,ro/ising +ecause an o,ti/al solution al"ays e7ists4
Unli8e #yna/ic Progra//ing, "hich sol-es the su+,ro+le/s +otto/2u,, a gree)y
strategy usually ,rogresses in a to,2)o"n fashion, /a8ing one gree)y choice after
another, re)ucing each ,ro+le/ to a s/aller one4
*ree)y2Ghoice Pro,erty
The Ngree)y2choice ,ro,ertyN an) No,ti/al su+structureN are t"o ingre)ients in the
,ro+le/ that len) to a gree)y strategy4It says that a glo+ally o,ti/al solution can
+e arri-e) at +y /a8ing a locally o,ti/al choice4
HNA*SACH *ROBLEM
The 5n'0)'5 0ro,$#& is a ,ro+le/ in co/+inatorial o,ti/i3ation4 It )eri-es its
na/e fro/ the /a7i/i3ation ,ro+le/ of choosing ,ossi+le essentials that can fit
into one +ag (of /a7i/u/ "eight0 to +e carrie) on a tri,4 A si/ilar ,ro+le/ -ery
often a,,ears in +usiness, co/+inatorics, co/,le7ity theory, cry,togra,hy an)
a,,lie) /athe/atics4 *i-en a set of ite/s, each "ith a cost an) a -alue, then
)eter/ine the nu/+er of each ite/ to inclu)e in a collection so that the total cost is
less than so/e gi-en cost an) the total -alue is as large as ,ossi+le4
!ree"# $%%ro&i$tion $lgorith
Martello an) Toth ($cc%0 ,ro,ose) a gree)y a,,ro7i/ation algorith/ to sol-e the
8na,sac8 ,ro+le/4 Their -ersion sorts the essentials in )ecreasing or)er an) then
,rocee)s to insert the/ into the sac8, starting fro/ the first ele/ent (the greatest0
until there is no longer s,ace in the sac8 for /ore4 If " is the /a7i/u/ ,ossi+le
nu/+er of essentials that can fit into the sac8, the gree)y algorith/ is guarantee)
to insert at least "< of the/4
D(n'&! *ro%r'&&!n% +or ;-1 Hn'0)'5 *ro,$#&
For this algorith/ let cYi,"Z M -alue of solution for ite/s $44i an) /a7i/u/ "eight
"4
cYi,"Z M
#P2%$.(-, ", n, 90
$ for " M % to 9
cY%,"Z M %
? for i M $ to n
S cYi,%Z M %
' for " M $ to 9
F if "YiZ "
C then if -YiZ H cYi2$,"2"YiZZ K cYi2$,"Z
& then cYi,"Z M -YiZ H cYi2$,"2"YiZZ
c else cYi,"Z M cYi2$,"Z
$% else cYi,"Z M cYi2$,"Z
The run ti/e ,erfor/ance of this algorith/ is 4
AN ACTIDITY SELECTION *ROBLEM
An acti-ity2selection is the ,ro+le/ of sche)uling a resource a/ong se-eral
co/,eting acti-ity4

*ro,$#& St't#&#nt
*i-en a set & of n acti-ities "ith an) start ti/e, &
i
an) '
i
, finish ti/e of an i
th

acti-ity4 Fin) the /a7i/u/ si3e set of /utually co/,ati+le acti-ities4

Co&0't!,$# At!/!t!#)
Acti-ities i an) j are co/,ati+le if the half2o,en internal Ys
i
, '
i
0 an) Ys
j
,
'
j
0 )o not o-erla,, that is, i an) j are co/,ati+le if s
i
` '
j
an) s
j
` '
i

Gr##d( A$%or!t"& +or S#$#t!on *ro,$#&
I4 Sort the in,ut acti-ities +y increasing finishing ti/e4
'
$
[ '
(
[ 4 4 4 [ '
n
II4 Gall GREEDY-ACTIDITY-SELECTOR (s, f0
$4 n M length YsZ
4 AMLiQ
?4 j M $
S4 +or i M to n
'4 do !+ s
i
` '
j

F4 t"#n AM AULiQ
C4 j M i
&4 r#turn set A


O0#r't!on o+ t"# '$%or!t"&
Let $$ acti-ities are gi-en & M Lp, ), r, s, t, u, v, w, x, y, *Q start an) finishe) ti/es
for ,ro,ose) acti-ities are ($, S0, (?, '0, (%, F0, ', C0, (?, &0, ', c0, (F, $%0, (&, $$0, (&,
$0, (, $?0 an) ($, $S04
A M L,Q Initiali3ation at line
A M L,, sQ line F 2 $
st
iteration of FO! 2 loo,
A M L,, s, "Q line F 2
n)
iteration of FO! 2 loo,
A M L,, s, ", 3Q line F 2 ?
r)
iteration of FO!2loo,
Out of the FO!2loo, an) !eturn A M L,, s, ", 3Q


An'$()!)
Part I re:uires O+n lg n, ti/e (use /erge of hea, sort04
Part II re:uires d+n, ti/e assu/ing that acti-ities "ere alrea)y sorte)
in ,art I +y their finish ti/e4


Corr#tn#))
Note that *ree)y algorith/ )o not al"ays ,ro)uce o,ti/al solutions +ut
*!;;#^2AGTITIT^2S;L;GTO! )oes4
T"#or#& Algorit-m ./00D-ACT121T3-&040CTO/ produces solution o'
maximum si*e 'or t-e activity-selection problem.

Proof
I4 Let & 5 6$, (, . . . , n7 +e the set of acti-ities4 Since acti-ities are in or)er +y
finish ti/e4 It i/,lies that acti-ity $ has the earliest finish ti/e4
Su,,ose, A & is an o,ti/al solution an) let acti-ities in A are or)ere) +y
finish ti/e4 Su,,ose, the first acti-ity in A is "4
If " 5 $, then A +egins "ith gree)y choice an) "e are )one (or to +e -ery
,recise, there is nothing to ,roof here04
If " $, "e "ant to sho" that there is another solution that +egins "ith
gree)y choice, acti-ity $4
Let 5 A 2 L"Q L$Q4 Because '
$
'
"
, the acti-ities in are )isjoint an)
since B has sa/e nu/+er of acti-ities as A, i4e4, 8A8 5 88, is also o,ti/al4
II4 Once the gree)y choice is /a)e, the ,ro+le/ re)uces to fin)ing an o,ti/al
solution for the ,ro+le/4 If A is an o,ti/al solution to the original ,ro+le/
&, then A9 5 A - 6$7 is an o,ti/al solution to the acti-ity2selection ,ro+le/
&9 5 6i &: &
i
'
i
74
"hyB Because if "e coul) fin) a solution 9 to &9 "ith /ore acti-ities then
A9, a))ing $ to 9 "oul) yiel) a solution to & "ith /ore acti-ities than A,
there +y contra)icting the o,ti/ality4 e



SECTION D
GRA*HS
!RAPH AL!ORITHMS
*ra,h Theory is an area of /athe/atics that )eals "ith follo"ing ty,es of
,ro+le/s
Gonnection ,ro+le/s
Sche)uling ,ro+le/s
Trans,ortation ,ro+le/s
Net"or8 analysis
*a/es an) Pu33les4
The *ra,h Theory has i/,ortant a,,lications in Gritical ,ath analysis, Social
,sychology, Matri7 theory, Set theory, To,ology, *rou, theory, Molecular
che/istry, an) Searching4
DIIHSTRAJS ALGORITHM
#ij8straDs algorith/ sol-es the single2source shortest2,ath ,ro+le/ "hen all e)ges
ha-e non2negati-e "eights4 It is a gree)y algorith/ an) si/ilar to Pri/Ds
algorith/4 Algorith/ starts at the source -erte7, s, it gro"s a tree, T, that ulti/ately
s,ans all -ertices reacha+le fro/ S4 Tertices are a))e) to T in or)er of )istance i4e4,
first S, then the -erte7 closest to S, then the ne7t closest, an) so on4 Follo"ing
i/,le/entation assu/es that gra,h * is re,resente) +y a)jacency lists4

DIIHSTRA 1GC .C )3
$4 INITIALI_; SIN*L;2SOU!G; (*, s0
4 S f L Q << S "ill ulti/ately contains -ertices of final
shortest2,ath "eights fro/ s
?4 Initiali3e ,riority :ueue W i4e4, W f TY*Z
S4 "hile ,riority :ueue W is not e/,ty )o
'4 u f ;RT!AGTgMIN(W0 << Pull out ne" -erte7
F4 S f S h LuQ << Perfor/ rela7ation for each -erte7 v
a)jacent to u
C4 for each -erte7 - in A)jYuZ )o
&4 !ela7 (u, v, w0

ANALYSIS
Li8e Pri/Ds algorith/, #ij8straDs algorith/ runs in O(a;algaTa0 ti/e4

FLOYD <ARSHALLGS ALGORITHM
Floy) "arshall algorith/ is use) to sol-e the all ,airs shortest ,ath
,ro+le/ in a "eighte), )irecte) gra,h +y /ulti,lying an a)jacency2/atri7
re,resentation of the gra,h /ulti,le ti/es4 The e)ges /ay ha-e negati-e
"eights, +ut no negati-e "eight cycles4
St#0) to !&0$#&#nt F$o(d <'r)"'$$G) '$%or!t"&
$4 YInitiali3e /atri7 /Z
!e,eat through ste, fir I M % ,$ , ,? ,i44, n $
!e,eat through ste, fir j M% ,$ , ,? ,i44, n $
4 YTest the con)ition an) assign the re:uire) -alue to /atri7 /Z
If a Y I Z YjZ M %
M Y I Z Y j Z M infinity
;lse
M Y I Z Y j Z M a Y I Z Y j Z
?4 Y Shortest ,ath e-aluation Z
!e,eat through ste, S for 8 M % , $ , , ? , i4 , n $
!e,eat through ste, S for I M % , $ , , ? , i4 , n $
!e,eat through ste, S for j M % , $ , , ? , i4 , n $
S 4 If / Y I Z YjZ J / Y I ZY8Z H /Y8ZYjZ
MYiZY jZ M / Y I Z Y j Z
;lse
M YI Z Y j Z M / Y I Z Y j Z H/ Y 8Z Y j Z
' 4 ;7it

ANALYSIS
The ti/e co/,le7ity is ; +2<,4
BELLMAN-FORD ALGORITHM
Bell/an2For) algorith/ sol-es the single2source shortest2,ath ,ro+le/ in the
general case in "hich e)ges of a gi-en )igra,h can ha-e negati-e "eight as long as
* contains no negati-e cycles4This algorith/, li8e #ij8straDs algorith/ uses the
notion of e)ge rela7ation +ut )oes not use "ith gree)y /etho)4 Again, it uses )YuZ
as an u,,er +oun) on the )istance )Yu, vZ fro/ u to v4
The algorith/ ,rogressi-ely )ecreases an esti/ate )Y-Z on the "eight of the
shortest ,ath fro/ the source -erte7 s to each -erte7 - in T until it achie-e the
actual shortest2,ath4 The algorith/ returns Boolean T!U; if the gi-en )igra,h
contains no negati-e cycles that are reacha+le fro/ source -erte7 s other"ise it
returns Boolean FALS;4

BELLMAN-FORD 1GC wC s3
$4 INITIALI_;2SIN*L;2SOU!G; (*, s0
4 for each -erte7 i M $ to TY*Z 2 $ )o
?4 for each e)ge (u, v0 in ;Y*Z )o
S4 !;LAR (u, v, w0
'4 For each e)ge (u, v0 in ;Y*Z )o
F4 if )YuZ H w(u, v0 J )Yv= then
C4 return FALS;
&4 return T!U;

ANALYSIS
The initiali3ation in line $ ta8es +v0 ti/e
For loo, of lines 2S ta8es O(;0 ti/e an) For2loo, of line '2C ta8es O(;0
ti/e4
Thus, the Bell/an2For) algorith/ runs in O(;0 ti/e4

TRADERSAL IN A GRA*H
DE*TH-FIRST SEARCH (DFS0 is an algorith/ for tra-ersing or searching a
gra,h4 Intuiti-ely, one starts at the so/e no)e as the root an) e7,lores as far as
,ossi+le along each +ranch +efore +ac8trac8ing4
For/ally, #FS is an uninfor/e) search that ,rogresses +y e7,an)ing the first chil)
no)e of the gra,h that a,,ears an) thus going )ee,er an) )ee,er until a goal no)e
is foun), or until it hits a no)e that has no chil)ren4 Then the search +ac8trac8s,
returning to the /ost recent no)e it ha)nDt finishe) e7,loring4 In a non2recursi-e
i/,le/entation, all freshly e7,an)e) no)es are a))e) to a LIFO stac8 for
e7,ansion4
St#0) +or !&0$#&#nt!n% D#0t" +!r)t )#'r"
$4 #efine an array B or Tert that store Boolean -alues, its si3e shoul) +e
greater or e:ual to the nu/+er of -ertices in the gra,h *4
4 Initiali3e the array B to false
?4 For all -ertices - in *
if BY-Z M false
,rocess (-0
S4 ;7it
DFS '$%or!t"& u)#d to )o$/# +o$$o.!n% 0ro,$#&)E
Testing "hether gra,h is connecte)4
Go/,uting a s,anning forest of gra,h4
Go/,uting a ,ath +et"een t"o -ertices of gra,h or e:ui-alently re,orting that no
such ,ath e7ists4
Go/,uting a cycle in gra,h or e:ui-alently re,orting that no such cycle e7ists4

ANALYSIS
The running ti/e of #SF is (T H ;04


BREADTH FIRST SEARCH 1BFS0 is an uninfor/e) search /etho) that ai/s to
e7,an) an) e7a/ine all no)es of a gra,h syste/atically in search of a solution4 In
other "or)s, it e7hausti-ely searches the entire gra,h "ithout consi)ering the goal
until it fin)s it4
Fro/ the stan),oint of the algorith/, all chil) no)es o+taine) +y e7,an)ing a no)e
are a))e) to a FIFO :ueue4 In ty,ical i/,le/entations, no)es that ha-e not yet
+een e7a/ine) for their neigh+ors are ,lace) in so/e container (such as a :ueue or
lin8e) list0 calle) No,enN an) then once e7a/ine) are ,lace) in the container
Nclose)N4
St#0) +or !&0$#&#nt!n% Br#'dt" +!r)t )#'r"
$4 Initiali3e all the -ertices +y setting Flag M $
4 Put the starting -erte7 A in W an) change its status to the "aiting state +y
setting Flag M %
?4 !e,eat through ste, ' "hile W is not NULL
S4 !e/o-e the front -erte7 - of W 4 ,rocess - an) set the status of - to the
,rocesse) status +y setting Flag M 2$
'4 A)) to the rear of W all the neigh+our of - that are in the rea)y state +y
setting Flag M $ an) change their status to the "aiting state +y setting flag
M %
F4 ;7it
Brea)th First Search algorith/ use) in
Pri/Ds MST algorith/4
#ij8straDs single source shortest ,ath
algorith/4
Testing "hether gra,h is
connecte)4
Go/,uting a cycle in gra,h or re,orting that no such cycle e7ists4


ANALYSIS
Total running ti/e of BFS is O(T H ;04

Li8e )e,th first search, BFS tra-erse a connecte) co/,onent of a gi-en gra,h an)
)efines a s,anning tree4
S,ace co/,le7ity of #FS is /uch lo"er than BFS (+rea)th2first search04 It also
len)s itself /uch +etter to heuritic /etho)s of choosing a li8ely2loo8ing +ranch4
Ti/e co/,le7ity of +oth algorith/s are ,ro,ortional to the nu/+er of -ertices ,lus
the nu/+er of e)ges in the gra,hs they tra-erse4

SECTION DI
STRING MATCHING
ALGORITHMS
STRING MATCHING
The ,ro+le/ of )tr!n% &'t"!n% is a ,re-alent an) i/,ortant ,ro+le/ in co/,uter
science to)ay4 There are really t"o for/s of string /atching4 The first, #6't
)tr!n% &'t"!n%, fin)s instances of so/e 0'tt#rn in a t'r%#t string4 For e7a/,le,
if the ,attern is NgoN an) the target is NagogoN, then t"o instances of the ,attern
a,,ear in the te7t (at the secon) an) fourth characters, res,ecti-ely04 The secon),
!n#6't )tr!n% &'t"!n% or )tr!n% '$!%n&#nt, atte/,ts to fin) the N+estN /atch of
a ,attern to so/e target4 Usually, the /atch of a ,attern to a target is either
,ro+a+ilistic or e-aluate) +ase) on so/e fi7e) criteria (for e7a/,le, the ,attern
NaggtgcN /atches the target NagtgcggtgN ,retty "ell in t"o ,laces, locate) at the
first character of the string an) the si7th character04 Both string /atching
algorith/s are use) e7tensi-ely in +ioinfor/atics to isolate structurally si/ilar
regions of #NA or a ,rotein (usually in the conte7t of a gene /a, or a ,rotein
)ata+ase04
;7act string /atching> The ,ro+le/ is to search for a ,attern string, ,atY$44/Z, in a
te7t string t7tY$44nZ4 Usually nKK/, an) t7t /ight +e -ery long in)ee), although
this is not necessarily so4 This ,ro+le/ occurs in te7t2e)itors an) /any other
co/,uter a,,lications4
NAIDE STRING MATCHING
The nai-e string searching algorith/ is to e7a/ine each ,osition, iKM$, in t7t,
trying for e:uality of ,atY$44/Z "ith t7tYi44iH/2$Z4 If there is ine:uality, ,osition iH$
is trie), an) so on4
Algorith
An$l#sis
!e/e/+er that a>a M m, aTa M n4
Inner loo, "ill ta8e m ste,s to confir/ the ,attern /atches
Outer loo, "ill ta8e n2mH$ ste,s
Therefore, "orst case is
RABIN-HAR* STRING SEARCH
The !a+in2.ar, algorith/ is a string searching algorith/ create) +y Michael O4
!a+in an) !ichar) M4 .ar, that see8s a ,attern, i4e4 a su+string, "ithin a te7t +y
using hashing4 It is not "i)ely use) for single ,attern /atching, +ut is of
consi)era+le theoretical i/,ortance an) is -ery effecti-e for /ulti,le ,attern
/atching4 For te7t of length n an) ,attern of length /, its a-erage an) +est case
running ti/e is O(n0, +ut the (highly unli8ely0 "orst case ,erfor/ance is O(n/0,
"hich is one of the reasons "hy it is not "i)ely use)4 Oo"e-er, it has the uni:ue
a)-antage of +eing a+le to fin) any one of 8 strings or less in O(n0 ti/e on
a-erage, regar)less of the si3e of 84
One of the si/,lest ,ractical a,,lications of !a+in2.ar, is in )etection of
,lagiaris/4 Say, for e7a/,le, that a stu)ent is "riting an ;nglish ,a,er on Mo+y
#ic84 A cunning stu)ent /ight locate a -ariety of source /aterial on Mo+y #ic8
an) auto/atically e7tract a list of all sentences in those /aterials4 Then, !a+in2
.ar, can ra,i)ly search through a ,articular ,a,er for any instance of any of the
sentences fro/ the source /aterials4 To a-oi) easily th"arting the syste/ "ith
/inor changes, it can +e /a)e to ignore )etails such as case an) ,unctuation +y
re/o-ing these first4 Because the nu/+er of strings "eDre searching for, 8, is -ery
large, single2string searching algorith/s are i/,ractical4
!ather than ,ursuing /ore so,histicate) s8i,,ing, the !a+in2.ar, algorith/ see8s
to s,ee) u, the testing of e:uality of the ,attern to the su+strings in the te7t +y
using a hash function4 A hash function is a function "hich con-erts e-ery string
into a nu/eric -alue, calle) its hash -alue= for e7a/,le, "e /ight ha-e
hash(NhelloN0M'4 !a+in2.ar, e7,loits the fact that if t"o strings are e:ual, their
hash -alues are also e:ual4 Thus, it "oul) see/ all "e ha-e to )o is co/,ute the
hash -alue of the su+string "eDre searching for, an) then loo8 for a su+string "ith
the sa/e hash -alue4
Oo"e-er, there are t"o ,ro+le/s "ith this4 First, +ecause there are so /any
)ifferent strings, to 8ee, the hash -alues s/all "e ha-e to assign so/e strings the
sa/e nu/+er4 This /eans that if the hash -alues /atch, the strings /ight not
/atch= "e ha-e to -erify that they )o, "hich can ta8e a long ti/e for long
su+strings4 Luc8ily, a goo) hash function ,ro/ises us that on /ost reasona+le
in,uts, this "onDt ha,,en too often, "hich 8ee,s the a-erage search ti/e goo)4
The algorith/ is as sho"n>
function !a+in.ar,(string sY$44nZ, string su+Y$44/Z0
2 hsu+ >M hash(su+Y$44/Z0
- hs >M hash(sY$44/Z0
4 +or i +ro& $ to n
5 !+ hs M hsu+
4 !+ sYi44iH/2$Z M su+
7 r#turn i
8 hs >M hash(sYiH$44iH/Z0
9 r#turn not foun)
Lines , ?, F an) & each re:uire \(/0 ti/e4 Oo"e-er, lines an) ? are only
e7ecute) once, an) line F is only e7ecute) if the hash -alues /atch, "hich is
unli8ely to ha,,en /ore than a fe" ti/es4 Line ' is e7ecute) n ti/es, +ut only
re:uires constant ti/e4 So the only ,ro+le/ is line &4
If "e nai-ely reco/,ute the hash -alue for the su+string sYiH$44iH/Z, this "oul)
re:uire \(/0 ti/e, an) since this is )one on each loo,, the algorith/ "oul) re:uire
\(/n0 ti/e, the sa/e as the /ost nai-e algorith/s4 The tric8 to sol-ing this is to
note that the -aria+le hs alrea)y contains the hash -alue of sYi44iH/2$Z4 If "e can
use this to co/,ute the ne7t hash -alue in constant ti/e, then our ,ro+le/ "ill +e
sol-e)4
9e )o this using "hat is calle) a rolling hash4 A rolling hash is a hash function
s,ecially )esigne) to ena+le this o,eration4 One si/,le e7a/,le is a))ing u, the
-alues of each character in the su+string4 Then, "e can use this for/ula to co/,ute
the ne7t hash -alue in constant ti/e>
sYiH$44iH/Z M sYi44iH/2$Z 2 sYiZ H sYiH/Z
This si/,le function "or8s, +ut "ill result in state/ent F +eing e7ecute) /ore
often than other /ore so,histicate) rolling hash functions such as those )iscusse)
in the ne7t section4
Notice that if "eDre -ery unluc8y, or ha-e a -ery +a) hash function such as a
constant function, line F /ight -ery "ell +e e7ecute) n ti/es, on e-ery iteration of
the loo,4 Because it re:uires \(/0 ti/e, the "hole algorith/ then ta8es a "orst2
case \(/n0 ti/e4
The 8ey to !a+in2.ar, ,erfor/ance is the efficient co/,utation of hash -alues of
the successi-e su+strings of the te7t4 One ,o,ular an) effecti-e rolling hash
function treats e-ery su+string as a nu/+er in so/e +ase, the +ase +eing usually a
large ,ri/e4 For e7a/,le, if the su+string is NhiN an) the +ase is $%$, the hash
-alue "oul) +e $%S j $%$
$
H $%' j $%$
%
M $%F%c (ASGII of DhD is $%S an) of DiD is
$%'04
Technically, this algorith/ is only si/ilar to the true nu/+er in a non2)eci/al
syste/ re,resentation, since for e7a/,le "e coul) ha-e the N+aseN less than one of
the N)igitsN4 See hash function for a /uch /ore )etaile) )iscussion4 The essential
+enefit achie-e) +y such re,resentation is that it is ,ossi+le to co/,ute the hash
-alue of the ne7t su+string fro/ the ,re-ious one +y )oing only a constant nu/+er
of o,erations, in)e,en)ent of the su+stringsD lengths4
For e7a/,le, if "e ha-e te7t Na+raca)a+raN an) "e are searching for a ,attern of
length ?, "e can co/,ute the hash of N+raN fro/ the hash for Na+rN (the ,re-ious
su+string0 +y su+tracting the nu/+er a))e) for the first DaD of Na+rN, i4e4 cC j $%$

(cC is ASGII for DaD an) $%$ is the +ase "e are using0, /ulti,lying +y the +ase an)
a))ing for the last a of N+raN, i4e4 cC j $%$
%
M cC4 If the su+strings in :uestion are
long, this algorith/ achie-es great sa-ings co/,are) "ith /any other hashing
sche/es4
Theoretically, there e7ist other algorith/s that coul) ,ro-i)e con-enient
reco/,utation, e4g4 /ulti,lying together ASGII -alues of all characters so that
shifting su+string "oul) only entail )i-i)ing +y the first character an) /ulti,lying
+y the last4 The li/itation, ho"e-er, is the li/ite) of the si3e of integer )ata ty,e
an) the necessity of using /o)ular arith/etic to scale )o"n the hash results, for
"hich see hash function article= /ean"hile, those nai-e hash functions that "oul)
not ,ro)uce large nu/+ers :uic8ly, li8e just a))ing ASGII -alues, are li8ely to
cause /any hash collisions an) hence slo" )o"n the algorith/4 Oence the
)escri+e) hash function is ty,ically the ,referre) one in !a+in2.ar,4
!a+in2.ar, is inferior for single ,attern searching to .nuth2Morris2Pratt
algorith/, Boyer2Moore string searching algorith/ an) other faster single ,attern
string searching algorith/s +ecause of its slo" "orst case +eha-ior4
Oo"e-er, !a+in2.ar, is an algorith/ of choice for /ulti,le ,attern search4
That is, if "e "ant to fin) any of a large nu/+er, say 8, fi7e) length ,atterns in a
te7t, "e can create a si/,le -ariant of !a+in2.ar, that uses a Bloo/ filter or a set
)ata structure to chec8 "hether the hash of a gi-en string +elongs to a set of hash
-alues of ,atterns "e are loo8ing for >
+unt!on !a+in.ar,Set(string sY$44nZ, set of string su+s, /0 L
set hsu+s >M e/,tySet
+or #'" su+ !n su+s
insert hash(su+Y$44/Z0 into hsu+s
hs >M hash(sY$44/Z0
+or i +ro& $ to n
!+ hs hsu+s
!+ sYi44iH/2$Z M a su+string "ith hash hs
r#turn i
hs >M hash(sYiH$44iH/Z0
r#turn not foun)
Q
Oere "e assu/e all the su+strings ha-e a fi7e) length m, +ut this assu/,tion can
+e eli/inate)4 9e si/,ly co/,are the current hash -alue against the hash -alues
of all the su+strings si/ultaneously using a :uic8 loo8u, in our set )ata structure,
an) then -erify any /atch "e fin) against all su+strings "ith that hash -alue4
Other algorith/s can search for a single ,attern in O(n0 ti/e, an) hence they can
+e use) to search for " ,atterns in O(n "0 ti/e4 In contrast, the -ariant !a+in2.ar,
a+o-e can fin) all " ,atterns in O(nH"0 ti/e in e7,ectation, +ecause a hash ta+le
chec8s "hether a su+string hash e:uals any of the ,attern hashes in O($0 ti/e4
T!&# Co&0$#6!t(
!a+inDs algorith/ is (al/ost al"ays0 fast, i4e4 O(/Hn0 average-case ti/e2
co/,le7ity, +ecause hash(t7tYi44iH/2$Z0 can +e co/,ute) in O($0 ti/e 2 i4e4 +y t"o
/ulti,lications, a su+traction, an a))ition an) a b/o)D 2 gi-en its ,re)ecessor
hash(t7tYi2$44i2$H/2$Z04
The worst-case ti/e2co/,le7ity )oes ho"e-er re/ain at O(/An0 +ecause of the
,ossi+ility of false2,ositi-e /atches on the +asis of the hash nu/+ers, although
these are -ery rare in)ee)4
SECTION DII
S*ANNING TREES
MINIM2M S*ANNING TREE
*i-en a connecte), un)irecte) gra,h, a s,anning tree of that gra,h is a su+gra,h
"hich is a tree an) connects all the -ertices together4 A single gra,h can ha-e /any
)ifferent s,anning trees4 A "eight can +e assignne) to each e)ge, "hich is a
nu/+er re,resenting ho" unfa-ora+le it is, an) use this to assign a "eight to a
s,anning tree +y co/,uting the su/ of the "eights of the e)ges in that s,anning
tree4 A /ini/u/ s,anning tree or /ini/u/ "eight s,anning tree is then a
s,anning tree "ith "eight less than or e:ual to the "eight of e-ery other s,anning
tree4 More generally, any un)irecte) gra,h has a /ini/u/ s,anning forest4
One e7a/,le "oul) +e a ca+le TT co/,any laying ca+le to a ne" neigh+orhoo)4
If it is constraine) to +ury the ca+le only along certain ,aths, then there "oul) +e a
gra,h re,resenting "hich ,oints are connecte) +y those ,aths4 So/e of those ,aths
/ight +e /ore e7,ensi-e, +ecause they are longer, or re:uire the ca+le to +e +urie)
)ee,er= these ,aths "oul) +e re,resente) +y e)ges "ith larger "eights4 A s,anning
tree for that gra,h "oul) +e a su+set of those ,aths that has no cycles +ut still
connects to e-ery house4 There /ight +e se-eral s,anning trees ,ossi+le4 A
/ini/u/ s,anning tree "oul) +e one "ith the lo"est total cost4
In case of a tie, there coul) +e se-eral /ini/u/ s,anning trees= in ,articular, if all
"eights are the sa/e, e-ery s,anning tree is /ini/u/4 Oo"e-er, one theore/
states that if each e)ge has a )istinct "eight, the /ini/u/ s,anning tree is uni:ue4
This is true in /any realistic situations, such as the one a+o-e, "here itDs unli8ely
any t"o ,aths ha-e e7actly the sa/e cost4 This generali3es to s,anning forests as
"ell4
If the "eights are non2negati-e, then a /ini/u/ s,anning tree is in fact the
/ini/u/2cost su+gra,h connecting all -ertices, since su+gra,hs containing cycles
necessarily ha-e /ore total "eight4
*RIMJS ALGORITHM
Pri/Ds algorith/ is an algorith/ in gra,h theory that fin)s a /ini/u/ s,anning
tree for a connecte) "eighte) gra,h4 This /eans it fin)s a su+set of the e)ges that
for/s a tree that inclu)es e-ery -erte7, "here the total "eight of all the e)ges in
the tree is /ini/i3e)4 If the gra,h is not connecte), then it "ill only fin) a
/ini/u/ s,anning tree for one of the connecte) co/,onents4 The algorith/ "as
)isco-ere) in $c?% +y /athe/atician Tojtkch 1arnl8 an) later in)e,en)ently +y
co/,uter scientist !o+ert G4 Pri/ in $c'C an) re)isco-ere) +y #ij8stra in $c'c4
Therefore it is so/eti/es calle) the #1P algorith/ or 1arni8 algorith/4
It "or8s as follo"s>
create a tree containing a single -erte7, chosen ar+itrarily fro/ the gra,h
create a set (the Dnot yet seenD -ertices0 containing all other -ertices in the
gra,h
create a set (the DfringeD -ertices0 that is initially e/,ty
loo, (nu/+er of -ertices 2 $0 ti/es
o /o-e any -ertices that are in the not yet seen set an) that are )irectly
connecte) to the last no)e a))e) into the fringe set
o for each -erte7 in the fringe set, )eter/ine if an e)ge connects it to the
last no)e a))e) an) if so, if that e)ge has s/aller "eight than the
,re-ious e)ge that connecte) that -erte7 to the current tree, recor) this
ne" e)ge through the last no)e a))e) as the +est route into the current
tree4
o select the e)ge "ith /ini/u/ "eight that connects a -erte7 in the
fringe set to a -erte7 in the current tree
o a)) that e)ge to the tree an) /o-e the fringe -erte7 at the en) of the
e)ge fro/ the fringe set to the current tree -ertices
o u,)ate the last no)e a))e) to +e the fringe -erte7 just a))e)
Only aTa2$, "here aTa is the nu/+er of -ertices in the gra,h, iterations are re:uire)4
A tree connecting aTa -ertices only re:uires aTa2$ e)ges (any/ore causes a cycle
into the su+gra,h, /a8ing it no longer a tree0 an) each iteration of the algorith/ as
)escri+e) a+o-e ,ulls in e7actly one e)ge4
A si/,le i/,le/entation using an a)jacency /atri7 gra,h re,resentation an)
searching an array of "eights to fin) the /ini/u/ "eight e)ge to a)) re:uires
O(Tm0 running ti/e4 Using a si/,le +inary hea, )ata structure an) an a)jacency
list re,resentation, Pri/Ds algorith/ can +e sho"n to run in ti/e "hich is O(;log
T0 "here ; is the nu/+er of e)ges an) T is the nu/+er of -ertices4 Using a /ore
so,histicate) Fi+onacci hea,, this can +e +rought )o"n to O(; H Tlog T0, "hich is
significantly faster "hen the gra,h is )ense enough that ; is \(Tlog T04
Mini/u/2S,anning2Tree2+y2Pri/(*, "eight2function, source0
$ for each -erte7 u in gra,h *
set 8ey of u to n
? set ,arent of u to nil
S set 8ey of source -erte7 to 3ero
' en:ueue to /ini/u/2hea, W all -ertices in gra,h *4
F "hile W is not e/,ty
C e7tract -erte7 u fro/ W << u is the -erte7 "ith the lo"est 8ey that is in W
& for each a)jacent -erte7 - of u )o
c if (- is still in W0 an) ("eight2function(u, -0 J 8ey of -0 then
$% set u to +e ,arent of - << in /ini/u/2s,anning2tree
$$ u,)ate -Ds 8ey to e:ual "eight2function(u, -0
HR2SHALJS ALGORITHM
.rus8alDs algorith/ is an algorith/ in gra,h theory that fin)s a /ini/u/
s,anning tree for a connecte) "eighte) gra,h4 This /eans it fin)s a su+set of
the e)ges that for/s a tree that inclu)es e-ery -erte7, "here the total "eight of
all the e)ges in the tree is /ini/i3e)4 If the gra,h is not connecte), then it fin)s
a /ini/u/ s,anning forest (a /ini/u/ s,anning tree for each connecte)
co/,onent04 .rus8alDs algorith/ is an e7a/,le of a gree)y algorith/4
It "or8s as follo"s>
create a forest F (a set of trees0, "here each -erte7 in the gra,h is a se,arate
tree
create a set S containing all the e)ges in the gra,h
"hile S is none/,ty
o re/o-e an e)ge "ith /ini/u/ "eight fro/ S
o if that e)ge connects t"o )ifferent trees, then a)) it to the forest,
co/+ining t"o trees into a single tree
o other"ise )iscar) that e)ge
At the ter/ination of the algorith/, the forest has only one co/,onent an) for/s a
/ini/u/ s,anning tree of the gra,h4
function .rus8al(*0
for each -erte7 - in * )o
? #efine an ele/entary cluster G(-0 f L-Q4
S Initiali3e a ,riority :ueue W to contain all e)ges in *, using the "eights as
8eys4
' #efine a tree T f o <<T "ill ulti/ately contain the e)ges of
the MST
F "hile T has fe"er than n2$ e)ges )o
C (u,-0 f W4re/o-eMin(0
& Let G(-0 +e the cluster containing -, an) let G(u0 +e the cluster containing u4
c if G(-0 p G(u0 then
$% A)) e)ge (-,u0 to T4
$$ Merge G(-0 an) G(u0 into one cluster, that is, union G(-0 an) G(u04
$ return tree T
DIDA F2ESTIONS
GOU!S; TITL;>AL*O!ITOM ANAL^SIS AN# #;SI*N
GOU!S; GO#;>;TGS2'S
$4 #efine O/ega notation4
4#efine Big2O notation4
?4#efine Theta notation4
S49hat is an algorith/B
'49hat is a ran)o/i3e) algorith/B
F49hat are loo, in-ariantsB Oo" it is sho"n that an algorith/ is correctB
C49hat is a Pseu)oco)eB
&49hat are the "orst case an) a-erage case running ti/e of insertion sortB
c49hich techni:ue is use) to sort ele/ents in /erge sortB
$%49hat is the running ti/e of /erge sortB
$$4Oo" /erge sort is )ifferent fro/ :uic8 sortB
$4Na/e )ifferent /etho)s to sol-e recurrences4
$?49hat is the "orst case running ti/e of :uic8 sortB
$S4#efine i2th or)er statistics of a set4
$'49hat is /e)ian of a setB
$F49hat are the )ifferences +et"een )yna/ic an) gree)y algorith/sB
$C49hat is a negati-e "eight cycleB
$&4#ij8stra algorith/ can ta8e into account the negati-e e)ge "eigthts4DIs the
state/ent trueB
$c4#efine /ini/u/ s,anning tree4D
%4Na/e any algorith/ for fin)ing the /ini/u/ s,anning tree4
$4Go/,are Pri/Ds an) .rus8alDs algorith/4
49hat are Ouff/an co)esB
?49hat are fi7e) length an) -aria+le length co)esB
S4Oo" gra,hs are re,resente) in co/,uter /e/oryB
'4Go/,are an a)jacency list an) a)jacency /atri7B
F49hat is the ti/e co/,le7ity of BFSB
C49hat is the ti/e co/,le7ity of #FSB
&4 #efine 9hite Paththeore/4
c49hat is Parenthesis theore/B
?%4Oo" ;uler tour is )ifferent fro/ Oa/iltonian cycleB
?$4Go/,are Bell/an For) an) #ij8straDs algorith/4
?4#efine Gli:ue ,ro+le/4
??4#efine Terte7 Go-er Pro+le/4
?S49hat are NP2co/,lete ,ro+le/s4
?'4Na/e so/e NPG ,ro+le/s4
?F4#efine Gircuit satisfia+ility ,ro+le/4
?C4#efine Tra-elling sales/an ,ro+le/4
LIST OF ADDANCED *RACTICALS
L',or'tor( N'&#E ALGORITHM ANALYSIS AND DESIGN
Su,K#t Cod# E ETCS 254
$4 To i/,le/ent Ouff/an co)e algorith/4
4 To I/,le/ent .nuth2 Morris Pratt algorith/4
?4 To i/,le/ent /agic s:uare4
S4 To i/,le/ent tas8 sche)uling4
H2FFMAN CODES
Ouff/an co)e is a techni:ue for co/,ressing )ata4 Ouff/anDs gree)y algorith/
loo8 at the occurrence of each character an) it as a +inary string in an o,ti/al "ay4
CONSTR2CTING A H2FFMAN CODE
A gree)y algorith/ that constructs an o,ti/al ,refi7 co)e calle) a Ouff/an co)e4
The algorith/ +uil)s the tree T corres,on)ing to the o,ti/al co)e in a +otto/2u,
/anner4 It +egins "ith a set of aca lea-es an) ,erfor/ aca2$ N/ergingN o,erations to
create the final tree4
#ata Structure use)> Priority :ueue M W
Ouff/an (c0
n M aca
W M c
+or i M$ to n2$
do 3 M Allocate2No)e (0
7 M leftY3Z M ;RT!AGTgMIN(W0
y M rightY3Z M ;RT!AGTgMIN(W0
fY3Z M fY7Z H fYyZ
INS;!T (W, 30
r#turn ;RT!AGTgMIN(W0


ANALYSIS
W i/,le/ente) as a +inary hea,4
line can +e ,erfor/e) +y using BUIL#2O;AP (P4 $S'= GL!0 in O(n0 ti/e4
FO! loo, e7ecute) ana 2 $ ti/es an) since each hea, o,eration re:uires O(lg
n0 ti/e4
MK the FO! loo, contri+utes (ana 2 $0 O(lg n0
MK O(n lg n0
Thus the total running ti/e of Ouff/an on the set of n characters is O(nlg
n04
HN2TH-MORRIS-*RATT ALGORITHM
.nuth, Morris an) Pratt )isco-ere) first linear ti/e string2/atching algorith/ +y
follo"ing a tight analysis of the naX-e algorith/4 .nuth2Morris2Pratt algorith/
8ee,s the infor/ation that naX-e a,,roach "aste) gathere) )uring the scan of the
te7t4 By a-oi)ing this "aste of infor/ation, it achie-es a running ti/e of O(n H m0,
"hich is o,ti/al in the "orst case sense4 That is, in the "orst case .nuth2Morris2
Pratt algorith/ "e ha-e to e7a/ine all the characters in the te7t an) ,attern at least
once4
HN2TH-MORRIS-*RATT 1TC *3
In0utE Strings TY% 4 4 nZ an) >Y% 4 4 mZ
Out0utE Starting in)e7 of su+string of T /atching >
' f co/,ute failure function of Pattern >
i f %
j f %
"hile i J lengthYTZ )o
if j f m2$ then
return i2 mH$ << "e ha-e a /atch
i f i H$
j f j H$
else if j K %
j f '(j 2$0
else
i f i H$
ANALYSIS
The running ti/e of .nuth2Morris2Pratt algorith/ is ,ro,ortional to the ti/e
nee)e) to rea) the characters in te7t an) ,attern4 In other "or)s, the "orst2case
running ti/e of the algorith/ is O(m H n0 an) it re:uires O(m0 e7tra s,ace4 It is
i/,ortant to note that these :uantities are in)e,en)ent of the si3e of the un)erlying
al,ha+et4
MAGIC SF2ARE
A /agic s:uare is a s:uare array of nu/+ers consisting of the )istinct ,ositi-e
integers $, , 444, arrange) such that the su/ of the nu/+ers in any hori3ontal,
-ertical, or main )iagonal line is al"ays the sa/e nu/+er 8no"n as the /agic
constant
If e-ery nu/+er in a /agic s:uare is su+tracte) fro/ , another /agic s:uare is
o+taine) calle) the co/,le/entary /agic s:uare4 A s:uare consisting of
consecuti-e nu/+ers starting "ith $ is so/eti/es 8no"n as a Nnor/alN /agic
s:uare4
<Aco)e is for generating o)) /agic s:uare4 Ghange the -alue of MAR in the
,rogra/ to generate the /agic s:uare4 Magic s:uare 2 Su/ of -alues or ro"s,
colu/ns or )iagonals is the sa/e4A<
Iinclu)eJst)io4hK
Iinclu)eJconio4hK
I)efine MAR
-oi) /ain(0
L
int arrYMARH$ZYMARH$Z,-alM$,i,j,8=
clrscr(0=
for(iM%=iJMMAR=iHH0
for(jM%=jJMMAR=jHH0
arrYiZYjZM%=
for(iM%,jMMAR<,8M%=8J((MARH$0A(MARH$00=8HH0
L
arrYi22ZYj22ZM-alHH=
if(iJ%0
iMMAR=
if(jJ%0
jMMAR=
if(arrYiZYjZ@M%0
L
iMiH=
jMjH$=
if(iKMAR0
iqM(MARH$0=
if(jKMAR0
jqM(MARH$0=
Q
Q
for(iM%=iJMMAR=iHH0
L
for(jM%=jJMMAR=jHH0
,rintf(Nq)PtN,arrYiZYjZ0=
,rintf(NPnN0=
Q
getch(0=Q
ANNEX2RE I
CODER *AGE OF THE LAB RECORD TO BE *RE*ARED BY THE
ST2DENTS
ANALYSIS AL!ORITHM AN' 'ESI!N
ETCS()*+
( si3e %VV , italics +ol) , Ti/es Ne" !o/an 0
,$cult# N$e- Stu"ent N$e-
( $VV , Ti/es Ne" !o/an 0 Roll No.-
Seester-
B$tch -
( $VV, Ti/es Ne" !o/an 0
Maharaja Agrasen Institute of technology, PSP area,
Sector , !ohini, Ne" #elhi $$%%&'
( $&VV +ol) Ti/es Ne" !o/an 0
ANNEX2RE II
FORMAT OF THE INDEX TO BE *RE*ARED BY THE
ST2DENTS
Stu)entVs Na/e
!oll No4
INDEX
S.No. N'&# o+ t"# *ro%r'& D't# S!%n'tur#
L D't#
R#&'r5)

Das könnte Ihnen auch gefallen