Tải bản đầy đủ (.pdf) (74 trang)

Phân tích thiết kế và đánh giá giải thuật toán potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.2 MB, 74 trang )


















 17208
  
DÙNG CHO SV NGÀNH :  












 - 2010
i

       2
    CNTT
  : 3







60
45
15
0
0
0


    
   


-    
-       
-   

 


-  
-   
-                
    
-   

 




TS
LT
TH/Xemina
BT
KT

5
4
0
1
0
   
  
   
  
 
         
 

 
          

 

1



2


0,5
0,5





1



15
7
5
2
1




  
 
 
 
 
  
 

0,5



3









2,5










1






ii




TS
LT
TH/Xemina
BT
KT
 

 
  
   
 
    
2,5




1
1,5



1
1
 
11
6
3
2
0
 quy
  
   
 

 
   

 
 
  
 

1





0,5

1
1
1
0,5
1





1
2
1










1



11
6
3
1
1
  
 
 

  
 
4.3.2. Quick sort
   
  
  
  


0,5
1,5


1,5


1
1


0,5


1


1


1













1


12
6
3
2
1
  

5.1.1.  
  
 

 
  
 
 
 

1,5



1


1
1,5
1




0,5


1
1
0,5








1

1


6
4
1
1
0
  

 
 

0,5
1
2



1







iii




TS
LT
TH/Xemina
BT
KT
   
        
  

0,5

1


               do giáo viên giao,
    


-           
2003

- 
 
- L
trình
-    Kumarss Naimipour, Foundations of Algorithms Using
C++ Pseudocode, Third Edition, Jones and Bartlett Publishers, 2004.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein,
Introduction to Algorithms, Second Edition, MIT Press, 2001.

   
-      
-      

 

  

                 
      cho sinh viên.




  
 
iv


 1
 2

- Algorithm 2
 2
 2
 2
 2
 3
 Algorithm Complexity 4
 4
 4
 5
 7
 Data structure 9
 9
 9
 Backtracking 9
 9
 10
 11
 11
 13
 13
 13
 13
 13
 14
 15
 15
 17
 19
 21

 
v

 23
 24
 31
 33
 33
 34
1.  34
2.  34
3.  35
 38
1.  38
2.  38
3.  43
4.  46
5.  48
 49
1.  49
2.  49
3.  51
4.  53
5.  53
6.  57
) 60
1.  60
2.  60
3. Bài t 61
4.  64

O 65
 66
 
1


      
                   
             
      
                   
             
                    
        
                  
                  
                
                
      
                   
                     
      
      
                 
        .
10







 
2


1.   - Algorithm
1.1.  
               
              Introduction to
Algorithms            
                 
     -              
       
                 
       -          
           
                


                 
        
.
    
                 
        
               

 
                  

  
             
 Input. 
   tput.
2. 
ng có hai cách biu din mt thut toán, cách th nht là mô t c thc hin
ca thut toán, cách th hai là s d  gii thut.
2.1. 
 biu din thu  i ta mô t    c thc hin ca thut toán,
ngôn ng   mô t thut toán có th là ngôn ng t nhiên hoc mt ngôn ng lai ghép
gia ngôn ng t nhiên vi mt ngôn ng l  n gi mã lnh.
 
3

Ví d: mô t thu  c s chung ln nht ca hai s nguyên.
Input: Hai s nguyên a, b.
    .
Thut toán:
 USCLN(a, b)=a.
 m USCLN ca a-b và b, quay li 
Nu a < b thì tìm USCLN ca a và b-a, quay li 
 )  (flowchart)
                 
(Algorithm Flowchart).
                   
                   
              
  
 
Bu

Kt c
Nhp xut dliu
Câu lnh
u kin
S

1
2
3
4
5

    
  
Kh     

Khi 4:    u kiu th
         u th        
(False).
 
4

  
3.    Algorithm Complexity
  
       tt, xu và so sánh các thut toán cùng loi, có th
da trên h :
+    
+ Da vào thi gian thc hin và tài nguyên mà thut toán s d thc hin trên các
b d liu.

Trên thc t các thut toán hiu qu thì không d hit hiu qu 
d dàng thc hin và hi c mt cách nhanh chóng. Và m u có v nghch lý là các
thut toán càng hiu qu thì càng khó hi  t càng phc tp li càng hiu qu (không
ph        nh giá và so sánh các thu    ng da
  phc tp v thi gian thc hin ca thut toán, g   phc tp thut toán
(algorithm complexity). V bn ch  phc tp thut toán là m   ng (có th
không chính xác) s phép tính mà thut toán cn thc hin (t   dàng suy ra thi gian
thc hin ca thu i vi mt b d lic N. N có th là s phn t
ca mng hp bài toán sp xp hoc tìm kim, hoc có th  ln ca s trong
bài toán kim tra s nguyên t chng hn.
3.2.  oán
 minh ha vi phc tp thut toán ta xem xét ví d v thut toán sp xp
chn (selection sort) và sp xi ch trc ti 
Cài t ca thut toán sp xp chn:
for(i=0;i<n;i++)
{
min_idx = i;
for(j=i+1;j<n;j++)
if(a[j]<a[min_idx])
min_idx = j;
if(min_idx!=i)
{
temp = a[i];
a[i] = a[min_idx];
a[min_idx] = temp;
}
 
5

}

S phép tính thut toán cn thc hin  au:
(N-1) + (N--1)/2.
Phân tích chi ti   -1)/2 là s phép toán so sánh cn thc hin, còn s ln
thc hi i ch hai phn t (s nguyên) ta thut toán là N.
t ca thut toán sp xi ch trc tip:
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
 i vi thut toán sp xp chn ta có  (N-1) + (N-2)
    -1)/2. Chi ti  -1)/2 là s ln so sánh thut toán thc hin, và
  ln i ch hai phn t (hai s nguyên) t a thut toán.
 ng hp trung bình, thut toán sp xp ch   ng t   i sp
x i ch trc tip vì s i ch ng hp tt nh 
ng hp ti nht thì chc chn thun sp xp chn t  kt lun thut
toán sp xp ch   i thut toán sp xi ch trc tip.
  
                   
      O
    
0
:
0
; ( ) . ( )N N f N c g N  

        O           

                   
 
     O        O    
                 O  
   O(f(N)).
          -   
    
 
6

  ta  f(N) = N*(N-1)/2 = 0.5N
2

  O(N
2
      
2
.
        
           
                  
                   
       
 O(N
2

     
                 
  
    O(N

2
  O(N
2

   


(omega)v

(theta):
  

 O
   
     

     O   O
  


 

 
        


 0.5N
2
 0.5N = O(N2)
 47 N*log(N) = O(N*log(N))

 N*log(N) + 1000047N =

(N*log(N))
  O (N
k
)
           


(N
2
)
   O(N
2
  O(N
5
)
                

(N*log(N))
        
      


   
       
          
  est case).
 
7


3.4. 
      
     

      
f(N) =

     C 
 
f(N) =

(log(N)): logarit
f(N) =

  
f(N) =

(N*log(N)): N log N
f(N) =

(N
2

f(N) =

(N
3

f(N) = O(N

k
    
f(N) =

(b
N
 
         

        

      
   O
 b             

(N
2
    
     
2
 
7
N
2
.
             
 
   .

          

                   
                 


 
    

(S + N).
Trong               
        



(N)
100 000 000

(N log N)
40 000 000

(N2)
10 000

(N3)
500
 
8


(N4)
90


(2N)
20

(N!)
11


O(Log(N))

10
-7
giây
O(N)

10
-6
giây
O(N*Log(N))

10
-5
giây
O(N
2
)

10
-4
giây

O(N
6
)


O(2
N
)
10
14

O(N!)
10
142

.
        
        

           
-                  
           -
 
             
      
int j=0;
for (int i=0; i<N; i++)
{
while ( (j<N-1) && (A[i]-A[j] > D) )
j++;

if (A[i]-A[j] == D)
return 1;
}
         O(N
2
         
                     
   O(N)             

 
9

        O(N
2
         
O      
4.    Data structure
Niklaus Wirth, mt lp trình viên và nhà khoa h   i phát minh ra ngôn
ng l    ng nói mt câu nói ni ti   c l   
(Programs) = Cu trúc d liu (Data Structures) + Gii thut (Algorithms). Câu nói này nói
lên bn cht ca vic l    t cu trúc d liu phù h biu din d liu ca
bài toán và t   ng gii thut phù hp vi cu trúc d lichn. Ngày nay vi s
phát trin ca các k thut lp trình, câu nói ca Wirth không h    i na
  n phn ánh s gn kt và tm quan trng ca các cu trúc d liu và gii thut.
Cu trúc d li c s d  biu din d liu còn các gii thuc s d  thc
hin các thao tác trên các d liu ca bài toán nhm hoàn thành các ch   
trình

               
                    

        
5.1.  
               
           
 
    
                  
                    
              
   
5.2.  Backtracking
                  
qui                 
  
        
                   
    

               
             
 
10

 
     
                    
             
  
 
   

    .
[6, trang 57]:      
N
a
.

N
a

N N/2 2
N/2 2
1 nÕu N = 0
a (a ) nÕu N ch½n
a*(a ) nÕu N lÎ

   
int power(int a, int n)
{
if(n==0)
return 1;
else{
int t = power(a, n/2);
if(n%2==0)
return t*t;
else
return a*t*t;
}
}
  (Greedy)
                 

              
                
   
 :                
                 
 
 
11

5.5. Qui (Dynamic Programming)
                 
       
 Trong
 

qui        
   
p
       
   
--
   
x x x  


       
 
    
1 2 3
456

7 8 9






                  
  
                  
              
        
  
            
            

 
n
*x
n
+ a
n-1
*x
n-1
+ +a
1
*x + a
0

f(x) = a

0
+ x*(a
1
+x*(a
2

n-1
+a
n
 
    

                    
  
 
12

            
 
    
 
13

II:  
1. B
 
Sp x c xem là mt trong nh  c nghiên cu c n ca khoa hc máy
      t toán chi tit chúng ta cn nm vng mt s khái nin
sau:
+ M ng (field) là m   d ling h i, s n thoi

ca mi
+ Mt bn ghi (record) là mt tp hng
+ Mt file là mt tp hp các bn ghi
Sp xp (sorting) là mt quá trình x t các bn ghi ca mt file theo mt th t nào
 c x   c thc hin da trên mt hay nhi
c gi là khóa xp xp (key). Th t ca các bnh da trên các khóa
khác nhau và vic sp x  c thc hi i vi mi khóa theo các th t khác nhau.
Chúng ta s tp trung vào các thut toán xp xp và gi s khóa ch gng duy nht.
Hu ht các thut toán xp xc gi là các thut toán xp xp so sánh: chúng s dng hai
i ch (swap) các phn t cn sp xp.
   
   (internal sorting):            
trong   
 
                
                    

            
                    
  
                
  

Khi các b    c ln vi  i các bn ghi là rt t
gii ta có th s d p xp gián tip. Vic này có th c
thc hin theo nhiu cách khác nhau và môt trong nh     o ra mt file
mi chng khóa cu, hoc con tr ti hoc là ch s ca các bn ghi ban
u. Chúng ta s sp xp trên file mi này vi các b    c nh 
cp vào các b     u thông qua các con tr hoc ch s    
ng thi vi các h qun tr  d liu).

 
14

Ví d: chúng ta mun sp xp các bn ghi c 
Index
Dept
Last
First
Age
ID number
1
123
Smith
Jon
3
234-45-4586
2
23
Wilson
Pete
4
345-65-0697
3
2
Charles
Philip
9
508-45-6859
4
45

Shilst
Greg
8
234-45-5784

Sau khi sp x   truy cp vào các bn ghi theo th t  p xp chúng ta s
dng th t c cung cp bi ct index (ch sng hp này là 3, 2, 4, 1. (chúng
ta không nht thit phi các b u).
1.    
Các thut toán sp xp có th c so sánh vi nhau da trên các yu t 
+ Thi gian thc hin (run-time): s các thao tác thc hi ng là s các phép so
s i các bn ghi).
+ B nh s d    ng b nh cn thi  thc hin thut toán
  ng b nh s d  cha d liu cn sp xp.
+ Mt vài thut toán thuc lo n (hoc cn mt s c nh) thêm
b nh cho vic thc hin thut toán.
+ Các thu   ng s dng thêm b nh t l thun theo hàm tuyn tính hoc
   c file sp xp.
+ Tt nhiên là b nh s dng càng nh càng tt mc dù vii gia thi gian và
b nh cn thit có th là có li.
+ S  nh (Stability):Mt thu  c gi là nh n   gi c
quan h th t ca các khóa b   i th t ca các khóa bng nhau).
  ng lo lng nhiu nht là v thi gian thc hin ca thut toán vì các thut
toán mà chúng ta bàn v ng s d c b nh  
Ví d v sp xp nh: Chúng ta mun sp x trên ký t u ca các
b t qu sp xp ca các thut toán nh và không nh:
 
15



Chúng ta s xem xét ti sao tính  nh trong các thut toán sp xp l
quan tr y.
2. C 
2.1.  (Selection sort)
 
Tìm phn t có khóa ln nht (nh nh p xp phn
còn li ca mng.
 
 
16

Bu
Kt c
Nhp n, a[0 n-1]
i=0
i<n
vtmin=i
j<n
j=i+1
a[j]<a[vtmin]
vtmin=j
vtmin!=i
i cha[i], a[vtmin]

S
i=i+1
j=j+1

S
S



S

n mã sau minh ha cho thut toán:
void selection_sort(int a[], int n)
{
int i, j, vtmin;
 
17

for(i=0; i<n-1;i++)
{
vtmin = i;     -1]
for(j=i+1;j<n;j++)
if(a[j] < a[vtmin])
vtmin = j;
swap(a[vtmin], a[i]);   
}
}
Ví d:

Vi mi giá tr ca i thut toán thc hin (n  i  1) phép so sánh và vì i chy t 0 cho ti
(n2), thut toán s cn (n-1) + (n--1)/2 tc là O(n
2
) phép so sánh. 
      
                     
            
   

2sort)
              
  
   

-  
            
a[j].
          
   
i=0, j=2: 1, 6, 2, 19, 3, 12
 
18

i=1, j=2: 1, 2, 6, 19, 3, 12
i=2, j=4: 1, 2, 3, 19, 6, 12
i=3, j=4: 1, 2, 3, 6, 19, 12
i=4, j=5: 1, 2, 3, 6, 12, 19
  .
 
Bu
Kt c
Nhp n, a[0 n-1]
i=0
i<n
j<n
j=i+1
a[j]<a[i]

S

i=i+1
j=j+1

S
S

i cha[i], a[j]

 
void exchange_sort(int a[], int n)
{
int i, j;
int tam;
for(i=0; i<n-1;i++)
 
19

for(j=i+1;j<n;j++)
if(a[j] < a[i])
{
 
tam = a[i];
a[i] = a[j];
a[j] = tam;
}
}
      
-
               -  
             

      -            
                   
   
2.3.  (Insertion sort)
 
        hèn mi khóa vào mc sp xp
ca dãy cn sc s dng trong vic sp xp các cây bài trong
  
   

×