You are on page 1of 17

1.

ALGORITMI SI COMPLEXI TATE



1. 1. ALGORI TMI

Def i ni i e: Un al gor i t m est e o pr ocedur ( o mul i me f i ni t de r egul i bi ne def i ni t e) car e
ndepl i net e un obi ect i v pr eci s. Al gor i t mul pl eac de l a o st ar e i ni i al i se t er mi n nt r - o
st ar e f i nal .

Un exempl u si mpl u de al gor i t m est e r e et a de buct r i e. Maj or i t at ea al gor i t mi l or sunt mai
compl eci : au pai care se r epet , necesi t deci zi i . Mai mul t , al gor i t mi i pot f i compui
pent r u a cr ea al i al gor i t mi .
I ni i al concept ul de al gor i t m a r epr ezent at o pr ocedur pent r u r ezol var ea unei pr obl eme
mat emat i ce ( ex. gsi r ea di vi zor ul ui comun a dou numer e, nmul i r ea a dou numer e) .
n f or ma sa act ual , concept ul a f ost f or mal i zat de ct r e Al an Tur i ng i Al onzo Chur ch n
l ucr r i l e l or " Mai ni Tur i ng" i r espect i v " Lambda Cal cul " .
Al gor i t mi i sunt esen i al i n pr ocesar ea i nf or ma i ei n cal cul at or deoar ece un pr ogr am de
cal cul at or est e pr act i c, un al gor i t m. Di n acest punct de veder e, al gor i t mul poat e f i
consi der at ca " o secven de oper a i i car e pot f i execut at e de ct r e un si st em Tur i ng
compl et " . Exi st def i ni i i al e no i uni i de al gor i t mi car e i l eag de " Mai na Tur i ng" ( $1. 2) .

Def i ni i e: Un al gor i t m est e un pr oces de cal cul def i ni t de ct r e o mai n Tur i ng ( Gur evi ch,
2000) .

Deoar ece pr oceseaz i nf or ma i e, al gor i t mul ci t et e dat e de l a un di spozi t i v i poat e
nr egi st r a ( sal va) dat e necesar e al t or pr ocesr i . Dat el e sal vat e sunt pr i vi t e ca o par t e a
st r i i i nt er ne a al gor i t mul ui . n pr act i c ast f el de st r i sunt nr egi st r at e n " st r uct ur i de
dat e" . Un al gor i t m necesi t i dat e i nt er ne pent r u oper a i i speci f i ce. Acest ea se numesc
" t i pur i de dat e abst r act e" .

Un al gor i t m t r ebui e s se t er mi ne sau nu ?
Uni i aut or i r est r i c i oneaz def i ni i a al gor i t mul ui l a o pr ocedur car e se t er mi n. Al i i
consi der c al gor i t mi i sunt pr ocedur i car e pot s se execut e l a i nf i ni t , o ast f el de
pr ocedur numi ndu- se " met od de cal cul " . n acest e cazur i se consi der c al gor i t mul
t r ebui e s gener eze obi ect e .

Exempl u: Un al gor i t m car e ver i f i c dac exi st mai mul t de un zer o nt r - o secven i nf i ni t
de ci f r e bi nar e al eat oar e t r ebui e s se execut e l a i nf i ni t .

1. 1. 1. Expri marea al gori t mi l or

Al gor i t mi i pot f i expr i ma i pr i n mai mul t e modur i :
l i mbaj nat ur al ;
pseudocod;
scheme l ogi ce;
l i mbaj e de pr ogr amar e.
Expr i mar ea pr i n l i mbaj nat ur al t i nde s f i e compl i cat i , uneor i , ambi gu.
Pseudocodul i schemel e l ogi ce sunt modal i t i st r uct ur at e de expr i mar e a al gor i t mi l or i
sunt i ndependent e de l i mbaj ul de pr ogr amar e.
n f i nal , l i mbaj ul de pr ogr amar e est e pr ef er at deoar ece r epr ezi nt expr i mar ea
al gor i t mul ui nt r - o f or m ce poat e f i execut at de ct r e cal cul at or .

Exempl ul 1: Un al gor i t m car e gset e cel mai mar e numr di nt r - o l i st de numer e.

Li mbaj nat ur al :
1. Admi t em c pr i mul el ement est e cel mai mar e.
2. Exami neaz f i ecar e di n el ement el e r mase n l i st i dac est e mai mar e dect " cel mai
mar e" , at unci not eaz acest f apt ( not eaz- l ca f i i nd " cel mai mar e" ) .
3. Ul t i mul el ement not at est e " cel mai mar e" .

Pseudocod :
I nt r ar e: O l i st L de el ement e nevi d
I ei r e: Cel mai mar e el ement di n l i st a L
cel _mai _mar e
0
L
f or el ement n
1 >
L do
i f ( el ement > cel _mai _mar e) t hen
cel _mai _mar e el ement
end_i f
r et ur n cel _mai _mar e

Exempl ul 2: Al gor i t mul l ui Eucl i d pent r u af l ar ea cel ui mai mar e di vi zor comun a dou
numer e nat ur al e.

Li mbaj nat ur al :
Fi i nd dat e dou numer e nat ur al e a i b, t est eaz dac b est e zer o. Dac da, a est e cel mai mar e
di vi zor comun. Dac nu, r epet pr ocesul ut i l i znd pe b i ce r mne dup mpr i r ea l ui a l a b.

Pseudocod:
f unct i on cmmdc( a, b)
i f 0 = b t hen r et ur n a
el se r et ur n cmmdc( b, a mod b)
end_i f

sau
f unct i on cmmdc( a, b)
whi l e b a do
i f b a > t hen b a a
el se a b b
end_i f
end_whi l e

Vom demonst r a cor ect i t udi nea acest ui al gor i t m.
Fi e r qb a b a N b a + = > , , . S ar t m c or i ce di vi zor al l ui a i b est e di vi zor i
pent r u r . Avem qb a r = . Dac d est e un di vi zor al l ui a i b , f i e sd a = , td b = . Rezul t
( ) d qt s qtd sd r = = . Deci d est e di vi zor al l ui r . Cum or i ce di vi zor al l ui a i b est e di vi zor
i pent r u r at unci i cel mai m mar e di vi zor se r ef er i l a r . Est e suf i ci ent s cont i num
pr ocesul cu numer el e b i r . Cum r est e mai mi c ca b n val oar e absol ut , vom gsi 0 = r
nt r - un numr f i ni t de pai .

1. 1. 2. Cl asi fi carea al gori t mi l or

n mul t e cazur i se dor et e a se r ecunoat e, pent r u un al gor i t m dat , ct e r esur se par t i cul ar e
sunt necesar e ( t i mp de execu i e, memor i e de st ocar e) . Est e vor ba despr e " anal i za
al gor i t mi l or " . Aceast a depi nde de " cl asi f i car ea al gor i t mi l or " .

Cl asi f i car e f unc i e de i mpl ement ar e:
r ecur si vi ( se i nvoc pe ei ni i ) i t er at i vi ( au const r uc i i r epet i t i ve) ;
l ogi ci ( cont r ol eaz deduc i i l ogi ce) ;
ser i al i ( un si ngur pr ocesor i o si ngur i nst r uc i une execut at l a un moment dat )
par al el i ( mai mul t e pr ocesoar e car e execut a i nst r uc i uni n acel ai t i mp) ;
det er mi ni st i ci ( o deci zi e exact l a f i ecar e pas) / nedet er mi ni st i ci ( r ezol v pr obl ema
pl ecnd de l a pr esupuner i ) ;

Cl asi f i car e f uc i e de "desi gn" ( met od) , ex. :
mpar t e i st pnet e ( mpar t e pr obl ema n una sau mai mul t e i nst an e mai
mi ci ) ;
pr ogr amar e di nami c ( caut st r uct ur i opt i mal e - o sol u i e opt i mal a pr obl emei
poat e f i const r ui t pl ecnd de l a sol u i i l e opt i mal e al e subpr obl emel or ) ;
met oda Gr eedy ( caut t ot st r uct ur i opt i mal e cu deosebi r ea c sol u i i l e l a
subpr obl eme nu t r ebui e s f i e cunoscut e l a f i ecar e pas; al eger ea Gr eedy se
r ef er l a ce ar at mai bi ne l a un moment dat ) .

Cl asi f i car e f unc i e de domeni ul de st udi u al gor i t mi i sunt :
de cut ar e, de sor t ar e, numer i ci , al gor i t mi de gr af ur i , de geomet r i e
comput a i onal , de nv ar e aut omat , de cr i pt ogr af i e, et c.

Cl asi f i car e f unc i e de compl exi t at e
Uni i al gor i t mi se nchei e nt r - un t i mp l i near , al i i n t i mp exponen i al , i ar al i i nu se
nchei e. Di n acest punct de veder e al gor i t mi i sunt cl asi f i ca i n cl ase de echi val en bazat e
pe compl exi t at e.
I n cont i nuar e noi vom f i i nt er esa i de acest ul t i m t i p de cl asi f i car e.
Pent r u a f or mal i za concept el e l egat e de Compl exi t at ea Al gor i t mi l or est e nevoi e de un
model comput a i onal . Un ast f el de model est e Mai na Tur i ng.


1. 2. MASI NA TURI NG

Def i ni i a 1. Un al f abet est e o mul i me nevi d i f i ni t de el ement e, numi t e si mbol ur i :
{ }
n
, ... , ,
2 1
= .
Def i ni i a 2. Un cuvnt pest e est e un t - upl u
k
i i i
x ....
2 1
= sau mai si mpl u
k
i i i
....
2 1
.
Def i ni i a 3. Un l i mbaj L pest e est e o submul i me a l ui

. Compl ement ul l ui L est e


L L
c

= . Cuvi nt el e n

pot f i puse n or di ne l exi cogr af i c. Ast f el :


pent r u N n cuvi nt el e de l ungi me n pr eced cuvi nt el e de l ungi me ( ) 1 + n ;
pent r u or i ce l ungi me, or di nea est e al f abet i c.

Mai na Tur i ng const di n ur mt oar el e:
a. 1 k benzi i nf i ni t e n dou di r ec i i . Benzi l e sunt const i t ui t e di nt r - un numr i nf i ni t de
cel ul e n ambel e di r ec i i . Fi ecar e band ar e o cel ul di st i nct , cel ul a de st ar t sau cel ul a
0. n f i ecar e cel ul a f i ecr ei benzi , se poat e scr i e un si mbol al unui al f abet . Se poat e
admi t e i cel ul a goal , car e con i ne si mbol ul nul al al f abet ul ui .
b. Fi ecar e band ar e un cap de ci t i r e/ scr i er e i f i ecar e pas al acest ui a se f ace pe band.
Depl asar ea poat e f i Le f t , Ri gh t , St ay .
c. O uni t at e cent r al car e est e un aut omat f i ni t . St r i l e acest ui a sunt di n mul i mea Q.
Exi st o st ar e di st i nct START i una STOP. Unel e mai ni Tur i ng i au n consi der a i e o
submul i me F a l ui Q ca f i i nd mul i mea de st r i f i nal e. La f i ecar e pas, aut omat ul se af l
nt r - o st ar e
i
q , avnd ca i nt r r i k si mbol ur i ( de pe cel e k benzi ) pe car e capet el e de ci t i r e
l e ci t esc ( si mbol ur i l e cur ent e) . I ei r ea est e r epr ezent at de al t e k si mbol ur i pe car e
capet el e l e scr i u pe benzi l e cor espunzt oar e ( si mbol ur i l e noi ) si o noua st ar e
j
q . Fi ecar e
capt excut apoi o depl asar e snga, dr eapt a sau st pe l oc.

Obs: Pe o band, si mbol ul cur ent i cel nou nu sunt neapr at di f er i t e.
Rezumnd, o mai n Tur i ng det er mi ni st i c cu k benzi ( 1 k ) est e qui nt - upl ul F q I Q , , , ,
0
,
unde:
Q : mul i mea f i ni t de st r i ;
: mul i me f i ni t al f abet ul ( unde est e si mbol ul nul ) ;
I : o mul i me f i ni t de el ement e q m s s q , , , , , unde

Q q q, ,
k
s s
'
, , { }
k
tay ight eft
S R L m , ,
mi cr i l e pe cel e k benzi .
0
q est e st ar ea i ni i al . Vom mai consi der a i Q F o mul i me de st r i f i nal e.
Qui nt - upl ul q m s s q , , , , est e un pas ( o i nst r uc i une) al ( a) mai ni i Tur i ng unde:
q : st ar ea cur ent ;
) ,... , (
2 1 k
s s s s = : cel e k si mbol ur i cur ent e l uat e n consi der a i e pe cel e k benzi ;
) ,..., , (
' '
2
'
1 k
s s s s = : cel e k si mbol ur i car e t r ebui e scr i se;
) ,..., , (
2 1 k
m m m m= : mi cr i l e pe cel e k benzi .

'
q : st ar ea nou.

Enumer am ct eva el ement e car e car act er i zeaz o mi n Tur i ng.
I nt r ar ea mai ni i Tur i ng: cel e k cuvi nt e de pe cel e k benzi , scr i se i ni i al ncepand cu
cel ul el e zer o.
I ei r ea mai ni i Tur i ng: cel e k cuvi nt e scr i se pe benzi n moment ul cnd mai na Tur i ng se
opet e. n mod f r ecvent , sunt em i nt er esa i doar e de unul si ngur .
St ar ea gl obal a mai ni i Tur i ng o not m cu S = {st ar ea cur ent a aut omat ul ui , con i nut ul
cur ent al cel or k benzi , pozi i i l e cur ent e al e cel or k capet e}.
Not m cu S0 st ar ea gl obal i ni i al .
Cal cul ul det er mi ni st al mai ni i Tur i ng T pent r u i nt r ar ea x ( unde ( )
k
x x x , ... ,
1
= , i ar
k
x x , ... ,
1

sunt cuvi nt e) l not m cu ( ) x T i est e secven a de st r i gl obal e car e ncepe cu st ar ea
i ni i al
0
S , i ar f i ecar e st ar e gl obal ur meaz al t ei a n i r .
Rezul t c mai na Tur i ng det er mi ni st est e car act er i zat de f apt ul c ar e cel mul t o
i nt r ar e pent r u or i ce combi na i e de si mbol ur i i st r i .
Dac x est e un cuvnt de i nt r ar e, at unci spunem c pr ocesul de cal cul al mai ni i Tur i ng,
pent r u x , se opr et e dac ( ) x T est e f i ni t i ul t i ma st ar e est e f i nal .
Cuvt ul x est e accept at de mai na Tur i ng T dac ( ) x T est e f i ni t i dac ul t i ma st ar e
est e o st ar e de accept ar e. Vom consi der a dou st r i f i nal e mai i mpor t ant e:

A
q : st ar ea de accept ar e;

R
q : st ar ea de r ej ect ar e.
Pent r u mai na Tur i ng T not m cu ( ) ( ) { }
A
q cu incheie se x T x T L /

= , l i mbaj ul accept at de
mai na T .

Mai na Tur i ng nedet er mi ni st est e car act er i zat de f apt ul c pent r u or i ce st ar e q i si mbol
s , or i car e di nt r e st r i l e di n Q poat e f i st ar e ur mt oar e. Unei st r i gl obal e S i poat e ur ma
or i ce st ar e gl obal P .

Pr ezent m ct eva exempl e de mai ni Tur i ng. Vom descr i e i nst r uc i uni l e ast f el :
( st ar e_cur ent a, si mbol _cur ent ) ( st ar e_noua, si mbol _nou, i ncr ement ) .

Exepl ul 1. Mai n Tur i ng car e nu ar e l a i nt r ar e ni ci un cuvnt i ar l a i ei r e t i pr et e
cuvnt ul HELLO .

St are
curent
Si mbol
curent
St are nou Si mbol nou I ncrement
0
q

H
q
H + 1
H
q

E
q
E + 1
E
q

1
L
q
L + 1
1
L
q

2
L
q
L + 1
2
L
q

O
q
O 0
O
q
0
A
q O 0
q0

1.

0
s

H
q

2. H

H
s


E
q

3. H E

E
s


1
L
q

4. H E L

1
L
s

2
L
q

5. H E L L O

O
q

6. H E L L O

0
s
A
q

7. H E L L O

A
s


Exempl ul 2. Mai na Tur i ng car e det er mi n cuvi nt el e pol i ndr om al e al f abet ul ui { } 1 , 0 .

St are curent Si mbol curent St are nou Si mbol nou I ncrement
0
s
0
0
q
+ 1
0
q
0
0
q
0 + 1
0
q
1
0
q
1 + 1
0
q

0
R
q
1
s0 1
1
q
+ 1
1
q
0
1
q
0 + 1
1
q
1
1
q
1 + 1
1
q

1
R
q
1
0
R
q
0
REV
q
1
1
R
q
1
REV
q
1
0
R
q
1
R
q
0
1
R
q
0
R
q
0
REV
q
0
REV
q
0 1
REV
q
1
REV
q
1 1
REV
q

0
s
+ 1
0
s

A
q
0

Mul i mea st r i l or est e { } , , , , , , , q
0 1
0 0 R R REV A R
q q q q q s Q = .
I n ur mt oar el e exempl e de cal cul pent r u aceast mai n, cuvnt ul s = 0110 va f i accept at
i ar cuvnt ul s = 0111 va f i r ej ect at .





P1 :
0
s ,
0 1 1 0

P2 :
0
q ,
1 1 0

P3 :
0
q ,
1 1 0

P4 :
0
q ,
1 1 0

P5 :
0
q ,
1 1 0

P6 :
0
R
q ,
1 1

P7 :
REV
q ,
1 1

P8 :
REV
q ,
1 1

P9 :
REV
q ,
1 1

P10 :
0
s ,
1 1

P11 :
1
Q ,
1

P12 :
1
Q ,
1

P13 :
1
R
q ,
1

P14 :
REV
q ,


P15 : s0 ,

P16 :
A
q
Accept

P1 :
0
s ,
0 1 1 1

P2 :
0
q ,
1 1 1

P3 :
0
q ,
1 1 1

P4 :
0
q ,
1 1 1

P5 :
0
q ,
1 1 1

P6 :
0
R
q ,
1 1 1

P7 :
R
q , 1 1
Rej ect


I n cont i nuar e dm enun ul a dou t eor eme.

Teorema 1. Pent r u or i ce 1 k i or i ce al f abet , exi st o mai n Tur i ng cu ( ) 1 + k benzi .

Teorema 2. Pent r u or i ce mai n Tur i ng S cu k benzi , exi st o mai n Tur i ng T cu o
band car e nl ocui et e pe S n ur mt or ul sens: pent r u or i ce cuvnt

x , mai na S se
opr et e nt r - un numr f i ni t de pai l a i nt r ar ea x dac i numai dac T se opr et e l a
i nt r ar ea x i , l a opr i r e, acel ai l ucr u est e scr i s pe ul t i ma band a l ui S i pe banda l ui T .
Mai mul t , dac S f ace N pai , at unci : T f ace ( )
2
N O pai .

O mai n Tur i ng car e est e capabi l s si mul eze or i ce al t mai n Tur i ng, se numet e
Mai n Tur i ng Uni ver sal .

Cal cul at oar el e moder ne, cu pr ogr ame nr egi st r at e sunt i nst an e al e unei mai ni Tur i ng
mai sof i st i cat e numi t Mai n Pr ogr am cu acces al eat or ( Random Acces St or ed Pr ogr am
Machi ne RASP) .
RASP nr egi st r eaz pr ogr amul n memor i e. Pr ogr amul est e o secven f i ni t de st r i al e
mai ni i ( numi t e i i nst r uc i uni ) . RASP ar e un umr i nf i ni t de r egi st r i i cel ul e de memor i e
car e pot con i ne or i ce nt r eg.
RASP est e car act er i zat de adr esar ea i ndi r ect con i nut ul unui r egi st r u poat e ar t a
spr e adr esa or i cr ui al t r egi st r u.

1. 3. MSURAREA COMPLEXI TI I

Exi st dou t i pur i de msur a compl exi t i i al gor i t mi l or .

Msur a st at i c est e bazat pe st r uct ur a al gor i t mul ui .
Msur a di nami c est e bazat pe cal i t at ea al gor i t mul ui , i nt r r i l e l ui , compor t ament ul
cal cul at or ul ui n t i mpul execu i ei al gor i t mul ui .
De i nt er es est e msur a di ami c.
n gener al , o msur a compl exi t i i est e i ndependent de model ul comput a i onal dac,
n r apor t cu o mul i me { }
i
P de al gor i t mi , ar e pr opr i et i l e:
1. i domeni ul l ui
i
est e acel ai cu al l ui
i
P , i ar codomeni ul est e i ncl us n N , unde
i
est e msur a l ui
i
P ;
2. Exi st un pr edi cat M , ast f el nct : ( ) ( ) m x m x i M
i
, , = , m x i , , .
Vom da o msur a compl exi t i i di n punct de veder e al t i mpul ui de execu i e al unui
al gor i t m. Vom f ol osi ca model comput a i onal mai na Tur i ng.
n gener al , cnd vor bi m despr e t i mpul de execu i e al unui al gor i t m, ne gndi m l a numr ul
de pai execut a i de pr ocesul comput a i onal ( cal cul ul det er mi ni st ) asoci at al gor i t mul ui .
Cnd pr ocesul de cal cul nu se opr et e, consi der m t i mpul ca f i i nd nedef i ni t .

Def i ni i e. Anal i za compl exi t i i det er mi n t i mpul n car e oper a i i l e de baz al e unui
al gor i t m sunt execut at e pet r u f i ecar e set de dat e de i nt r ar e.

Sunt mai mul t e cazur i n car e se det er mi n compl exi t at ea unui al gor i t m:
1. Cazul def avor abi l , ( ) n W : n ct t i mp oper a i i l e de baz sunt execut at e n cazul
def avor abi l .
2. Cazul cel mai bun, ( ) n B : n ct t i mp oper a i i l e de baz sunt execut at e n cazul cel
mai bun.
3. Fi ecar e caz, ( ) n T : n ct t i mp oper a i i l e de baz sunt execut at e pent r u f i ecar e
caz.
4. Cazul medi u, ( ) n A : n ct t i mp oper a i i l e de baz sunt execut at e n medi e.

n cont i nuar e dm ct eva exempl e de cal cul a compl exi t i i .

Exempl ul 1. Cut ar ea secven i al nt r - un t abl ou [ ] n s ,..., 1 = :
begi n
l oca i e 1
whi l e ( l oca i e n) and ( s[ l oca i e] x) do
l oca i e l oca i e + 1
end_whi l e
i f ( l oca i e > n) t hen
l oca i e 0 / el ement negsi t /
end_i f
end

S anal i zm compl exi t at ea n cel e pat r u cazur i .
1. ( ) 1 + = n n W el ement ul x nu est e n t abl ou.
2. ( ) n B = 1 x se af l n pr i mul el ement .
3. ( ) n T nu se cal cul eaz deoar ece oper a i i l e de baz nu sunt execut at e de acel ai
numr de or i pent r u t oat e i nst an el e de di mensi une n .
4. Pent r u ( ) n A :
Cazul 1. x est e n t abl ou. Toat e component el e t abl oul ui au val or i di f er i t e. Rezul t c
x poat e f i gsi t n f i ecar e di nt r e el e cu aceeai pr obabi l i t at e,
n
1
. Rezul t c
( )
( )
2
1

2
1

1

1

1

1 1
+
=
+
= = =

= =
n n n
n
k
n n
k n A
n
k
n
k
.
2
1 + n
est e, pr act i c, val oar ea medi e at ept at a
compar a i i l or .
Cazul 2. x se af l sau nu n t abl ou. Fi e p pr obabi l i t at ea ca el s se af l e n t abl ou.
Pr obabi l i t at ea ca el s se af l e n unul di n el ement e est e
n
p
, i ar pr obabi l i t at ea ca el s nu
f i e n t abl ou est e ( ) p 1 .
Dac x se af l pe pozi i a k , vom avea k t r ecer i pr i n bucl , i ar dac x nu apar i ne
t abl oul ui , vom avea n t r ecer i . At unci
( ) ( ) ( )
= +

+ = +
+

= + = + =

= =
np n
n p p
np n
n
p
p n k
n
p
p n
n
p
k n A
n
k
n
k
2 2 2
1
1 1
1 1
.
2
1
2 2 2
|

\
|
+ =

+ =
p
n
p n p
n
p

Pent r u 1 = p r ezul t ( )
2
1

+
=
n
n A i pent r u 5 , 0 = p r ezul t ( ) 1 3 + = n n A .

Exempl ul 2. Sor t ar ea unui t abl ou [ ] n s ,..., 1 = pr i n met oda i nt er schi mbr i i .

begi n
f or 1 = i t o 1 n do
f or 1 + = i j t o n do
i f [ ] [ ] ( ) i s j s < t hen
] [ ] [ j s i s
end_i f
end

Not m cu ( ) n T . numr ul de pai execut a i de al gor i t m.
Oper a i i l e de baz sunt : compar a i a [ ] i s cu [ ] j s i schi mbar ea [ ] i s cu [ ] j s .
Bucl a ext er i oar se execut de 1 n or i .
La pr i mul pas al bucl ei ext er i oar e se vor execut a 1 n pai n bucl a i nt er i oar .
La al doi l ea pas al bucl ei ext er i oar e vor f i 2 n pai n bucl a i nt er i oar . Apoi 3 n , 4 n
pai , et c.
Deci ( ) ( ) ( )
( )
2
1
1 2 .... 2 1

= + + + + =
n n
n n n T , i ar compl exi t at ea est e ) (
2
n O .
Ur mt oar el e def i ni i i r epr ezi nt cazur i de mr gi ni r e a compl exi t i i .

Def i ni i a 1. Pent r u o f unc i e ( ) n f not m cu ( ) ( ) n f O mul i mea f unc i i l or ( ) n g , cu
pr opr i et at ea c R c i N n
0
, ast f el nct ( ) ( ) n f c n g ,
0
n n .

Exempl u. ( )
2
n n f = i ( ) n n n g 10
2
+ = .

2 2
10 n c n n + ; ( ) 0 10 1
2
+ n n c ; ( ) [ ] 0 10 1 + n c n ; ( ) [ ] 0 10 1 + n c n
Pent r u un
1
10
0

>
c
n , pr i ma i negal i t at e est e adevr at .

Def i ni i a 2. Pent r u o f unc i e ( ) n f not m cu ( ) ( ) n f mul i mea f unc i i l or ( ) n g , cu
pr opr i et at ea c R c i
+
N n
0
, ast f el nct ( ) ( ) n f c n g ,
e
n n .
Exempl ul 1. ( )
2
3
n n pent r u c n n 1
3
pent r u 1 n .
Exempl ul 2.
( )
( )
2

2
1
n
n n

+
. Ast f el ,

( )
4

2

2

2
1

2

2
1
2
n n n n n n n
=
+

+
pent r u 2 n .
Deci pent r u
4
1
= c i 2
0
= n avem ndepl i ni t condi i a i mpus n def i ni i e.

Def i ni i a 3. Pent r u o f unc i e ( ) n f not m cu ( ) ( ) ( ) ( ) ( ) ( ) n f n f O n f = mul i mea f unc i i l or
( ) n g , cu pr opr i et at ea c
2 1
, c c i N n
0
ast f el nct ( ) ( ) ( ) n f c n g n f c
2 1
,
0
n n .

Exempl e de cl ase de compl exi t at e:
( )
2
n O : ( ) , 8 log 3 + n
2
4 n , 7 5 + n , 9 6
2
+ n , ( ) n n log 2 , n n 2 5
2
+
( )
2
n :
2
4 n ,
2 3
3 4 n n + , 9 6
2
+ n ,
4 6
6 n n + , n n 2 5
2
+ , n
n
4 2 +
( )
2
n :
2
4 n , 9 6
2
+ n , n n 2 5
2
+

Pr ezent m n cont i nuar e un al gor i t m de caut ar e car e ar e o compl exi t at e mai bun dect
al ogor i t mul de caut ar e secven i al .


Cut ar ea bi nar n t abl oul [ ] , ... , 1 n s .
begi n
mi n 1; max n; l oca i e 0;
whi l e ( mi n max) and ( l oca i e = 0) do
mi j l oc ( mi n + max) di v 2;
i f ( x = s [ mi j l oc] ) t hen l oca i e mi j l oc
el se i f ( x < s [ mi j l oc] ) t hen max mi j l oc1
el se mi n mi j l oc + 1
end_i f
end_i f
end_whi l e
end

Anal i za compl exi t i i .
Consi der m cazul def avor abi l ( ) n W . Pr esupunem c n est e put er e a l ui 2, mai pr eci s
k
n 2 = . Pr i ma i nst r uc i une di n bucl nj umt et e di mensi unea t abl oul ui de cut ar e.
Aceast a se nt mpl de f i ecar e dat cnd se t r ece pr i n bucl .
Pent r u cazul cnd x est e mai mi c sau mai mar e dect t oat e el ement el e di n l i st vom ar t a
c ( ) ( ) 1 log + = n n W .
Di n nj umt i r ea secven ei de cut ar e deducem:
( ) 1
2
+ |

\
|
=
n
W n W
( ) 1 1 = W
( ) ( ) 2 1 1 2 = + = W W
( ) ( ) 3 1 2 4 = + = W W
( ) ( ) 4 1 3 1 4 8 = + = + = W W
( ) ( ) 5 1 4 1 8 16 = + = + = W W
S demonst r m pr i n i nduc i e c ( ) ( ) 1 log + = n n W .
Pent r u 1 = n : ( ) ( ) 1 1 log 1 1 + = = W
Pr esupunem ( ) ( ) 1 log + = n n W
( ) ( ) ( ) ( ) ( )
( ) 1 2 log
1 2 log log 1 1 log 1 2
+
= + + = + + = + =
n
n n n W n W

n cazul cnd
k
n 2 avem n gener al compl exi t at ea

)) ( (log 1 1 ) ( log n O n = + + .
Al gor i t mul de sor t ar e bi nar est e un exempl u de pr obl em Di vi de- et - I mper a ( $Err or !
Ref erence source not f ound. ) .

1. 3. 1. Ti puri de probl eme i compl exi tatea l or

( ) ( ) n O log apar e cnd o pr obl em mai mar e est e r ezol vat pr i n t r ansf or mar ea ei nt r - o
pr obl em de di mesi une mai mi c ( t i p l ogar i t mi c) ;
( ) n O apar e cnd f i ecar e el ement al pr obl emei necesi t un mi c ef f or t ( t i mp) de
pr ocesar e ( t i p l i near ) ;
( ) ( ) n n O lg apar e cnd o pr obl em est e spar t n mai mul t e subpr obl eme r ezol vabi l e
i ndependent , i ar apoi sunt combi nat e sol u i i l e ( t i p l i near i t mi c) ;
( )
2
n O
apar e cnd al gor i t mul pr oceseaz t oat e per echi l e de el ement e al e unei
mul i mi ( t i p quadr at i c) ;
( )
n
O 2
t i p exponen i al .



Pr i mel e t r ei cur be di n st nga gr af i cul ui de mai sus r epr ezi nt n or di ne ( de l a st nga l a
dr eapt a) compl exi t i l e ( )
n
O 2 , ( )
2
n O i ( ) ( ) n n O lg .

I at ct eva cazur i pr act i ce de st abi l i r e a compl exi t i i .

Cazul de r ezol var e al unei si ngur e i nst r uc i uni ce se execut o si ngur dat :
( ) 1 O
Cazul de r ezol var e:
f or 1 = i t o n do
s ;
unde s est e ( ) 1 O . Rezul t compl exi t at ea ( ) ( ) n O O n 1 = .
Cazul de r ezol var e:
f or 1 = i t o n do
f or 1 = j t o n do
s ;
unde s est e ( ) 1 O . Rezul t compl exi t at ea ( ) ( )
2
n O n O n = .
Cazul de r ezol var e:
1 = h
whi l e ( ) n h do
s ;
h h 2
end_whi l e
ai ci var i abi l a h i a val or i l e 1, 2, 4, 8, pn cnd depet e pe n . Rezul t c sunt

n
2
log 1+
t r ecer i . Avem compl exi t at ea ( ) n O
2
log .
Cazul de r ezol var e:
f or 1 = i t o 1 n do
f or i j = t o n do
s ;
Bucl a i nt er i oar se execut de i or i unde i = 1, 2, , n. Numr ul de execu i i est e

=
+
=
n
i
n n
i
1
2
) 1 (
, deci compl exi t at ea est e ( )
2
n O .
Cazul de r ezol var e:
h = n; j =1
whi l e j < h do
f or i = 1 t o n do
s;
h h/ 2
end_whi l e
Bucl a i nt er i oar ar e compl exi t at ea ( ) n O i ar bucl a ext er i oar execut

n
2
log i t er a i i .
Compl exi t at ea est e deci ( ) n n O
2
log .

1. 4. RECURSI VI TATE
n gener al , spunem c un obi ect sau o met od est e r ecur si v ( ) dac se poat e def i ni n
f unc i e de el ( ea) nsui ( nsi ) . Mai pr eci s, un ast f el de obi ect sau met od, se poat e
def i ni pr i n:
ct eva cazur i si mpl e sau met ode si mpl e;
r egul i car e t r ansf or m cazur i l e compl exe n cazur i si mpl e.

Exempl u :
or i ce pr i nt e ar e un st r mo;
pr i n i i or i cr ui st r mo sunt de asemenea st r moi ai per soanei consi der at e.

Cazul f unc i i l or :
O f unc i e est e def i ni t r ecur si v dac por ni nd de l a anumi t e val or i al e ei , se pot cal cul a
al t e val or i pr i n aut oapel ar ea f unc i ei . Al t f el spus, o f unc i e r ecur si v est e par i al def i ni t n
t er meni de ea nsi .
Def i ni i a unei f unc i i r ecur si ve t r ebui e s sat i sf ac condi i a de consi st en : val oar ea
f unc i ei r ecur si ve t r ebui e s f i e di r ect cal cul abi l sau cal cul abi l cu aj ut or ul unor val or i
di r ect cal cul abi l e.
n def i ni r ea unei f unc i i r ecur si ve t r ebui e s apar cel pu i n o condi i e de opr i r e di n
r ecur si vi t at e.
Expr esi a unei f i nc i i r ecur si ve est e dat de o r el a i e de r ecur en . Fi e o secven { }
1 n n
t
i N k . Dac avem:

k k k n k n k n
t a t a t a t ....
2 2 1 1
+ + + =
+ + +

at unci secven a est e o secven r ecur si v de or di n k , i ar r el a i a pr ecedent est e o r el a i e
r ecur si v de or di n k .

1. 4. 1. Exempl e de funci i recursi ve

1. For mul r ecur si v pent r u al gor i t mul l ui Eucl i d
Pent r u N n m , , n m > avem ( ) ( )
1
, , r n cmmdc n m cmmdc = , unde
1
r est e r est ul mpr i r i i l ui m
l a n . Deci

n m n m r /
1
= .
At unci ( ) , cmmdc n m cmmdc = (

n m n m n / , ) .
Al gor i t m Eucl i d scr i s ca f unc i e r ecur si v:
f unct i on ( ) n m cmmdc ,
i f ( ) 0 = n t hen r et ur n m
el se
ret urn cmmdc (

n m n m n / , )
end_i f
end_f unc
Apel : r spuns ( ) b a cmmdc ,

Agor i t mul Eucl i d scr i s ca pr ocedur r ecur si v
procedure cmmdc ( i n : m, n; out r esul t )
i f ( ) 0 = n t hen r esul t m
el se cmmdc
|
|

\
|
(

result
n
m
n m n ; ,
end_i f
end_proc.
Apel : ( ) raspuns b a cmmdc ; ,

2. Func i a f act or i al
: fact N N
( )
( )


=
=
1 , 1
0 1,

n daca n fact n
n daca
n fact

f unct i on ( ) n fact
i f ( ) 0 = n t hen ret urn 1
el se ret urn ( ) 1 n fact n ;
end_i f
end_f unc.

Exempl u de cal cul ef ect i v pent r u f unc i a f act or i al :
f act or i al ( 3)
f act or i al ( 2)
f act or i al ( 1)
f act or i al ( 0) r et ur n ( 0)
r et ur n 2 1 2 =
r et ur n 1 1 1 =
r et ur n 6 2 3 =


3. Func i a put er e
n
x
Pent r u un nt r eg 0 > x s se cal cul eze
n
x , unde 0 n . Vom consi der a f unc i a
( ) n x putere , : N N N . Evi dent ( ) 1 0 , = x putere . At unci , expr esi a r ecur ent a f unc i ei est e:
( )
( )


=
=
1 , 1 ,
0 1,
,
n dac n x putere x
n dac
n x putere
Cor ect i t udi nea def i ni i ei r ezul t di n 1
0
= x ( pasul de ver i f i car e) i
n n
x x x =
+

1
( pasul de
i nduc i e) .
Af i r mm c f unc i a ( 1) ar e compl exi t at ea ( ) n O .
Not m cu ( ) n T t i mpul de execu i e a f unc i ei put er e.
( ) ( ) ( ) 1 1 O n T n T + = , unde ( ) 1 O est e t i mpul necesar pent r u a r et ur na val oar ea x .
( ) ( ) ( ) ( ) ( ) .... 1 2 2 1 1 + + = + = O n T O n T n T
( ) ( ) ( ) 1 0 O n T n T + = .
Rezul t compl exi t at ea ( ) n O .
Expr i mnd al t f el f unc i a put er e, vom ob i ne un al gor i t m cu o compl exi t at e mai bun.
Obser vm c
4
x se poat e cal cul a mai uor dac am cal cul at
2
x , deoar ece ( )
2
2 4
x x = . La
f el , ( )
2
3 6
x x = . Not m

2 2 / div n n =
Consi der m ) 2 , (
2
div n x putere x
div n
= i not m cu sqr r i di car ea l a put er ea doi . At unci
( ) ( )

par n dac x
impar n dac x
div n x putere sqr
n
n
,
,
2 ,
1

At unci :

( ) ( )
( ) ( )

=
par n dac div n x putere sqr
impar n dac div n x putere sqr x
x
n
, 2 ,
, 2 , *
,
i ar al gor i t mul compl et est e:
( ) ( ) ( )
( ) ( )

=
=
par n dac div n x putere sqr
impar n dac div n x putere sqr x
n dac
n x putere
, 2 ,
, 2 ,
0 , 1
,
Not m cu ( ) n T t i mpul de execu i e pent r u aceast f or mul . Fi e
m
n 2 = . At unci :
( )

> + |

\
|
=
=
1 , 1
2

1 , 1

n pentru
n
T
n pentru
n T

( ) ( ) ( ) ( ) ( ) ( ) 1 log 1 1 2 .... 2 2 1 2 2
2
0 2 1
+ = + = + = + = = + = + = =

n m m T m T T T T n T
m m m
Rezul t compl exi t at ea
( ) n O
2
log .

4. Al t e exempl e de f unc i i r ecur si ve:

a) Fi bonacci : N N fib :
( )
( ) ( )

> +
= =
=
2 , 2 1
2 , 1 , 1

n n n fib
n n
n fib



b) Acker man:
( ) ( )
( ) ( )


=
= +
=
rest n m , ack m ack
n , m ack
m n
n m ack
, 1 , 1
0 , 1 1
0 , 1
,



c) Manna Punel l i
( )
( ) ( ) [ )

+

=
12 0 , 2
12 , 1

, x x f f
x x
n mana

1. 4. 2. Ti puri de recursi e
Pent r u expr i mar e, vom f ol osi f unc i i l e: ( ) x f , ( ) x g , ( ) x h , ( ) x i , ( ) v u, .

a) Recur si a l i ni ar
( )
( ) ( )
( ) ( ) ( ) ( )

=
=
altfel x i x h f
true x p dac x g
x f
, ,
,


Exempl u: ( ) n fact

b) Recur si a nel i ni ar de t i p cascad
( )
( ) ( )
( ) ( ) ( ) ( ) ( )

=
=
altfel x i f x h f
true x p dac x g
x f
, ,
,


Exempl u: ( )
( ) ( )

> +
= =
=
2 , 2 1
2 1 , 1

n altfel n fib n fib
n sau n
n fib

c) Recur si a nel i ni ar de t i p mpachet at
( )
( ) ( )
( ) ( ) ( ) ( )

=
=
altfel x h f f f
true x p dac x g
x f
, ........ , ..... .
,

Exempl u: ( ) n mana




1. 4. 3. Ecuai i recurent e
n mul t e cazur i , anal i za compl exi t i i conduce l a ecua i i r ecur ent e i l a r ezol var ea
acest or a. Pr ezent m dou t ehni ci gener al e pent r u r ezol var ea ecua i i l or r ecur ent e.

Teorema 1. Fi e ecua i a r ecur ent , omogen i l i ni ar cu coef i ci en i const an i ,
0 ....
1 1 0
= + + +
k n k n n
t a t a t a .
Dac ecua i a sa car act er i st i c, 0 ....
0 1
1 0
= + + +

r a r a r a
k
k k
, ar e k sol u i i di st i nct e,
k
r r r , ... , ,
2 1
, at unci si ngur el e sol u i i al e ecua i ei r ecur ent e sunt de f or ma
0 ....
2 2 1 1
= + + + =
k
k k
n n
n
r c r c r c t .

Exempl u: 0 6 5
1 1
= +
n n n
t t t , 0
0
= t , 1
1
= t .
Ecua i a car act er i st i c est e 0 6 5
2
= + r r . Rezul t 3
1
= r , 2
2
= r . Sol u i a gener al est e
n n
n
c c t 2 3
2 1
+ = i di n condi i i l e i ni i al e avem
n n
n
t 2 3 = .

Teorema 2. Fi e r r dci na de mul t i pl i ci t at e m pent r u ecua i a car act er i st i c a unei ecua i i
omogene, l i ni ar e i r ecur ent e cu coef i ci en i const an i :
0 ....
1 1 0
= + + +
k n k n n
t a t a t a
At unci
n
n
r t = ,
n
n
nr t = ,
n m
n
n
n
r n t r n t , ..... ,
1 2
= = sunt t oat e sol u i i al e ecua i i r ecur ent e.
Fi ecar e di nt r e acet i t er meni t r ebui e i nt r odus n sol u i a gener al ( pr i n adunar e) .

Exempl u: S se r ezol ve: 0 9 15 7
3 2 1
= + +
n n n n
t t t t , 0
0
= t , 1
1
= t , 2
2
= t .
Ecua i a car act er i st i c est e 0 9 15 7
2 3
= + r r r . Rezul t 1
1
= r , 3
3 , 2
= r .
Sol u i a gener al est e
n n n
n
n c c c t 3 3 1
3 2 1
+ + = i di n condi i a i ni i al , sel ect m sol u i a
1
3 3 1

+ =
n n
n
n t .

You might also like