Beruflich Dokumente
Kultur Dokumente
(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)