Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

13

Anadys
Best case, occuSS element is presen exacl
exacl al he
h e
loy
Cenbe dvolim he o y inlo equal povs

Tn) n-
TPla)T (A)+nt o pavBkon n elarnents

T(n)- aT ("/a)+r

a aT ()+n
a'n) an

+in

in'k Conde bon T(o) =o


n
nT)+ n.
ni

a'n
1ake dog on eth Sieles

i les hs

T(n) nlogn
TC, Tn) -
A(no9,")
WOYst Case
Aeach invccohon c) the proceeluve, the curenl awa
is pabboneel ino 3 ub cs
ay Cwith one ) dhem being e"phy
Occw if al he elements are Gogpol in scerol'ng orolen or

descending Ovclen)
n=
T(n)
T(o) +T(n-1) tn obherwise

T(n) - T ( 6 ) 4 T(n-1)tn

T (n-1)+n

T(n-2) + (n-i) +n

T(n-3) +(n-a )+(n -1) +n

T(n-n) +|+24.. + (n -a) +(n-tn


041+ a4. + (n-a) +
(n-) trn
=nhiu)

TC, T(n) 0(^)

Avenaa Ca
Con. nau
may hot be exactly parbkoneol nt
3keues as in he cwoYst Cap . oe
Subamau oy may hot be

element may be placoel al anq avbilvany bitran pos?kon in de

Onging o m 0 lv h-
me t 301 ! pat
B 0, 08 k-1

Nec2itemo On t Og- LB+


= A--0+|

k-0
1me b sost vght pot
B k), 0B h-
Ne te ms on ight UB-B4|
h-- -14 1
n-I-k
at he
whle valus rs placael
nanag, hisghet integp
end be gn: u
,
ota no elcmenh (hi)
Time unit to pavbeton (n41) elcmenb *s n+I Bme riG -

T(n) (n1) +T(1) +T(n -)-k)


Avenaga n-l T(o) O

T(n):L2 (n1) +7 ( k) +T (n -i-k) Ar nz1 T(1) 0

Mspeyb n_
n T(n)= na1)(ha)tT() T(n-)-k)

(na) T(1) T/n-|-k)|

-
(na)I+ =D
T(1) +T{n-)-k) n-1-ta
=)
O,),a, n-2,n-
n-1-(n-)
n--n4
-
(h4) (h--o4) +
T(o) 4 T(n-1) T() T(n2)
T(n-2)+ T( 1)4
T(n-)1 Tlo)
T(n) n(n+1) +2 T(o)+T(1)
T(2)4 +
T(n-)
nln+)
Replac by h-
n
n-(nJ)

h-) T(n-1) =h (n-1)


+2) To) +T(4T (2)» -.
T(n-2)
nT(n) -(n-) T(n -1) -
n'+h -h+n
+aT(h-)
ant T(n
hT(n) (n-1) T (n-1) +an +aT
(n -1)
T(n -) (n-1+a) +2n
T(n-1) n+1) +2n
diviou by h(nA)

nT(n) T(n-D(n4) n
4
n(nt) n(n4) n(nt
T) T(n ) +
n
nt)
Raploa n byn-
T(n-1) T(n-3)
n -
Th-3)
h-a n-

T(n-k) + 4
n-3
n-a n- m)

T(o)+ 2 n n+)

2 dk

2 (nai)
og, -os 2 b

aog, (n4) e Lony Valu n


Venans

T) a loga (n+)
T(n) (n+1) 2 log, (n+)
-

venylag vauu 9,h


TC Tn) =n os)
Stvassen's matr'x mulbplaborn
.

Ael and B be d n matnces


meti Whese
Product matt C A x abe nxn
eme by takmg the elemenks 'n
dhe
h element
stw A ard he J column e
B anlmuliply

CCij)= Ai,
K=
k) B(xj)

i and be tueen 1 and n


ue al j
10
Compule C(j) need n
mulbpl:co bon s -

O3)
Divi and ConGue,
AsSune h isa a, Ce) dhere esls
powben a
hon negakve
tntegcK uch that n2.
n is not a
powen u) wo dhen enoug h OWS apd

Columns
umns 2eoS ae acdoled to bth A rd B So that the

eul HrG dmensfo ns ave a


POwe twp

An a
C Cia
Ca Ca
Ba2

C A B, 4 Aia Ba|

C12 An B12 t
Pa Baa
C , B,taa Ba
C20
32 P2, B12 Paa Baa
Lmanine that ond B a ench pavbitioneol int ou
Tmanine
Squ are Bubmatke, each Bubmatry havtg omensons n/a
10
n 3, above OYnulas ao Compuled tusing C

mbphabon opeahon fo the elements 2 9 and B

tov h a, the ele mers C Can be COmpu ted

matrix muihplicabon arno aobl'hor operakon appheo


n

formuks, heeol o
lo Compule B fov the obove

malices ond foe


Pessorm eght mushphokonm a Va

aebora /a matdes
can be aololeol tn
mabsces
Since tuwo ala %
two

frne Cn B0me Contont C.


na
T(n) na
8T(a)+4Cr

Matia mulhplta kon 's mOTe


xpensive than

mobis addikon O(n3) and )

Hefovmusole the equabon to have even mulkphrahon

Gnd possibly move adolhons

se
se mulkplrakos Cnd 18 aoldbom 0Y Subtra cho

Corpute h2Va matntes P.0R, S,T, U, V


P-P 2a) ( Bu + B2a)

-Pa +Paa) B
R n B12 Baa
S Paa (Ba-B)
T (Pn Pia) Ba2
0(Pa -P) (Bn t Bia)
V
Pa Pas) ( 3,t Baa)
-

mpute sing mat mulkbpcabons ard

malix ado hom 03ubtvachons

C P+ S-T+ V
Cia R+T

C +s
Cag P+Re-0+u
Yequre 8 adolboas t Subtvatkons

b n a

T(n)
3T()+an n 7a

Compae
C P+S-T+V
=
( 4Oza)( B,t C22) + P2al B2-8,)(P»tPia) Baa t
i-A2) (24Baa

ABn +Aa
Anas
T) =
T(n) -

7T( "/a)

n , take og on both sioles


K Log 9 log.n, K =

=n
2.81

:81
TC, T(n)= (n

MOYe
Move eMient o Veny ange Value n
Example
B 5
9
6T
Tmplement Stvasen's mati malBplio kon on Aond B
AXh mabx, Bubdeviel nto a Bub motrire

Cempte PO, R.S, T, U ael V


Bia
An

B Ba Baa
(3

P - (AntP2a) ( B, t2a)

3 ) (EJ )
J
18428 30+42
30 th0 SO+bo

: 13
30

([-)R)
6 6o 76
45433 30 455)67 9s
R- Pn B1g-Baa

20t 3h
1610 1613.

S 2&-B,)

0435

3S

T (9n 4a) Baa

6+10 ISS
018 1849
= (Pa-n)B t Br

18 0
354 s +h

3 88
39
59

V (Pig-92,) (%t)
(C: E3-1 ) (n:)
-

-3-18 -12-I
-13-9
-a6
f-31
- 55

C, P4S-T+ V
36
ss

35 37
66
Ci2 R4T
a19

16
30
3H

+S
Can
8s 35 41

C68 87
134

Cp
2P+R -&+u
88
to 1o ho 51

S
S8

Prpdluet rmatwr C 37 30 6
34 6
fc C1a
68 87 Sa
Ca C29 0a 34 S8

35 31 30
66 99 3 69
68 87 Sa a3S

/0a 13 H 58
dvantages and Disaduanlng_dv'e Ond COrque

Advanloge
Idicll pm blem bwken deun inbo ubprn Bems ored

ecch Suhppbems ched tndlepereantly


Can be execuleo On paralal pror eYs
Subpmblents
3.Divide and Congues helps in the detceveny2

ePctent algoithm
I also tuse» memon ache effectively, eDon is
the Aoheo
ohn Subpvklems become Simple erough, thuy on be
COthin a Cache, co?l hut havirg to aocess the 3loue man memons

Cohich scve ime ard makes bhe agordhm moe elkcier!.

DFsacsanages
are Ceakeo ord necol
B u bests
anGe
AaG numbe

lt he pu eNseel
decuxsive methts , vecusion is 3loce
a make uSe
Ond Cmple

3. Once he pmblem s bpken down nu Subprbems

the are ub pblem Can Occun many bme


Decveae and Cong ue

Decvease and Congues lechnlg ue 's baeol On


explotng he velalfonship bet wecn a BouHen b a given iratare
oF a problem arol a 8olubon to its Smallo notance.

Thene Gne 3 maioy Vaniabons decreae and Corgues


clecreae bu a coNant ( olfault hy )
a)
oleevease a nalan hocbor
3)
Vautabie B'ze decvense

)the Sx a n lance is vedu Ced by r Same ratanl

On each tevabon of the alqvithm Tqpfcally thu Censtart s

equas to One. (EA) Znsevbon 8oYt.

3) Sgupsts Yeclcing
a problem instarre by the Same

Corstant ackov on ach tenab'on of Che algovibhm In most


ppicabora thu Cormlan acor 1S
eGual to
Ewo (Ey Tcpclociat 30d
3) Dhe
Sze educkon pallen Vanies Um One
ena hon
ea bon )
e2 an
alo Gnt then (Eo EuctJ's
alqorthm foY Compukn Ehe
uatest Common divisov

lopoloqical S0v
near ovolen ) Ventex
DHeere belwon verled ernod und'eelrd yaph
he adaoer malix diveted paph loe nu
have y be
Bmmeic
d Gn
eolqp in a
d vected eph has
ut One (rot hwo)
Cvepondrg naes i'n the dpaph's acyacerey l s

(a
tree eolge
bock edag
> fovwamd edg
CYOSs eqa

bacl edae vom vevbices to ther ancestors


SoscwaTd edeg -

um Vesbte to her descencants in the tree othe


bhan thEY chidben

back edge in a DFs


fovext ofa derted qraph can
Connect a vestex lo
gaph ca
its
Parent peene a bacl
hal bhe
edgo incdlcale
dapaph ha a
ivected cycle
A deced cycle in a
d'gaph s a
Seuene hvee
OY moTeof its Vevtkes hat Slats and
eds with the Sme
Vesles and in Whth
evey Vesley Connecbed to ts
immeolake
Pseoleceso by an edge
edge 'verted om he Predecexor
to the SaCcesoY

EAample is COuxse nd pre vequtsites.

ist e Vesces, the Vestex whew the


edeo stat
issbeol bebye be Vevtex where he
edgo ersTh probem
is Calld kopoloycal sovig
he pDble m Cannot have a olubon a
ioryaph
has a drected Cyce.
Fov 'opoloccal soking the d'ghaph mut he
DAG
(Dineeted Prgcic haph) Depth rst eavck
Two alaovidhns
Soure Temoval meted

Depth sgeavch
Pexhrs a DFS raversad and note the ordo

in Cwhich Ve»ice become deod encd ( popped off the

oNersal stacle) Reversing th Ovoles yrelch a Soluben k


Be topolegical `orBra problem, ho bac eda has been
Enccuntewd dwning the vavesal
IP the bacle eclge ha been
encounterod, dhe
dgnaph is DAC, Gnd lopolgtral Sovking of its

Vetice 1s impoSSh)e
SooNce Yemoval mehod
basecl On a dtheel impermenlakon of bh cloc and
Conque lechniquw (hy One)
Repeatelidenq in a
emal nin
oghaph a
Soute, whch fs a Veslex wdh 0
Ording edges ard dolete i
along wth ad he edge
Several Soures break the ke
outgein rvm it IP dhane n

abitaily and f dhor ae


none,
Seop becaune dthe Problem Cannot be schveo).
The ovolen in which dhe Vesicee ae deleke ds
a
Sohulson v dhe
opolocras Ses ing poblen
The Solukon
obtained by dhe SOunce
S0Uce YemOvas algen thm S
d lerent em the one
obtafned by the DFS baseel
oh
algendhm.
dhen ane conet. Topo
logrcads0ing pvblem may hae
Sevead alten dive
solulon
pplicoko lopologjcal 807t in Compuute
scYence
Insbuckoo Bchudeding in Pmgiem
d Cedl Complabon
evaluakon Ovdei'ng in
8preadsheet fo
3Yesoing mbo depenrlenei in inkes e
das

Consbychon, veseaschy So twarr


invoves a
mulilude
levelepmen
Pre Yequisttes )inteelaleod
inteselaleol tasks cvidh ko cwn

4 Source Yemoval method


-indecyee ZeND
DFS -

Stat om s vesley and use stCcl


(c
delelec,
doleleC loheke
ca c) (C {)
Vsted cdzocant Pyped vrden
delele ch C C3
delete Cs C3 CACS
C C5 Topols cal sort
Cs
Ca
Theolulion o blaineo f's
C,C C3 C, C

3
oo

3 o

O
so O

Indegre O 2 3

Visted
Adjacent Incleene O à 3 4S
O0 a 33
,3 OOO 3

0,1 3, O0 0 O 3

0,1,3
0,),3,3 O0

O
0,),2.3,

O,2,3,4,S
Oulput ts 0,l,a.3,A.S
Vis:led Staek adi aount Vevte x Poppeol elernent
b
&.biC bc,d
ab.c.d abiC b.cd,g
a.bC,d,9 Gb ,d9
Cb C,d
ab,C g.d
ab
a 3,d.c
3.d.c,b
a,b.c.d,9.e 3,d.c, b,a
S,dc b,a
Gsb.c,d.g.e,f 9.dc, b,a,e
9,d,c, b,4,e, f
Popped eerment is 9-d-c-b-a-e-f
Topolqtal 8ovt -e-a -b- C- d-q
pp
Applu the DFs basel
alcorithm to solve the
topolegrcad Svbng
Prchlem hy the
ellowfng dgnaphs

You might also like