Do Phuc Tap Cua Thuat Toan

  • View
    221

  • Download
    0

Embed Size (px)

Text of Do Phuc Tap Cua Thuat Toan

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    1/95

    1

    Chng 2:Phn tch cc thut ton sp xp v tm kim

    Trnh Huy Hong

    Khoa Cng ngh thng tin

    i hc S phm TPHCM

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    2/95

    2

    Mc ch

    p dng k php O ln phn tch nh gi ccphng php sp xp:

    Sp xp bng phng php chn (selection sort) Sp xp bng phng php chn (insertion sort) Sp xp bng phng php i ch (bubble sort) Sp xp bng phng php Shell (Shell Sort) Sp xp bng phng php trn (merge sort)

    Sp xp bng phng php vun ng (heap sort) Sp xp nhanh (quick sort) Sp xp bng phng php th (bucket sort) Sp xp bng phng php c s (radix sort)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    3/95

    3

    Sp xp bng phng php chn

    tng: Tm phn t nh nht a v u dy hin ti

    Tip tc thc hin phn cn li ca dy Thut ton: AlgorithmselectSort(A)

    Input: Mt mng n phn t s AOutput: Mng A c sp xp tng dn.Fori 1 ton-1 do

    min iForj i+1 tondo

    if A[j] < A[min] thenmin j

    swap(A, i, min)

    Returnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    4/95

    4

    Phn tch SX bng pp chn

    Vng lp ngoi (bin i) c thi hnh n-1 ln: O(n)

    Tng i: n-1 ln

    Kim tra i: n ln

    Gn i vo min: n-1 ln

    i ch: ti a n-1 ln

    Vi mi gi tr ca i, vng lp trong (bin j) c thihnh n-1-i lntng cng (n-1) + (n-2) + + 1 =(n-1)n/2 ln: O(n2)

    So snh: (n-1)n/2 ln

    Gn: ti a (n-1)n/2 ln

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    5/95

    5

    Phn tch SX bng pp chn (tt)

    Thi gian thc thi:

    T(n) = O(n) + O(n2

    ) = O(n2

    +n) = O(n2

    )

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    6/95

    6

    Sp xp bng phng php chn

    tng: Chn tng phn t mt vo dy c sp xp n bc hin ti,

    vo ng v tr ca n bo m sau khi chn dy vn c th t Thut ton: AlgorithminsertSort(A)

    Input: Mt mng n phn t s AOutput: Mng A c sp xp tng dn.Fori 2tondo

    temp A[i]j i- 1whiletemp 0 do

    A[j+1] A[j]j j- 1

    A[j+1] tempReturnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    7/957

    Phn tch thut ton SX bng pp chn

    Vng lp for (bin i) c thc hin n-1 ln Tng i: n-1 ln So snh i vi n: n ln Gn gi tr vo cc bin temp, j, A[j+1]: n ln

    Vi mi gi tr i, thn vng lp while (bin j) ti

    thiu c thc hin 0 ln v ti a c thchin i ln Tmin(n) = n-1

    Tmax(n) = 1++(n-1) = (n-1)n/2 = O(n2)

    Ttb(n) = Tmax(n)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    8/958

    Sp xp bng phng php i ch

    tng: So snh hai phn t nu ngc v tr th i ch vi nhau (thng

    thng l hai phn t lin tip) Thut ton: AlgorithmbubleSort(A)

    Input: Mt mng n phn t s AOutput: Mng A c sp xp tng dn.Fori 1 ton-1 do

    Forj n downtoi+1 doif A[j] < A[j-1] then

    swap(A,j-1,j)

    Returnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    9/959

    Phn tch SX bng pp i ch

    Vng lp for ngoi (bin i) c thi hnh n-1 ln

    Tng i: n-1 ln So snh i: n ln

    Vi mi gi tr i, vng lp for trong (bin j) cthi hnh (n-1-i) ln Tng j: n(n-1)/2 ln So snh j: n(n+1)/2 ln

    Php so snh: n(n-1)/2 ln

    Php i ch: ti a n(n-1)/2 ln

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    10/9510

    Phn tch SX bng pp i ch

    Thi gian thc thi: T(n) = O(n) + O(n2) =

    O(n2

    )

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    11/9511

    Bi tp

    Ci t 3 thut ton sp xp selection sort,insertion sort,v bubble sort bng ngn ng C/C++.

    Kho st thi gian thc thi 3 thut ton ln lt vi ccgi tr n khc nhau vi cng mt dy s

    Thi gian thc thi ca 3 thut ton vi cng mt gi tr n

    (rt ln, >10000) vi cng mt dy s c khc nhau haykhng? Nu c gii thch v sao c. Nu khng gii thchv sao khng.

    V th th hin thi gian thc thi ca mi thut ton

    ph thuc vo n.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    12/9512

    Sp xp bng phng php Shell

    tng:

    L mt m rng ca insertion Sort cho php dchchuyn cc phn t xa nhau.

    AlgorithmShellSort(A)

    Input: Mt mng n phn t s A

    Output: Mng A c sp xp tng dn.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    13/95

    13

    h 1repeat

    h 3 * h + 1untilh > nrepeat

    h h div 3fori h+1 tondo

    v A[i]

    j iwhilea[j-h] > vandj>hdoa[j] a[j-h]

    j j-hA[j] v

    untilh=1

    Returnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    14/95

    14

    Phng php Chia v Tr

    Mt m hnh thit k thut ton c 3 bc:

    Chia: Nu kch thc d liu u vo nh hn mt ngng no

    th gii trc tip.

    Ngc li chia nh d liu u vo thnh hai hoc nhiu tpd liu ri nhau.

    qui: Gii mt cch qui cc bi ton con ly cc li gii

    Tr: Kt hp cc li gii ca cc bi ton con thnh li gii ca

    bi ton ban u.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    15/95

    15

    Sp xp bng phng php trn

    p dng m hnh chia tr thit k thut ton spxp bng phng php trn.

    Chia:

    Nu mng A rng hoc ch c mt phn t th tr v chnh A (c th t).

    Ngc li A c chia thnh 2 mng con A1 v A2, mi mng

    cha n/2 phn t qui:

    Sp xp mt cch qui hai mng con A1 v A2

    Tr:

    To mng A bng cch trn hai mng c sp xp A1 v A2.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    16/95

    16

    Sp xp bng phng php trn (2)

    AlgorithmmergeSort(A, n)

    Input: Mt mng n phn t s A

    Output: Mng A c sp xp tng dn.Fori 0ton/2do

    A1[i] = A[i]

    Fori n/2+1 ton-1 do

    A2[i-n/2-1] = A[i]

    mergeSort(A1,n/2)

    mergeSort(A2, n-n/2-1)

    merge(A1,A2,A)

    Returnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    17/95

    17

    Cy sp xp trn

    PP sp xp trn

    c th biu dinbng mt cynh phn.

    Chiu cao ca

    cy: [log2n]+1

    A

    A1 A2

    1. Chia i d liu

    2. Gii qui 2. Gii qui

    3. Trn

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    18/95

    18

    Trn hai mng c th tAlgorithmmerge (A1,A2,A)

    Input: Mng A1, A2 c th t tng dn.Output: Mng A c hnh thnh t A1, A2v c th t tng dn.

    while not(A1.isEmpty and A2.isEmpty)if A1[0]

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    19/95

    19

    Phn tch SX bng pp trn

    Hm merge c phc tp O(n1+n2) vi n1, n2 l

    kch thc ca A1, A2. Gi s mng A ban u c kch thc n=2m.

    Ti mc th i trong cy sp xp trn: 2int

    Mi nt cha bi ton vi mng c n/2iphn t.

    Thi gian thc thi: 2i*O(n/2i) = O(n)

    Cy c log2n mc (chiu cao ca cy)

    phc tp O(logn*n)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    20/95

    20

    Phn tch SX bng pp trn (2)

    n

    n/2 n/2

    n/2 n/2 n/2 n/2

    .

    .

    .

    O(n)Chiu cao

    O(n)

    O(n)

    O(logn)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    21/95

    21

    Phn tch SX bng pp trn (3)

    Gi t(n) l thi gian thc thi ca merge-sort

    , 1( )

    ( / 2 ) ( / 2 )

    b nt n

    t n t n cn

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    22/95

    22

    Phn tch SX bng pp trn (4)

    2 2 2

    2 3 2 3 3

    , 1

    ( ) 2 ( / 2)

    ( ) 2 (2 ( / 2 ) / 2)) 2 ( / 2 ) 2

    ( ) 2 (2 ( / 2 ) / 2 )) 2 2 ( / 2 ) 3

    ...

    ( ) 2 ( / 2 )

    Thay i=m:

    ( ) 2 ( / 2 ) (1) log ( log )

    i i

    m m

    b n

    t n t n cn

    t n t n cn cn t n cn

    t n t n cn cn t n cn

    t n t n icn

    t n t n mcn nt c nn O n n

    Gi s n=2m:

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    23/95

    23

    Sp xp nhanh (Quick Sort)

    Chia:

    Nu mng A rng hoc ch c mt phn t th tr v chnh A (

    c th t). Ngc li chn mt phn t x bt k ca A, chia A thnh 3 mng:

    L: cha cc phn t ca A nh hn x

    E: cha cc phn t ca A bng x

    G: cha cc phn t ca A ln hn x

    qui: Sp xp mt cch qui hai mng con L v G

    Tr: To mng A bng cch lin tip 3 mng L, E, G theo th t.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    24/95

    24

    Cy sp xp nhanh

    Cy nh phn

    Chiu cao khngxc nh c,ph thuc vo x.

    Trong trng

    hp xu nht,chiu cao cacy l n-1 (mng sp xp)

    E(=x)

    L(x)

    1. Chia d liu theo x

    2. Gii qui 2. Gii qui

    3. Ghp

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    25/95

    25

    Sp xp nhanhAlgorithmquickSort(A, left, right)

    Input: A: mng s, left v tr cc tri, right v tr cc phiOutput: Mng A c sp xp tng dn.

    if r>l thenj leftk right + 1repeat

    repeatj j+1

    until a[j]>=a[left]

    repeat k k-1until a[k]

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    26/95

    26

    Phn tch SX nhanh

    Trng hp xu nht

    Dy cn sp xp c th t Cy sp xp nhanh c chiu cao l O(n)

    Mi ln gi qui gim mt phn t (x)

    T(n) = n + (n-1) + + 1 = O(n2)

    Trng hp tt nht Mi ln chia, chia i c dy

    Cy sp xp nhanh c chiu cao l O(logn)

    T(n) = 2T(n/2)+cn = O(nlogn) (xem mergesort)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    27/95

    27

    Phn tch SX nhanh (2)

    im mu cht l chn phn t dng sosnh (x) chia mng.

    Trng hp trung bnh Mi phn t u c xc sut c chn l phn

    t dng so snh l nh nhau v xc sut l

    1/n

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    28/95

    28

    Phn tch SX nhanh (2)

    1

    1

    1

    1

    1

    1( ) ( 1) ( ( 1) ( ))

    2( ) ( 1) ( 1)

    Nhan 2 ve voi n:

    ( ) ( 1) 2 ( 1)

    ( 1) ( 1) ( 1) 2 ( 1)

    ( ) ( 1) ( 1) ( 1) ( 1) 2 ( 1)

    ( ) 2 ( 1) ( 1)Chia 2

    n

    k

    n

    k

    n

    k

    n

    k

    T n n T k T n k n

    T n n T k n

    n T n n n T k

    n T n n n T k

    nT n n T n n n n n T n

    nT n n n T n

    2

    ve cho n(n+1):

    ( ) ( 1) 2

    1 1

    ( ) ( 2) 2 2

    1 1 1

    ...

    ( ) (0) 21 1 1

    n

    k

    T n T n

    n n n

    T n T n

    n n n n

    T n Tn k

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    29/95

    29

    Phn tch SX nhanh (3)

    1 1

    ( ) 1 1

    2 2 2ln1

    ln ln 2 log 0.69log

    2 ln 1.38 log

    nn

    k

    T n

    dx nn k x

    n n n

    n n n n

    Trng hp tt nht tt hn 38% so vitrng hp trung bnh

    phc tp O(nlogn)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    30/95

    30

    Sp xp vun ng

    Mt s khi nim v cy nh ngha cy Cy nh phn Cy nh phn c tnh cht vun ng Biu din cy nh phn y bng mng

    Cc thao tc trn cy nh phn c tnh cht vunng Thm mt phn t Xa mt phn t

    Sp xp vun ng

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    31/95

    31

    Mt s khi nim v cy

    Cy:

    Rng Mt nt

    Mt nt v cc cy con

    Cy nh phn

    Cy c s nt cy con ti mi nt ti a l 2

    Cy nh phn c tnh vun ng (heap binary tree) Gi tr ti nt gc ln hn gi tr ti tt c cc nt

    thuc 2 cy con ca n.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    32/95

    32

    Biu din cy nh phn y bng mng

    Xt phn t A[k] C 2 con l A[2*k] v A[2*k+1]

    V d: A = (10, 3, 4, 2, 6, 7, 8)

    10

    2

    43

    6 7 8

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    33/95

    33

    Cc thao tc trn cy NP vun ng

    Thm mt phn t vo cy

    Xa phn t khi cy (phn t gc)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    34/95

    34

    Thm mt phn t vo cy

    tng:

    Thm phn t mi vo cui ca mng tng ngvi cy.

    Phn t mi thm vo c th vi phm tnh cht heapvi nt cha ca n. Do phi iu chnh v tr ca

    phn t mi thm vo. Tip tc iu chnh v tr phn t mi thm vo.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    35/95

    35

    Thao tc upheap

    Algorithm upheap(A, n, k)

    Input:

    A: mng tng ng vi cy heap c th b vi phmn: s phn t ca mngk: v tr phn t cn iu chnh (di ln trn)

    Output:

    Cy ng th t heap

    v A[k]A[0] maxint

    while A[k / 2]

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    36/95

    36

    Thm mt phn t vo cy

    Algorithm insert(A, n , v)

    Input:

    A: mng tng ng vi cy c n phn tv: gi tr thm vo cy

    Output:

    cy mi thm vo phn t gi tr v

    n n + 1

    A[n] v

    upheap(A, n, n)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    37/95

    37

    Phn tch

    upheap: S ln di chuyn nhiu nht tngng l chiu cao ca cy O(logn)

    Thao tc thm mt phn t c phc tp lO(logn).

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    38/95

    38

    Thao tc xa mt phn t khi cy

    tng:

    Lun lun ly phn t gc Hon i phn t cui cng ca cy vi phn t gc.

    Phn t gc mi c th vi phm tnh cht heap (nh

    hn mt trong hai nt con) hon i v tr ca n.

    Thc hin thao tc di ch phn t gc xung di

    cho n khi n nm ng v tr

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    39/95

    39

    downheapAlgorithm downheap(A, n, k)

    Input:A: mng tng ng vi cy heap c th b vi phm

    n: s phn t ca mngk: v tr phn t cn iu chnh (di xung di)Output:

    Cy ng th t heapv A[k]while k = A[j] then

    breakA[k] A[j]

    k

    jA[k] v

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    40/95

    40

    Thao tc xa mt phn t khi cyAlgorithm remove(A, n)

    Input:

    A: mng c n phn t tng ng vi cy heapOutput:

    Cy c n-1 phn t sau khi ly phn t gc rax: phn t gc b loi b

    x A[1]A[1] A[n]n n 1downheap(A, n, 1)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    41/95

    41

    Phn tch

    Downheap: di ch ti a tng ng vichiu cao ca cy

    Thao tc xa mt phn t O(logn)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    42/95

    42

    Heapsort

    Algorithm heapsort(A, n)Input:

    A: mng c n phn tOutput:

    Mng A c sp xpm 0for k 1 to n do

    insert(A, m, A[k])for k n downto 1 do

    A[k] = remove(A, m)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    43/95

    43

    Phn tch

    phc tp O(nlogn)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    44/95

    44

    Sp xp da trn s so snh

    Cc phng php kho st u da trnphp so snh l php ton chnh.

    chng minh l chn di trong trng hpxu nht l O(nlogn)khng c phng phpsp xp no da trn s so snh c phc tp

    nh hn O(nlogn) trong trng hp xu nht. p dng trong trng hp tng qut.

    Trong mt s trng hp c bit c th cnhng phng php sp xp tt hn: O(n).

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    45/95

    45

    Sp xp th (Bucket Sort)

    Xt dy A c n kha v min gi tr ca ccphn t l [0, m-1].

    tng: S dng mt mng B gi l mng th (bucket

    array). Mng th c m phn t. S dng gi tr ca A chnh l ch s trong mng

    B. t cc phn t ca A vo B vi v tr tng ng

    vi gi tr ca n.

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    46/95

    46

    Sp xp th (bucket sort) (tt)

    AlgorithmbucketSort(A, n)

    Input: Mt mng n phn t s A c min gi tr [0,m-1]

    Output: Mng A c sp xp tng dn.

    Gi B l mng c m phn t, ban u u trng htfori 1 tondo

    insert(B[A[i]], A[i])

    remove(A,i)

    fori 0 to m-1

    while (B[i] empty)

    insert(A, i)

    returnarray A

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    47/95

    47

    Sp xp th (Bucket Sort)

    phc tp: Vng for u tin: O(n)

    Vng for th hai: O(m)

    O(m+n)

    Nu m t l vi n: m = cn th phc tp l

    O(n + cn)= O(n) phc tp l tuyn tnh. Lu : phc tp v khng gian O(m+n).

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    48/95

    48

    Tnh n nh trong sp xp

    Th t sau khi sp xp ca cc phn t ckha bng nhau khng thay i so vi th ttrc khi sp xp.

    V d:

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    49/95

    49

    Radix Sort

    M rng tng ca sp xp th.

    Mi phn t khng phi l mt gi tr n lm n c to thnh t nhiu thnh phnkhc nhau.

    V d: So snh Long, Loan: L = L, o = o, a < n Loan keythen

    returnx

    else

    ifk < x->keythen

    returnTK_NPTK(x->left, k)else

    returnTK_NPTK(x->right, k)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    91/95

    91

    Thut ton tm kim trong cy NPTK:nh gi

    Trng hp xu nht: phc tp thut ton t l vi ng i di nht

    trong cy = chiu cao ca cy T(n) = O(h)

    Trng hp trung bnh:

    T(n) = O(logn)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    92/95

    92

    Chng minh

    Trng hp tm kim thnh cng Gi S(n) l thi gian trung bnh tm kim thnh

    cng

    Gi I(n) l tng cc mc ca cc nt trong cy cn nt

    npl s nt trong cy con phi.

    nt l s nt trong cy con tri. nt = n np- 1

    I(n) = I(nt) + I(np) + n-1 (do co n-1 nt con)

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    93/95

    93

    1

    0

    1

    0

    1

    0

    1( ) ( ( ) ( 1) 1)

    ( ) ( ( ) ( 1) 1)

    ( ) 2 ( ) ( 1)

    ( ) ( 1) ( 1) 2 ( 1) ( 1) ( 1)( 2)

    ( ) ( 1) ( 1) 2

    n

    tb tb tb

    i

    n

    tb tb tb

    i

    n

    tb tb

    i

    tb tb tb

    tb tb

    I n I i I n i nn

    nI n I i I n i n

    nI n I i n n

    nI n n I n I n n n n n

    nI n n I n I

    ( 1) 2 2

    ( ) ( 1) ( 1) 2( 1)

    Chia 2 ve cho n(n+1)

    ( ) ( 1) 2( 1)

    1 ( 1)

    tb

    tb tb

    tb tb

    n n

    nI n n I n n

    I n I n n

    n n n n

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    94/95

    94

    1

    1

    ( 1) ( 2) 2( 2)Thay

    1 ( 1)

    ( ) ( 2) 2( 2) 2( 1)1 1 ( 1) ( 1)

    ...

    ( ) (1) 12

    1 2 ( 1)

    ( ) 1( ) (lg )

    1

    ( ) ( lg )

    ( )( ) (lg )

    tb tb

    tb tb

    n

    tb tb

    i

    n

    tb

    i

    tb

    tb

    I n I n n

    n n n n

    I n I n n n

    n n n n n n

    I n I i

    n i i

    I nO O n

    n i

    I n O n n

    I nS n O n

    n

  • 7/31/2019 Do Phuc Tap Cua Thuat Toan

    95/95

    Chng minh

    Trng hp tm kim khng thnh cng Gi U(n) l thi gian trung bnh tm kim khng

    thnh cng

    Gi E(n) l tng cc mc ca cc nt trong cyc n nt v 2n nt rng

    E(n) = I(n) + 2n

    U(n) = O(lgn)