agenda - university at buffalohungngo/classes/2012/spring/250/lectures/... · agenda cse 250,...

38
Agenda 2/8/12 CSE 250, Spring 2012, SUNY Buffalo 1 The worst algorithm in the history of humanity Asymptotic notations: Big-O, Big-Omega, Theta An iterative solution A better iterative solution The repeated squaring trick

Upload: trinhthu

Post on 29-Jul-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Agenda

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

1

  The worst algorithm in the history of humanity

  Asymptotic notations: Big-O, Big-Omega, Theta

  An iterative solution

  A better iterative solution

  The repeated squaring trick

Page 2: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

T H E W O R S T A L G O R I T H M I N H I S T O R Y

CSE 250, Spring 2012, SUNY Buffalo 2/8/12

2

Fibonacci sequence

Page 3: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Fibonacci sequence

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

3

  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

  F[0] = 0   F[1] = 1   F[2] = F[1] + F[0] = 1   F[3] = F[2] + F[1] = 2   F[4] = F[3] + F[2] = 3   F[n] = F[n-1] + F[n-2]

Page 4: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Recursion – fib1()

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

4

/** ! *---------------------------------------------------------- ! * the most straightforward algorithm to compute F[n] ! *---------------------------------------------------------- ! */!unsigned long long fib1(unsigned long n) { ! if (n <= 1) return n; ! return fib1(n-1) + fib1(n-2); !} !

Page 5: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Run time on my laptop

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

5

Page 6: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

On large numbers

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

6

  Looks like the run time is doubled for each n++

  We won’t be able to compute F[120] if the trend continues

  The age of the universe is 15 billion years < 260 sec

  The function looks … exponential ¡  Is there a theoretical justification for this?

Page 7: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Functions

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

7

  Sometimes we mean a C++ function

  Sometimes we mean a mathematical function like F[n]

  A C++ function can be used to compute a mathematical function ¡  But not always

  What we mean should be clear from context

Page 8: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

G U E S S A N D I N D U C T S T R A T E G Y

T H I N K I N G A B O U T T H E M A I N B O D Y

CSE 250, Spring 2012, SUNY Buffalo 2/8/12

8

Analysis of fib1()

Page 9: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Guess and induct

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

9

  For n > 1, suppose it takes c mili-sec in fib1(n) not counting the recursive calls

  For n=0, 1, suppose it takes d mili-sec   T[n] = time fib1(n) takes   T[0] = T[1] = d   T[n] = c + T[n-1] + T[n-2] when n > 1

  To estimate T[n], we can ¡  Guess a formula for it ¡  Prove by induction that it works

Page 10: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

The guess

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

10

  Bottom-up iteration ¡  T[0] = T[1] = d ¡  T[2] = c + 2d ¡  T[3] = 2c + 3d ¡  T[4] = 4c + 5d ¡  T[5] = 7c + 8d ¡  T[6] = 12c + 13d

  Can you guess a formula for T[n]? ¡  T[n] = (F[n+1] – 1)c + F[n+1]d

Page 11: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

The Proof

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

11

  The base cases: n=0,1   The hypothesis: suppose

  T[m] = (F[m+1] – 1)c + F[m+1]d for all m < n

  The induction step:  T[n] = c + T[n-1] + T[n-2]

= c + (F[n] – 1)c + F[n]d + (F[n-1] – 1)c + F[n-1]d = (F[n+1] – 1)c + F[n]d

Page 12: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

How does this help?

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

12

F [n] =�n � (�1/�)np

5

� =1 +

p5

2⇡ 1.6

The golden ratio

Page 13: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

So, there are constants C, D such that

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

13

C�n T [n] D�n

This explains the exponential-curve we saw

Page 14: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

-  B A C K O F T H E E N V E L O P E T I M E / S P A C E E S T I M A T I O N -  I N D E P E N D E N T O F W H E T H E R O U R C O M P U T E R I S F A S T

-  B I G - O , B I G - O M E G A , T H E T A

CSE 250, Spring 2012, SUNY Buffalo 2/8/12

14

Asymptotic analysis

Page 15: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

From intuition to formality

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

15

  If we ran fib1() on a computer with C = 10-9:

  We need a formal way to state that (1.6)n is the “correct” measure of fib1()’s runtime ¡  How fast the target computer runs shouldn’t concern us

10

�9(1.6)140 � 3.77 · 1019 > 100 · age of univ.

Page 16: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Big-O

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

16

f, g : N ! R+

such that f(n) Cg(n), 8n � n0

f(n) = O(g(n)) i↵ 9 constants C, n0 > 0

Page 17: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Intuition

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

17

in our case T [n] = O(�n)

Page 18: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

In English

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

18

  f(n) = O(g(n)) means: for n sufficiently large, f(n) is bounded above by a constant scaling of g(n) ¡  Does the “English translation” make things worse?

  An algorithm with runtime f(n) is at least as good as an algorithm with runtime g(n), asymptotically

Page 19: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Examples

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

19

n2 = O(n2)

n2 = O(n2/106)

n = O(n2)

Page 20: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Big-Omega

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

20

f, g : N ! R+

such thatf(n) � Cg(n), 8n � n0

f(n) = ⌦(g(n)) i↵ 9 constants C, n0 > 0

Page 21: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

In picture

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

21

Page 22: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Examples

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

22

n log n = ⌦(n)

2n/106 = ⌦(n100)

Page 23: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Equivalence

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

23

f(n) = O(g(n)) , g(n) = ⌦(f(n))

Page 24: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Theta

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

24

f(n) = ⇥(g(n)) , f(n) = O(g(n) and g(n) = O(f(n))

in fib1() example: T [n] = ⇥(�n)

We say they “have the same growth rate”

Page 25: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

In picture

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

25

Page 26: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

-  A L I N E A R T I M E A L G O R I T H M U S I N G V E C T O R S

-  A L I N E A R T I M E A L G O R I T H M U S I N G A R R A Y S

-  A L I N E A R T I M E A L G O R I T H M W I T H C O N S T A N T S P A C E

CSE 250, Spring 2012, SUNY Buffalo 2/8/12

26

Better algorithms for F[n]

Page 27: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

An algorithm using vector

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

27

unsigned long long fib2(unsigned long n) { ! // this is one implementation option! if (n <= 1) return n; ! vector<unsigned long long> A; ! A.push_back(0); A.push_back(1); ! for (unsigned long i=2; i<=n; i++) { ! A.push_back(A[i-1]+A[i-2]); ! } ! return A[n]; !}

Guess how large an n we can handle this time?

Page 28: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Data

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

28

n 106 107 108 109

# seconds 1 1 9 Eats up all my CPU/RAM

Page 29: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

How about an array?

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

29

unsigned long long fib2(unsigned long n) { ! if (n <= 1) return n; ! unsigned long long* A = new unsigned long long[n]; ! A[0] = 0; A[1] = 1; ! for (unsigned long i=2; i<=n; i++) { ! A[i] = A[i-1]+A[i-2]; ! } ! unsigned long long ret = A[n]; ! delete [] A; ! return ret; !} !

Page 30: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Data

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

30

n 106 107 108 109

# seconds 1 1 1 Segmentation fault

Data structure matters a great deal!

Some assumptions we made are false if too much space is involved: computer has to use hard-drive as memory

Page 31: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Dynamic programming!

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

31

unsigned long long fib3(unsigned long n) { ! if (n <= 1) return n; ! unsigned long long a=0, b=1, temp; ! unsigned long i; ! for (unsigned long i=2; i<= n; i++) { ! temp = a + b; // F[i] = F[i-2] + F[i-1]! a = b; // a = F[i-1]! b = temp; // b = F[i]! } ! return temp; !}

Page 32: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Data

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

32

n 108 109 1010 1011

# seconds 1 3 35 359

The answers are incorrect because F[108] is greater than the largest integer representable by unsigned long long But that’s ok. We want to know the runtime

Page 33: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

-  T H E R E P E A T E D S Q U A R I N G T R I C K

CSE 250, Spring 2012, SUNY Buffalo 2/8/12

33

An even faster algorithm

Page 34: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Math helps!

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

34

  We can re-formulate the problem a little: 1 11 0

� 10

�=

11

1 11 0

� 11

�=

21

1 11 0

� 21

�=

32

1 11 0

� F [n� 1]F [n� 2]

�=

F [n]

F [n� 1]

F [n+ 1]F [n]

�=

1 11 0

�n 10

Page 35: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

How to we compute An quickly?

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

35

  Want

  But can we even compute 3n quickly?

1 11 0

�n

Page 36: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

First algorithm

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

36

unsigned long long power1(unsigned long n) { ! unsigned long i; ! unsigned long long ret=1; ! for (unsigned long i=0; i<n; i++) ! ret *= base; ! return ret; !}

When n = 1010 it took 44 seconds

Page 37: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Second algorithm

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

37

unsigned long long power2(unsigned long n) { ! unsigned long long ret; ! if (n == 0) return 1; ! if (n % 2 == 0) { ! ret = power2(n/2); ! return ret * ret; ! } else { ! ret = power2((n-1)/2); ! return base * ret * ret; ! } !}

When n = 1019 it took < 1 second Couldn’t test n = 1020 because that’s > sizeof(unsigned long)

Page 38: Agenda - University at Buffalohungngo/classes/2012/Spring/250/lectures/... · Agenda CSE 250, Spring 2012, SUNY Buffalo 2/8/12 1 The worst algorithm in the history of humanity Asymptotic

Runtime analysis

2/8/12 CSE 250, Spring 2012, SUNY Buffalo

38

  First algorithm O(n)

  Second algorithm O(log n)

  We can apply the second algorithm to the Fibonacci problem: fib4() has the following data

n 108 109 1010 1019

# seconds 1 1 1 1