�������� Design and Analysis of Algorithms ������������������� ������ �����ก��� ������ ��� �������� ������������������� ����������������� ��. ��. ����� �������������ก�� ����������������������������ก������� ������ก�� �����������ก�������������� ��������������� �����������กก��������ก��������� ������ก������������� 2542 http://www.cp.eng.chula.ac.th/faculty/spj ������ Analysis of Algorithms � Complexity Measures � Size of Problem Instance � Worst-, Best-, and Average-Case Analysis Introduction � Exact Analysis � Analysis Simplification http://www.cp.eng.chula.ac.th/faculty/spj Complexity Measures Analysis of Algorithms � �����������������ก�����������ก � predict the behavior of an algorithm without implementing it on a specific computer � ����ก�������� � impossible to predict �exact� behavior � ����� memory ������ก�������� � analysis = �approximate� the main characteristics � use for comparison http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Instances and Sizes Size of Problem Instance � ก��������������ก���������ก�� � �������������������� ��������������������� ���������������������������� � ������� problem instance � ����� bits, bytes, words � ��ก������ problem instance � ����� vertices, edges, triangles, literals, ... � �������������� instances ������������ก�� � ��������������� instances ���������������� http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Sorting Minimum Spanning Tree � Input : a list of n numbers � Input : a weighted graph G=(V, E) � Assumption : each number can be stored in a � Assumption : edge weights are real numbers single computer word. each can be stored in a computer word � Input size : � V � , � E � � Input size : n http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Primality Testing Primality Testing � Input : an positive integer n , n < 2 237 � Input : a positive integer n � Input size : log n � Input size : constant � e.g., 1,000,000 -> 1 + log 2 10 6 = 20 bits http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Cost vs. Instance Size Cost vs. Problem Instances Running time Running time worst case avg. case best case n sizes of instances Instances of size n http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Analysis of Algorithms Average-Case Analysis Running time worst case � Expected cost avg. case � Depend on frequencies of instances of size n occur in practice best case ( ) ( ) ( ) t n � p I t I � � avg I I � n n sizes of instances http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Insertion Sort Insertion Sort : Analysis Insertion_Sort( A[1..n] ) for j = 2 to n c 1 n Demo c 2 ( n- 1) key = A[j] c 3 ( n- 1) i = j-1 n ( ) while i>0 and A[i]>key � � t c 4 t j j j 2 n A[i+1] = A[i] ( 1 ) � � t c 5 t j -1 � j j 2 n i = i-1 � � ( t 1 ) c 6 t j -1 � j j 2 A[i+1] = key c 7 ( n- 1) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Insertion Sort : Best-Case Insertion Sort : Worst-Case n n t ( n ) � c n Г c ( n � 1 ) Г c ( n � 1 ) Г c � ( t ) Г t ( n ) � c n Г c ( n � 1 ) Г c ( n � 1 ) Г c � ( t ) Г 1 2 3 4 j 1 2 3 4 j j 2 j 2 � � n n n n ( 1 ) ( 1 ) ( 1 ) ( 1 ) ( 1 ) ( 1 ) c � t � Г c � t � Г c n � c � t � Г c � t � Г c n � 5 j 6 j 7 5 j 6 j 7 j � 2 j � 2 j � 2 j � 2 Worst-case : t j = j Best-case : t j =1 n � Г � Г � Г Г t ( n ) c n c ( n 1 ) c ( n 1 ) c � ( j ) worst 1 2 3 4 n j 2 � t ( n ) c n c ( n 1 ) c ( n 1 ) c ( 1 ) � Г � Г � Г � Г best 1 2 3 4 j � 2 n n c ( j � 1 ) Г c ( j � 1 ) Г c ( n � 1 ) � � 5 6 7 n n j � 2 j � 2 c � ( 1 1 ) c � ( 1 1 ) c ( n 1 ) � Г � Г � 5 6 7 j 2 j 2 � � � c Г c Г c � � c � c � c � 4 5 6 2 4 5 6 � Г Г Г Г Г � � � n � c c c c � n ( c c c c c ) n ( c c c c ) � Г Г Г Г � Г Г Г 1 2 3 7 1 2 3 4 7 2 3 4 7 � 2 � � 2 � linear function � � c Г c Г c Г c � 2 3 4 7 � quadratic function http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Insertion Sort : Average-Case Insertion Sort n time ( ) ( 1 ) ( 1 ) ( ) t n � c n Г c n � Г c n � Г c � t Г worst case 1 2 3 4 j j � 2 n n c � ( t 1 ) c � ( t 1 ) c ( n 1 ) � Г � Г � 5 j 6 j 7 j 2 j 2 average case � � Average-case : t j = j /2 n t ( n ) � c n Г c ( n � 1 ) Г c ( n � 1 ) Г c ( j / 2 ) Г � avg 1 2 3 4 j � 2 n n � Г � Г � c � ( j / 2 1 ) c � ( j / 2 1 ) c ( n 1 ) 5 6 7 j 2 j 2 � � best case Г Г � � � c c c � � c c c � 2 4 5 6 4 5 6 � Г Г Г Г Г � � � n � c c c c � n 1 2 3 7 � 4 � � 4 � n � Г Г Г � c c c c 2 3 4 7 sizes of instances � quadratic function http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Best, Average, Worst Analysis : Simplification � Time � � Best-case is usually ruled out the number of �elementary� instructions get executed � Average-case is good, � but hard to measure effectively � not clear what an �average� input is � Worst-case is usually fairly easy to analyze and often close to the average http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Time vs. # Instructions Elementary Operation n t ( n ) � c n Г c ( n � 1 ) Г c ( n � 1 ) Г c � t Г � Operation whose execution time can be bounded 1 2 3 4 j j � 2 above by a constant n n � ( 1 ) � ( 1 ) ( 1 ) c t � Г c t � Г c n � 5 j 6 j 7 j 2 j 2 � � � Examples � max( , , , , , , ) � c c c c c c c 1 2 3 4 5 6 7 � 128-bit multiplication � n n n � � Г � Г � Г Г � Г � Г � � n ( n 1 ) ( n 1 ) � t � ( t 1 ) � ( t 1 ) ( n 1 ) � j j j � � n -bit multiplication � � j 2 j 2 j 2 � � � � n � � � � � Г � c 2 n 1 3 � t � � j � � j 2 � http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Selection Sort Wilson�s Algorithm SelectionSort( A[1..n], n ) Wilson( n ) { m = (n-1)! + 1 for j = n downto 2 if m mod n = 0 then return TRUE { else return FALSE k = MaxIndex( A[1..j], j ) Swap( A, k, j ) } } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Analysis : More Simplification Barometer � To simplify running-time analysis Insertion_Sort( A[1..n] ) � count only �Barometer� instructions for j = 2 to n key = A[j] � use asymptotic analysis i = j-1 n � Sufficient for obtaining growth rate of running ( ) while i>0 and A[i]>key � � t j j 2 A[i+1] = A[i] time n i = i-1 ( ) � t n � c � t j A[i+1] = key j 2 � http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Asymptotic Analysis Conclusion � ����ก�������������� n n t ( n ) � c � � t t ( n ) � c � � t j j j 2 j 2 � � � ���������ก������ instance n n t ( n ) � c � � j t ( n ) � c � � j worst worst � ����������������� instances j � 2 j � 2 Г � ( n 1 ) � n 2 � � � � � O n � 1 � c � � worst-case, best-case, average-case analysis � 2 � c 2 � ก����������� � � � � Г � 2 n n 2 � �������������� �Barometer� � �� asymptotic notation ������ก����ก�� http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj
Recommend
More recommend