A sy m p to ti c C o m p le x it y
Running TimeRunning Time
R u n n in g T im e R u n n in g T im e R u n n in g T im e R u n n in g T im e
Input SizeInput SizeR un ni ng T im e R un ni ng T im e
R un ni ng T im e In p ut S iz e In p ut S iz e
R un ni ng T ime
• H o w d o y o u co un t th e ru nn in g t im e? – Ex : # o f ite ra ti o ns i n fo r lo o p , # o f ex ec ut io ns o f a lin e, # o f fu nc ti o n ca lls , e tc . • C he ck t he f o llo w in g e xa m p le s.
R u n n in g T im e R u n n in g T im e R u n n in g T im e R u n n in g T im e • C he ck t he f o llo w in g e xa m p le s.
sa m p le 1( A [ ], n) { k = n /2 ; re tu rn A [k ] ;
R un ni ng T ime re tu rn A [k ] ; }
Constant time regardless of nConstant time regardless of nsa m p le 2( A [ ], n) { su m ← 0 ; fo r i ← 1 to n
R un ni ng T ime fo r i ← 1 to n su m ← s um + A [i] ; re tu rn su m ; }
Time proportional to nTime proportional to nsa m p le 3( A [ ], n) { su m ← 0 ; fo r i ← 1 to n
R un ni ng T ime fo r i ← 1 to n fo r j ← 1 to n su m ← s um + A [i] *A [j ] ; re tu rn su m ; }
Time proportional to nTime proportional to n22sample4(A[ ], n) { sum ← 0 ; fori← 1ton forj ← 1ton { k ← Max of n/2 elements randomly drawn
R un ni ng T ime
k ← Max of n/2 elements randomly drawn from A[1 ... n]; sum ← sum + k ; } returnsum ; } Proportional to nProportional to n33sam p le 5( A [ ], n) { su m ← 0 ; fo r i ← 1 to n fo r j ← i+ 1 to n
R un ni ng T ime fo r j ← i+ 1 to n su m ← s um + A [i] *A [j ] ; re tu rn su m ; }
Proportional to nProportional to n22fac to ri al (n ) { if (n = 1) re tu rn 1 ; re tu rn n* fac to ri al (n -1 ) ;
R un ni ng T ime re tu rn n* fac to ri al (n -1 ) ; }
Proportional to nProportional to nRe cu rr en ce a nd I nd uc ti ve T hi nk in g • Re cu rs iv e st ru ct ur e – A p ro b le m c o nt ai ns i ts s m al le r ve rs io n. – Ex 1 : fac to ri al – Ex 1 : fac to ri al
•N! = N×(N-1)!– Ex 1 :
•a n= a n-1+ 2Ex am p le o f Re cu rr en ce : M er g es o rt
mergeSort(A[ ], p, r) ▷Sort A[p ... r] { if(p < r) then{ q ← (p+q)/2;---①▷the center of p & q mergeSort(A, p, q);----②▷Sort the first mergeSort(A, q+1, r); ---③▷Sort the nextmergeSort(A, q+1, r); ---③▷Sort the next merge(A, p, q, r);---④▷Merge } } merge(A[ ], p, q, r) { sort the two arrays A[p ... q] and A[q+1 ... r] merge the two into one array A[p ... r] }mergeSort(A[ ], p, r) ▷Sort A[p ... r] { if(p < r) then{ q ← (p+q)/2;---①▷the center of p & q mergeSort(A, p, q);----②▷Sort the first mergeSort(A, q+1, r); ---③▷Sort the nextmergeSort(A, q+1, r); ---③▷Sort the next merge(A, p, q, r);---④▷Merge } }
② , , ③ - Re cu rs io n ① , ④ - O ve rh ead
D iv er se A p p lic at io ns o f A lg o ri th m s • C ar n av ig at io n • Sc he d ul in g
–TSP, Vehicle Routing, Job Task, …• H um an G en o m e Pr o je ct
–Matching, Phylogenetictree, functional analyses, …–Matching, Phylogenetictree, functional analyses, …• Se ar ch an d R et ri ev al
–Database, Web pages, …• Re so ur ce A llo cat io n • Se m ic o nd uc to r D es ig n
–Partitioning, placement, routing, …• …
W hy d o w e an al yz e al g o ri th m s? • A ss ur e th e in te g ri ty • M ea su re t he e ff ic ie nc y in r es o ur ce m an ag em en t m an ag em en t – Re so ur ce •Time •Memory, Communication Bandwidth, …
A lg o ri th m A na ly si s • Sm al l in p ut ? – Th e al g o ri th m d o es n’ t hav e to b e ef fic ie nt • Bi g i np ut ? • Bi g i np ut ? – Ef fic ie nc y m at te rs ! – In ef fic ie nt al g o ri th m c an b e fat al • A sy m p to ti c an al ys is d ea ls t he c as e w it h b ig in p ut s.
A sy m p to ti c A na ly si s • A na ly si s o n a b ig i np ut • Ex am p le o f A sy m p to ti c N o ta ti o n • Ex am p le o f A sy m p to ti c N o ta ti o n • Ο , Ω , Θ , ω , ο no ta ti o ns
limlimf(n)f(n) nn→→∞∞