You are on page 1of 113

I HC QUC GIA H NI

TRNG I HC CNG NGH

CC H PHN TN

1/113

Ti liu tham kho


Gio trnh
Hagit Attiya, Jennifer Welch. Distributed Computing: Fundamentals, Simulations, and Advanced Topics, Second Edition. John Wiley & Sons, 2004.

Sch tham kho


Valmir C. Barbosa. An Introduction to Distributed Algorithms. MIT Press, 1996. Vijay K. Garg. Elements of Distributed Computing. John Wiley & Sons, 2002. Vijay K. Garg. Concurrent and Distributed Computing in Java. John Wiley & Sons, 2004. Nancy A. Lynch. Distributed Algorithms. Morgan Kaufmann, 1997. Gerard Tel. Introduction to Distributed Algorithms, Second Edition. Cambridge University Press, 2001.
2/113

Web sites
Web site sch gio trnh
http://www.cs.technion.ac.il/~hagit/DC/
Xy dng bi Hagit Attiya C nhiu a ch cc kha hc khc v cng ch

Web site bi ging


http://faculty.cs.tamu.edu/welch/teaching/668.f02/
Xy dng bi Jennifer Welch Cha cc slides dng lm cn c chnh cho bi ging trn lp

Cc Web site khc


http://www.google.com/
V d c th tm theo cc t kha distributed algorithms course
3/113

Chng 1

Gii thiu

4/113

Khi nim h phn tn


H phn tn l tp hp cc thit b tnh ring r c th giao tip vi nhau
V d : chp VLSI, b a x l, mng cc b, Internet

H phn tn vs. h song song


H song song phi hp nhiu b x l nhm gii quyt mt vn cho trc mt cch nhanh nht Trong h phn tn, mi b x l ni chung c chng trnh lm vic ring bn c lp, v li ch chung nn cn phi hp hnh ng vi nhau

Mc ch mn hc l nghin cu cc cng c v k thut xy dng phn mm phn tn


Tp trung vo kha cnh gii thut
5/113

Vai tr ca cc h phn tn
Trao i thng tin
Tin pht trin cc mng my tnh

Chia s ti nguyn
Gip gim chi ph h thng

Nng cao tin cy thng qua sao lp


H thng vn hot ng khi mt b phn gp s c

Nng cao hiu sut thng qua song song ha


Thc hin cng vic chung hay chia s cng vic

n gin thit k thng qua chuyn dng ha


Phn h thng thnh cc tin trnh tc hp vi nhau
6/113

c trng ca cc h phn tn
Khng c ng h chung
Khng th ng b ha ng h ca cc b x l khc nhau v khng th bit chc tr truyn thng Dng khi nim nhn qu thay cho ng h vt l

Khng c b nh ton cc
Cc b x l khng th bit c trng thi ton cc ca h thng Cn c gii thut nh gi cc tnh cht ton cc

Khng c c ch pht hin s c chnh xc


Khng th phn bit c b x l chm hay b s c Cn xy dng c ch pht hin s c
7/113

L thuyt h phn tn
Pht hin v khi qut ha cc vn c bn Pht biu cc vn mt cch chnh xc Thit k cc gii thut gii quyt cc vn Chng minh tnh ng n ca cc gii thut Phn tch phc tp ca cc gii thut
Da trn cc tiu ch nh thi gian thc hin, lng b nh s dng, s thng bo trao i

Chng minh cc kt qu v tnh khng th v cc kt qu cn di


Ph thuc nhiu vo gi thit
8/113

Cc lnh vc ng dng
H iu hnh
Cc tin trnh cn giao tip vi nhau

C s d liu phn tn
Cc server CSDL cn c phi hp ng b

Khc phc li phn mm


Cho chy nhiu chng trnh nng cao tin cy

Mng my tnh
Thit k cc dch v iu khin mng

Kin trc a x l
Cc b x l kt ni vi nhau cn phi hp thc hin
9/113

T l thuyt n ng dng
H iu hnh phn chia thi gian
Cng gp phi cc vn loi tr ln nhau, kha cht,... nh trong cc h phn tn vt l

Cc b a x l (MIMD)
Nu c ng h chung th s dng m hnh ng b Nu khng th s dng m hnh khng ng b

Cc mng kt ni lng (nh Internet)


S dng m hnh truyn thng bo khng ng b

Khc phc s s
M hnh treo nu ngng hot ng khng thng bo M hnh Byzantine nu kh hiu hoc c dng
10/113

PHN I

KIN THC C S

11/113

Chng 2

TRUYN THNG BO C BN

12/113

M hnh truyn thng bo


1 p0 2 p3 1 1 2 p2 3 2 p1 1

Cc b x l p0, p1,..., pn-1 (cc nt ca th) Cc knh hai chiu ni tng cp nt (cc cnh khng nh hng ca th) Mi b x l pi gn nhn cho cc knh k n 1, 2, 3,... r (r l bc ca pi)
13/113

Trng thi
Mi b x l l mt my trng thi
Mi trng thi ca pi c 2r thnh phn c bit outbufi[l] v inbufi[l], vi l = 1..r
outbufi[l] cha cc thng bo pi gi cho nt bn cnh trn knh l nhng cha n ni inbufi[l] cha cc thng bo pi nhn c trn knh l nhng cha x l

Tp trng thi Qi cha mt tp con gm cc trng thi ban u


trng thi ban u cc inbufi[l] phi rng

Hm chuyn ca pi c u vo (trng thi kh truy nhp) khng bao gm cc outbufi[l] v u ra l mt trng thi c cc inbufi[l] rng
14/113

Cu hnh
Cu hnh l mt vect C = (q0,..., qn-1) trong qi l mt trng thi ca pi
Trng thi ca cc bin outbuf ca cu hnh th hin cc thng bo ang di chuyn trn cc knh truyn Cu hnh ban u l mt vect (q0,..., qn-1) tha mn mi qi l mt trng thi ban u ca pi
Ni cch khc cu hnh ban u tt c cc b x l u trng thi ban u tng ng

C th coi cu hnh l trng thi tng th ca ton b h thng

15/113

S kin
C hai loi s kin
S kin giao : chuyn mt thng bo t outbuf ca bn gi n inbuf ca bn nhn
Thng bo s c x l ngay khi bn nhn thc hin s kin tnh ton tip theo

S kin tnh : mt bc thc hin ca b x l


Tng ng vi vic p dng hm chuyn ln trng thi kh truy nhp hin thi Trng thi kh truy nhp mi Trng thi kh + truy nhp c S kin tnh Cc thng bo - X l cc thng bo gi n gi i - Chuyn trng thi b x l
16/113

Thc hin v tnh tha ng


Cc iu kin i vi chui cc cu hnh xen k s kin m t hot ng ca h thng
iu kin an ton : iu kin phi ng vi mi tin t hu hn ca chui m t
Cha c iu g xu xy ra

iu kin sng ng : iu kin phi ng mt s ln nht nh (c th v hn ln)


iu tt s n

Thc hin l mt chui m t tha mn mi iu kin an ton t ra Thc hin tha mn mi iu kin sng ng t ra c gi l thc hin tha ng
17/113

Thc hin khng ng b


H thng gi l khng ng b nu khng c cn trn i vi thi gian
t lc thng bo c gi i cho n lc giao hoc gia cc bc thc hin ca b x l

Thc hin l chui cc cu hnh xen k s kin


cu hnh, s kin, cu hnh, s kin, cu hnh,... Vi mi b ba (cu hnh, s kin, cu hnh), cu hnh ging nh cu hnh c ngoi tr
Trng hp s kin giao : thng bo cho c chuyn t outbuf ca bn gi n inbuf ca bn nhn Trng hp s kin tnh : trng thi ca b x l cho (bao gm cc outbuf) thay i theo hm chuyn ca n
18/113

Thc hin ng b
Trong m hnh ng b, cc b x l hot ng theo cc vng nhp Thc hin l dy cc vng, mi vng nh sau
giao giao ... giao tnh tnh tnh ... tnh u tin giao tt c cc thng bo c trong cc outbuf Sau mi p0, p1,..., pn-1 thc hin mt bc tnh

M hnh ng b khng thc t nhng thch hp cho thit k gii thut v khng phi tnh n nhiu iu khng chc chn
C th m phng hot ng theo cc m hnh thc t hn
19/113

Tha ng vi cc m hnh
Vi m hnh khng ng b, mt thc hin l tha ng nu
Mi thng bo trong cc outbuf nht nh s c giao Mi b x l thc hin v hn bc tnh

Vi m hnh ng b, mt thc hin l tha ng nu n l dy v hn


Ngm tha mn hai iu kin tha ng khng ng b

20/113

Kt thc
Cc thc hin tha ng ko di v hn khng c ngha l cc chng trnh cc b khng bao gi kt thc (theo ngha thng thng)
Ch m phng cc b x l khng gp s c

m hnh ha kt thc ca gii thut, nh ra cc trng thi kt thc ca cc b x l


L cc trng thi mt khi vo khng bao gi ra Sau cc bc tnh ch l gi

Mt thc hin kt thc khi tt c cc b x l kt thc v khng cn thng bo treo no


Khng cn thng bo no trong cc inbuf v outbuf
21/113

Cc phc tp
Tp trung vo hiu sut xu nht phc tp thng bo l s ti a cc thng bo gi i trong thc hin tha ng phc tp thi gian l thi gian ti a n khi kt thc trong thc hin tha ng
Vi m hnh ng b o bng s vng ti a Vi m hnh khng ng b o da trn cc gi thit
Thi gian x l mt s kin l khng n v Thi gian truyn mt thng bo (t s kin gi n s kin x l thng bo) ti a l mt n v

Lu : chnh xc ca gii thut khng ng b cn c chng minh c lp vi cc gi thit thi gian


22/113

Gii thut pht trn


Vn
B x l p0 mun gi mt thng bo M cho tt c cc b x l khc trn mng

Gii thut
Cc trng thi
Bin color c hai gi tr green hoc red Cc inbuf v outbuf

Cc trng thi u
Vi p0, color l green v tt c cc outbuf cha M Vi cc b x l khc, color l red v cc outbuf rng

Hm chuyn
Nu M nm trong inbuf v color l red th i color thnh green v gi M vo tt c cc outbuf
23/113

V d pht trn khng ng b


green M p0 M S kin giao ti p1 t p0 green M p0 S kin tnh bi p1 M p2 red p1 M green S kin giao ti p2 t p1 p2 red M green M p0 M p2 red p1 red M green p0 M p1 green
24/113

p2 red

p1 red

phc tp gii thut pht trn


Xc nh trng thi kt thc
Trng thi kt thc c gi tr bin color l green

phc tp thng bo
S thng bo gi i trong mi thc hin tha ng l 2m, trong m l s cnh ca th
Mt thng bo c gi i trn mi cnh theo mi hng

phc tp thi gian


Thi gian ti a n khi kt thc gii thut l D + 1, trong D l ng knh ca th
n thi gian t, thng bo M ti c tt c cc b x l cch p0 t cnh (hoc nh hn)
25/113

Pht trn ng b
Thc hin v d cho theo m hnh ng b
Vng 1
Giao M ti p1 t p0 Giao M ti p2 t p0 p0 khng lm g p1 chuyn thnh green v gi M ti p0 v p2 p2 chuyn thnh green v gi M ti p0 v p1 Giao M ti p0 t p1 v p2 Giao M ti p1 t p2 Giao M ti p2 t p1 p0, p1 v p2 khng lm g

Vng 2

Cc phc tp thng bo v thi gian ging nh m hnh khng ng b


26/113

Pht ta qua mt cy khung (1)


Vn
p0 mun gi mt thng bo M cho tt c cc b x l khc thng qua mt cy khung cho trc c gc p0

Gii thut
Cc bin cc b
Bin parenti ch ra cha ca pi trn cy khung Bin childreni ch ra tp cc con ca pi trn cy khung Bin terminatedi ch ra pi trng thi kt thc cha

Trng thi u
Cc bin parent v children to nn cy khung c gc p0 Vi tt c cc b x l, terminated = false outbuf0[j] cha M j children0, cc outbuf khc ca p0 rng; cc b x l khc c cc outbuf u rng
27/113

Pht ta qua mt cy khung (2)


Gii thut (tip theo)
Hm chuyn
Vi p0, nu terminated0 = false th terminated0 = true Vi pi p0, nu inbufi cha M th t M vo cc outbufi[j] j childreni, v chuyn terminated thnh true

phc tp
C m hnh ng b v khng ng b u c phc tp thng bo l n - 1 v phc tp thi gian l d, trong d l su ca cy khung
Vi m hnh ng b, mi b x l cch p0 t cnh u nhn c thng bo M vng th t Vi m hnh khng ng b, mi b x l cch p0 t cnh u nhn c thng bo M thi im t
28/113

Hi nhp qua mt cy khung


Vn
p0 mun nhn thng tin ca tt c cc b x l gi n thng qua mt cy khung cho trc c gc p0
Ngc li ca pht ta

Gii thut
Mi b x l l gi thng tin n cha ca n Mi b x l khng phi l ch nhn thng bo t tt c cc con, sau kt hp v gi n cha ca n

phc tp
phc tp thng bo l n - 1 v phc tp thi gian l d ging nh gii thut pht ta
29/113

V d hi nhp qua cy khung


a b,d,e,g b d d e,g e c,f,h c f,h f

g g h

30/113

Tm cy khung bit gc (1)


Tm cy khung da trn gii thut pht trn
Gc p0 gi M n tt c cc b x l bn cnh
Nu khng c b x l no bn cnh th kt thc

Mi pi p0 khi nhn c M ln u tin


Gi mt b x l bt k gi M l cha Gi nhn parent n cha ca n Gi M n tt c cc b x l khc bn cnh B xung b x l cha vo tp cc nt k cn khc Nu tp cc b x l hoc l con hoc l k cn khc ng bng tp cc nt k cn th kt thc

Mi pi khi nhn c M thm mt ln na


Gi nhn reject n tng b x l gi M
31/113

Tm cy khung bit gc (2)


Tm cy khung (tip theo)
Mi b x l khi nhn c nhn parent
B xung b x l gi nhn vo tp cc con Nu tp cc b x l hoc l con hoc l k cn khc ng bng tp cc nt k cn th kt thc

Mi b x l khi nhn c nhn reject


B xung b x l gi nhn vo tp cc k cn khc Nu tp cc b x l hoc l con hoc l k cn khc ng bng tp cc nt k cn th kt thc

32/113

Thc hin gii thut cy khung


Trng hp ng b, to nn cy BFS Trng hp khng ng b, cy to nn c th khng phi l cy BFS

C hai trng hp u dng O(m) thng bo v O(D) thi gian


33/113

Tm cy khung DFS bit gc (1)


Tm cy khung DFS da trn gii thut tun t
Gc p0 thc hin th tc (*) sau
Chn mt b x l k cn cha c M, nh du n c M, gi M n b x l chn Nu tt c cc b x l k cn u c M th gi nhn parent n b x l cha (nu c) v kt thc

Mi pi p0 khi nhn c M ln u tin


Gi b x l gi M l cha, nh du b x l c M Thc hin th tc (*)

Mi b x l khi nhn c M c
nh du b x l gi M c M, gi nhn reject n

Mi b x l khi nhn c nhn reject


Lp li th tc (*)
34/113

Tm cy khung DFS bit gc (2)


Tm cy khung DFS (tip theo)
Mi b x l khi nhn c nhn parent
Gi b x l gi nhn l con Lp li th tc (*)

phc tp
Gii thut c phc tp thng bo v phc tp thi gian u l O(m)
S thng bo ti a s dng bi gii thut l 2m Thi gian ti a thc hin gii thut l 2m

35/113

Tm cy khung khng bit gc


Vn
Xy dng cy khung khi khng bit nt no l gc, vi gi thit mi nt c mt s nh danh duy nht
Gi thit l bt buc, nu khng th khng thc hin c

Gii thut
Mi nt khi ng mt gii thut cy khung DFS bit gc, phn bit bi s nh danh ca nt Nu hai phin bn ng ti mt nt th nt s tham gia cy khung DFS c s nh danh ln hn

phc tp
phc tp thng bo l O(nm) v phc tp thi gian l O(m)
36/113

Chng 3

BU TH LNH TRN VNG

37/113

Mng vng
2 1 p4 2 p 1 0 2 p1 1 1 = tri = theo chiu kim ng h 2 = phi = ngc chiu kim ng h p2 2 1

1 p3 2

Khi nim mng vng


Mt loi hnh mng trong cc b x l c ni vi nhau theo dng vng
Cc b x l c nhn nhn thng nht v hng (tri v phi)

L do nghin cu mng vng


n gin, d phn tch C ng dng thc t l mng vng th (token ring)
38/113

Bu th lnh
nh ngha gii thut bu th lnh
C hai trng thi kt thc l elected v not-elected ng mt b x l (th lnh) s vo trng thi elected, tt c cc b x l khc s vo trng thi not-elected

iu kin thc hin tha ng


Nhiu nht l mt b x l vo trng thi elected (iu kin an ton) Mi b x l nht nh s vo mt trong hai trng thi elected hoc not-elected (iu kin sng ng)

Mt s ng dng
Tm cy khung vi th lnh l gc Gii quyt tnh trng kha cht
39/113

Vng v danh
Khi nim vng v danh
Vng v danh l vng m cc b x l khng c s nh danh duy nht
Cc b x l c cng my trng thi

Phn loi gii thut cn c vo gi thit c bit kch thc vng hay khng
Gii thut ng dng
Khng s dng n kch thc vng Cng my trng thi cho mi b x l vi mi kch thc vng

Gii thut khng ng dng


S dng kch thc vng Vi mi kch thc vng c mt my trng thi khc nhau
40/113

Bu th lnh trn vng v danh


nh l
Khng tn ti gii thut bu th lnh cho vng v danh
Ngay c vi gii thut khng ng dng v vng ng b

Chng minh
Khng c mt s bt i xng no ban u, tnh i xng gia cc b x l lun c duy tr
Lc u, mi b x l c cng trng thi v c cng cc thng bo gi mi vng, mi b x l nhn c cng cc thng bo nn thc hin cng bc chuyn trng thi v gi i cng cc thng bo

Khng th c vic ch mt b x l vo trng thi elected cn cc b x l khc th khng


41/113

Vng c nh danh
Khi nim vng c nh danh
Mi b x l trong vng c mt s nh danh duy nht Cn phn bit gia ch s v s nh danh
Ch s thuc khong t 0 n n - 1, ch dng phn tch, gii thut khng bit c S nh danh l cc s nguyn khng m bt k, gii thut bit c thng qua mt thnh phn trng thi c bit l id

c t vng bng cch bt u vi id nh nht v lit k cc id theo chiu kim ng h

Phn loi gii thut


Gii thut ng dng : mt my trng thi cho mi id Gii thut khng ng dng : mt my trng thi cho mi id v mi kch thc vng
42/113

Bu th lnh vng c nh danh


Trong trng hp vng c nh danh, vic bu th lnh c th thc hin c nh gi cc gii thut bu th lnh da trn phc tp thng bo
Vng khng ng b
(nlogn) thng bo

Vng ng b
(n) thng bo trong mt s trng hp c bit (nlogn) thng bo trong cc trng hp khc

Lu : cc cn di nu u tim cn cht
43/113

Bu th lnh O(n2)
Gii thut
Mi b x l thc hin
u tin gi id ca n sang bn tri Khi nhn c mt id (t bn phi)
Nu id nhn c > id ca n th chuyn id sang bn tri (b x l hin thi s khng bao gi tr thnh th lnh) Nu hai id bng nhau th b x l hin thi tr thnh th lnh Nu id nhn c < id ca b x l th khng lm g c

Tnh ng n
B x l c id ln nht s c bu
Thng bo cha id ln nht s i qua tt c cc b x l

phc tp thng bo
Ph thuc vo cch thc sp t cc id
44/113

phc tp gii thut O(n2)


n-2 n-1 0 1 2 i i-1

Cc id c sp t theo th t gim dn trong trng hp xu nht


id ln nht lun i khp vng, gp n thng bo id ln nh gp n - 1 thng bo id ln ba gp n - 2 thng bo ... n Tng s thng bo l i = ( n 2 )
i =1

45/113

Bu th lnh O(nlogn) (1)


Gii thut
Mi b x l thm d cc min ln cn theo t xem c th tr thnh th lnh min c khng
Bng cch gi thng bo probe cha id ca b x l

C mi t li tng gp i kch thc min ln cn Mi b x l mi t


Nu nhn c probe c id > id ca n th chuyn tip hay gi li reply (nu l b x l cui cng ca min ln cn) Nu nhn c probe c id < id ca n th khng lm g Nu nhn c probe c id ca chnh n th tr thnh th lnh Nu nhn c reply khng gi cho n th chuyn tip Nu nhn c hai reply t hai pha gi cho n th tin hnh t thm d tip theo
46/113

Bu th lnh O(nlogn) (2)


Tnh ng n
Chng minh tng t nh gii thut O(n2)

phc tp thng bo
Vi mi b x l cn tip tc thm d t th i
Khong cch thm d l 2i S thng bo to nn 4.2i

S b x l cn tip tc thm d t th i
Vi i = 0, c n b x l Vi i > 0, nhng b x l l th lnh min t th i - 1

S th lnh min t th i - 1 n / (2i-1 + 1) logn n Tng s thng bo 4 n + 4 2i i1 + n 5n + 8nlogn 2 +1 i =1 = O(nlogn)


47/113

Cn di (nlogn) (1)
Xt gii thut bu th lnh A vi cc gi thit
1. Thc hin trn vng khng ng b 2. L gii thut ng dng 3. Bu b x l c id ln nht lm th lnh 4. m bo cc b x l khc u bit id ca th lnh

Ta s chng minh phc tp thng bo ca A l (nlogn)


Gi thit 1 cn (nlogn) thc s l cn di Gi thit 2 cn chng minh s trnh by l ng Cc gi thit 3 v 4 khng lm mt tnh tng qut
Ch cn thm O(n) thng bo gii thut bt k tha mn
48/113

Cn di (nlogn) (2)
Mt s khi nim
Lch biu
Chui cc s kin trong mt thc hin

Lch biu m i vi mng vng


Lch biu c mt cnh trn khng c s kin giao no

nh l cn di
n, tp S gm n id, mt vng cc id ca S c mt lch biu m ca gii thut A s dng t nht M(n) thng bo
M(2) = 1 n 1 n vi n > 2 M ( n) = 2 M + 1

V M(n) = (nlogn) t nh l suy ra cn di


49/113

2 22

Chng minh nh l cn di (1)


n gin trnh by, gi s n l ly tha ca 2 Quy np trn n
Bc c s (n = 2)
Gi s x > y p0 phi gi mt thng bo n p1 p1 bit x Ly lch biu ngay sau khi thng bo u tin c giao trn mt trong hai cnh Cnh cn li trong lch biu l cnh m lch biu m

x p0

p1 y

50/113

Chng minh nh l cn di (2)


Quy np trn n (tip theo)
Bc quy np (n 4)
Chia S thnh hai na S1 v S2, mi na gm n/2 id vng R1 cc id ca S1 c mt lch biu m 1 s dng t nht M(n/2) thng bo trong e1 = (p1, q1) l cnh m vng R2 cc id ca S2 c mt lch biu m 2 s dng t nht M(n/2) thng bo trong e2 = (p2, q2) l cnh m Dn R1 v R2 vo vi nhau to nn R p1 R1 e1 q1 q2 p2 e2 R2 R1 q1 p1 ep eq q2
51/113

p2 R2

Chng minh nh l cn di (3)


Xy dng mt lch biu m ca A trn R s dng t nht M(n) thng bo
u tin lch biu 12 s dng 2M(n/2) thng bo
Cc b x l trn R1 khng phn bit c R1 v R Chng hot ng ging nh trn R1 v dng M(n/2) thng bo trn R Tng t vi cc b x l trn R2

Xy dng phn tip din ca 12 s dng thm 1n 1 thng bo


22

22 Khng khai thng ep v eq, nu tip din ca 12 s dng n thm 1 1 thng bo th l lch biu mong mun
Xt trng hp nu khng khai thng ep v eq th mi tip din ca 12 u dn n mt cu hnh b tc
52/113

Chng minh nh l cn di (4)


Trng hp mi tip din ca 12 u dn n b tc nu khng khai thng ep v eq
Khi nim cu hnh b tc khi chn ep v eq
Khng c thng bo treo no ngoi tr trn ep v eq Khng b x l no gi thm thng bo tr khi nhn c mt thng bo mi

Gi 3 l lch biu tip din ca 12 dn n b tc Gi 4' l lch biu tip din ca 123 ngay sau gii thut kt thc C t nht n/2 thng bo c giao trong 4'
n/2 b x l bn na ca R khng cha th lnh phi nhn c thng bo cho bit id ca th lnh Cho n trc 4' hai na ca R cha h giao tip vi nhau
53/113

Chng minh nh l cn di (5)


Xt lch biu 1234' dn n kt thc gii thut (tip theo)
Trong lch biu 4', cc b x l ln cn ep v eq nhn v gi thng bo theo hiu ng dy chuyn Gi 4" l tin t ca 4' s dng n 1 thng bo 2 P v Q khng th giao nhau trong 4" n thng bo Gi s cc b x l trong P giao 1 1
P R1 ep eq Q
54/113

22

R2

Chng minh nh l cn di (6)


Xt lch biu 1234 (4 ch bao gm cc s kin ca 4" lin quan n cc b x l trong P)
1234 khng bao gi giao thng bo trn eq Trong 1234 cc b x l trong P hot ng ging ht nh trong 1234'
V P v Q ri nhau, P v Q khng c lin h g vi nhau, cc b x l trong P hot ng c lp vi cc b x l trong Q n Trong 4 cc b x l trong P cng giao 1 1 22 thng bo
n 1n C 2M + 1 thng bo c giao

= 1234 chnh l lch biu mun xy dng


eq l cnh m
2 22

55/113

BTL ng b khng ng dng


Gii thut
Hot ng theo t, mi t gm n vng t th i, b x l c id l i, nu tn ti, gi mt thng bo i khp vng v tr thnh th lnh
Cc b x l khng c id l i nu nhn c thng bo th chuyn tip sang bn cnh

V d
n = 4 v id nh nht l 7
T t 0 n 6, khng c thng bo no c gi u t 7, b x l c id l 7 gi thng bo i khp vng

phc tp thng bo l O(n) (ti u) phc tp thi gian l O(nx), x l id nh nht


56/113

BTL ng b ng dng (1)


M hnh
Cc b x l khng buc phi bt u cng nhau Mt b x l bt u thc hin mt vng bt k hoc vng u tin nhn c thng bo

Gii thut
u tin cc b x l trng thi asleep Mt s b x l ngu nhin i trng thi thnh active
Gi id ca n trong mt thng bo fast

Mi b x l asleep khi nhn c mt thng bo th chuyn sang trng thi relay


Nut hoc chuyn tip thng bo nhn c
57/113

BTL ng b ng dng (2)


Gii thut (tip theo)
Mi b x l active khi nhn c mt thng bo nu l fast th s i fast thnh slow
Nut hoc chuyn tip thng bo

B x l nhn li c thng bo cha id ca chnh n t tr thnh th lnh Quy tc nut thng bo


Thng bo b nut ti id trong thng bo > id nh nht b x l bit c (b qua id ca cc b x l relay)

Quy tc chuyn tip thng bo


Nu thng bo l fast th chuyn tip ngay Nu thng bo l slow th lm tr 2i - 1 vng trc khi chuyn tip (i l id ca b x l khi to thng bo)
58/113

Phn tch gii thut ng dng (1)


Tnh ng n
Ch b x l c id nh nht trong s cc b x l active nhn c thng bo cha id ca chnh n

phc tp thng bo
Thng bo c id nh nht i nhanh nht s chn cc thng bo khc khng cho pht sinh qu nhiu Phn cc thng bo ra lm ba loi
Loi 1: Cc thng bo fast Loi 2: Cc thng bo slow gi i trong khi thng bo ca th lnh vn cn l fast Loi 3: Cc thng bo slow gi i trong khi thng bo ca th lnh tr thnh slow
59/113

Phn tch gii thut ng dng (2)


S thng bo loi 1
Khng b x l no nhn qu mt thng bo fast
Gi s mt b x l pi nhn c hai thng bo fast, mt t pk, mt t pj, vi pk cch xa pi hn pj theo chiu kim ng h B x l pj phi tr thnh active trc khi nhn c bt k thng bo no n c pi, thng bo fast ca pk phi n pj khi pj ang trng thi active Thng bo fast ca pk s chuyn thnh slow trc khi n pi

Tng s thng bo loi 1 n ... pk ...


pj

... pi ...
60/113

Phn tch gii thut ng dng (3)


S thng bo loi 2
S bc dch ca thng bo loi 2 c id l i n/2i
Thng bo ca th lnh l fast trong n vng Thng bo slow c id l i c 2i vng mi dch chuyn mt ln n 1 n Tng s thng bo loi 2 i n i =1 2 Nhiu thng bo nht khi c cc id t 0 n n - 1

S thng bo loi 3
S bc dch ca thng bo loi 3 c id l i n2x/2i
S vng thng bo slow ca th lnh c id l x tr v n2x Sau , khng c thm thng bo no c gi n 1 20 Tng s thng bo loi 3 n i 2n 2 i =0 Nhiu thng bo nht khi c cc id t 0 n n - 1 (x = 0)
61/113

Phn tch gii thut ng dng (4)


phc tp thng bo l O(n)
Tng s thng bo 4n

phc tp thi gian l O(n2x)


Tng s vng n2x (x l id ca th lnh)

Tnh cht chung ca hai gii thut ng b


S dng id xc nh tr thng bo
Phng cch khng chun

S vng thc hin tha ng ph thuc cc id


Cc id c th rt ln so vi n

S chng minh cc gii thut ch s dng id so snh c phc tp thng bo (nlogn)


62/113

Gii thut BTL da trn so snh


Cc khi nim
Hai vng R1 = (x1, x2,..., xn) v R2 = (y1, y2,..., yn) tng ng th t nu xi < xj yi < yj Cc b x l pi trong R1 v pj trong R2 tng nh nu chng c cng khong cch t id nh nht Hai b x l hot ng nh nhau nu mi vng
B x l ny gi mt thng bo sang tri (phi) B x l kia gi mt thng bo sang tri (phi) B x l ny thnh th lnh B x l kia thnh th lnh

Mt gii thut bu th lnh gi l da trn so snh nu cp vng R1 v R2 tng ng th t, mi cp b x l tng nh pi trong R1 v pj trong R2 hot ng nh nhau trong exec(R1) v exec(R2)
63/113

Cn di thng bo (1)
nh l
n 8 l ly tha ca 2, mt vng kch thc n sao cho bt k gii thut ng b da trn so snh no thc hin trn cng gi (nlogn) thng bo

Chng minh
Vi mi n, xy dng mt vng Sn c tnh i xng cao sao cho mi gii thut da trn so snh u tiu tn nhiu thng bo
Tnh i xng cao ngha l nhiu b x l c min ln cn tng ng th t thc hin (gi thng bo) nh nhau Phi chng minh hai b x l c min ln cn tng ng th t trn cng mt vng thc hin nh nhau (khng phi trn cc vng khc nhau)
64/113

Cn di thng bo (2)
Mt s khi nim
Mt vng l tch cc nu trong vng c t nht mt thng bo c gi
Vi gii thut thng thng, mt b x l c th bit c mt thng tin no ngay c trong vng khng tch cc Vi gii thut da trn so snh, khng th bit nh vy Mt vng tch cc th k c th ln hn rt nhiu vng th k

Mt vng kch thc n l cch tha nu id x thuc vng, cc id x - 1 n x - n khng thuc vng

B 1
Nu vng R cch tha v pi v pj c cc min ln cn bn knh k tng ng th t th pi v pj hot ng nh nhau qua k vng tch cc
65/113

Cn di thng bo (3)
Chng minh b 1
Xy dng R' sao cho
Cc min ln cn bn knh k ca pj trong R' v pi trong R ging ht nhau Cc id trong R' i mt khc nhau R' tng ng th t vi R pj trong R' tng nh vi pj trong R

pi trong exec(R) qua k vng tch cc hot ng ging pj trong exec(R')


V c cng min ln cn bn knh k

pj trong exec(R') qua k vng tch cc hot ng nh pj trong exec(R)


V tng nh vi nhau
66/113

Cn di thng bo (4)
Xy dng Sn i xng cao qua hai bc
Xy dng Rnrev
Vi mi i (i = 0,..., n - 1), ly id ca pi l rev(i), trong rev(i) l s nguyn c biu din nh phn dng logn bit l o ngc ca biu din nh phn ca i Cc phn on k tip di j ca Rnrev (j l ly tha ca 2) tng ng th t vi nhau

Sn l phin bn cch tha ca Rnrev


Nhn mi id trong Rnrev ln n + 1 ln ri cng thm n

B 2
k < n/8, min ln cn N bn knh k ca Sn, t n nht min ln cn bn knh k ca Sn tng
2(2k + 1)

ng th t vi N (k c N)
67/113

Cn di thng bo (5)
B 3
S vng tch cc trong exec(Sn) n/8

Chng minh b 3
Gi s s vng tch cc T < n/8
Gi pi l th lnh c bu n Theo b 2, t nht
2(2T + 1)

min ln cn bn knh T tng

ng vi min ln cn bn knh T ca pi n n 8 v T < n/8 >1 pj pi c min ln cn bn knh T tng ng th t vi min ln cn bn knh T ca pi Theo b 1, pj cng c bu Mu thun
68/113

2(2T + 1)

Cn di thng bo (6)
B 4
C t nht th k ca exec(Sn), 1 k n/8
n thng bo c gi vng tch cc 2(2k + 1)

Chng minh b 4
mi vng tch cc c 1 b x l pi gi 1 thng bo
Theo b 2, c t nht knh k tng ng th t vi min ln cn k ca pi Theo b 1, mi b x l gi mt thng bo vng tch cc th k
n b x l c min ln cn bn 2(2k + 1)

Tng s thng bo c gi trong exec(Sn)


n n n/8 1 = (n log n) 6 k =1 k k =1 2( 2 k + 1)
69/113
n/8

Chng 4

LOI TR LN NHAU

70/113

M hnh b nh chung phn (1)


Cc b x l giao tip thng qua mt tp cc bin chung phn thay v truyn thng bo
Ch xt cc h thng khng ng b

Mi bin chung phn c kiu bin c t cc thao tc c th thc hin mt cch nguyn t Thay i so vi m hnh truyn thng bo
Khng c cc thnh phn inbuf v outbuf Cu hnh c bao gm gi tr ca cc bin chung phn Ch c cc s kin tnh
K hiu bng ch s ca b x l thc hin s kin Trong mi s kin tnh c mt thao tc nguyn t trn mt bin chung phn
71/113

M hnh b nh chung phn (2)


Khi nim thc hin tha ng
Mi b x l thc hin mt s v hn bc tnh

Khi nim kt thc


Mi b x l c mt tp trng thi kt thc Khi trng thi kt thc, b x l khng thay i bt k bin chung phn no Gii thut kt thc khi tt c cc b x l vo trng thi kt thc

phc tp
Tp trung vo phc tp b nh (chung phn)
S bin chung phn s dng Dung lng b nh chung phn (s bit hay s gi tr)
72/113

Vn loi tr ln nhau
Khi nim
Nhiu b x l c th cn truy cp mt ti nguyn khng th s dng ng thi bi hn mt b x l

M lnh ca mi b x l chia thnh 4 on


on vo: chun b c truy nhp ti nguyn on gng: s dng ti nguyn on ra: gii phng ti nguyn on cn li: khng cn s dng ti nguyn
on vo on cn li on ra
73/113

on gng

Cc gii thut loi tr ln nhau


Gm m lnh cho on vo v on ra m bo
Loi tr ln nhau
Ti bt k thi im no ch c mt b x l trong on gng

Hoc khng c kha cht


Nu mt b x l on vo th mt b x l s on gng

Hoc khng c kha ng


Nu mt b x l on vo th b x l s on gng

Hoc c cn ch
Khng c kha ng v khi mt b x l on vo th cc b x l khc khng vo on gng qu mt s gii hn ln

Gi thit l khng b x l no on gng vnh vin v cc bin trong cc on vo v ra khng c s dng trong cc on gng v cn li
74/113

Gii thut test&set


Bin test&set V c hai gi tr c th 0 v 1, cho php cc thao tc
test&set(V)
temp := V V := 1 return temp

reset(V)
V := 0

Gii thut loi tr ln nhau


on vo : wait until test&set(V) = 0 on ra : reset(V)

Khng c kha cht nhng c th c kha ng


75/113

Gii thut read-modify-write (1)


Kiu bin chung phn mnh hn test&set c gi tr bt k, cho php thao tc
RMW(V, f) trong f l mt hm bt k
temp := V V := f(V) return temp

Nu c nhiu bin chung phn th ch vic gp chung vo mt bin tng hp Gii thut loi tr ln nhau
T chc cc b x l ch vo on gng nh mt hng i vng Mi b x l lu li v tr ca n trong hng i
76/113

Gii thut read-modify-write (2)


Gii thut loi tr ln nhau (tip theo)
Bin chung phn ch ra phn cha phn t ca hng i vi cc bin con tr first v last (t 0 n n - 1) on vo
Tng last (mod n) ln mt t b xung vo hng i Ch n khi first trng vi v tr ca b x l

on ra
Tng first ln mt t loi ra khi hng i

Tnh ng n
Tha mn iu kin loi tr ln nhau v c cn ch n

phc tp b nh
n2 gi tr, hay O(logn) bit
77/113

Cn di s trng thi (1)


Khi nim
Gii thut loi tr ln nhau c cn ch k
thc hin, khng c b x l no vo on gng qu k ln trong khi mt b x l khc ang ch trong on vo

nh l
Bt k gii thut loi tr ln nhau no c cn ch k cng s dng n trng thi b nh chung phn

Chng minh
Gi s gii thut s dng < n trng thi (phn chng) Xt mt thc hin trong ln lt p0 vo on gng (v khng c kha cht), p1, p2,..., pn-1 vo on vo
C C0 C1 C2 ... Cn-1
78/113

Cn di s trng thi (2)


Chng minh (tip theo)
i < j : Ci v Cj c cng trng thi b nh chung phn T Ci xt mt lch biu v hn ' theo p0,..., pi thc hin v hn bc tnh, pi+1,..., pn-1 khng lm g tin t ca ' sao cho ph vo on gng k + 1 ln trong exec(Ci, ) (v khng c kha cht) Cc b x l p0,..., pi hot ng trong exec(Ci, ) ging nh trong exec(Cj, )
Cng trng thi b x l Cng trng thi b nh chung phn

ph vo on gng k + 1 ln trong exec(Cj, ) Nhng pj ang ch nn mu thun nh ngha


79/113

Gii thut mua bnh read/write


Cc bin chung phn
Choosing[i] c gi tr ban u l false (0 i n - 1) Number[i] c gi tr ban u l 0

Gii thut cho pi


on vo
Choosing[i] := true Number[i] := max(Number[0],..., Number[n-1]) + 1 Choosing[i] := false for j := 0 to n-1 ( i) do wait until Choosing[j] = false wait until Number[j] = 0 or (Number[j], j) > (Number[i], i)

on ra
Number[i] := 0
80/113

Phn tch gii thut mua bnh


Tnh ng n
Loi tr ln nhau
Trong cc b x l ch trong on vo, ch b x l c Number nh nht (v id thp nht) vo c on gng

Khng c kha ng
B x l pi c Number nh nht (v id thp nht) s vo on gng ngay sau b x l hin ang trong on gng Mi b x l khc nu ly Number trong khi pi ang ch s nhn c mt s ln hn Gii thut c cn ch n

phc tp b nh
S bin chung phn l 2n S gi tr ca cc bin Number khng c cn trn
81/113

Gii thut 2 b x l c cn
Cc bin chung phn
Want[i] c gi tr ban u l 0 (i = 0 hoc 1) Priority c gi tr ban u l 0

Gii thut cho pi


on vo
1: 2: 3: 4: 5: Want[i] := 0 wait until Want[1-i] = 0 or Priority = i Want[i] := 1 if (Priority = 1-i) then if (Want[1-i] = 1) then goto 1 else wait until (Want[1-i] = 0)

on ra
6: Priority := 1 - i 7: Want[i] := 0
82/113

Phn tch gii thut 2 b x l (1)


Loi tr ln nhau
Gi s p0 v p1 cng trong on gng (phn chng) Trong on gng Want[0] = Want[1] = 1 Gi s p1 vit vo Want trc p0
Want[0] = Want[1] = 1 Ln cui p0 c Want[1] (dng 4 hoc 5) Ln cui p0 vit 1 vo Want[0] (dng 3) Ln cui p1 vit 1 vo Want[1] (dng 3)

p0 c c 1 t Want[1], khng th vo on gng


83/113

Phn tch gii thut 2 b x l (2)


Khng c kha cht
Nu ch mt trong hai b x l trong on ch th b x l pi s vo c on gng
Do p1-i trong on cn li nn Priority = i v Want[1-i] = 0

Xt trng hp c hai b x l cng trong on vo


Gi s Priority = 0 p0 khng phi ch dng 2, nhng ch dng 5 p1 n dng 4, v dng 1 v ch dng 2 vi Want[1] = 0 p0 khng phi ch dng 5 m vo on gng p0 ch dng 5 p1 ch dng 2, Want[1] = 0 p0 vo on gng
84/113

p0, p1 trong on vo, Priority = 0

Phn tch gii thut 2 b x l (3)


Khng c kha ng
Gi s p0 ch mi khng vo c on gng V khng c kha cht nn p1 vo on gng v hn ln lin tip Ln u tin p1 thc hin dng 7 trong khi p0 ang ch trong on vo, Priority lun = 0 p0 ch dng 5 iu kin Want[1] = 0, vi Want[0] = 1 Ln tip theo p1 vo on vo, n ch dng 2 vi Want[1] = 0, mu thun gi thit khng c kha cht
p0 trong on vo p1 dng 8, Priority lun = 0 p1 ch dng 5 vi Want[0] lun = 1 p1 vo on vo, Want[1] = 0, ch dng 2
85/113

Gii thut u loi (1)


Gii thut loi tr ln nhau khng c kha ng cho n b x l, s dng cc bin c cn trn
Da trn mt cy u loi
Cy nh phn hon chnh vi n - 1 nt

Thc hin gii thut 2 b x l trn mi nt ca cy Mi th tc on vo bt u ti mt nt l


Hai th tc tng ng vi hai b x l trn mi nt l

B x l chim quyn trong gii thut 2 b x l tip tc tranh quyn nt cha ca nt hin thi
B x l bn tri ng vai tr nh p0 B x l bn phi ng vai tr nh p1

B x l chim quyn ti nt gc c vo on gng


86/113

Gii thut u loi (2)


Ci t gii thut mt cch quy
pi bt u ti nt 2k + i/2, ng vai tr nh pi mod 2
k = log n - 1 Cc nt c nh s t 1, bt u t nt gc, t tri sang phi, t trn xung di

Sau khi chim quyn ti nt v, b x l tip tc thc hin quy ti nt v/2, ng vai tr nh pv mod 2

Tnh ng n
Cc tnh loi tr ln nhau, khng c kha ng, khng c cn ch suy ra t gii thut 2 b x l

phc tp b nh
3(n - 1) bin read/write logic
87/113

Cn di s bin R/W
nh l
Mi gii thut loi tr ln nhau khng c kha cht da trn cc bin read/write cn n bin chung phn

Chng minh
Bng quy np trn n mt cu hnh ti c ca gii thut trong mi b x l khng ch mt bin chung phn ring r
Mt b x l khng ch mt bin trong mt cu hnh nu n sp sa vit vo bin theo trng thi ca n trong cu hnh

Cu hnh ti c tha mn iu kin b tc i vi mt tp nht nh cc b x l


Cu hnh C l P-b tc trong P l mt tp cc b x l nu mt cu hnh b tc ti c D sao cho C ~P D
88/113

B ph (1)
B
Nu C l pi-b tc ti c th lch biu ch lin quan n pi tha mn
(a) pi trong on gng trong (C) (b) Trong exec(C, ), pi vit vo mt bin no khng b khng ch bi bt k b x l no khc trong C

Chng minh (a)


V C l pi-b tc nn C i vi pi ging nh mt cu hnh b tc D no V khng c kha cht nn lch biu ch lin quan n pi t D khin cho pi trong on gng p vo C cng khin pi trong on gng
89/113

B ph (2)
Chng minh (b)
Gi s trong exec(C, ), pi ch vit vo cc bin W b khng ch trong C bi cc b x l P
pi P

Xy dng lch biu 12


1 bt u t C, mi b x l trong P thc hin mt bc tnh (vit vo W), dn ti D 2 bt u t D, lin tc p dng iu kin khng c kha cht dn ti cu hnh b tc Q bt u t Q, mt b x l pj P thc hin cc bc tnh cho n khi vo c on gng (khng c kha cht)

Gi tp cc b x l c thc hin bc tnh trong 2 l P'


90/113

B ph (3)
Chng minh (b) (tip theo)
p vo C, dn ti C' c pi trong on gng p 1 vo (C), dn ti D' ~V\{pi} D
V mi bc tnh trong 1 u l php vit, v cc php vit trong u b vit trong 1

p 2 vo D', dn ti Q' ~V\{pi} Q


V cc cu hnh ban u tng ng nu ch xt V\{pi}, li p cng lch biu ch lin quan n P' V\{pi}

p vo Q' khin pj trong on gng


Q' i vi pj ging nh Q

Nhng pi vn trong on gng t C' n Q' Mu thun vi tnh loi tr ln nhau


91/113

B chnh (1)
B (suy ra nh l vi k = n)
k, 1 k n, cu hnh b tc ti c C, cu hnh D sao cho
D ti c t C vi mt lch biu ch lin quan n {p0,..., pk-1} p0,..., pk-1 khng ch k bin ring r trong D D l {pk,..., pn-1}-b tc

Chng minh quy np trn k


Bc c s k = 1
Theo b ph, p0 s vit vo mt bin trong mt lch biu ch lin quan p0. D l cu hnh ngay trc khi p0 vit vo bin D ti c t C vi lch biu ch lin quan p0 p0 khng ch mt bin ring r trong D v C b tc D l {p1,..., pn-1}-b tc v mem(D) = mem(C) v ch p0 tnh
92/113

B chnh (2)
Bc quy np gi s b ng vi k 1, chng minh b ng vi k + 1
Theo gi thit quy np
lch biu 0 ch lin quan {p0,..., pk-1} dn C ti C1 {pk,..., pn-1} b tc v p0,..., pk-1 khng ch tp W gm k bin

Theo b ph
lch biu ch lin quan pk t C1 sao cho pk khng ch mt bin x W trong (C1)

Tng t nh trong chng minh b ph


lch biu (= 12) t (C1) trong p0,..., pk-1 vit ln W, sau cc b x l ny tr thnh b tc

Gi D'1 = (C1)
pk trong on vo, cc b x l khc trong on cn li
93/113

B chnh (3)
Bc quy np (tip theo)
p vo C1 dn n cu hnh b tc D1 Theo gi thit quy np
lch biu ch lin quan {p0,..., pk-1} dn D1 ti C2 {pk,..., pn-1} b tc v p0,..., pk-1 khng ch mt tp gm k bin, gi s vn l tp W

p vo D'1 dn n cu hnh mong mun C'2


ti c t C vi lch biu ch lin quan {p0,..., pk} p0,..., pk khng ch k + 1 bin ring r W {x} {pk+1,..., pn-1} b tc

Tuy nhin vic p vo D1 c th gy ra mt tp k bin b khng ch W' W


94/113

B chnh (4)
Xt trng hp tp k bin b khng ch bi p0,..., pn-1 trong C2 W' W
C th x W' ch c k bin b khng ch trong C'2 p dng lin tc gi thit quy np c C1, D1, C2, D2, C3, D3,... V ch c mt s hu hn cc bin chung phn i j sao cho trong cc cu hnh Ci v Cj cng mt tp k bin b khng ch L lun tng t nh trng hp W' = W, thay C1 v C2 bng Ci v Cj
95/113

Chng 4

NHN QU V THI GIAN

96/113

L do s dng ng h logic
Trong h thng khng ng b, thng khng th bit s kin no xy ra trc s kin no Mt s v d
V d A
p0 gi thng bo m0 cho p1 Trc khi m0 ti p1, p1 gi thng bo m1 cho p0 p0 v p1 khng th bit thng bo no c gi trc

V d B
p0 gi thng bo m0 cho p1 Sau khi m0 ti p1, p1 gi thng bo m1 cho p0 p0 v p1 bit m0 c gi v nhn trc khi m1 c gi

Cn sp xp cc s kin theo th t b phn


97/113

Th t b phn xy ra trc
Trong mt thc hin, s kin tnh a xy ra trc s kin tnh b, k hiu a b, nu
1. a v b xut hin trn cng mt b x l v a xy ra trc b trong thc hin trn b x l , hoc 2. a gy ra vic gi thng bo m v b bao hm vic nhn thng bo m, hoc 3. s kin tnh c sao cho a c v c b

Xy ra trc c ngha l thng tin t s kin a c th nh hng n s kin b Nu trong hai s kin khng c s kin no xy ra trc th chng tng tranh, k hiu
98/113

ng h logic
Khi nim ng h logic
L cc gi tr gn cho cc s kin tnh cung cp thng tin v th t xy ra ca chng
S nguyn L(e) gn cho s kin e trong mt thc hin tha mn iu kin a b L(a) < L(b)

Gii thut nhn thi gian logic


Mi b x l pi duy tr mt bin m (nhn thi gian logic) Li, c gi tr ban u l 0 Mi thng bo pi gi i c gn nhn thi gian gi tr hin thi ca Li Vi mi s kin tnh a, Li c tng > gi tr trc v nhn thi gian ca mi thng bo nhn c
L(a) l gi tr ca Li khi s kin a kt thc, k hiu Li(a)
99/113

V d ng h logic
p0 p1 p2 a 1 b c 1 2 g 2 1 d h 2 e 3 f 4 i 5

Th t b phn
V d

a b : L(a) = 1 < 2 = L(b) f i : L(f) = 4 < 5 = L(i) a e : L(a) = 1 < 3 = L(e)

Th t ton phn
S dng thm id ca b x l trong nhn thi gian V d
L(a) = (1,0) < (1,1) = L(c)
100/113

ng h vector
Vn i vi ng h logic
a b L(a) < L(b); cha chc L(a) < L(b) a b "Xy ra trc" l th t b phn, trong khi cc gi tr ng h logic l nhng s nguyn c th t ton phn

ng h vector tng qut ha ca ng h logic


Cc gi tr c ly t mt tp th t b phn thay v mt tp th t ton phn

Khi nim ng h vector


L cc gi tr gn cho cc s kin cung cp cc thng tin khng nhn qu v nhn qu gia chng
Gi tr V(e), thuc tp th t b phn, gn cho s kin e trong mt thc hin tha ng tha mn a b V(a) < V(b)
101/113

Gii thut nhn thi gian vector


Mi b x l pi duy tr mt vector Vi n chiu, c gi tr ban u l (0,..., 0) Mi thng bo pi gi i c gn nhn thi gian gi tr hin thi ca Vi Vi mi s kin tnh a ca pi
Gn Vi[i] := Vi[i] + 1 Vi mi thng bo pi nhn c c nhn thi gian T
Cp nht Vi[j] := max(T[j], Vi[j]) j i

Nhn thi gian vector V(a) = Vi(a) l gi tr ca Vi khi s kin a kt thc


102/113

So snh nhn thi gian vector


Cho V1 v V2 l hai vector nguyn n chiu Bng
V1 = V2 V1[i] = V2[i] i

Nh hn hoc bng
V1 V2 V1[i] V2[i] i

Nh hn
V1 < V2 V1 V2 nhng V1 V2

Khng th so snh
V1 V2 V1 V2 v V2 V1

L u
Cc php so snh khng theo nh th t ch ci
103/113

ng n ca nhn vector (1)


nh l
Nhn thi gian vector p ng cc tiu chun ca ng h vector

Chng minh a b V(a) < V(b)


Trng hp a v b u xy ra ti pi, gi s a trc b
V vi mi s kin tnh ca pi, Vi u tng nn V(a) < V(b)

Trng hp a xy ra ti pi gy ra vic gi m, trong khi b xy ra ti pj bao hm vic nhn m


pj cp nht V(b) tha mn V(a) V(b) V[j](a) < V[j](b) v s kin ln cui pi cp nht Vi[j] trc khi gi m xy ra trc hoc ng thi vi s kin a, s kin b xy ra sau s kin a, c ngha l sau s kin pj gi Vj[j] cho pi cp nht vo Vi[j] (c th gin tip)
104/113

ng n ca nhn vector (2)


Chng minh a b V(a) < V(b) (tip theo)
Trng hp c sao cho a c v c b
Chng minh bng quy np v p dng tnh truyn ng

Chng minh V(a) < V(b) a b


Tng ng vi chng minh a b V(a) < V(b) Gi s a xut hin ti pi v b xut hin ti pj V[i](b) < V[i](a) v khng c chui thng bo no t pi n pj xut pht t s kin a ca pi Nh vy V(a) < V(b)

105/113

Cn di ng h vector (1)
Vn Cc nhn thi gian vector kh ln
Mi nhn bao gm n thnh phn

Bt k ci t no ca ng h vector s dng vector s thc cng cn cc vector c ti thiu n thnh phn Xt mt thc hin trn mt mng y Mi b x l pi lin tip gi mt thng bo n pi+1, pi+2,..., pn-1, p0,..., pi-2
Gi s kin gi u tin l ai

Mi pi lin tip nhn c mt thng bo t pi-1, pi-2,..., p0, pn-1,..., pi+2


Gi s kin nhn cui cng l bi
106/113

Cn di ng h vector (3)
B 1 i, 0 i n - 1 : ai+1 bi (quay vng ch s) Chng minh Khng c thng bo no c gi t pi+1 n pi Mi b x l lun gi trc khi nhn nn khng c quan h th t theo truyn ng B 2 i v j, 0 i j n - 1 : ai+1 bj Chng minh Nu j = i + 1 th hin nhin Nu j i + 1 th pi+1 gi ti pj v j i
107/113

Cn di ng h vector (2)
nh l Nu V ci t ng h vector cho mt h thng gm n b x l, s dng cc vector thuc Rk th k n Chng minh Theo b 1, ai+1 bi V(ai+1) v V(bi) khng th so snh c vi nhau V(ai+1) > V(bi) mt thnh phn l(i) no , l : {0,..., n-1} {0,..., k} Cn chng minh l l hm song nh Gi s i v j sao cho l(i) = l(j) = r, V(ai+1)[r] > V(bi)[r] v V(aj+1)[r] > V(bj)[r] V(aj+1)[r] > V(bj)[r] V(ai+1)[r] > V(bi)[r] Mu thun b 2
108/113

ng dng nht ct nht qun


Gi thit mi bc tnh nhn ti a 1 thng bo C th thc hin bng mt hng i thng bo nh s lin tc cc bc tnh ca mi b x l Mi nht ct ca mt thc hin l mt vector n chiu K = (k0,..., kn-1), trong ki ch s bc tnh thc hin bi pi i vi nht ct nht qun, bc tnh th ki+1 ca pi khng xy ra trc bc tnh th kj ca pj

109/113

Tm nht ct nht qun gn nht


Vn Tt c cc b x l c cho mt nht ct K v phi tm mt nht ct nht qun gn K nht ng dng khi phc sau s c nh ghi du Gii php Gn nhn thi gian vector vo cc thng bo Lu vector ca mi bc tnh vo mt mng store Khi pi c cho K m := K[i] for index := m downto 1 do if store[index] <= K then return index
Tr v index ca bc tnh gn nht c vector K
110/113

Chp hnh trng phn tn


Vn Mt tp con S cc b x l c yu cu bt u tm mt nht ct nht qun bao gm trng thi ca t nht mt b x l thuc S ti thi im nhn c yu cu Gii php Mi u answer = -1 v num = 0 Khi nhn c thng bo ng dng num := num + 1; thc hin thao tc ng dng Khi nhn c thng bo nh du hoc yu cu chp Nu answer = -1 th answer := num; gi thng bo nh du ti cc nt k cn Chi ph l O(m) thng bo, m l s cnh
111/113

Trng thi knh truyn (1)


Vn Trng thi h thng bao gm khng ch trng thi cc b x l m c trng thi cc thng bo ang di chuyn Gii php 1 Mi thnh phn ca mng store cha thm s thng bo nhn c (trc tip) t mi b x l tnh nht ct nht qun gn nht, mi pi thc hin Duyt mng store t ch s nh nht n ch s ln nht, gi lp li cc bc tnh Ghi du cc thng bo nh gi trong qu trnh gi lp Ngng gi lp khi tm thy m' gn nht sao cho store[m'] c nhn vector K
112/113

Trng thi knh truyn (2)


Gii php 1 (tip theo) Mi pi thc hin (tip theo)
pi gi ti mi b x l k cn pj s thng bo nhn c t pj theo thng tin trong store[m'] pj sau khi kt thc gi lp s xc nh c cc thng bo n nh gi cho pi nhng cha n (trng thi knh truyn) l cc thng bo thuc nht ct nht qun gn nht

Gii php 2 Mi pi ghi li chui cc thng bo nhn c t pj gia thi im pi thit lp bin answer v thi im pi nhn c thng bo nh du t pj l cc thng bo ang di chuyn thuc nht ct nht qun
113/113