Documente Academic
Documente Profesional
Documente Cultură
+
+
j p
p i
k k r p j
k k p i
..., , 1
1 ..., , 1 1 , 1
. Acest pr oces va put ea cont i nua pent r u
secven el e de chei { } , ... ,
1 1 p
k k i { } , ... ,
1 r p
k k
+
i ndependent una de al t a.
procedure Pozi i onar e ( ) p k r n, , var ; , 1
[ ] ... 1 n k : t abl oul chei l or
r l , : i ndi ci n t abl ou cu semni f i ca i a de mai sus.
p : i ndi ce cu semni f i ca i a de mai sus.
1 i ; r j ; 0 _ i inc ; 1 j dec ;
whi l e j i < do
i f [ ] [ ] j k i k > t hen [ ] [ ] j k i k > ; j c d i inc e ;
end i f
1 inc i i + ; j dec j j ;
end whi l e
i p ;
end_proc.
Al gor i t mul de sor t ar e r api d por net e cu per echea ( ) ( ) n r , 1 , 1 = ncer cnd
pozi i onar e ( ) p k r n , , , 1 , . Va cont i nua pr ocesul cu una di n per echi l e ( ) 1 , 1 p sau
( ) r p , 1 + ceal al t f i i nd i nt r odus nt r - o st i v S . O i nt r ar e n st i va S , de f or ma
( ) b a , r epr ezi nt o cer er e de sor t ar e a subsecven ei { } , ... ,
b a
k k l a un anumi t t i mp,
n vi i t or . Pr ocesul de sor t ar e poat e cont i nua doar dac per echea cur ent ( ) r , 1 ar e
pr opr i et at ea r l < . Cnd r l = pr ocesul va cont i nua cu per echea ( ) ( ) b a r , , 1 = ,
unde ( ) b a , est e pr i ma per eche di n vr f ul st i vei . Cnd st i va devi ne vi d, pr ocesul
de sor t ar e est e compl et nchei at .
procedure Qui cksor t ( ) k n, var
[ ] ... 1 n k : t abl oul chei l or
S : st i va men i onat mai sus. Poat e avea cel mul t n
2
log i nt r r i .
S ; 1 l ; n r ; cont i nu t r ue;
repeat
whi l e r l < do; Se pl aseaz n st i v secven a cea mai l ung
Pozi i onar e ( ) p k r l n, , , , ;
i f 1 p p r t hen ( ) { } 1 ; , 1 + p r S r p
el se ( ) { } 1 ; 1 , 1 + p l S - p
end i f
end whi l e
i f = S t hen cont i nu f al se
el se ( ) S r l , ;
end i f
unt i l ( not cont i nu) ;
end_proc;
1. 2. CAUTARE
Pr obl ema cut r i i est e ur mt oar ea: pr esupunnd c s- au memor at N n
nr egi st r r i , se cer e s se l ocal i zeze o anumi t nr egi st r ar e; n cazul cnd aceast a
nu est e gsi t se va r spunde pr i nt r - un mesaj cor espunzt or . Ca i n cazul sor t r i i
se pr esupune c f i ecar e nr egi st r ar e posed un cmp speci al denumi t chei a
nr egi st r r i i , i ar cut ar ea f cndu- se n f unc i e de aceast chei e. Pr act i c, dac W
est e chei a cut at ea t r ebui e l ocal i zat pr i nt r e chei l e
n
k k , ... ,
1
. n cont i nuar e vom
i gnor a ac i uni l e car e se execut dup ce chei a k a f ost gsi t .
1. 2. 1. Cutarea secveni al
Pent r u n i , 1 = se t est eaz i dent i t at ea
i
k W = . n caz de egal i t at e al gor i t mul se
t er mi n cu succes. Al t f el , cnd 1 + = n i , al gor i t mul se t er mi n cu i nsucces.
Remar cm f apt ul c n acest caz deci zi a ar e dou var i ant e de cont i nuar e.
procedure Caut _secven a_1( k W, ; gsi t )
W : chei a cut at
[ ] ... 1 n k : t abl oul chei l or
gsi t : var i abi l a bool ean
gsi t f al se; 1 i ;
whi l e ( n i and not gsi t ) do
i f
i
k W = t hen gsi t t r ue
el se 1 + i i
end i f
end whi l e
end_proc.
O var i ant mai r api d de cut ar e secven i al est e cea n car e, pr i nt r e chei l e
n
k k , ... ,
1
, se i nt r oduce o chei e f i ct i v W k
n
1
=
+
.
procedure Caut _secven a_2 ( k W, ; gsi t )
gsi t f al se; 1 i ; W k
n
1
+
;
whi l e
i
k W do
1 + i i ;
end whi l e
i f n i t hen gsi t t r ue;
end i f
end_proc.
Cut ar ea secven i al nt r - un t abel or donat de chei
Vom pr esupune c nr egi st r r i l e
n
R R , ... ,
1
sunt ast f el memor at e nct
n
k k k ...
2 1
< < < . Pent r u a gsi nr egi st r ar ea cu chei a W vom i nt r oduce n t abl oul
de chei o chei e =
+
1 n
k , ( ) , 1 ,
1
n i k k
i n
= >
+
i < W ( event ual
{ } , max
1
W k k
n n
=
+
) .
procedure Caut _secven a_3 ( k W, ; gsi t )
gsi t f al se; 1 i ;
+
1 n
k ;
whi l e
i
k W > do
1 + i i ;
end whi l e
i f
i
k W = t hen gsi t t r ue;
end i f
end_proc.
1. 2. 2. Cutarea prin comparaie de chei
Acest t i p de cut r i pr esupune o or donar e l i ni ar a chei l or ( exempl u: or donar e
l exi cogr af i c pent r u chei de t i p i r de car act er e sau numer i c pent r u chei
numer i ce) . n cont i nuar e vom consi der a or donar ea numer i c cr esct oar e a chei l or :
n
k k k ...
2 1
< < < . n cazul acest or t i pur i de cut r i deci zi a se i a nt r e t r ei var i ant e.
Se compar chei a W cu chei a
i
k , n i , 1 = i cut ar ea cont i nu dup una di n
var i ant el e:
i
k W < ,
i
k W = ,
i
k W > .
1.2.2.1. Cutarea binar
Se compar chei a cut at W cu chei a car e se gset e l a mi j l ocul t abel ul ui de
chei
n
k k k ...
2 1
< < < . Ca r ezul t at al acest ei compar a i i se i ndi c n car e j umt at e
de t abel se cont i nu cut ar ea. Se det er mi n noul mi j l oc i se r epet compar a i a.
Dup cel mul t n
2
log compar a i i se va gsi chei a cut at sau se va const at a c
l i pset e di n t abel .
procedure Cut ar e_bi nar ( k W, ; gsi t )
[ ] ... 1 n k : t abel ul chei l or ;
n
k k k ...
2 1
< < <
gsi t f al se; 1 i ; n u ;
whi l e u 1 and ( not gsi t ) do
( )
(
+
2
1
u
i ;
i f
i
k W < t hen 1 i u
el se i f
i
k W > t hen 1 + i l
el se gsi t t r ue;
end if
end i f
end whi l e
end_proc
Exempl u: cut ar ea chei i k = 653
k = 61, 87, 154, 426, 512, [ [ 610, [ 653] ] , 703, 897, 908]
653
Cut ar ea bi nar poat e f i exempl i f i cat pr i n i nt er medi ul unui ar bor e bi nar de
cut ar e. Nodur i l e unui ast f el de ar bor e pot f i nr egi st r r i l e
n
R R , ... ,
1
sau chei l e
asoci at e l or . Pent r u uur i n vom consi der a nodur i l e ar bor el ui bi nar et i chet at e cu
chei l e
n
k k , ... ,
1
( )
n
k k k ...
2 1
< < < .
Ar bor el e bi nar cor espunzt or unei cut r i bi nar e se const r ui et e ast f el :
r dci na ar bor el ui va f i et i chet at cu
(
2
n
K ;
subar bor el e st ng va avea 1
2
(
n
nodur i et i chet at e cu
1
2
1
, ... ,
n
k k ;
subar bor el e dr ept va avea
(
2
n
nodur i et i chet at e cu
n n
k k , ... ,
1
2
+
(
;
pent r u cel e dou subi nt er val e ( submul i mi ) de chei se pr ocedeaz l a f el ,
det er mi nndu- se mi j l ocul ca f i i nd r dci na subar bor el ui r espect i v.
Observa i e: Se poat e f ol osi unul di n al gor i t mi i de i nser ar e nt r - un ar bor e bi nar .
Cut ar ea nt r - un ast f el de ar bor e se r eal i zeaz cu secven a de i nst r uc i uni
cor espunzt oar e despr i nse di n al gor i t mul de cut ar e i i nser ar e pr ezent at l a ar bor i
bi nar i .
Un ar bor e bi nar de cut ar e, cr eat n acest f el , ar e ur mt oar ea pr opr i et at e: pent r u
or i ce nod, numr ul de ni vel ur i di n subar bor el e st ng i subar bor el e dr ept di f er cu
cel mul t o uni t at e.
Un ar bor e cu o ast f el de pr opr i et at e poar t numel e de ar bor e echi l i br at . n
f el ul acest a, numr ul de cut r i n subar bor el e st ng r espect i v dr ept va di f er i cu
cel mul t o cut ar e.
Exempl u: { } 6 , 5 , 4 , 3 , 2 , 1 = k
Cut ar ea chei i 2 necesi t 3 compar a i i
Cut ar ea chei i 6 necesi t 4 compar a i i
Cut ar ea chei i 1. 5 necesi t 3 compar a i i
Cut ar ea chei i 4. 5 necesi t 3 compar a i i
Pent r u cel e ce vor ur ma, vom t r ansf or ma ar bor el e bi nar de cut ar e nt r - un ar bor e
bi nar st r i ct .
Def i ni i e: Un ar bor e bi nar est e st r i ct dac or i ce nod net er mi nal ar e doi
descenden i .
Ar bor el e bi nar st r i ct l vom ob i ne n f el ul ur mt or :
pent r u or i ce nod
i
k car e nu ar e descendent st ng vom aduga un ast f el de
descendent pe car e l vom not a cu
j
k ( nod pt r at i c) ;
pent r u or i ce nod
i
k car e nu ar e descendent dr ept vom aduga un ast f el de
descendent pe car e l vom not a cu
1 + j
k ;
Nodur i l e di n ar bor el e bi nar or i gi nal l e vom not a cu
i
k , n i , 1 = ( nodur i
ci r cul ar e) .
Observa i e: Nodur i l e ci r cul ar e pot f i numi t e nodur i i nt er ne i ar cel e pt r at i ce
nodur i ext er ne.
n f el ul acest a cut ar ea chei i 1. 5 se poat e nchei a cu r spunsul c ea se af l
n i nt er val ul ( 1, 2) , sau chei a 6. 3 est e n i nt er val ul ( 6, 7) .
n cont i nuar e, pr i n ar bor e bi nar de cut ar e at aat mul i mi i de chei
n
k k k ...
2 1
< < < vom n el ege ar bor el e bi nar st r i ct cor espunzt or .
Par cur ger ea n i nor di ne a unui ast f el de ar bor e va f ur ni za secven a:
Cel e 1 + n nodur i t er mi nal e ( pt r at i ce) ,
n
k k , ... ,
1
, cor espund n i nor di ne
i nt er val el or
1 1
, ... ,
+ n
I I :
j j j
k k I ,
1
= , 1 , 1 + = n j .
Cunoat em pr obl ema cut r i i chei i W n t abl oul ( )
n
k k , ... ,
1
f ol osi nd st r at egi a
r epr ezent abi l pr i n ar bor i bi nar i ( vezi cur sul de ar bor i ) . Acest t i p de cut r i s- au
execut at ( dei nu am spus- o) n condi i i l e n car e f r ecven el e de apar i i e al e
val or i l or
n
k k , ... ,
1
au f ost consi der at e egal e i l a f el f r ecven el e i nt er val el or
1 1
, ... ,
+ n
I I . n cont i nuar e vom gener al i za pr obl ema pent r u cazul pr obabi l i t i l or
oar ecar e dat e.
1. 2. 3. Arbori bi nari de cutare opti mi
Pr esupunem c pr obabi l i t at ea ca
j
k W = est e
i
p , n j , 1 = . Pr obabi l i t at ea ca
j
I W est e
j
q , 1 , 1 + = n j i est e sat i sf cut r el a i a 1
1
1 1
= +
+
= =
n
j
j
n
j
j
q p .
Dor i m s gsi m un ar bor e bi nar de cut ar e, cu nodur i l e et i chet at e
n
k k , ... ,
1
aa
nct numr ul de compar a i i di n t i mpul cut r i i s f i e mi ni m. Pr act i c, dac not m cu
A ar bor el e bi nar de cut ar e i cu ( ) ( ) 1
1
1 1
+ =
+
= =
j
n
j
j j
n
j
j
niv q niv p A ne
i nt er eseaz s gsi m acel ar bor e A pent r u car e val oar ea ( ) A est e mi ni m. n
acest caz ar bor el e bi nar de cut ar e, A, va f i opt i m.
Pr i nci pi ul car e st l a baza r ezol vr i i acest ei pr obl eme est e ur mt or ul : t o i
subar bor i i unui ar bor e opt i m sunt opt i mi .
Exempl u:
Dac A est e opt i m pent r u ponder i l e: ( )
4 3 2 1 3 2 1
, , , ; , , q q q q p p p , at unci subar bor el e
di n st nga r dci ni i ci r cul ar e 3 est e i el opt i m pent r u ( )
3 2 1 2 1
, , ; , q q q p p .
Not m cu
( ) j i
C
,
cost ul unui subar bor e opt i m det er mi nat de secven a de nodur i
{ }
1 1 1
, ... ,
+ j j
k k k . Ponder i l e cor espunzt oar e acest ui subar bor e sunt :
( )
1
, ... , ; , ... ,
j i j i
q q p p
.
Fi e suma acest or ponder i . Cost ul mi ni m al unui ar bor e bi nar de r dci n
1
k ,
1 1 j i est e ( ) ( ) ( ) j l C l i C j i w , 1 , , + + + , unde ( ) l i C , i ( ) j l C , 1 + sunt
cost ur i l e subar bor el ui st ng i dr ept ai ar bor el ui de r dci n
1
k . At unci :
( )
( )
( ) ( ) ( ) ( ) { }
<
+ + + =
=
j i pentru
j l C i C j i w j i C
i i C
j l i
, 1 1 , , min ,
0 ,
Fi e ( ) j i R , mul i mea t ut ur or mr i mi l or l pent r u car e n ( ) se at i nge val oar ea
mi ni m; aceast mul i me speci f i c r dci ni l e posi bi l e al e ar bor i l or opt i mi .
Rel a i i l e ( ) , cu val or i l e de por ni r e ( ) 0 , = i i C , f ac posi bi l s eval um
( ) j i C , pent r u n j m , ... , 3 , 2 , 1 1 = = .
Vom apl i ca met oda pr ogr amr i i di nami ce ca n cazul pr obl emei de nmul i r e
opt i m a mat r i ci l or . Vom cal cul a
j i
w
,
i
j i
C
,
succesi v pent r u
n j m , ... , 3 , 2 , 1 1 = = . Vom r e i ne n ( ) j i C , , j i < val oar ea 1 pent r u car e se
r eal i zeaz mi ni mul r el a i ei ( ) .
procedure Gener ar e_ar bor e_opt i m ( ) w C q p n , var ; , ,
[ ] 1 ... 1 , 1 ... 1 + + n n C : mat r i cea cost ur i l or
[ ] 1 ... 1 , 1 ... 1 + + n n w : mat r i cea sumei ponder i l or
[ ] ... 1 n p : ponder i l e
j
p , n j , 1 =
[ ] ... 1 n q : ponder i l e
j
q , 1 , 1 + = n j
f or i =1 t o n do
( ) 0 , i i C ;
i
q w
ii
next i
f or m=1 t o n do { } . 1 dif este j n =
f or i =1 t o n+1- m do
m i j + ;
j j j i j i
q p w w
1 1 ,
+ +
;
( ) ( ) ( ) { } , 1 , min ,
j l C l i C w j i C
j i
j l i
+ + + =
( ) 1 , j i C , { unde l est e val oar ea j l i < pent r u car e se i ndepl i net e
acest mi ni m}
next i
next m
end_proc.
Dac ( ) 1 , 1 + n C est e cost ul pent r u ar bor el e bi nar opt i m de cut ar e, at unci
r dci na l ui va f i
1
k , unde ( ) 1 , 1 + = n C l . Subar bor el e st ng al l ui
1
k va avea
nodur i l e et i chet at e
1 1
, ... ,
l
k k i ar r dci na l ui ( descendent st ng di r ect al l ui
1
k ) va
f i ( ) 1 , 1 C k
r
= . Subar bor el e dr ept al l ui
1
k va avea nodur i l e et i chet at e
1 1
, ... ,
+ + n l
k k , i ar r dci na l ui ( descendent dr ept al l ui
1
k ) va f i
( ) 1 , 1
+ +
=
l n t
C k ,
et c.
f unct i on Ar bor e ( ) j i C n , , , : nt oar ce P r ef er i n l a nodur i l e ar bor el ui
[ ] 1 ... 1 , 1 ... 1 + + n n C : mat r i cea cost ur i l or
j i , : i ndi ci pent r u nodur i l e subar bor el ui ( i <j ) .
( ) i j C l , ;
Al oc spa i u pent r u P
( )
1
k P INFO ; ( ) ( ) P RLINK P LLINK ;
i f ( ) 0 , l i C t hen
( ) ( ) l i C ARBORE P LLINK , , ;
end i f
i f ( ) 0 , 1 + j l C t hen
( ) ( ) j l C ARBORE P RLINK , 1 , + ;
end i f
ret urn P;
end_proc
Apel : ( ) RAD n l ARBORE 1 , + ; RAD: r ef er i n l a r dci na ar bor el ui .