• No results found

Advanced Algorithmics (6EAP) Order of growth maths MTAT Jaak Vilo 2014 fall

N/A
N/A
Protected

Academic year: 2021

Share "Advanced Algorithmics (6EAP) Order of growth maths MTAT Jaak Vilo 2014 fall"

Copied!
79
0
0

Loading.... (view fulltext now)

Full text

(1)

Advanced  Algorithmics  (6EAP)  

 

MTAT.03.238  

 

 Order  of  growth…  maths  

Jaak  Vilo  

2014  fall  

1   Jaak  Vilo  

(2)
(3)

Program  execuGon  on  input  of  size  n  

• 

How  many  steps/cycles  a  processor  would  need  to  do  

• 

Faster  computer,  larger  input?    

• 

But  bad  algorithm  on  fast  computer  will  be  

outcompeted  by  good  algorithm  on  slow…  

• 

How  to  relate  algorithm  execuGon  to  nr  of  steps  on  

input  of  size  n?      f(n)            

(4)

Big-­‐Oh  notaGon  classes  

Class   Informal   Intui5on   Analogy  

f(n)  ∈  ο  (  g(n)  )     f  is  dominated  by  g   Strictly  below  

<  

f(n)  ∈  

O

(  g(n)  )     Bounded  from  above   Upper  bound  

≤  

f(n)  ∈  

Θ

(  g(n)  )     Bounded  from    

above  and  below   “equal  to”  

=  

f(n)  ∈  

Ω

(  g(n)  )     Bounded  from  below   Lower  bound  

≥  

f(n)  ∈  

ω

(  g(n)  )    

 

(5)

MathemaGcal  Background  

JusGficaGon  

•  As  engineers,  you  will  not  be  paid  to  say:  

Method  A  is  be#er  than  Method  B  

 or  

Algorithm  A  is  faster  than  Algorithm  B  

•  Such  descripGons  are  said  to  be  qualita-ve  in  nature;  from   the  OED:  

     

qualita5ve,  a.  a  RelaGng  to,  connected  or  concerned  with,  

 quality  or  qualiGes.  Now  usually  in  implied  or  expressed   opposiGon  to  quanGtaGve.

 

(6)

MathemaGcal  Background  

JusGficaGon  

• 

Business  decisions  cannot  be  based  on  

qualitaGve  statements:  

– 

Algorithm  A  could  be  be#er  than  Algorithm  B,  but  

Algorithm  A  would  require  three  person  weeks  to  

implement,  test,  and  integrate  while  Algorithm  B  

has  already  been  implemented  and  has  been  used  

for  the  past  year  

– 

there  are  circumstances  where  it  may  beneficial  

to  use  Algorithm  A,  but  not  based  on  the  word  

(7)

MathemaGcal  Background  

JusGficaGon  

•  Thus,  we  will  look  at  a  quan-ta-ve  means  of  describing  data   structures  and  algorithms  

•  From  the  OED:  

   

quan5ta5ve,  a.    RelaGng  to  or  concerned  with  

quanGty  or  its  measurement;  that  assesses  or  

expresses  quanGty.  In  later  use  freq.  contrasted  with  

qualita-ve.  

   

(8)

Program  Gme  and  space  

complexity  

• 

Time:  count  nr  of  elementary  calculaGons/

operaGons  during  program  execuGon  

• 

Space:  count  amount  of  memory  (RAM,  disk,  

tape,  flash  memory,  …)  

– 

usually  we  do  not  differenGate  between  cache,  

RAM,  …  

– 

In  pracGce,  for  example,  random  access  on  tape  

impossible  

(9)

• 

Program  1.17  

 

float  Sum(float  *a,  const  int  n)    {  

   float  s  =  0;  

   for  (int  i=0;  i<n;  i++)                s  +=  a[i];  

   return  s;    }

 

– 

The  instance  characterisGc  is  n.  

– 

Since  a  is  actually  the  address  of  the  first  element  

of  a[],  and  n  is  passed  by  value,  the  space  needed  

by  Sum()  is  constant  (S

sum

(n)=1).    

(10)

• 

Program  1.18    

 float  RSum(float  *a,  const  int  n)    {  

   if  (n  <=  0)    return  0;  

   else  return  (RSum(a,  n-­‐1)  +  a[n-­‐1]);    }  

 

– 

Each  call  requires  at  least  4  words  

•  The  values  of  n,  a,  return  value  and  return  address.  

– 

The  depth  of  the  recursion  is  n+1.    

•  The  stack  space  needed  is  4(n+1).  

n=997

n=1000 n=999 n=998

(11)

Input  size  =  n  

• 

usually  input  size  denoted  by  n  

• 

Time  complexity  funcGon  =  f(n)    

– 

array[1..n]  

– 

e.g.  4*n  +  3  

 

(12)
(13)
(14)
(15)
(16)
(17)
(18)
(19)
(20)

• 

So,  we  may  be  able  to  count  a  nr  of  operaGons  

needed  

(21)
(22)
(23)

n2

(24)

n2

100 n log n

(25)

0.00001*n2

100 n log n

(26)

0.00001*n2

100 n log n

(27)
(28)

logscale x logscale y

(29)
(30)
(31)
(32)
(33)

Algorithm  analysis  goal  

• 

What  happens  in  the  “long  run”,  increasing  n  

• 

Compare  f(n)  to  reference  g(n)              (or  f  and  g  )    

• 

At  some  n

0

 >  0  ,      if  n  >  n

0    

,  always  f(n)  <  g(n)

 

(34)
(35)
(36)
(37)
(38)

Θ  ,  O,  and  Ω  

Figure 2.1 Graphic examples of the Θ , O, and Ω notations.

In each part, the value of n0 shown is the minimum possible value; any greater value would also work.

(39)
(40)
(41)
(42)

Dominant  terms  only…  

• 

EssenGally,  we  are  interested  in  the  largest  

(dominant)  term  only…  

• 

When  this  grows  large  enough,  it  will  

overshadow”  all  smaller  terms  

(43)

Theorem  1.2  

). ( ) ( Thes, . for , ) ( have we , 1 , let Therefore, . 1 for , ) ( : Proof ). ( ) ( then , ... ) ( If 0 0 0 0 0 0 0 0 1 m m m i i m i i m m i m i i m m i i i m i i i m m m n O n f n n cn n f n a c n a n n a n n a n a n f n O n f a n a n a n f = ≥ ≤ = = ≥ ≤ ≤ ≤ = = + + + =

= = = − = =

(44)

AsymptoGc  Analysis  

•  Given  any  two  funcGons  f(n)  and  g(n),  we  will  restrict   ourselves  to:  

–  polynomials  with  posiGve  leading  coefficient   –  exponenGal  and  logarithmic  funcGons  

•  These  funcGons                                            as  

•  We  will  consider  the  limit  of  the  raGo:  

)

g(

)

f(

lim

n

n

n ∞

n

(45)

AsymptoGc  Analysis  

•  If  the  two  funcGon  f(n)  and  g(n)  describe  the  run  Gmes  of  two   algorithms,  and  

   

 that  is,  the  limit  is  a  constant,  then  we  can  always  run  the   slower  algorithm  on  a  faster  computer  to  get  similar  results  

<

<

∞ →

g(

)

)

f(

lim

0

n

n

n

(46)

AsymptoGc  Analysis  

•  To  formally  describe  equivalent  run  Gmes,  we  will  say  that     f(n) = Θ(g(n))  if  

•  Note:    this  is  not  equality  –  it  would  have  been  bezer  if  it  said   f(n) ∈ Θ(g(n))  however,  someone  picked  =

<

<

g(

)

)

f(

lim

0

n

n

n

(47)

AsymptoGc  Analysis  

•  We  are  also  interested  if  one  algorithm  runs  either   asymptoGcally  slower  or  faster  than  another  

•  If  this  is  true,  we  will  say  that  f(n) = O(g(n))

<

∞ →

g(

)

)

f(

lim

n

n

n

(48)

AsymptoGc  Analysis  

•  If  the  limit  is  zero,  i.e.,  

 then  we  will  say  that  f(n) = o(g(n)) t  

•  This  is  the  case  if  f(n)  and  g(n)  are  polynomials  where  f  has  a   lower  degree  

0

)

g(

)

f(

lim

=

∞ →

n

n

n

(49)

AsymptoGc  Analysis  

• 

To  summarize:  

0

)

g(

)

f(

lim

>

∞ →

n

n

n

))

(g(

)

f(

n

=

O

n

))

(g(

)

f(

n

=

Θ

n

))

(g(

)

f(

n

=

Ω

n

<

∞ →

g(

)

)

f(

lim

n

n

n

<

<

∞ →

g(

)

)

f(

lim

0

n

n

n

(50)

AsymptoGc  Analysis  

•  We  have  one  final  case:  

=

∞ →

g(

)

)

f(

lim

n

n

n

))

(g(

)

f(

n

=

o

n

))

(g(

)

f(

n

=

Θ

n

))

(g(

)

f(

n

=

ω

n

0

)

g(

)

f(

lim

=

∞ →

n

n

n

<

<

∞ →

g(

)

)

f(

lim

0

n

n

n

(51)

AsymptoGc  Analysis  

• 

Graphically,  we  can  summarize  these  as  

follows:  

       We  say  

 

(52)

AsymptoGc  Analysis  

• 

All  of  

n

2

100000 n

2

– 4 n + 19 n

2

+ 1000000

323 n

2

– 4 n ln(n) + 43 n + 10 42n

2

+ 32

n

2

+ 61 n ln

2

(n) + 7n + 14 ln

3

(n) + ln(n)

 are  big-­‐Θ  of  each  other  

 

(53)

AsymptoGc  Analysis  

• 

We  will  focus  on  these  

     Θ(1)

constant  

     Θ(ln(n))

logarithmic  

     Θ(n)

linear  

     Θ(n ln(n))

“n–log–n”  

     Θ(n

2

)

quadraGc  

     Θ(n

3

)

cubic  

     2

n

, e

n

, 4

n

, ...

exponenGal

 

O(1)<O(logn)<O(n)<O(nlogn)<O(n

2

)<O(n

3

)<O(2

n

)<O(n!

(54)

Growth  of  funcGons  

• 

See  Chapter  “Growth  of  FuncGons”  (CLRS)  

(55)
(56)
(57)
(58)
(59)

Logarithms  

(60)
(61)

Change  of  base  a  →  b  

x

b

x

a

a

b

log

log

1

log

=

)

(log

log

b

x

=

Θ

a

x

(62)

Big-­‐Oh  notaGon  classes  

Class   Informal   Intui5on   Analogy  

f(n)  ∈  ο  (  g(n)  )     f  is  dominated  by  g   Strictly  below  

<  

f(n)  ∈  

O

(  g(n)  )     Bounded  from  above   Upper  bound  

≤  

f(n)  ∈  

Θ

(  g(n)  )     Bounded  from    

above  and  below   “equal  to”  

=  

f(n)  ∈  

Ω

(  g(n)  )     Bounded  from  below   Lower  bound  

≥  

f(n)  ∈  

ω

(  g(n)  )    

 

(63)

Family  of  Bachmann–Landau  nota5ons  

!

Notation Name Intuition As , eventually... Definition

Big Omicron; Big O; Big Oh f is bounded above by g(up to constant factor) asymptotically for some k or

(Note that, since the beginning of the 20th century, papers in number theory have been increasingly and widely using this notation in the weaker sense that f = o(g) is false) Big Omega f is bounded below by g(up to constant factor) asymptotically

for some positivek

Big Theta

f is bounded both

above and below

bygasymptotically for some positive k1, k2

Small Omicron; Small O; Small Oh

f is dominated

by gasymptotically for every ε

Small Omega f dominates gasympt

otically for every k

on the order of; "twiddles"

f is equal

(64)
(65)
(66)
(67)

How  much  Gme  does  sorGng  take?  

• 

Comparison-­‐based  sort:    A[i]  <=  A[j]    

– 

Upper  bound  –  current  best-­‐known  algorithm  

– 

Lower  bound  –  theoreGcal  “at  least”  esGmate    

– 

If  they  are  equal,  we  have  theoreGcally  opGmal  

soluGon  

(68)

Lihtne  sorteerimine  

 

   for  i=2..n  

 for  j=i  ;    j>1  ;  j-­‐-­‐      

 

 if  A[j]  <  A[j-­‐1]  

 

 then    swap(  A[j],  A[j-­‐1]    )  

 

 

else

 next  

i

   

 

 

(69)

The  divide-­‐and-­‐conquer  design  

paradigm  

1.  

Divide

 the  problem  (instance)  into  

subproblems.  

2.  

Conquer

 the  subproblems  by  solving  them  

recursively.  

(70)

Merge  sort  

 

Merge-­‐Sort(A,p,r)

   

if  p<r    then    q  =  (p+r)/2

 

   

   Merge-­‐Sort(  A,  p,  q  )    

   Merge-­‐Sort(  A,  q+1,r)  

   Merge(  A,  p,  q,  r  )  

 

(71)

Example  

(72)

Wikipedia  /  viz.  

Va

lu

e

(73)
(74)
(75)
(76)
(77)

Kui  palju  võtab  sorteerimine  aega?  

• 

Võrdlustel  põhinev:    A[i]  <=  A[j]    

– 

Ülempiir  –  praegu  parim  teadaolev  algoritm  

– 

Kas  saame  hinnata  alampiiri?    

• 

Aga  kui  palju  kahendotsimine?    

• 

Aga  kuidas  lisada  elemente  tabelisse?  

• 

(kahend-­‐otsimis)  Puu  ?    

(78)

n

2.3727

(79)

Conclusions  

• 

Algorithm  complexity  deals  with  the  behavior  

in  the  long-­‐term  

– 

worst  case  

 

 -­‐-­‐  typical  

– 

average  case  

 

 -­‐-­‐  quite  hard  

– 

best  case

 

 

 -­‐-­‐  bogus,  

chea-ng

 

• 

In  pracGce,  long-­‐term  someGmes  not  

necessary  

– 

E.g.  for  sorGng  20  elements,  you  don’t  need  fancy  

algorithms…  

References

Related documents

It has been accepted for inclusion in TREC Final Reports by an authorized administrator of PDXScholar. Please contact us if we can make this document

&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore.&lt;/p&gt;

Area Custodial Coordinators 5 FTE Custodial Staff 14 FTE Custodial Services Administrator 1 FTE Director of Maintenance 2 FTE Environmental Programs 9 FTE Energy Management 2

a. “Corporation” means the Food Corporation of India Established under Section3 of the Food Corporation Act’1964 and will include its Managing Director/Secretary and its

Over the past nearly two decades, Nutrasource has expanded its services far beyond its original omega-3 blood test to include international regulatory capabilities,

The data center manager should ensure that formal standards exist for systems development and maintenance, program and system testing, file conversion, program and system

Of the 16 programs, 12 reported at least one standing subcommittee (range = 1 to 10), with smaller programs reporting a mean of 1.5 committees, medium-sized programs a mean of 4.0

In light of the above examples, the question of how to make ISE more efficient and in what ways the existing (or new) legal regulations could be introduced and implemented to