You are on page 1of 37

Mt s phng php thit k thut gii

Ni dung ca chng ny l mt s chin lc thit k thut gii nh chia tr, vt


cn, tham lam, quy hoch ng... Mc d l cc chin lc tng qut, tuy nhin
mi phng php ch p dng cho nhng lp bi ton ph hp. Ni dung ca chng,
ngoi phn trnh by v cc phng php cn c nhng v d c th, c thut gii v
ci t, ngi c c mt ci nhn chi tit v vic t thut ton n chng trnh.

8.1. Chia tr (Divide and Conquer)


Chia tr l mt t tng rt ph bin trong cuc sng v c p dng rt hiu qu
trong Tin hc. T tng c bn ca phng php chia tr l chia mt bi ton
ln, kh gii thnh cc bi ton tng t, c kch thc nh hn v d gii hn
sao cho ta c th phi hp kt qu ca cc bi ton con c kt qu ca bi ton
ln.
Rt nhiu thut ton ta gp chng trc u mang t tng "chia tr": thut
ton sp xp nhanh Quick sort, thut ton sp xp trn Merge sort, thut ton tm
kim nh phn, Chng ta s nghin cu bi ton Thp H ni, l mt bi ton in
hnh c gii bng phng php chia tr.

8.1.1. Bi ton Thp H Ni


C N a c ng knh khc nhau c t chng ln nhau theo th t gim dn ca
ng knh tnh t di ln. C ba v tr c th t cc a nh s 1, 2, 3. Chng a
ban u c t v tr 1:

Cn chuyn c chng a t v tr 1 sang v tr 2, theo nhng quy tc sau:


Khi di chuyn mt a, phi t n vo mt trong ba v tr cho.
Mi ln ch c th chuyn mt a v phi l a trn cng.
Ti mt v tr, a no mi chuyn n s phi t ln trn cng. a ln hn
khng bao gi c php t ln trn a nh hn (hay ni cch khc: mt a ch
c t trn mt t hoc t trn mt a ln hn)
Bi ton ny c ngun gc l mt truyn thuyt ca n rng c mt nhm cao
tng n gio c giao trng trch chuyn dn 64 a vng gia 3 cc kim cng

theo cc iu kin ni phn trn. Khi no hon tt cng vic, tc l khi chuyn
xong ton b 64 a t v tr ban u sang v tr kt thc th cng l thi im tn th.
Chng ta gii bi ton bng cch chia bi ton chuyn N a, t v tr 1 sang v tr 2
thnh ba bi ton n gin hn nh sau:
1. Chuyn N-1 a trn cng t v tr 1 sang v tr 3, dng v tr 2 lm trung gian.
2. Chuyn a th N t v tr 1 sang v tr 2.
3. Chuyn N-1 a t v tr 3 sang v tr 2, dng v tr 1 lm trung gian.
Ch rng bi ton 1 v 3 tng t nh bi ton ban u, ch khc l kch thc nh
hn. Chng cng s c gii bng phng php chia tr ging nh bi ton ban
u. D dng kim tra l khi gii nh vy chng vn tho mn cc iu kin. Bi ton
2 th c gii rt n gin.
Thut ton c vit di dng gi m nh sau:
Procedure Hanoi;
begin
Move(n,1,2,3);
end;
Procedure Move(n,a,b,c);
{chuyn n a, t v tr a sang v tr b, dng v tr c lm trung gian }
begin
if n=0 then exit;
Move(n-1,a,c,b);
writeln('Chuyn a ',n, ' t v tr ',a, 'sang vi tri ',b);
Move(n-1,c,b,a);
end;

Chng ta hy dng li mt cht phn tch phc tp tnh ton. Gi T(n) l s


thao tc chuyn a cn thit chuyn xong n a. Theo thut ton trn ta c:
T(n) = T(n-1) + 1 + T(n-1).
Bng cc phng php gii cng thc truy hi ta c c T(n) = 2n-1. p dng kt
qu ny vi gi thit rng mi cao tng phi mt 1 giy chuyn xong mt a t
cc ny sang cc kia, ta thy thi gian chuyn ton b 64 a vng l T(64)=2161=18446744073709551615 giy. Nh vy ngy tn th (nu c) theo truyn thuyt
phi 600 t nm na mi n.
Chng ta s phn tch mt thut ton na thy c s hiu qu ca phng php
chia tr. Bi ton c chn phn tch tip theo l bi ton nhn 2 s ln.

8.1.2. Bi ton nhn hai s ln


Rt nhiu ng dng trong thc t i hi phi x l cc s rt ln, nm ngoi khong
biu din ca cc kiu c s ca ngn ng lp trnh (chng hn vic tm cc s

nguyn t rt ln trong m ho RSA). gii quyt cc yu cu , chng ta phi


xy dng cc kiu s rt ln v xy dng cc php ton tng ng. Trong phn ny ta
ch xt php ton nhn i vi hai s rt ln. Gi thit c hai u c n ch s v c
biu din bng mng. Bi ton nhn 2 s ln pht biu nh sau:
Input. A,B l 2 s nguyn c n ch s: A=A1A2.An v B=B1B2.Bn.
Output. C=A.B
Thut ton t nhin (brute-force) ca bi ton nhn 2 s ln l gii thut nhn tay ta
vn thc hin: ln lt nhn tng ch s ca s th hai vi s th nht, dch kt qu
theo v tr v cng cc kt qu trung gian li.
Chng hn nhn A=1981 v B=1234 ta tin hnh nh sau:
1981
1234
------7924
+ 5943
3962
1981
------2444554
Thut ton nhn 2 s ln kiu nhn tay c m t bng gi m:
function Mul(A,B,n)
begin
T := 0;
for i := 1 to n do begin
D := A* Bi;
D := D shl (i-1); {dch D sang tri i-1 ch s, tc l nhn D vi 10i-1}
T := T + D;
end;
return T;
end;

D dng tnh c phc tp tnh ton ca thut ton ny l O(n2). Chng ta c


gng gim bc ca thut ton vi t tng chia tr. n gin, ta gi thit n=2k
v tch A,B di dng XY v UV trong X,Y,U,V l cc s c k ch s. Nh vy
php nhn 2 s A,B c tnh nh sau:
A.B = (X.10k + Y).(U.10k+V) = XU.102k + (XV+YU).10k+UV.
Kt qu l bi ton nhn 2 s A.B c 2k ch s c chia thnh 4 bi ton con nhn
cc s k ch s v mt s php cng, tr. Nhng nh vy vn cn nhiu. Ta tip tc
ci tin bng nhn xt:
XV+YU = (X+U).(Y+V) - (XY + UV).

t P = XY; Q = UV; R = (X+U).(Y+V). T 2 ng thc trn ta c:


A.B = P.102k + (R-P-Q).10k+Q.
Nh vy bi ton nhn 2 s A.B c n ch s c chia thnh 3 bi ton con nhn cc
s n/2 ch s v mt s php cng, tr. Thut ton c vit dng gi m nh sau:
function Mul(A,B,n)
begin
if n=1 then return A1*B1;
k = n / 2;
X :=A(1..k); Y := A(k+1..n);
U :=B(1..k); V := B(k+1..n);
P := Mul(X,Y,k);
Q := Mul(X,Y,k);
R := Mul(X+U,Y+V,k);
T := P shl n + (R-P-Q) shl k + Q;
return T
end;

xc nh phc tp ca thut ton, ta coi l thao tc c bn l php nhn, cng


v dch tri tng ch s. Gi T(n) l s thao tc c bn thc hin nhn 2 s c n
ch s. Ta c:
T(n) = 3T(n/2) + C.n
Gii ra ta c T(n) = n log23 = n1.59, tc l c mt s ci thin so vi thut ton nhn tay.
(Tuy nhin khc bit cng khng r rt v ch th hin khi n kh ln nn thng
thng trong cc bi ton khng ln lm ngi ta thng dng thut ton u tin).
Qua cc phn trn chng ta thy c phn no hiu qu ca phng php chia
tr. Cui chng ny chng ta s gp li t tng chia tr trong phn ni v
phng php quy hoch ng. Quy hoch ng l t tng chia tr trit v l
mt phng php cc k hiu qa trong vic gii cc bi ton ti u.

8.2. Vt cn (Exhausted search)


Vt cn, duyt, quay lui l mt s tn gi tuy khng ng ngha nhng cng ch
mt phng php rt n gin trong tin hc: tm nghim ca mt bi ton bng
cch xem xt tt c cc phng n c th. i vi con ngi phng php ny
thng l khng kh thi v s phng n cn kim tra qu ln. Tuy nhin i vi my
tnh, nh tc x l nhanh, my tnh c th gii rt nhiu bi ton bng phng
php vt cn.
u im ln nht ca phng php vt cn l lun m bo tm ra nghim chnh
xc. Ngoi ra phng php vt cn cn c mt s u im so vi cc phng php
khc l i hi rt t b nh v ci t n gin. Hn ch duy nht ca phng php

ny l thi gian thc thi rt ln, phc tp thng bc m. Do vt cn


thng ch p dng tt vi cc bi ton c kch thc nh.
Mc d vy, khng nn coi thng phng php ny. Rt nhiu bi ton ch c thut
ton duy nht l vt cn: t bi ton n gin nh tm s ln nht trong mt dy s
n cc bi ton NPC. Trong mt s tnh hung khc, chng hn nh thi gian lp
trnh hn ch th vt cn c th coi nh mt gii php tnh th. Rt nhiu trng hp
ta c th s dng vt cn theo phng chm: th mt 1h vit mt chng trnh
vt cn chy trong trong 4 ting, cn hn mt 4 ngy tm thut ton hiu qa
chng trnh chy trong 1 pht.
Chng ta khng cp k v vic p dng phng php vt cn i vi cc bi ton
n gin nh tm gi tr nh nht, ln nht hay tm tt c cc s nguyn t ca mt
tp hp. Chng ta s xem xt thut ton vt cn i vi cc bi ton tm cu hnh t
hp v bi ton ti u t hp, l lp cc bi ton rt tng qut v ph bin trong tin
hc.

8.2.1. Bi ton tm cu hnh t hp


C rt nhiu bi ton trong Tin hc c yu cu dng: tm cc i tng x tho mn
nhng iu kin nht nh trong mt tp S cc i tng cho trc. Bi ton tm cu
hnh t hp l bi ton yu cu tm cc i tng x c dng l mt vector tho mn
cc iu kin sau:
1. i tng x gm n phn t: x = (x1,x2,xn).
2. Mi phn t xi c th nhn mt trong cc gi tr ri rc a1, a2, am.
3. x tho mn cc rng buc c th cho bi hm logic G(x).
Tu tng trng hp m bi ton c th yu cu: tm mt nghim, tm tt c nghim
hoc m s nghim.
Trc ht chng ta nhc li mt s cu hnh t hp c bn.
a) T hp
Mt t hp chp k ca n l mt tp con k phn t ca tp n phn t.
Chng hn tp {1,2,3,4} c cc t hp chp 2 l: {1,2}, {1,3, {1,4, {2,3}, {2,4},
{3,4}. V trong tp hp cc phn t khng phn bit th t nn tp {1,2} cng l tp
{2,1} v do , ta coi chng ch l mt t hp.
Bi ton t ra cho chng ta l hy xc nh tt c cc t hp chp k ca tp n
phn t. n gin ta ch xt bi ton tm cc t hp ca tp cc s nguyn t 1

n n. i vi mt tp hu hn bt k, bng cch nh s th t ca cc phn t, ta


cng a c v bi ton i vi tp cc s nguyn t 1 n n.
Nghim cn tm ca bi ton tm cc t hp chp k ca n phn t phi tho mn cc
iu kin sau:
1. L mt vector x =(x1,x2,xk)
2. xi ly gi tr trong tp {1,2,n}
3. Rng buc: xi<xi+1 vi mi gi tr i t 1 n k-1.
C rng buc 3 l v tp hp khng phn bit th t phn t nn ta sp xp cc phn
t theo th t tng dn.
b) Chnh hp lp
Chnh hp lp chp k ca n l mt dy k thnh phn, mi thnh phn l mt phn t
ca tp n phn t, c xt n th t v khng yu cu cc thnh phn khc nhau.
Mt v d d thy nht ca chnh hp lp l cc dy nh phn. Mt dy nh phn
di m l mt chnh hp lp chp m ca tp 2 phn t {0,1}. Chng hn 101 l mt
dy nh phn di 3. Ngoi ra ta cn c 7 dy nh phn di 3 na l 000, 001,
010, 011, 100, 110, 111. V c xt th t nn dy 101 v dy 011 l 2 dy khc nhau.
Nh vy, bi ton xc nh tt c cc chnh hp lp chp k ca tp n phn t yu
cu tm cc nghim nh sau:
1. L mt vector x =(x1,x2,xk)
2. xi ly gi tr trong tp {1,2,n}
3. Khng c rng buc no gia cc thnh phn.
Ch l cng nh bi ton tm t hp, ta ch xt i vi tp n s nguyn t 1 n n.
Nu tp hp cn tm chnh hp khng phi l tp cc s nguyn t 1 n n th ta c
th nh s cc phn t ca tp a v tp cc s nguyn t 1 n n
c) Chnh hp khng lp
Khc vi chnh hp lp l cc thnh phn c php lp li, tc l c th ging nhau,
chnh hp khng lp chp k ca tp n phn t cng l mt dy k thnh phn ly t tp
n phn t c xt th t nhng cc thnh phn khng c php ging nhau.
Chng hn c n ngi, mt cch chn ra k ngi xp thnh mt hng l mt chnh
hp khng lp chp k ca n.

Mt trng hp c bit ca chnh hp khng lp l hon v. Hon v ca mt tp n


phn t l mt chnh hp khng lp chp n. Ni mt cch trc quan th hon v ca
tp n phn t l php thay i v tr ca cc phn t (do mi gi l hon v).
Nghim ca bi ton tm cc chnh hp khng lp chp k ca tp n s nguyn t
1 n n l cc vector x tho mn cc iu kin:
1. x c k thnh phn: x = (x1,x2,xk)
2. Cc gi tr xi ly trong tp {1,2,..n}
3. Rng buc: cc gi tr xi i mt khc nhau, tc l xixj vi mi ij.
l mt s bi ton tm cu hnh t hp c bn. Chng ta s xem xt mt s bi
ton khc thy tnh ph bin ca lp cc bi ton dng ny.
d) Bi ton xp hu
Cho bn c vua nxn. Hy xp n con hu ln bn c sao cho khng con no khng ch
con no. Hai 2 con hu khng ch nhau nu chng trn cng mt hng, mt ct
hoc mt ng cho.
Chng hn ta c mt cch t sau, cc en l cc v tr t hu:

chuyn bi ton ny v dng chun ca bi ton tm cu hnh t hp, ta c c nhn


xt: mi con hu phi trn mt hng v mt ct. Do ta coi con hu th i hng i
v nu bit x[i] l ct t con hu th i th ta suy ra c li gii. Vy nghim ca bi
ton c th coi l mt vector x gm n thnh phn vi ngha:
1. Con hu th i c t hng i v ct x[i].
2. x[i] ly gi tr trong tp {1,2n}
3. Rng buc: cc gi tr x[i] khc nhau tng i mt v khng c 2 con hu
trn cng mt ng cho.
Trong phn ci t, chng ta s phn tch chi tit v cc rng buc trn.

e) Bi ton t p (xu ABC)


Mt t p l mt xu di n ch gm cc k t A,B,C m khng c 2 xu con lin
tip no ging nhau. Chng hn ABAC l mt t p di 4, BABCA l mt t p
di 5.
Bi ton tm tt c cc t p di n cho trc yu cu tm nghim l cc vector x
c n thnh phn:
1. xi nhn gi tr trong tp {A,B,C}
2. x khng c 2 on con lin tip no bng nhau.
Trc khi trnh by v phng php vt cn gii cc bi ton tm cu hnh t hp,
chng ta xem xt cc bi ton ti u t hp, v cc bi ton ti u t hp thc cht l
s m rng ca bi ton tm cu hnh t hp.
8.2.2. Bi ton ti u t hp

Bi ton ti u tng qut c th pht biu nh sau: Cho tp B khc rng v mt hm


f:BR gi l hm mc tiu. Cn tm phn t x thuc B sao cho f(x) t gi tr nh
nht hoc ln nht. Phn t x l nghim ca bi ton cn c gi l phng n ti
u.
Bi ton ti u t hp l bi ton tm phng n ti u trn tp cc cu hnh t hp.
Nghim ca bi ton cng l mt vector x gm n thnh phn sao cho:
1. x = (x1,x2,xn)
2. xi ly gi tr trong tp {a1,a2,am}
3. x tho mn cc rng buc cho bi hm G(x).
4. f(x) min/max.
Chng ta s phn tch mt s bi ton ti u t hp in hnh. Phn ln u l cc bi
ton NPC.
a) Bi ton xp bal
C mt bal c ti trng m v n vt, vt i c trng lng wi v c gi tr vi.
Hy la chn cc vt cho vo bal sao cho tng trng lng ca chng khng qu
M v tng gi tr ca chng l ln nht.
Mi cch chn cc vt cho vo bal u tng ng vi mt vector x gm n thnh
phn m xi=1 nu chn a vt th i vo bal, v xi=0 nu vt th i khng c
chn.

Khi rng buc tng trng lng cc vt khng qu ti trng ca bal c vit
thnh:
n

x w
i

i =1

Hm mc tiu l tng gi tr ca cc vt c chn:


n

f ( x ) = x i v i max
i =1

Nghim ca bi ton cng l mt vector x gm n thnh phn sao cho:


1. x = (x1,x2,xn)
2. xi ly gi tr trong tp {0,1}
3. Rng buc:

x w
i

i =1

4. f ( x ) = x i v i max .
i =1

b) Bi ton ngi du lch

C n thnh ph, d[i,j] l chi ph di chuyn t thnh ph i n thnh ph j. (Nu


khng c ng i th d[i,j] = ). Mt ngi mun i du lch qua tt c cc thnh
ph, mi thnh ph mt ln ri tr v ni xut pht sao cho tng chi ph l nh nht.
Hy xc nh mt ng i nh vy.
Phng n ti u ca bi ton cng l mt vector x, trong xi l thnh ph s n
thm ti ln di chuyn th i. Cc iu kin ca x nh sau:
1. x = (x1,x2,xn)
2. xi ly gi tr trong tp {1,2,n}
3. Rng buc: xi xj vi mi ij v d[xi,xi+1]< vi mi i=1,2,..n, coi xn+1=x1.
4. f(x) =

d[ x , x
i =1

i +1

] min

Trn y ta xt mt s bi ton tm cu hnh t hp v bi ton ti u t hp.


Trong phn tip chng ta s tm hiu phng php vt cn gii cc bi ton .
8.2.3. Phng php vt cn gii cc bi ton cu hnh t hp v ti u t hp

Phng php vt cn l phng php rt tng qut n gin gii cc bi ton


cu hnh t hp v ti u t hp. tng c bn l: bng mt cch no sinh ra tt

c cc cu hnh c th ri phn tch cc cu hnh bng cc hm rng buc v hm


mc tiu tm phng n ti u (do phng php ny cn c gi l duyt ton
b).
Da trn tng c bn , ngi ta c 3 cch tip cn khc nhau duyt ton b
cc phng n.
Phng php th nht l phng php sinh tun t. Phng php ny cn xc nh
mt quan h th t trn cc cu hnh (gi l th t t in) v mt php bin i
bin mt cu hnh thnh cu hnh ngay sau n. Mi ln sinh c mt cu hnh th
tin hnh nh gi, so snh vi cu hnh tt nht ang c v cp nht nu cu hnh
mi tt hn.
Gi m ca thut ton tm cu hnh ti u bng phng php sinh nh sau:
Procedure Generate;
begin
x := FirstConfig;
best := x;
Repeat
x := GenNext(x);
if f(x) "tt hn" f(best) then best := x;
Until x = LastConfig;
end;

Thut ton thc hin nh sau: tm cu hnh u tin v coi l cu hnh tt nht.
Sau ln lt sinh cc cu hnh tip theo, mi ln sinh c mt cu hnh ta so snh
n vi cu hnh tt nht hin c (best) v nu n tt hn th cp nht best. Qu trnh
dng li khi ta sinh c cu hnh cui cng. Kt qu ta c phng n ti u l
best.
Phng php sinh tun t thng rt kh p dng. Kh khn ch yu l do vic xc
nh th t t in, cu hnh u tin, cu hnh cui cng v php bin i mt cu
hnh thnh cu hnh tip theo thng l khng d dng.
Phng php th hai l phng php th sai quay lui (Backtracking). T tng c
bn ca phng php l xy dng tng thnh phn ca cu hnh, ti mi bc xy
dng u kim tra cc rng buc v ch tip tc xy dng cc thnh phn tip theo
nu cc thnh phn hin ti l tho mn. Nu khng cn phng n no xy dng
thnh phn hin ti th quay li, xy dng li cc thnh phn trc .
Gi m ca thut ton quay lui nh sau.
procedure Backtrack;
begin
i := 1; x[1] := a0;
repeat
x[i] := next(x[i]);
if ok then Forward else Backward;
until i=0;

end;
procedure Forward;
begin
if i = n then Update
else begin
i := i + 1;
x[i] := a0;
end;
end;
procedure Backward;
begin
i := i 1;
end;
procedure Update;
begin
if f(x) "tt hn" f(best) then best := x;
end;

Trong on m ny, hm Ok kim tra cc thnh phn c sinh ra c tho mn cc


rng buc hay khng, cn hm Next tr li la chn tip theo ca mi thnh phn.
Nhn chung phng php quay lui lm gim ng k nhng kh khn ca phng
php sinh (khng cn tm th t t in v nht l khng cn tm quy tc sinh cu
hnh tip theo). Tuy nhin, trong mt s bi ton m cn nh du trng thi, phng
php quay lui khng quy c trnh by trn phi x l phc tp hn nhiu so
vi phng php quay lui quy.
Phng php quay lui quy l phng php n gin v tng qut nht sinh cc
cu hnh t hp. Do c ch cc b ho ca chng trnh con quy v kh nng quay
li im gi quy, thao tc quay lui tr thnh mc nh v khng cn x l mt
cch tng minh nh phng php quay lui khng quy.
M hnh c bn ca phng php quay lui quy nh sau:
Procedure Search;
begin
Try(1);
end;
procedure Try(i);
var j;
Begin
for j := 1 to m do
if <chn c a[j]> then begin
x[i] := a[j];
<ghi nhn trng thi mi>;
if i=n then Update
else Try(i+1);
<tr li trng thi c>;
end;
end;
procedure Update;
begin
if f(x) "tt hn" f(best) then best := x;
end;

duyt tan b cc cu hnh, ban u ta gi n Try(1). Try(1) s la chn cho x1


mt gi tr thch hp u tin, ghi nhn trng thi ri gi quy n Try(2). Try(2) li
la chn mt gi tr cho x2, ghi nhn trng thi v gi n Try(3). C nh vy bc
th i, thut ton tm mt gi tr cho xi, ghi nhn trng thi ri gi quy sinh
thnh phn xi+1. Khi sinh n thnh phn ca x th dng li cp nht phng n
ti u. Nu mi kh nng ca xi+1 u xt qua th vng for ca Try(i+1) thc hin
xong, theo c ch quy chng trnh s quay v im gi quy ca Try(i). Trng
thi c trc khi chn xi c phc hi v vng for ca Try(i) s tip tc chn gi
tr ph hp tip theo ca xi, chnh l thao tc quay lui. Khi quay lui v n Try(1)
v xt ht mi kh nng ca x1 th chng trnh con quy kt thc v ta duyt
c ton b cc cu hnh.
Trn y l cc thut ton vt cn i vi bi ton tm cu hnh ti u. Trong trng
hp bi ton cn tm mt cu hnh, tm mi cu hnh hay m s cu hnh th thut
ton cng tng t, ch khc phn cp nht (Update) khi sinh c mt cu hnh
mi.
Chng hn th tc Update i vi bi ton tm v m mi cu hnh s tng s cu
hnh v in ra cu hnh va tm c:
procedure Update;
begin
count := count + 1;
print(x);
end;

Chng ta s dng thut ton quay lui quy gii cc bi ton cu hnh t hp v
ti u t hp trnh by trn.
a) Sinh cc t hp chp k ca n

y l bi ton sinh t hp c chng ta trnh by phn trn. Ta s gii bng


thut ton tm cu hnh t hp bng quy quay lui.
V cu trc d liu ta ch cn mt mng x biu din t hp. Rng buc i vi gi
tr x[i] l: x[i1]< x[i] nk+i. Th tc quy sinh t hp nh sau:
procedure Try(i);
var j;
begin
for j := x[i1]+1 to nk+i do begin
x[i] := j;
if i=k then Print(x)
else Try(i+1);
end;
end;

Di y l ton vn chng trnh sinh t hp vit bng ngn ng Pascal. n


gin, cc gi tr n,k c nhp t bn phm v cc t hp c in ra mn hnh. Ngi
c c th ci tin chng trnh nhp/xut ra file.
program SinhTohop;
uses crt;
const
max = 20;
var
n,k : integer;
x : array[0..max] of integer;
{===============================}
procedure input;
begin
clrscr;
write('n,k = '); readln(n,k);
writeln('Cac to hop chap ',k,' cua ',n);
end;
procedure print;
var
i : integer;
begin
for i := 1 to k do write(' ',x[i]);
writeln;
end;
procedure try(i:integer);
var
j : integer;
begin
for j := x[i-1]+1 to n-k+i do begin
x[i] := j;
if i = k then Print
else try(i+1);
end;
end;
procedure solve;
begin
x[0] := 0;
try(1);
end;
{===============================}
BEGIN
input;
solve;
END.

Ch trong phn ci t l c khai bo thm phn t x[0] lm "lnh canh", v vng


lp trong th tc quy c truy cp n x[i1], v khi gi Try(1) th s truy cp n
x[0].
b) Sinh cc chnh hp lp chp k ca n

Xem li phn tch ca bi ton sinh chnh hp lp chp k ca n ta thy hon ton
khng c rng buc no i vi cu hnh sinh ra. Do , cu trc d liu ca ta ch
gm mt mng x lu nghim. Thut ton sinh chnh hp lp nh sau:

procedure Try(i);
var j;
begin
for j := 1 to n do begin
x[i] := j;
if i=k then Print(x)
else Try(i+1);
end;
end;

Di y l chng trnh sinh tt c cc dy nh phn di n. n gin, chng


trnh nhp n t bn phm v in cc kt qu ra mn hnh.
program SinhNhiphan;
uses crt;
const
max = 20;
var
n : integer;
x : array[1..max] of integer;
{===============================}
procedure input;
begin
clrscr;
write('n = '); readln(n);
writeln('Cac day nhi phan do dai ',n);
end;
procedure print;
var
i : integer;
begin
for i := 1 to n do write(' ',x[i]);
writeln;
end;
procedure try(i:integer);
var
j : integer;
begin
for j := 0 to 1 do begin
x[i] := j;
if i = n then Print
else try(i+1);
end;
end;
procedure solve;
begin
try(1);
end;
{===============================}
BEGIN
input;
solve;
END.

c) Sinh cc chnh hp khng lp chp k ca n

Chnh hp khng lp yu cu cc phn t phi khc nhau. m bo iu , ngoi


mng x, ta s dng thm mt cu trc d liu na l mng d nh du. Khi mt gi

tr c chn, ta nh du gi tr , v khi chn, ta ch chn cc gi tr cha nh


du. Mng d s l "trng thi" ca thut ton. Bn c xem phn gi m di y
thy r hn tng .
procedure Try(i);
var j;
begin
for j := 1 to n do
if d[j]=0 then begin
x[i] := j; d[j] := 1;
if i=k then Print(x)
else Try(i+1);
d[i] := 0;
end;
end;

Chng trnh di y s sinh ton b cc hon v ca tp n s nguyn t 1 n n.


Gi tr n c nhp t bn phm v cc hon v c in ra mn hnh.
program SinhHoanvi;
uses crt;
const
max = 20;
var
n : integer;
x,d : array[1..max] of integer;
{===============================}
procedure input;
begin
clrscr;
write('n = '); readln(n);
writeln('Cac hoan vi cua day ',n);
end;
procedure print;
var
i : integer;
begin
for i := 1 to n do write(' ',x[i]);
writeln;
end;
procedure try(i:integer);
var
j : integer;
begin
for j := 1 to n do
if d[j] = 0 then begin
x[i] := j; d[j] := 1;
if i = n then Print
else try(i+1);
d[j] := 0;
end;
end;
procedure solve;
begin
try(1);
end;
{===============================}
BEGIN

input;
solve;
END.

d) Bi ton xp hu

Khc vi nhng bi ton sinh cc cu hnh n gin phn trc, sinh cc cu hnh
ca bi ton xp hu i hi nhng phn tch chi tit hn v cc iu kin rng buc.
Rng buc th nht l cc gi tr x[i] phi khc nhau. Ta c th dng mt mng nh
du nh thut ton sinh hon v m bo iu ny.
Rng buc th 2 l cc con hu khng c nm trn cng mt ng cho chnh v
ph. Cc bn c th d dng nhn ra rng 2 v tr (x1,y1) v (x2,y2) nm trn cng
ng cho chnh nu:
x1y1=x2y2=const.
Tng t, 2 v tr (x1,y1) v (x2,y2) nm trn cng ng cho ph nu:
x1+y1=x2+y2=const
Do , con hu i t ti v tr (i,x[i]) v con hu j t ti v tr (j,x[j]) phi tho mn
rng buc:
ix[i] jx[j] v i+x[i] j+x[j] vi mi ij
Ta c th vit ring mt hm Ok kim tra cc rng buc . Nhng gii php tt
hn l dng thm cc mng nh du m t rng mt ng cho chnh v ph
c mt con hu khng ch. Tc l khi ta c con hu i v tr (i,j), ta s nh du
ng cho chnh i-j v ng cho ph i+j.
Nh vy v cu trc d liu, ta dng 4 mng:
1. Mng x vi ngha: x[i] l ct ta s t con hu th i.
2. Mng cot vi ngha: cot[j]=1 nu ct j c mt con hu c t, ngc li
th cot[j]=0.
3. Mng dcc vi ngha: dcc[k]=1 nu ng cho chnh th k c mt con
hu c t, tc l ta t mt con hu ti v tr (i,j) m ij=k; ngc li th
dcc[k]=0.
4. Tng t ta dng mng dcp vi ngha: dcp[k]=1 nu ng cho ph th k
c mt con hu c t.
Gi m ca thut ton xp hu nh sau:
procedure Try(i);
var j;
begin

for j := 1 to n do
if (cot[j]=0) and (dcc[i-j]=0) and (dcp[i+j]=0) then begin
x[i] := j;
cot[j]:=1; dcc[i-j]:=1; dcp[i+j]:=1; {ghi nhn trng thi mi}
if i=n then Update
else Try(i+1);
cot[j]:=0; dcc[i-j]:=0; dcp[i+j]:=0; {phc hi trng thi c}
end;
end;
procedure Update;
begin
count := count + 1;
print(x);
end;

Phn di l ton b chng trnh tm cc phng n xp hu trn bn c 8x8.


Chng trnh tm c 92 phng n khc nhau.
e) Bi ton t p

Tt c cc bi ton ta gii trn u c cu hnh c thnh phn l cc s nguyn.


Ring bi ton t p th cn tm cu hnh l mt xu. Ta c th dng mt mng k t
thay th, tuy nhin iu khng cn thit v ngn ng Pascal cng c kh nng
x l xu k t rt tt.
M hnh quay lui ca bi ton t p c th vit nh sau:
procedure Try(i)
var c;
begin
for c := 'A' to 'C' do begin
x := x + c;
if Ok(i) then
if i=n then Update
else Try(i+1);
delete(x,i,1);
end;
end;
procedure Update;
begin
count := count + 1;
print(x);
end;

Cc th tc Try, Update kh tng t cc bi ton khc. Ring vit hm Ok kim


tra la chn hin ti cho x[i] c ph hp khng, chng ta phn tch su hn nh sau:
Trc ht ta thy rng khi la chn n x[i] th xu x[1..i-1] tho mn tnh cht
ca t p. Nh vy nu x[1..i] khng tho mn tnh cht ca t p th ch c kh
nng l do k t th i mi c chn khng ph hp. Vy hm Ok(i) ch cn kim tra
cc xu con c cha x[i] c ging mt xu con lin k trc n hay khng? Nu c
th gi tr x[i] khng tho mn v ta phi chn gi tr khc. Ngc li nu gi tr
x[i] tho mn th ta cp nht kt qu hoc quy tip tu thuc vo vic ta chn
n k t cha.

Hm Ok c th vit nh sau:
function Ok(l)
begin
Ok := false;
for k := 1 to l div 2 do
if copy(x,l-k+1,k) = copy(x,l-2*k+1,k) then exit;
Ok := true;
end;

Nu c gi thy hm Ok kh hiu th chng ti c th gii thch nh sau: ta cn


kim tra mi xu con c cha k t cui cng c bng xu con lin k trc n hay
khng? di xu ang c l l, do cc xu con c cha k t th l c kh nng
bng xu lin k trc n ch c di t 1 n l/2. Biu thc copy(x,l-k+1,k) cho
kt qu l xu con gm k k t cui cng ca x v biu thc copy(x,l-2*k+1,k) cho
xu con k k t ngay trc xu con c k k t cui cng.
Phn ci t chng trnh c th xin dnh cho c gi. Phn tip theo chng ti xin
cp n bi ton ti u t hp.
f) Bi ton ngi du lch.

c gi d dng nhn thy mi phng n ca bi ton ngi du lch l mt hon v


ca n thnh ph. Do ta c th dng m hnh vt cn ca bi ton sinh hon v
tm cc phng n. V ta s dng thm rng buc: d[xi-1,xi]<. Mt khc v phng
n l mt chu trnh nn ta c th coi thnh ph xut pht l thnh ph 1.
Thut gii bi ton ngi du lch bng vt cn nh sau:
procedure Search;
begin
min := ;
x[1] := 1; dd[1] := 1;
try(2);
end;
procedure Try(i)
var j;
begin
for j := 1 to n do
if (dd[j]=0) and (d[x[i-1],j] < ) then begin
x[i] := j; dd[j] := 1;
if i=n then Update
else Try(i+1);
dd[j] := 0;
end;
end;
procedure Update;
var s,i;
begin
s := d[x[n],1];
for i := 1 to n-1 do s := s + d[x[i],x[i+1]];
if s < min then begin
min := s;
best := x;
end;

end;

Lp cc bi ton ti u t hp rt rng. Phn ln cc bi ton trong trng hp


tng qut ch c thut ton ti u duy nht l vt cn. Tuy nhin, nhc im ca
phng php vt cn l phc tp tnh ton rt ln do hin tng bng n t hp.
Cc bn nh li rng s hon v ca tp n phn t l n!. Do trong trng hp xu
nht thut ton vt cn i vi bi ton ngi du lch l O(n!).
C 2 gii php khc phc vn ny. Gii php th nht ci tin phng php vt
cn bng k thut nhnh cn, tc l loi b ngay cc hng i chc chn khng dn
n phng n ti u. Gii php th 2 l s dng cc phng php khc, m hai
phng php ni bt nht l phng php quy hoch ng v phng php tham lam.
Phn tip theo, chng ti s trnh by s lc v k thut nhnh cn.
8.2.4. K thut nhnh cn

Nguyn nhn dn n phc tp ca cc bi ton ti u t hp l hin tng bng


n t hp. l hin tng s cu hnh t hp tng theo hm m i vi s thnh
phn t hp n. n gin nht l cc dy nh phn, mi thnh phn t hp ch c 2 kh
nng l 0 v 1 th s cc dy nh phn di n l 2n. Do vic sinh ton b cc
cu hnh t hp s khng kh thi khi n ln.
Qu trnh vt cn kiu quay lui l mt qu trnh tm kim phn cp, tc l cc thnh
phn x1, x2 s c chn trc. Nu ti bc i ta chn mt gi tr xi khng ti u
th ton b qu trnh chn xi+1, xi+2 s hon ton v ngha. Ngc li, nu ta xc
nh c rng gi tr xi khng dn n cu hnh ti u th ta s tit kim c
ton b cc bc chn xi+1, xi+2 Tit kim i khi l ng k. Chng hn nu
i vi bi ton duyt nh phn (ti u cc cu hnh l dy nh phn) ta xc nh c
x1=0 khng hp l th ta tit kim c 2n-1 bc duyt pha sau. chnh l t
tng ca phng php nhnh cn.
M hnh quay lui c nhnh cn nh sau:
Procedure Search;
begin
Try(1);
end;
procedure Try(i);
var j;
Begin
for j := 1 to m do
if <chn c a[j]> then begin
x[i] := a[j];
<ghi nhn trng thi mi>;
if i=n then Update
else
if Ok(i) then Try(i+1);
<tr li trng thi c>;

end;
end;

Ci tin so vi phng php vt cn thun tu l cu lnh if Ok(i) then Try(i+1);. Hm


Ok y c dng nh gi tnh trng ca cu hnh hin ti. Th nht l c m
bo dn n cu hnh ti u hay khng. Nu khng th t nht cng phi m bo cho
gi tr hm mc tiu tt hn phng n tt nht ta ang c.
K thut nhnh cn rt a dng, ph thuc vo tng bi ton v t duy ca ngi lp
trnh. Chng ta s xem xt mt s bi ton ti u gii bng phng php nhnh cn.
u tin l bi ton ngi du lch. Ta c nhn xt: ti ln di chuyn th i, nu tng
chi ph ang c chi ph ca phng n tt nht ta ang c th r rng vic i tip
khng mang n kt qu tt hn. Do ta c th t mt nhnh cn n gin nh
sau:
procedure Try(i)
var j;
begin
for j := 1 to n do
if (dd[j]=0) and (d[x[i-1],j] < ) then begin
x[i] := j; dd[j] := 1; s := s + d[x[i-1],j];
if i=n then Update
else
if s < min then Try(i+1);
dd[j] := 0; s := s - d[x[i-1],j];
end;
end;

Hai bin s, min l cc bin ton cc, trong min dng lu chi ph ca phng n
tt nht cn s lu tng chi ph hin ti.
Ta c th tip tc ci tin cn ny bng vic khng ch xt chi ph n thi im hin
ti m cn xt lun c chi ph ti thiu kt thc hnh trnh. Gi dmin l gi tr nh
nht ca bng d, tng ng vi chi ph nh nht ca vic di chuyn t thnh ph
ny n thnh ph kia. Ti bc th i th ta cn phi thc hin ni+1 bc di chuyn
na th mi kt thc hnh trnh (i qua ni thnh ph cn li v quay v thnh ph 1).
Do chi ph ca c hnh trnh s ti thiu l s + (ni+1)*dmin. Nu chi ph ny cn
ln hn chi ph ca phng n tt nht th r rng la chn hin ti cng khng th
dn n mt phng n tt hn. Chng trnh con vt cn quy c th sa thnh:
procedure Try(i)
var j;
begin
for j := 1 to n do
if (dd[j]=0) and (d[x[i1],j] < ) then begin
x[i] := j; dd[j] := 1; s := s + d[x[i1],j];
if i=n then Update
else
if s + (ni+1)*dmin < min then Try(i+1);
dd[j] := 0; s := s d[x[i1],j];
end;

end;

Nhn chung nhng cn c ci thin tnh hnh i cht nhng cng khng thc s hiu
qu. Ngi ta cng nghin cu nhiu cn cht hn, c gi c th tm c cc ti
liu khc.
Ta xt tip bi ton t p nht. nh ngha t p c m t bi ton t p.
T p nht l t c t k t C nht. R rng bi ton tm t p nht l mt bi ton
ti u t hp.
Chng ta xy dng nhnh cn vi nhn xt: nu x[1..n] l t p th trong 4 k t lin
tip ca x phi c t nht mt k t C. Vy, nu ta xy dng i k t th phn cn li
gm ni k t s c t nht (ni)/4 k t C. Do s k t C ti thiu ca c xu s l t
+ (ni)/4, trong t l s k t C ca x[1..i]. Ta c th dng t+(ni)/4 lm cn.
Chng trnh con quy nh sau:
procedure Try(i)
var c;
begin
for c := 'A' to 'C' do begin
x := x + c;
if c = 'C' then t := t + 1;
if Ok(i) then
if i=n then Update
else
if t + (n-i) div 4 < minC then Try(i+1);
delete(x,i,1);
if c = 'C' then t := t - 1;
end;
end;

Bin minC y dng lu s k t C ca phng n tt nht ang c.


Nhnh cn l mt k thut mnh v i hi t duy su sc. Chn c mt cn tt
thng khng n gin, i hi phi c nhng phn tch su sc v t m. Mt s ch
khi chn cn:
1. Cn phi nh gi chnh xc tnh trng cu hnh hin ti. Nu qu lng th s
cu hnh loi b khng ng k, nu qu cht th s dn n b st nghim.
2. Cn phi tnh ton n gin. V thao tc tnh cn thc hin ti tt c cc bc
nn nu tnh ton cn qu phc tp th thi gian rt ngn nh t cn tit kim
c th li mt ng k cho vic tnh cn.
Mc d nhnh cn l k thut mnh nhng mun p dng tt i hi nhng phn
tch rt chi tit. Hn na nhiu trng hp c t cn th s phng n cn duyt vn
qu nhiu. Trong nhng trng hp nh vy chng ta cn phi c nhng cch tip
cn khc. Phn tip theo trnh by v mt phng php cc k hiu qu, l phng
php quy hoch ng.

8.3. Phng php quy hoch ng (Dymnamic plan)


Quy hoch ng l mt phng php rt hiu qu gii cc bi ton ti u t hp.
tng c bn ca phng php ny l: c li gii ca bi ton ti u kch thc n,
ta gii cc bi ton tng t c kch thc nh hn v phi hp li gii ca chng
c li gii ca bi ton ban u. chnh l t tng chia tr mt cch rt
nghim ngt.
Khng phi bi ton no cng c th gii bng quy hoch ng. gii c bng
quy hoch ng, bi ton tho mn nguyn l ti u Bellman: mi dy con ca mt
dy ti u cng phi l dy ti u.
Thng thng hm mc tiu ca bi ton c xy dng t mt hm c dng: f(n) =
max(f(k)+g(n)), trong k l mt s gi tr ph hp nh hn n.
Hm f(n) c gi l hm quy hoch ng. Vic tnh gi tr hm f c hin t di
ln, tc l cc gi tr n nh c tnh trc. Tt c cc kt qu c lu vo bng
phc v cho vic tnh hm quy hoch ng vi cc gi tr n ln hn.
Chng ta s xem xt mt s bi ton quy hoch ng tiu biu minh ho cho cc
t tng trn.
8.3.1. Dy con n iu di nht

Cho dy a1,a2,..an l dy n s nguyn. Xo i mt s phn t ca dy trn v gi


nguyn trnh t ca cc phn t cn li th ta c mt dy con ca dy ban u. Bi
ton yu cu l xo i mt s t nht cc phn t dy con thu c l dy n iu
(chng hn n iu tng). Yu cu ny tng ng vi yu cu chn ra mt s
nhiu nht cc phn t ca dy dy thu c (gi nguyn th t) l mt dy n
iu tng.
Chng hn dy con tng di nht ca dy 7 s hng sau:
1354267
L dy con 1 3 5 6 7 c c t vic xo 2 phn t 4 v 2 i.
gii bi ton ny bng phng php quy hoch ng, ta trc ht phi xy dng
hm quy hoch ng.
Gi L(i) l di dy con n iu tng ln nht, c cc phn t ly trong min t a1
n ai v phn t cui cng l ai.
Ta c cng thc quy hoch ng nh sau:
1. L(1) = 1

2. L(i) = max(1, L(j)+1 vi mi j<i v ajai).


Chng ta gii thch cng thc nh sau:
L(1)=1 l hin nhin.
Nu ta khng ghp ai vo dy no th L(i)=1. Nu ta ghp ai vo mt dy no th
phi c mt phn t j ng trc i, tho mn ajai. Dy con di nht c phn t cui
cng l aj c L(j) phn t, vy ghp thm ai s c dy con c phn t cui cng l
ai v c L(j)+1 phn t.
Trong tt c cc la chn , ta chn la chn ti u nht tnh L(i). l nguyn
nhn v sao ta c hm max.
ci t thut ton ta dng cu trc d liu l mt mng 1 chiu L, L[i] dng lu
tr gi tr tng ng ca L(i). S khc bit gia phng php quy hoch ng v chia
tr t trn xung l im ny: cc bi ton nh c gii trc, v kt qu c
lu tr li gii cc bi ton ln hn. Cu trc d liu dng lu tr cc kt qu
gi l bng phng n.
procedure Optimize;
var i,j;
begin
L[1] := 1;
for i := 2 to n do begin
L[i] := 1;
for j := 1 to i-1 do
if (a[j] <= a[i]) and (L[i] < L[j] + 1) then
L[i] := L[j] + 1;
end;
end;

Sau khi tnh xong hm quy hoch ng, lm th no tm li kt qu? C 2 phng


n. Phng php th nht l da vo bng phng n. Phng php th hai l xy
dng bng truy vt.
Da trn bng phng n ta s tm c phn t c L[i] ln nht. chnh l phn t
cui cng ca dy kt qu. Phn t ng ngay trc n s l phn t j m aj<ai v
L[i]=L[j]+1. Tm c phn t j , ta li tm c phn t ng ngay trc n bng
phng php tng t. Th tc ln vt tm li kt qu nh sau:
procedure Trace;
begin
i := 1;
for j := 2 to n do
if L[i] < L[j] then i := j;
for k := L[i] downto 1 do begin
kq[k] := i;
for j := 1 to i do
if (a[j]<=a[i]) and (L[i]=L[j]+1) then begin
i := j;
break;
end;

end;
end;

Phng php th hai l dng mt cu trc d liu na truy vt. Ta t T[i] l phn
t ng ngay trc i trong dy. Chng trnh con quy hoch ng c vit li nh
sau:
procedure Optimize;
var i,j;
begin
L[1] := 1;
for i := 2 to n do begin
L[i] := 1; T[i] := 0;
for j := 1 to i-1 do
if (a[j] <= a[i]) and (L[i] < L[j] + 1) then begin
L[i] := L[j] + 1;
T[i] := j;
end;
end;
end;

Qu trnh truy vt ca chng ta do s n gin hn:


procedure Trace;
var i,j;
begin
i := 1;
for j := 2 to n do
if L[i] < L[j] then i := j;
for k := L[i] downto 1 do begin
kq[k] := i; i := T[i];
end;
end;

D dng chng minh c thut ton ny c phc tp tnh ton O(n2) v chi ph
khng gian l O(n).
8.3.2. Xu con chung di nht

Cho 2 xu X,Y. Hy tm mt xu S tho mn:


1. S c th nhn c t X,Y bng cch xo i mt s k t (tc l S l xu con
ca X v ca Y).
2. di ca S l ln nht.
Trc ht ta xy dng hm quy hoch ng:
Gi L(i,j) l di xu con chung di nht ca xu X(i) gm i k t phn u ca X
(X(i) = [1..i]) v xu Y(j) gm j k t phn u ca Y (Y(j) = [1..j]).
Ta c cng thc quy hoch ng nh sau:
1. L(0,j)=L(i,0)=0.
2. L(i,j) = L(i1,j1)+1 nu X[i] = Y[j].

3. L(i,j) = max(L(i1,j), L(i,j1)) nu X[i] Y[j].


Cng thc 1. l hin nhin. Cng thc 2 v 3 c hiu nh sau: Nu X[i]=Y[j] th ta
s chn ngay cp phn t , on cn li ca 2 xu l X[1..i1] v Y[1..j1] c xu
con chung di nht L(i1,j1) phn t, vy X(i) v Y(j) c xu con chung di nht
L(i1,j1)+1.
Ngc li, nu X[i] Y[j] th ta c 2 gii php: hoc b qua X[i] v so X(i-1) vi
Y(j). Cch cho xu con di nht L(i-1,j) phn t. Ngc li, ta c th b qua Y[j]
v so X(i) vi Y(j-1) c xu con di nht L(i,j-1) phn t. Trong 2 cch chn ta s
chn cch ti u hn.
Bng phng n ca ta s l mt bng 2 chiu L[0..m,0..n], vi n,m ln lt l di
ca X v Y. Chng trnh con tnh bng phng n nh sau:
procedure Optimize;
var i,j;
begin
for i := 0 to m do L[i,0] := 0;
for j := 0 to n do L[0,j] := 0;
for i := 1 to m do
for j := 1 to n do
if X[i]=Y[j] then L[i,j] := L[i-1,j-1]+1
else
L[i,j] := max(L[i-1,j],L[i,j-1]]);
end;

ln vt tm nghim, ta da trn bng phng n. Xu con chung ln nht ca X,Y


c di L[m,n]. Da vo tng quan gi tr gia L[i,j], L[i1,j], L[i,j1] v
L[i1,j1] ta s bit trong qu trnh quy hoch ng ta chn hng i no:
procedure Trace;
begin
i := m; j := n;
repeat
if X[i]=Y[j] then begin
kq[i] := 1;
i:=i-1; j:=j-1;
end
else
if L[i,j]=L[i-1,j] then i:=i-1
else j:=j-1;
until (i=0) or (j=0);
S := '';
for i:=1 t m do
if kq[i]=1 then S:=S+X[i];
end;

D dng kim tra thut ton quy hoch ng tm xu con chung di nht c phc
tp tnh ton l O(n2) v i hi khng gian b nh cng l O(n2).

Qua hai v d trn, chc bn c phn no nhn thy s hiu qu ca phng php
quy hoch ng. Chng ta s tm hiu tip mt s bi ton na nhn thy sc
mnh ca quy hoch ng so vi cc phng php khc.
8.3.3. Bi ton xp bal

Trong phn ni v phng php vt cn, chng ti cp n bi ton xp bal


nhng cha trnh by thut gii. Dng ca chng ti l mun gii bi ton bng
phng php quy hoch ng. Bn c d dng kim chng c, bi ton xp bal
m gii bng vt cn th s c phc tp tnh ton O(2n). Trong khi gii bng
quy hoch ng ch c phc tp l O(n.m) m thi.
Tht vy, gi F(i,j) l gi tr ln nht thu c khi ta c chn i vt t 1 n i
xp vo bal c trng ti j. D thy l F(0,j) = F(i,0)=0. Nu i,j>0 th ta thy:
- trng hp th nht: j<wi. Khi ta khng th a vt i vo bal, v gi tr ln nht
thu c s l chn i-1 vt cn li v trng ti bal vn l j, tc l F(i,j)=F(i-1,j).
- trng hp th hai: j>=wi. Trong tnh hung ny ta c th chn vt i a vo
bal hoc khng. Nu khng chn th F(i,j)=F(i-1,j) nh trng hp trn. Nu chn
th F(i,j)=F(i-1,j-wi) + vi. Cng thc ny rt d hiu, v khi ta chn vt i th ti trng
ca bal cn li l j-wi, ta cn i-1 vt na chn nn gi tr ln nht thu c l F(i1,j-wi) + vi. Trong 2 cch ta s chn cch tt hn. Vy F(i,j)=max(F(i-1,j-wi) + vi,
F(i-1,j)).
Cng thc quy hoch ng c th tm tt li nh sau:
1. F(0,j) = F(i,0)=0
2. F(i,j) = F(i-1,j) nu j<wi
3. F(i,j)=max(F(i-1,j-wi) + vi, F(i-1,j)) nu j>=wi.
Chng trnh con tnh bng phng n s thc hin tnh cc gi tr i,j t nh n ln.
C th nh sau:
procedure Optimize;
begin
for i := 0 to n do F[i,0]:=0;
for j := 0 to m do F[0,m]:=0;
for i := 1 to n do begin
for j := 1 to m do
if j<w[i] then F[i,j] := F[i-1,j]
else
F[i,j] := max(F[i-1,j], F[i-1,j-w[i]]+v[i]);
end;
end;

Sau qu trnh tnh ton, gi tr ln nht thu c khi c chn n vt vi bal


trng ti m s l F[n,m]. Qu trnh ln vt da vo tng quan gia F[i,j], F[i-1,j] v

F[i-1,j-w[i]]+v[i] s xc nh c cc vt c chn. c gi c th d dng xy


dng chng trnh con truy vt v chng minh c thut gii quy hoch ng ny
c phc tp tnh ton l O(n.m). Chi ph b nh cng l O(n.m).
Trong phn sau trnh by v nhng ci tin i vi quy hoch ng, chng ti s trnh
by nhng cch ci t cc thut ton trn vi chi ph b nh thp hn. Trc khi kt
thc, chng ti s trnh by mt bi ton quy hoch ng kinh in na, l bi ton
nhn ma trn.
8.3.4. Bi ton nhn ma trn

T cch tnh nhn ma trn cho thy: nhn mt ma trn kch thc mn vi mt ma
trn np, s php nhn phi thc hin l m.n.p. Mt khc php nhn cc ma trn c
tnh kt hp, tc l:
(A.B).C = A.(B.C)
Do khi tnh tch nhiu ma trn, ta c th thc hin theo cc trnh t khc nhau.
Vic la chn trnh t tnh s quyt nh s php nhn cn thc hin.
V d ta c 4 ma trn: A(13,5), B(5,89), C(89,3), D(3,34) th ta c mt s cch tnh
A.B.C.D theo cc trnh t khc nhau nh sau:
1. Tnh theo trnh t (((A.B).C).D) th s php nhn l 10592.
2. Tnh theo trnh t ((A.B).(C.D)) th s php nhn l 54201.
3. Tnh theo trnh t ((A.(B.C)).D th s php nhn l 2856.
4. Tnh theo trnh t A.((B.C).D) th s php nhn l 4055.
5. Tnh theo trnh t A.(B.(C.D)) th s php nhn l 26418.
Php nhn (nht l cc php nhn s thc) thng c thi gian thc hin lu hn
php cng, tr. Do ngi ta t ra bi ton:
Cho n ma trn A1,A2An, ma trn Ai c kch thc l di-1di. Hy xc nh trnh t
nhn ma trn A1.A2An sao cho s php nhn cn thc hin l t nht.
Nu dng phng php vt cn gii bi ton ny th chng ta s xt mi cch t
cc du ngoc khc nhau tm s php nhn ca tng cch t, t tm c cch
t ti u. Gi T(n) l s cch t du ngoc vi n ma trn th ta d dng chng minh
c cng thc tnh T(n) nh sau:
N 1

T (n ) = T(i).T(n i)
i =1

Dy T(n) c gi l dy Catalan, bng sau cho mt s phn t ca dy:

n
T(n)

1
1

2
1

3
2

4
5

5
14

10
4862

15
2674440

D thy l T(n) tng rt nhanh theo n. Ngi ta chng minh c T(n) c 4n/n. Kt
lun l gii bng phng php vt cn th ch gii c vi nhng gi tr n rt b.
ng ngc nhin l ta c th gii bi ton nhn ma trn bng phng php quy hoch
ng vi phc tp tnh ton ch l O(n3).
Gi F(i,j) l s php nhn tnh tch Ai.Ai+1....Aj th ta thy:
Nu i=j th ch c mt ma trn do F(i,i)=0.
Nu j=i+1 th ta ch phi nhn Ai v Ai+1. Ai c kch thc di-1di, Ai+1 c kch thc
didi+1, do F(i,i+1)=di-1.di.di+1.
Nu j>i+1 th ta thy c th tnh Ai.Ai+1....Aj cch chn mt v tr k no t
ngoc theo trnh t:
Ai.Ai+1....Aj = (Ai..Ak).(Ak+1..Aj)
Ma trn kt qu ca php nhn (Ai..Ak) c kch thc di-1dk, ma trn kt qu ca
php nhn (Ak+1..Aj) c kch thc dkdj. Vi cch t ta s mt F(i,k) php nhn
c kt qu trong du ngoc th nht, mt thm F(k+1,j) php nhn c kt qu
trong du ngoc th hai, v cui cng mt di-1.dk.dj nhn 2 ma trn kt qu . T
tng s php nhn ca cch t l:
F(i,k)+F(k+1,j)+di-1.dk.dj
C nhiu cch chn k : k=i+1,i+2,j-1. Ta chn cch cho php s php nhn t nht:
F(i,j) = min(F(i,k)+F(k+1,j)+di-1.dk.dj) vi mi k=i+1,i+2,...j-1.
Tm li ta c cng thc quy hoch ng nh sau:
1. F(i,i)=0
2. F(i,i+1)=di-1.di.di+1
3. F(i,j) = min(F(i,k)+F(k+1,j)+di-1.dk.dj, k=i+1,i+2,...j-1)
c cng thc quy hoch ng, ta s chn cu trc d liu l mt bng 2 chiu F
lu F(i,j). Tuy nhin c mt ch khi ci t l tnh c F(i,j), ta phi tnh F(i,k)
v F(k+1,j) trc. Nh vy trnh t tnh khng phi l tnh cc gi tr F(i,j) vi i,j t
nh n ln. Ta c nhn xt l trong cng thc trn j-i ln hn k-i v j-k. Do nu
ta tnh cc phn t F(x,y) vi y-x t nh n ln th khi tnh F(i,j), ta c F(i,k) v
F(k+1,j).
procedure Optimize;
begin
for i := 1 to n do

F[i,i] := 0;

for i := 1 to n-1 do
F[i,i+1] := d[i-1]*d[i]*d[i+1];
for m := 2 to n-1 do begin
for i := 1 to n-m do begin
j := i + m; F[i,j] := ;
for k := i+1 to j-1 do
F[i,j] := min(F[i,j], F[i,k]+F[k+1,j]+d[i-1]*d[k]*d[j]);
end;
end;
end;

Kt qu ca bi ton l F[1,n]. ln vt ta c th da trn bng phng n hoc t


ring mt bng truy vt: T[i,j] s l ch s k m F[i,j] t min. Khi ta bit rng
t cp du ngoc (AiAk) v (Ak+1Aj). Ta li da vo T[i,k] v T[k+1,j] cch
t du ngoc cc ma trn bn trong.
8.3.4. Ci tin phng php quy hoch ng

Chng ta nhn thy sc mnh v tnh hiu qu ca phng php quy hoch ng.
Tuy nhin khng c phng php no l hon ho. Phng php quy hoch ng c
mt s nhc im ln nh sau:
1. Khng phi bi ton no cng gii c bng quy hoch ng. Hin nay ngi ta
vn cha tm c iu kin cn v mt bi ton c th gii c bng phng
php quy hoch ng. Do xc nh c mt bi ton c th gii c bng
quy hoch ng v tm c cng thc quy hoch ng ca n thng l vic rt kh
khn.
2. Nu bi ton c th gii c bng quy hoch ng th yu cu v khng gian nh
cng kh ln. a s cc bi ton u c i hi khng gian b nh c O(n2). Nu mi
trng lp trnh hn ch b nh th s khng th lm vic c vi cc d liu ln,
mc d phng php quy hoch ng c phc tp tnh ton khng cao.
Nhc im 1 nhn chung l kh gii quyt. Ch c cch c gng a bi ton ang
xt v mt m hnh no c chng minh l lm c bng quy hoch ng v
tm c cng thc quy hoch ng. Chng hn: m hnh dy con n iu tng
di nht, xu con chung, xp bal, nhn ma trn
Nhc im 2 th c th gii quyt d dng hn. Ch vo cng thc quy hoch
ng, ta thng thy F[n] thng ch phi tnh qua F[n-1]. Do ta khng cn phi
lu tr c bng phng n m ch cn lu mt s v tr cn thit. Nu bi ton c yu
cu truy vt tm nghim th ta c th dng bng truy vt c kch thc thng l nh
hn bng phng n.
Chng hn ta c k thut sau gii bi ton xu con chung di nht cho 2 xu c di
c 1000. (Ch , xu trong TP ch c di ti a 255 nn trong trng hp ny ta
phi dng mng k t lu tr 2 xu).

Nhc li cng thc quy hoch ng ca bi ton:


1. L(0,j)=L(i,0)=0.
2. L(i,j) = L(i1,j1)+1 nu X[i] = Y[j].
3. L(i,j) = max(L(i1,j), L(i,j1)) nu X[i] Y[j].
Nh vy tnh L(i) ta ch cn L(i-1). Do tnh c bng phng n, ta ch cn
lu tr hai dng ca n: L lu L(i) v L1 lu L(i-1). Chng trnh con quy
hoch ng c vit li nh sau:
procedure Optimize;
var i,j;
begin
for j := 0 to n do L1[j] := 0;
{L(0,j):=0}
for i := 1 to m do begin
L[0]:=0;
for j := 1 to n do
if X[i]=Y[j] then L[j] := L1[j-1]+1 {L(i,j)=L(i-1,j-1)+1}
else
L[j]:=max(L1[j],L[j-1]]);
{L(i,j)=max(L(i1,j), L(i,j1))}
L1 := L;
end;
end;

Nu ch yu cu tm di xu con chung di nht (L[m,n]) th vi cch ny ta c th


gii vi kch thc m,n=10000 cng c. Nu bi ton yu cu tm li kt qu th ta
phi dng thm mt bng truy vt 2 chiu. Khi mi phn t ca bng truy vt ch
cn lu 1 trong 3 gi tr: T[i,j]=1 nu L(i,j)=L(i-1,j-1)+1, T[i,j]=2 nu L(i,j)=L(i-1,j)
v T[i,j]=3 nu L(i,j)=L(i,j-1). Do kch thc bng truy vt nh hn nhiu so vi
bng phng n. (Ta ch cn 2 bit cho mt phn t ca bng truy vt nn kch thc
bng truy vt l 1000x1000x2bit=2.000.000 bit, tc l khong 250KB. Trong khi
bng phng n 2 chiu c kch thc l 1000x1000x2byte=2.000.000 byte, tc l
ln hn 8 ln).
Chi tit ci t c th, c gi c th xem phn ph lc.
K thut ny c th p dng c bi ton xp bal.
8.3.5. Phng php quy hoch ng vi bi ton m cu hnh t hp

Bi ton m cu hnh t hp c cp trong phn trnh by v phng php


vt cn. Tng t cc bi ton ti u t hp, kh nhiu bi ton m cu hnh t hp
cng c th gii mt cch tng i hiu qu bng phng php quy hoch ng.
Chng ta s xem xt mt s bi ton.
a) Bi ton phn tch s

Cho s t nhin n. Hy cho bit c bao nhiu cch phn tch s n thnh tng cc s
nguyn dng. Cc cch phn tch khng xt th t, chng hn 6=1+5=5+1 c coi
l mt cch. n=0 cng c coi l 1 cch phn tch.
gii bi ton bng phng php quy hoch ng, trc ht ta phi lp c cng
thc quy hoch ng.
Gi F(i,j) l s cch phn tch s j thnh tng cc s nguyn dng nh hn hoc
bng i.
Cc cch phn tch ch thuc mt trong 2 nhm:
1. Nhm th nht khng cha gi tr i. Nh vy, j ch c phn tch thnh tng
cc s nguyn dng nh hn i v c F(i-1,j) cch nh vy.
2. Nhm th hai c cha t nht 1 gi tr i. Loi b gi tr i ny, ta s thy s cch
phn tch j theo kiu ny bng s cch phn tch j-i thnh tng cc s nguyn
nh hn hoc bng i, tc l c F(i,j-i) cch. Ch rng iu ny ch ng khi
ta khng xt th t cc s hng trong phn tch. V j phi ln hn hoc bng i
th mi phn tch nh vy c.
Tm li, ta c cng thc quy hoch ng nh sau:
1. F(i,0)=1 vi mi i>0.
2. F(i,j)=F(i-1,j) nu j<i.
3. F(i,j)=F(i-1,j)+F(i,j-i) vi j>=i.
Sau khi xc nh c cng thc quy hoch ng, ta d dng xy dng c thut
ton tnh bng phng n. V cu trc d liu, ta c th s dng mt bng phng n
2 chiu (hoc p dng k thut ci tin da trn nhn xt: tnh F(i) th ch cn F(i1)).
Thut ton tnh bng phng n:
procedure Calculate;
begin
F[0,0]:= 1;
for j := 1 to n do F[i,0] := 0;
for i := 1 to n do begin
for j := 0 to n do
if j<i then F[i,j]:=F[i-1,j]
else F[i,j] := F[i-1,j]+ F[i,j-i];
end;
end;

p s ca bi ton l F(n,n).
b) Bi ton dy s Catalan
Dy s Catalan bc n l mt dy s c nh ngha nh sau:

1. C 2n+1 phn t nguyn dng. Phn t u tin v cui cng bng 0.


2. Phn t sau hn km phn t trc 1.
Chng hn, cc dy s Catalan bc 3 l:
0101010
0101210
0121010
0121210
0123210

Bi ton ca chng ta l tnh T(n) s dy Catalan bc n. Chng hn T(0)=0, T(1)=1,


T(2)=2, T(3)=5
Bc u tin l thit lp cng thc quy hoch ng:
Gi F(i,j) l s dy s kiu Catalan c i phn t, phn t cui cng l j. S dy
Catalan ca ta s l F(2n+1,0). D dng chng minh c cng thc truy hi sau:
1. F(1,0)=1.
2. F(i,0) = F(i-1,1).
3. F(i,j) = F(i-1,j+1) + F(i-1,j-1) nu j>=1.
4. Cng thc p dng vi i<=1<=2n+1, 0<=j<=n. Vi cc gi tr i,j khc F(i,j)=0.
Sau khi c cng thc th vic xy dng thut ton tr nn d dng hn:
procedure Calculate;
begin
F[i,j]:=0 vi mi i,j;
F[1,0]:= 1;
for i:=2 to 2*n+1 do begin
F[i,0]:=F[i-1,1];
for j:=1 to n do F[i,j]:=F[i-1,j-1]+F[i-1,j+1];
end;
end;

Qua cc bi ton trn, chng ta c th nhn xt mt cch c c s rng quy hoch


ng l mt phng php rt hiu qu gii cc bi ton m cu hnh t hp v ti
u t hp. Tuy nhin gii mt bi ton bng quy hoch ng thng gp 2 kh khn:
th nht l tm cng thc quy hoch ng, th 2 l lu tr bng phng n.
Cng qua hai phng php vt cn v quy hoch ng, ta thy rng vic gii nhiu
bi ton ti u thng i hi chi ph kh ln v thi gian tnh ton v khng gian b
nh. Nhng trong thc t, nhiu bi ton khng nht thit phi tm nghim ti u.
Trong iu kin thi gian v khng gian hn ch, chng ta ch cn nhng nghim
"tt" l c. chnh l t tng ca phng php tham lam v cc thut ton xp
x.

8.4. Phng php tham lam (Gready)


Tm nghim ca bi ton ti u thng i hi chi ph ln v thi gian tnh ton v
khng gian b nh. Tuy nhin trong nhiu trng hp ta ch tm c mt nghim
tt, kh gn vi nghim ti u l t yu cu, nht l khi c hn ch v mt thi gian
v b nh.
Phng php tham lam xy dng cc thut ton gii cc bi ton ti u da trn t
tng ti u cc b theo mt chin lc t duy kiu con ngi, nhm nhanh chng
t n mt li gii "tt".
C mt s thut ton da trn t tng ca phng php tham lam thc s tm c
phng n ti u (chng hn thut ton Kruscal tm cy khung cc tiu), cn li a s
cc thut ton da trn phng php tham lam thng l thut ton gn ng, ch
cho mt li gii xp x li gii ti u.
8.4.1. Mt s chin lc tham lam

Phng php tham lam tm nghim ti u da trn cc chin lc ti u cc b ca


con ngi. Trc khi trnh by nhng thut gii tham lam cho nhng bi ton c th,
chng ti cp n hai chin lc ti u cc b c bn:
- Chn ci tt nht trc (cn gi l "chn ming ngon trc". y l l do v sao
phng php ny c gi l "tham lam" hay "tham n").
- Ci tin ci ang c thnh ci tt hn.
Chin lc th nht thng c p dng khi xy dng dn tng thnh phn ca
nghim ti u. Thut gii s nh gi cc la chn theo mt tiu chun no v sp
xp t nh n ln ri tin hnh chn theo trnh t .
Chin lc th hai thng bt u bng mt hay mt vi phng n. Sau , bng
mt s cch thc no , cc phng n c iu chnh c gi tr tt hn. Qu
trnh iu chnh s dng li khi khng iu chnh c thm hoc s ci thin rt nh
hoc ht thi gian cho php Phn ln cc thut ton hin nay p dng cho b d
liu ln c thit k theo chin lc ny: chng hn tm kim leo i, gii thut di
truyn c gi c th tham kho trong nhng ti liu khc.
Chng ta s i vo mt s bi ton c th v vn dng nhng chin lc trn thit
k cc thut gii tham lam.
8.4.2. Bi ton xp bal

Gii thut tham lam gii bi ton xp bal da trn chin lc "chn ci tt nht
trc". Vic chn cc vt a vo bal c th theo 3 chin lc nh sau:

- u tin vt nh trc (vi hi vng chn c nhiu vt).


- u tin vt c gi tr cao trc.
- u tin chn nhng vt c t s gi tr/trng lng ln trc.
Ta thy chin lc th 3 tng qut hn nht. Do vy vic tm nghim ca chng ta s
tin hnh theo 2 bc:
1. Sp xp cc vt gim dn theo t l: gi tr/trng lng.
2. Ln lt a vo bal nhng vt no c th a c theo trnh t sp
xp .
Thut gii chi tit nh sau:
procedure Gready;
begin
for i := 1 to n do begin a[i]:=v[i]/w[i];id[i] := i; end;
sp xp w,v,a,id theo a;
for i:=1 to n do begin
if m >= w[i] then begin
m := m - w[i];
t := t + v[i];
s := s + [id[i]];
end;
end;
end;

Kt qu: S l tp cc vt c chn, T l tng gi tr ca chng. Thut gii ny c


phc tp O(nlogn) do thao tc ch yu phn sp xp.
8.4.3. Bi ton ngi du lch

C nhiu gii thut tham lam khc nhau gii bi ton ny. Chng ti xin trnh by
mt gii thut theo chin lc chn ci tt trc v mt gii thut theo chin lc ci
tin ci hin c.
Gii thut theo chin lc chn ci tt nht trc c tng rt n gin: ti mi
bc ta s chn thnh ph tip theo l thnh ph cha n thm m chi ph t thnh
ph hin ti n thnh ph l thp nht.
Gii thut theo chin lc ci tin ci hin c cng c tng rt n gin: xut pht
t mt l trnh no (1,2,..n chng hn), ta ci tin l trnh hin c bng cch tm 2
thnh ph m i ch chng cho nhau th tng chi ph gim i. Qu trnh dng li
khi khng cn ci tin c hn na.
Da trn 2 tng , bn c c th d dng xy dng c cc thut gii. Thut
gii th nht c phc tp tnh ton l O(n2), thut gii th hai c phc tp tnh
ton l O(n3).

Ngoi 2 gii thut trn, ngi ta cn xy dng c gii thut di truyn cho bi ton
ny. tng c bn l thay v xut pht t mt phng n, chng ta x l nhiu
phng n ng thi, ci tin chng bng vic m phng qu trnh di truyn, thch
nghi v tin ho ca sinh vt c c nhng phng n ngy mt tt hn. Tuy
nhin vn nm ngoi khun kh ca cun gio trnh ny.
i vi th metric (tc l c bt ng thc tam gic d[i,j]<=d[i,k]+d[k,j] vi mi
i,j,k) ngi ta cn tm c thut gii tham lam cho nghim c tng chi ph <=2 tng
chi ph ca nghim ti u. Thut gii tin hnh cng theo 2 bc:
1. Tm cy khung cc tiu ca th.
2. Duyt cy khung theo chiu su.
3. Xy dng l trnh l th t duyt ca cc nh, loi b cc nh trng nhau.
8.4.4. Bi ton ng thng (Bin-Packing)

Cho N vt, mi vt c trng lng l ai. C rt nhiu thng ging nhau v mi


thng ch c kh nng cha c cc vt c tng khi lng khng qu M (tt
nhin ai<=M) . Hy xp cc vt vo cc thng sao cho s thng s dng l t nht.
Bi ton ng thng cng l mt bi ton NPC, tc l cha c thut gii a thc.
Thut gii tham lam ca bi ton ny da trn tng:
1. Sp xp cc vt theo th t trng lng gim dn.
2. Ln lt a cc vt vo cc thng bng cch tm thng u tin vn cn
cha vt y. Nu khng c th thm mt thng mi cha vt y.
procedure Gready;
begin
for i:=1 to n do id[i] := i;;
sxep a,id on a;
k:=0;
for i:=1 to n do begin
for j := 1 to k+1 do
if t[j]+a[i]<=M then break; {tm thng j u tin}
if j=k+1 then k:=k+1; {k thng u tin khng th cha c => dng thng mi}
t[j] := t[j] + a[i];
s[j] := s[j] + [id[i]];
end;
end;

ngha ca cc cu trc d liu nh sau: id[i] l ch s ban u vt i sau khi sp


xp, k l s thng cn dng, t[j] l tng trng lng cc vt ang c trong thng j, s[j]
l tp hp cc vt c a vo thng j.
phc tp tnh ton ca thut ton l O(n2).
Johnson chng minh c thut gii ny cho nghim x tho mn:

f(x) xp x 11*f(x0)/9+4 vi x0 l nghim ti u.


Trc khi kt thc trnh by v phng php tham lam, chng ti cp n thut
ton Kruscal, mt thut ton tham lam thc s ti u.
8.4.5. Thut ton Kruscal

Thut ton Kruscal gii bi ton tm cy khung cc tiu ca th v hng c trng


s. Bi ton cy khung cc tiu c trnh by chng th. Ni mt cch n
gin, cy khung cc tiu ca mt th N nh l mt th con N nh, N-1 cnh,
lin thng v c tng trng s cc cnh l nh nht.
L thuyt th chng minh mt th lin thng khng c chu trnh s l mt
cy.
T tng tham lam trong thut ton Kruscal l "chn ci tt nht trc". Chng ta s
tin hnh chn N-1 cnh u tin cc cnh c trng s nh trc sao cho khi chn
cnh c chn phi khng to thnh chu trnh vi cc cnh chn.
Nh vy thut ton s tin hnh qua 2 bc: sp xp v chn. kim tra mt cnh
khi c chn c to thnh chu trnh hay khng, ta s lu tr cc nh vo cc cy
con. Nu hai nh u mt ca mt cnh m cng thuc mt cy th thm cnh s
to thnh chu trnh. ng thi, khi thm mt cnh ta cng hp nht 2 cy ca 2 nh
tng ng.
V cu trc d liu: ta biu din th bng danh sch cnh, gm cc b ba (u,v,d)
vi ngha trng s cnh (u,v) l d. biu din cc cy con ta dng mng T, trong
T[u] l nh cha ca nh u trong cy. Nu T[u] bng 0 th u l nh gc. Hai nh
cng thuc mt cy nu chng cng nh gc.
Thut ton chi tit nh sau:
procedure Kruscal;
begin
sp xp u,v,d tng dn theo d; {sp xp danh sch cnh tng dn}
for i := 1 to m do begin
x := Root(u[i]); y := Root(v[i]);
{tm gc ca mi nh }
if x <> y then begin
{thuc 2 cy khc nhau }
k := k + 1;
s := s + [i];
{chn cnh i}
T[y] := x;
{hp nht 2 cy}
if k=n-1 then exit;
{chn n-1 cnh th xong}
end;
end;
end;
function Root(u);
{tm gc ca nh u, l nh c T = 0}
begin
while T[u] <> 0 do u := T[u];
Root := u;
end;

Kt qu ta c s l danh sch cc cnh c chn. D dng chng minh c


phc tp ca thut ton l O(mlogm), ch yu l thi gian sp xp cc cnh.
Qua cc thut gii tham lam trnh bi, chng ta c th kt lun:
1. Phng php tham lam c phc tp tnh ton thp, thng nhanh chng tm
c li gii .
2. Li gii ca phng php tham lam thng ch l mt li gii tt ch khng phi
li gii ti u.

8.5. kt lun
Trong chng ny chng ta tm hiu v bn phng php thit k thut ton ph
bin: chia tr, vt cn, quy hoch ng v tham lam. Chng ta cng vn dng
chng, c bit l 3 phng php sau gii cc bi ton ti u.
Mi phng php c nhng u im v nhc im ring. Phng php vt cn c
u im l n gin v chc chn tm c li gii ti u. B li nhc im ca n
l phc tp qu ln. Phng php quy hoch ng c phc tp khng ln, cng
chc chn tm c li gii ti u nhng li i hi lng b nh rt ln, nhiu khi
vt qu mc p ng ca h thng. Phng php tham lam th nhanh chng, n
gin v khng i hi nhiu b nh, nhng li khng chc chn tm c li gii ti
u.
Do mi phng php u c u, nhc im ring nn tu vo yu cu ca bi ton
v kh nng cho php m ta c th la chn hay phi hp cc phng php khc
nhau tm kim li gii tt nht. Chng hn nu b nh hn hp th ta c th dng
vt cn vi b d liu nh v tham lam vi b d liu ln, hoc dng phng php
tham lam xc nh mt s cn cho vt cn. Cn nu b nh rng ri hn th c th
phi hp quy hoch ng v tham lam.