You are on page 1of 37

c 



a An algorithm is all about the procedure with


the input and output

6
M 

 

a ’nput
© A sequence of numbers <a1, a2, ǥ,an>
a Output
© A permutation (reordering) of the input sequence
such that aǯ1 Ôaǯ2 Ôǥ Ôaǯn

 
    6 66 
G

 

a A correct algorithm
© Always produces the correct output for any input
a For example:
© ’nput <31,41,59,26,41,58>
© >>>>>A correct algorithm<<<<<
© <26,31,41,41,58,59>

 
    6 66 
c 
 

a A detail step by step instruction


a Describe the instruction in words
© What kind of computer programming languages?
r Doesnǯt matter
© What kind of human spoken languages?
r Mhe language you like, even Chinese, Martian
Language

 
    6 66 
Î


  


 
a delection dort
‰  selectiondort( numbers[],  array_size)
{
 i, j, min;
  (i = 0; i < array_size-1; i++)
{
min = i;
  (j = i+1; j < array_size; j++)
{
 (numbers[j] < numbers[min])
min = j;
}
if(min!=i) swap(numbers[x],numbers[i]);
}
}
 
    6 66 
Î


  


 
a Mergedort
© deparate the list into halves
© dort the two halves recursively
© Merge the two sorted halves into one sorted list

 
    6 66 
c  
   

a etter means
© Mime
© dpace
a Generally focusing on the worst-case number
of comparisons required to solve the
problem.

 
    6 66 
M   

a delection dort
© n-1, n-2, n-3, ǥ., 1
© O(c1n2)
a Merge dort
© M(n)=M(n/2) + M(n/2) + cn
© ǥ
© M(n)=O(c2nlog2n)

 
    6 66 
M   

a Merge dort Wins


a And dpace?

 
    6 66 
M 


a Mwo machine
© Machine A: 109 per second instructions
© Machine : 106 per seconds instructions
a dupposed c1=2, c2=50
a We want to sort 102 integers
© Which one runs faster?
a We want to sort 106 integers
© Which one runs faster?

 
    6 66 6

  G
 

a Mhe Human Genome Project


© ’dentify 100,000 genes
© Determine 3 billion chemical based pairs
© Genes search between different human

 
    6 66 66

  G
 

a ’nternet and the Web


© ’P route method.
© dearch Engine
© Compress the data
© How to store the password (RdA)
© Network decurity
© Lots of problems

 
    6 66 6

  G
 

a Manufacturing
© Resources price
© Human power price
© Mransportation price
© Market
© Mhe place to set the
r Factories
r Warehouse
r dtores

 
    6 66 6

G


a Hardware
a GU’
a Object-Oriented dystem
a WAN, LAN
a ǥ

 
    6 66 6
M 


a Program P1 solves a problem in p days.


a Program P2 solves the same problem in 2p
seconds.

a : Which one you will use?


a A: Program P1 runs faster for p > 20.

 
    6 66 6
 



a Mhe rate of growth affects more.


a We need notation to capture the concept of
Dzrate of growthdz when we measure the time
and space efficiency of algorithms

 
    6 66 6
 




a Edmund Landau
© 1877~1938
© ’nventor of the asymptotic notation
a Donald E. Knuth
© 1938 ~
© Muring Award, 1974.
© Father of the analysis of algorithms
© Popularizing the asymptotic notation

 
    6 66 6
 




a §
a
a 

 
    6 66 6
V  



   
a Let n be the number of input integers
© Program 1 takes time O(n)
© ’t takes time O(n) for program 1 to solve the
problem
© Mhe time required by program 2 is O(2n)
a O(n) reads?
© ig-Oh of n
© Order n

 
    6 66 6
  

a dentence
Program p2 takes time O(n) to solve the problem.
a Means
© Mhe time needed by p2 is a function f(n) with
f(n)=O(n)

 
    6 66 
   

a dentence
© Mhe comparison-based sort methods can not be
solved in O(n) time.
a Means
© Mhe comparison-based sort problems can not be
solved in h(n) time, for any function h(n) with
h(n)=O(n)

 
    6 66 6
×  

a ’ntuitive meaning
© f(n) does not grow faster then g(n)
a Comments
© f(n)=O( g(n) ) roughly means f(n) Ô g(n) in terms of
rate of growth
© = is not the meaning of equation. ’tǯs more like
© We do not write O(g(n))=f(n)

 
    6 66 
u
! ×

×

For any two functions f(n) and g(n), we write


f(n)=O(g(n))
’f there exists positive constants p and  such
that the inequality pÔp
holds for each integer p½ p

 
    6 66 
  c


a Mhe definition of ppstates that


there exists a positive constant  such that
the value of pis upper-bounded by p
for all sufficiently large positive integers p.

 
    6 66 
 


a n=O(n)?
a 999n=O(n)?
a 5n2=O(n3-n2)?
a n2=O(n)?

 
    6 66 
 

a åes, for n0=1 and c=1


a n Ô 1n , for n 1

 
    6 66 
èèè 

a åes, for n0=1, c=999


a 999n Ô 999n , for n 1

 
    6 66 

Ñ  " 
 # 

a åes, let n0=2, c=5


a 5n2Ô5(n3-n2)
a 5n2 Ô 5n3-5n2
a 10n2Ô 5n3
a 2Ô n

 
    6 66 

 

a ’s n can be the upper-bound of n2


© ’nstinctive feeling: NO
a How to proof?
© Contradiction Method

 
    6 66 


a Assume for a contradiction that there exists


constants  and p such that
a p Ôp
a holds for any integer p with p½ p . p is an
arbitrary integer strictly larger than
max(p ,).
a For instance, let p
p 
a n2>cn (n>c), contradiction.

 
    6 66 
 



a ig-Oh only gives the upper bounds. We need


other asymptotic notations.
© f(n)=O(g(n)) f(n) Ô g(n) in rate of growth
© f(n)=š(g(n)) f(n) ½ g(n) in rate of growth
© f(n)=±(g(n)) f(n) È g(n) in rate of growth
© f(n)=o(g(n)) f(n) ß g(n) in rate of growth
© f(n)= ɘ(g(n)) f(n) g(n) in rate of growth

 
    6 66 6


a D
 
©
!"#š! !""

 $%&
&' &
!"(& !"
  )( *
© 
& !" + , -'
!"*
©
!"#š! !""-
 %) !" '-
  
&
&&
,
 ,&
!"#š! !"" *

.6/0&&  6 66 
M 

a D
 /
©
!"#±! !""

 $%&
&6)& ' &&6 !"1
!"1& !"
 
 )( *
a 2, -'' -'
!"
a   &
-3- 4' *

.6/0&&  6 66 
G
  M

 

a We say that Algorithm A is no worse than


Algorithm  in terms of worst-case time
complexity if there exists a positive function
f(n) such that
© Algorithm A runs in time O(f(n)) and
© Algorithm  runs in time (f(n)) in the worst case

 
    6 66 
G
  M

  

a Algorithm A is strictly better than algorithm 


in terms of worst-case time complexity if
there exists a positive function f(n) such that
© Algorithm A runs in time O(f(n)) and
© Algorithm  runs in time ɘ (f(n)) in the worst case
OR
Algorithm A runs in time o(f(n)) and
© Algorithm  runs in time (f(n)) in the worst case

 
    6 66 
M  

a dupposed we figure out that the time


complexity of a algorithm is O(f(n)). We say
that the analysis is tight if the algorithm runs
in (f(n)) in the worst case.
a ’n others words, if the time complexity of the
algorithm is O(f(n)) and the analysis is tight,
then the time complexity of the algorithm is
(f(n))

 
    6 66 
M   


a We say that Algorithm A is a optimal


algorithm for a problem P in terms of worst-
case time complexity if
© Algorithm A runs in time O(f(n)) and
© Any algorithm that solves the problem P requires
time (f(n)) in the worst case.

 
    6 66