You are on page 1of 5

HOW

 DO  WE  ANALYZE  AN  ALGORITHM  


•  We  want  a  predic-ve  science  
•  We  measure  running  -me  as  a  func-on  of  n,  the  size  of  the  
input  (in  bytes  assuming  a  reasonable  encoding).  
•  We  work  in  the  RAM  model  of  computa-on.  All  “reasonable”  
opera-ons  take  “1”  unit  of  -me.  (e.g.  +,  *,  -­‐,  /,  array  access,  
pointer  following,  wri-ng  a  value,  one  byte  of  I/O...)  

What  is  the  running  ;me  of  an  algorithm  


•   Best  case  (seldom  used)  
•   Average  case  (used  if  we  understand  the  average)  
•  Worst  case  (used  most  oLen)  

ColumbiaX  |  Machine  Learning  for  Data  Science  and  Analy-cs:  Algorithms  


EXAMPLE  
1  input:  A[n]  
2  for  i  =  1  to  n  
3    if  (A[  i]  ==  7)  
4      for  j  =  1  to  n  
5        for  k  =  1  to  n  
6          Print  “hello”  
 
 
•   What  is  the  worst  case  running  -me?  O(n3)  
•   What  is  the  best  case  running  -me?    O(n)  
•   What  is  the  average  case  running  -me?  What  is  an  
                       average  array?  

ColumbiaX  |  Machine  Learning  for  Data  Science  and  Analy-cs:  Algorithms  


ASYMPTOTIC  NOTATION  
big-­‐O  
•  O(g(n))  =  {f(n)  :  there  exist  posi-ve  constants  c  and  n0  such  
that    
  0   ≤ f   (n)  ≤ cg(n)    for  all   n   ≥ no    }  
 
Alterna-vely,  we  say  
   f(n)  =  O(g(n))  if  there  exist  posi-ve  constants  c  and  n0  
such  that  
    0 ≤   f (n)   ≤ cg(n)
     for  all   n   ≥ no    }  
 
Informally,  f(n)  =  O(g(n))  means  that  f(n)  is  asympto-cally  less  
than  or  equal  to  g(n).  

ColumbiaX  |  Machine  Learning  for  Data  Science  and  Analy-cs:  Algorithms  


SIMPLE  FUNCTIONS  
Given  a  func-on  f(n)  ,  we  want  to  know  the  “smallest”  g(n)  such  that  
   f(n)  =  O(g(n))  and  g(n)  is  “simple”  
   
Typical  simple  func-ons  include  (but  are  not  limited  to)  
 1  
 log  log  n    n  log  n  
 log  n    n2  
 log2  n    n3  
 n    2n  
   n!  
We  use  these  to  classify  algorithms     into  classes  

ColumbiaX  |  Machine  Learning  for  Data  Science  and  Analy-cs:  Algorithms  


GROWTH  OF  FUNCTIONS  
We  assume  that  1  opera-on  takes  10-­‐6  seconds.  All  -mes  are  approximate.  Missing  
entries  are  too  large  to  include.  For  comparison,  the  number  of  atoms  in  the  universe  
has  been  es-mated  at  somewhere  between  1080  and  10120  

n/Running   1   log  n   n   n  log  n   n2   n3   2n   n!  


;me  

10   10-­‐6  sec.   10-­‐6  sec.   10-­‐5  sec.   10-­‐5  sec.   10-­‐4  sec.   10-­‐3  sec.   10-­‐3  sec.   3  sec.  

100   10-­‐6  sec.   2  x  10-­‐6  sec.   10-­‐4  sec.   2  x  10-­‐4  sec.   10-­‐2  sec.   1  sec.   1015  yrs.   >  10140  yrs.  

1000   10-­‐6  sec.   3  x  10-­‐6  sec.   10-­‐3  sec.   3  x  10-­‐3  sec.   1  sec.   16  min.   >  10250  yrs.  
 

10000   10-­‐6  sec.   4  x  10-­‐6  sec.   10-­‐2  sec.   4  x  10-­‐2  sec.   100  sec.   11  days.  

100000     10-­‐6  sec.   5  x  10-­‐6  sec.   10-­‐1  sec.   5  x  10-­‐1  sec.   2.5  hrs.   31  yrs.  

1000000   10-­‐6  sec.   6  x  10-­‐6  sec.   1  sec.   6  sec.   11  days.   >31000  yrs.  

ColumbiaX  |  Machine  Learning  for  Data  Science  and  Analy-cs:  Algorithms  

You might also like