Click here to load reader

Thiet ke giai thuat

  • View
    218

  • Download
    0

Embed Size (px)

Text of Thiet ke giai thuat

  • 8/13/2019 Thiet ke giai thuat

    1/39

    Phn tch thit k gii thutPh m Nguyn Khang, Thanh Ngh BM. Khoa h c my tnhKhoa CNTT i h c C n Th {pnkhang,dtnghi}cit.ctu.!du."n

  • 8/13/2019 Thiet ke giai thuat

    2/39

    # Mc tiu# T bi ton n chng trnh# Cc k thut thit k gii thut

    Chi ! tr" #u$ %ui

    &'t c(n )hnh cn *u +n,Th- +n,Th- %-,. /0r112$3 #u$ ho(ch 4ng

    # 5i t6

    $

    )4i 2ung

  • 8/13/2019 Thiet ke giai thuat

    3/39

    Mc tiu# 5it cc k thut thit k gii thut7 t 8 t9ng

    cho n gii thut chi tit:

    # *i!u r; ngu$n %8 cth! 6 2ng ?c k thut n$:

    %

  • 8/13/2019 Thiet ke giai thuat

    4/39

    T bi ton n chng trnh

    Bi tonthc t

    Thit k @6 trnh

    0ii thut

    #include

    Chng trnh

    K thut thit k giithut:

    Chia tr, quy hochng, hu n, nhnhcn, !

    )gAn ngB %6 [email protected] C,CFFEG&E.

    Hnh gi

    I thut 6hn tchnh gi gii thut7H4 6hJc t(6 c

  • 8/13/2019 Thiet ke giai thuat

    5/39

    I thut chi ! tr" /8 t9ng3#Lu cu7

    Cn 6hi gii bi ton c> kch thNc n:

    # Phng 6h67 T chi bi ton bn u thnh -4t OK bi ton con ng

    2(ng QNi bi ton bn u c> kch thNc nhR hn n: 0ii cc bi ton con ?c cc %Si gii con Tng h?6 %Si gii con %Si gii c

  • 8/13/2019 Thiet ke giai thuat

    6/39

    I thut chi ! tr" /6hn tch3

    (

    u ")o*n, m,

    u +a*o

    u ")o*n1, m2,

    u +a*o1

    u ")o*n2, m2,

    u +a*o2

    u ")o*nk, mk,

    u +a*ok

    T ng h p k t qu :

    Tnh o t o, o$, -, ok

    B)i ton an/ u

    B)i ton con

  • 8/13/2019 Thiet ke giai thuat

    7/39

    I thut chi ! tr" /gii thut3solve(n){

    if(n nh c th gii c)gii bi ton KQreturnKQ;else{hi! bi ton thnh cc bi ton con

    "ch th$c n%& n'&

    KQ% solve(n1); gii bi ton con %KQ' solve(n2); gii bi ton con '

    *+ng h, cc "-t .u KQ%& KQ'& KQreturnKQ;

    /

    0

  • 8/13/2019 Thiet ke giai thuat

    8/39

    & 27 #uick Oort# 0ii thut #uick Dort

    DY6 Z6 2[$ nOK th1o thJ t= t+ng 2n#\6 2ng k thut chi ! tr"7

    Chi 2[$ nOK thnh ] 2[$ con TrNc khi chi 6hi 6hn ho(ch

    0ii ] bi ton con DY6 Z6 2[$ bn tri DY6 Z6 2[$ bn 6hi

    Tng h?6 kt ^u7

    IhAng cn tng h?6

    1

  • 8/13/2019 Thiet ke giai thuat

    9/39

    & 27 #uick Oort2

  • 8/13/2019 Thiet ke giai thuat

    10/39

    H4 6hJc t(6 c

  • 8/13/2019 Thiet ke giai thuat

    11/39

    & 27 M1rg1 Dort# 0ii thut M1rg1 Dort

    DY6 Z6 2[$ nOK th1o thJ t= t+ng 2n#\6 2ng k thut chi ! tr"7

    Chi 2[$ nOK thnh ] 2[$ con IhAng cn 6hn ho(chE cJ cYt 2[$ OK r %- ]

    0ii ] bi ton con DY6 Z6 2[$ bn tri I# DY6 Z6 2[$ bn 6hi I#]

    Tng h?6 kt ^u7

    Tr4n kt ^u /th1o thJ t=3 c

  • 8/13/2019 Thiet ke giai thuat

    12/39

    & 27 M1rg1 Dort$

  • 8/13/2019 Thiet ke giai thuat

    13/39

    H4 6hJc t(6 c

  • 8/13/2019 Thiet ke giai thuat

    14/39

    5i t67 T- 6hn t tr4i# Cho -ng n6hn t# Phn t tr4i7 6hn t Zu`t hin nhiu hn n,]

    %n# T- 6hn t tr4i c th! Oo Onh dd hojc d

    # 0?i 87 Chi -ng thnh ] -ng con

    &

  • 8/13/2019 Thiet ke giai thuat

    15/39

    0i- ! tr"# TrSng h?6 jc bit c

  • 8/13/2019 Thiet ke giai thuat

    16/39

    & 2# T- ki- nh" 6hn trn -4t 2[$ [ OY6 Z6

    T- 6hn t c> gi tr"trong -ng n6hn t: Phn t

    u tin c> Q" tr -: Tr Q Q" tr t- th`$E nu khAng t-th`$ tr Q .# I thut gi- ! tr"

    T- 6hn t giB

    Do OnhQNi 6hn t giB )u bfng nhuTr Q Q" tr giB )unhR hn T- n tri )u%Nn hnT- n 6hi

    Tr Q .

    (

  • 8/13/2019 Thiet ke giai thuat

    17/39

    I thut ^u$ %ui /8 t9ng3# 0ii bi ton tKi u

    T- -4t +/i gii t0i 1utrong OK cc %Si gii Mi +/i giig- thnh nthnh 2h3n: #u trnh Z$ 2=ng -4t %Si gii ?c Z1- nh

    ^u trnh t- nthnh 6hn: Mi thnh 6hn?c t- ki- trong bNc:

    Cc 415c6hi c> 6ng gi0ng nhau: p -i bNcE t c> th! 2W 2ng chXn %= -4t

    thnh 6hn: Du khi th=c hin < nbNc t ?c %Si gii

    0

    1

  • 8/13/2019 Thiet ke giai thuat

    18/39

    I thut ^u$ %ui /6hng 6h63# Phng 6h6

    &'t c(n /brut1 qorc13 T- ht t`t c cc %Si gii H4 6hJc t(6 thSi gin %$ th

    )hnh cn /brnch n2 boun23 Ch t- nhBng %Si gii c> %?i Ci tin thSi gin th=c hin

    1

  • 8/13/2019 Thiet ke giai thuat

    19/39

    &'t c(n /8 t9ng3#s t9ng7

    0n giKng chi ! tr" nhng Z$ 2=ng %Si gii t 2Ni %n trongkhi chi ! tr" % 6hn tch t trn ZuKng

    # M4t 6hng n,%Si gii C7 0- nthnh 6hn CE C]E .E Cn

    # p -i bNc iE c> -4t OK %= chXn cho thnh 6hn i: ChXn -4t gi tr" no > cho thnh 6hn i 0Xi ^u$ ! t- thnh 6hn i F

    *

  • 8/13/2019 Thiet ke giai thuat

    20/39

    $3

    B c i*

    B c i4

    C5i6 7 B c i4

    C5i6 7 $ B c i4

    C5i6 7 k

    B c i t8m th)nh ph n th i c a 9 i gi i C

    : a ch n

    : a ch n $ : a ch n k

    &'t c(n /6hn tch3

    $

  • 8/13/2019 Thiet ke giai thuat

    21/39

    &'t c(n /gii thut3search(int i){

    if(i 0 n)

    Kie1 t2!& 3o 3nh l4i gii 5$i ccl4i gii hi6n c 74i gii t8i u

    else{

    9o2 (: l! ch :; 7! ch

  • 8/13/2019 Thiet ke giai thuat

    22/39

    & 27 bi ton v hu#&`n 7

    5n cS Qu c> kch thNc vZv Hjt v con hu Oo cho chUng khAng +n nhu

    $$

    $%

  • 8/13/2019 Thiet ke giai thuat

    23/39

    & 27 bi ton v hu

    $%

    $&

  • 8/13/2019 Thiet ke giai thuat

    24/39

    & 27 bi ton v hu

    $&

    $'

  • 8/13/2019 Thiet ke giai thuat

    25/39

    &'t c(n /gii thut3

    try(int i){ for"%B1 {

    cu2C,o3 " if(3!9e(cu2C,o3)) {

    3!5e(cu2C,o3)if(i D n)

    [email protected](iE%)

    else,2int(3olution) c!ncel(cu2C,o3)

    / /

    /

    $'

    $(

  • 8/13/2019 Thiet ke giai thuat

    26/39

    )hnh cn# Ci tin gii thut ^u$ %ui Q't c(n

    T(i -i bNcE t OV Z1- Z't Z1- c> nn i bNc

    k ti6 nB h$ khAng &ic Z1- Z't 2= trn khi ni78 cn c

  • 8/13/2019 Thiet ke giai thuat

    27/39

    &'t c(n QO )hnh cnVt c n Nhnh c n

    else{

    for(: 7 c! i){=i> :;

    3e!2ch(i E %);

    =i> null;

    /

    /

    else{

    for(: 7 c! i) tnh cFn cho 7 :

    GH I-, cc 7 theo cFn

    for(: 7 c! i) {

    if(cn ca j cn tt){

    c=i> :;3e!2ch (i E %);

    =i> null;

    /

    /

    /

    $0

    $1

  • 8/13/2019 Thiet ke giai thuat

    28/39

    I thut hu +n /gr112$3# Mc ch7

    T- -4t %Si gii tKt trong thSi gin ch`6 nhn ?c /4

    6hJc t(6 thJc th$ Q %$ th3#s t9ng Chi ^u trnh t- %Si gii thnh nhiu bNc nh k thut

    ^u$ %ui#&Ni -i bNc

    DY6 Z6 cc %= chXn cho bNc > th1o thJ t= no > wc>%?ix /t+ng 2n hojc gi- 2n tl$ th1o cch %6 %un3

    ChXn %= chXn tKt nh`t ri i ti6 bNc k /kh9ng quay+ui3

    $1

    $2

  • 8/13/2019 Thiet ke giai thuat

    29/39

    & 2# M$ rUt tin TM7

    @o(i gi`$7 yy:yyy QnE zy:yyy QnE ]y:yyy QnQ y:yyy Qn:

    Mi %o(i tin u c> OK %?ng khAng h(n ch: Ihch hng cn rUt -4t OK tin nQn /tnh ch{nn y:yyy QnE h$ nchi ht cho y:yyy3:

    Phng n tr tin Oo cho tr < nQn Q OK tS

    gi`$ b(c 6hi tr % t nh`t:

    $2

    %3

  • 8/13/2019 Thiet ke giai thuat

    30/39

    & 2 -$ rUt tin TM /tt3# M$ rUt tin TM7

    0Xi _ d /_E _]E _|E _}3 % -4t 6hng n tr tin: _ % OK tS gi`$ b(c yy:yyy QnE _] % OK tS gi`$ b(c

    zy:yyy QnE _| % OK tS gi`$ b(c ]y:yyy Qn Q _} %OK tS gi`$ b(c y:yyy Qn: Mc tiu % _ F _] F _| F _} (t nhR nh`t QNi rng

    bu4c %7

    _~yy:yyyF_]~zy:yyyF_|~]y:yyyF_}~y:yyy d n

    %3

    #s t9ng7 H! /_ F _] F _| F _}3 nhR nh`t th cc tS gi`$ b(c

    -nh gi %Nn 6hi ?c chXn nhiu nh`t 733

    %

  • 8/13/2019 Thiet ke giai thuat

    31/39

    & 2 -$ rUt tin TM /tt3

    %

    #s t9ng7 H! /_ F _] F _| F _}3 nhR nh`t th cc tS gi`$ b(c

    -nh gi %Nn 6hi ?c chXn nhiu nh`t 733 TrNc ht t chXn tKi cc tS gi`$ b(c yy:yyy QnE

    _ % OK ngu$n %Nn nh`t Oo cho _ ~ yy:yyy n: DK tin cn rUt cn %(i % n _ ~ yyyyy Chu$!n Ong chXn %o(i gi`$ b(c zy:yyy ngE Q cJ

    ti6 tc nh th cho cc %o(i -nh gi khcE Q:Q:

    %$

  • 8/13/2019 Thiet ke giai thuat

    32/39

    #u$ ho(ch 4ng# Mc ch7

    Ci tin thut ton chi ! tr" hojc ^u$ %ui Q't c(n !gi- thSi gin th=c hin

    #s t9ng7 @u trB cc kt ^u c K= c"a cc

    4i ton con %c9ng th?c truy [email protected]' @6 bng ^u$ ho(ch

    %$

    %%

  • 8/13/2019 Thiet ke giai thuat

    33/39

    #u$ ho(ch 4ng# @6 bng ^u$ ho(ch

    DK chiu d OK bin trong cAng thJc tru$ hi Thit %6 ^u$ tYc in kt ^u Qo bng ^u$ ho(ch

    Hin cc A khAng 6h thu4c trNc Hin cc A 6h thu4c Ou

    Tr bng t- kt ^u /thSng ch t- ?c gi tr"3 @n Qt trn bng ! t- %Si gii tKi u

    %%

    %&

  • 8/13/2019 Thiet ke giai thuat

    34/39

    & 27 tnh t h?6# T h?67

    C/nEk3 d nu /ndk3 hojc kdy C/nEk3 d C/nE k3 F C/nE k3

    %&

    C(4,2)

    C(3,1) C(3,2)

    C(2,0) C(2,1) C(2,1) C(2,2)

    %'

  • 8/13/2019 Thiet ke giai thuat

    35/39

    & 27 tnh t h?6# H4 6hJc t(6 gii thut ^u$7

    T/n3 % thSi gin ! tnh OK t h?6 ch6 k c 6hng trnh ^u$7T/3 d CT/n3 d ]T/n3 F C]de &$ 4 6hJc t(6 ^u %Nn7 T/n3 d /]n3

    %'

    %(

  • 8/13/2019 Thiet ke giai thuat

    36/39

    & 27 tnh t h?6# #u$ ho(ch 4ng7 T/n3 d /n]3

    %

    C 0 1 2 3 4

    0 1

    1 1 1

    2 1 2 1

    3 1 3 3 1

    4 1 4 6 4 1

    k

    n

    %0

  • 8/13/2019 Thiet ke giai thuat

    37/39

    Chi ! tr" QO ^u$ ho(ch 4ngChia tr Quy ho ch ng

    # ; t ng

    Phu