introduction: efficient algorithms for the problem of ...reif/courses/alg... · introduction:...

23
Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John Reif, Ph.D. Analysis of Algorithms

Upload: others

Post on 17-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Introduction: Efficient Algorithms for the Problem of Computing

Fibonocci Numbers

Prepared by

John Reif, Ph.D.

Analysis of Algorithms

Page 2: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Readings

•  Main Reading Selection: – CLR, Chapter 1

Page 3: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Goal

•  Devise Algorithms to solve problems on a TI calculator

•  assume each step (a multiplication, addition, or control branch) takes 1 µ sec = 10-6 sec on 16 bit words

Page 4: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Time Efficiency

Page 5: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Fibonocci Sequence

•  0, 1, 1, 2, 3, 5, 8, 13…

•  Recursive Definition •  Fn = if n < 1 then n

else Fn-1 + Fn-2

•  Problem •  Compute Fn for n=109 fast.

Page 6: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Fibonacci Growth •  Can show as n à ∞

•  Golden ratio

•  So Fn ~ .45 ∙ 2 .7n is .7n bit number grows exponentially!

nn

n 5)( ~ F−−− φφ

...62.12

51 =

+=φ

Page 7: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Obvious Method

•  Fn =

> .01 n2 steps > 1016 µ sec for n = 109

= 1010 sec ~ 317 years!

ncosts at least adds of 2

.7n .35n bit numbers2=

n if n < 1

Fn-1 + Fn-2 if n > 1

⎟⎠

⎞⎜⎝

⎛⎟⎠

⎞⎜⎝

⎛≥bits 61bitsn 35.adds

2n TotalCost

Page 8: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Wanted!

•  An efficient algorithm for Fn

•  Weapons: •  Special properties of computational

problems

= combinatorics (in this case)

Page 9: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Theorem:

•  Proof by Induction – Basis Step

•  Holds by definition for n=1 –  Inductive Step

•  Assume holds for some n>0

⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

+

1nn

n1nn

FFFF

1011

Page 10: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Inductive Step

⎟⎟⎠

⎞⎜⎜⎝

⎛=

⎟⎟⎠

⎞⎜⎜⎝

⎛ ++=

⎟⎟⎠

⎞⎜⎜⎝

⎛⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

+

++

+

−+

+

+

n1n

1n2n

n1n

1nnn1n

1nn

n1n1n

FFFF

FFFFFF

FFFF

0111

1011

Page 11: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Powering Trick

•  Fix M =

•  To compute Mn when n is a power of 2 For i=1 to log n do

1 1

1 0

( ) ( )logn2

1-i1-ii

222

222

M ... ,M,M M, gives

MMM ×=

Page 12: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

General Case of Powering •  Decompose n = 2 j1 + 2 j2 + ... + 2 jk

as sum of powers of 2 •  Compute:

•  Example:

23= 24 + 22 + 21 + 20 =16+ 4+ 2+1=10111 2

Mn = M2ji

i=1,...,k∏

M23 = M16+4+2+1 =M244 +22+21+20

= M24

M22

M21

M20

Page 13: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Algorithm

⎟⎟⎠

⎞⎜⎜⎝

⎛=1011

M

(1) compute M, M2, ..., M2 logn

by power method

(2) if n = 2 j1 + 2 j2 +...+ 2 jk then Mn = M2 ji

i=1,...,k∏

(3) output Fn = upper right of Mn

Page 14: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Bounding Mults

•  = 2 log n matrix products on symmetric matrices of size 2 x 2

•  Each matrix product costs 6 integer mults

•  Total Cost = 12 (log n) integer mults > 360 for n = 109 integer mults

Page 15: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Time Analysis

•  Fn ~ .45 2 .7n

•  So Fn is m = .7n bit integer

•  New method to compute Fn requires multiplying m bit number

•  BUT •  Grammar School Method for Mult

takes m2 bool ops = m2/16 steps ~ 1016 steps for n = 109 ~ 1016 µsec ~ 1010 seconds ~ 317 years!

Page 16: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Fast Multiplication of m bit integers a,b by •  “Divide and Conquer”

•  seems to take 4 multiplications, but…

Rk

L

Rk

L

b2bb

a2aa

+⋅=

+⋅=

aL aR

bL bR

RRk

LRRL2k

LL ba)2baba(2baba2m k where

+++=⋅

=

Page 17: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

3 Mult Trick: Karatsubu Algorithm

LRRL

RLRL

RR

LL

baba y)(x)bb()aa( z )3(

bay )2(

ba x(1)

+=

+−+⋅+=

⋅=

⋅=

y2z2 x

ba2)bab(a2ba ba

bits-mon adds 6 and integersbit - 2mon

3only Requires

k2kRR

kLRRL

2kLL

+⋅+=

+++=⋅

mults

Page 18: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Recursive Mult Algorithm

•  Cost Cm bit ops C1 =1

Cm ≤ 3Cm2

+6m for m >1

≤32

⎝⎜⎞

⎠⎟

i=0

log m

∑i

6m

≤ 2 32

⎝⎜⎞

⎠⎟

log m

6m

≤12mlog2 3

~ 12m1.4 bit ops

~ 1013 bit ops if m = .7 109

~1016

13

steps

~.05 years

Page 19: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Schonhage-Strassen Integer Multiplication Algorithm

•  This is feasible to compute!

12 9

10

4

10m log m log log m bit ops~ .7 10 bit ops for m .7 10~ 4.3 10 steps ~4.3 10 sec. ~3 days

⋅ = ⋅

Page 20: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Improved Algorithm

•  Computed Fn for n = 109 using 360 = 12 log n integer mults each taking ~ 3 days

•  Total time to compute Fn •  ~ 3 years with 1 µsec/step

Page 21: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

How Did We Get Even More Speed-Up?

1)  New trick •  a 2 log n mult algorithm

(rather than n adds) 2)  Old Trick

•  use known efficient algorithms for integer multiplication (rather than Grammar School mult)

3)  Also used careful analysis of efficiency of algorithms to compare methods

Page 22: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Problem (to be pondered later…)

•  How fast can 109 integers be sorted?

•  What algorithms would be used?

Page 23: Introduction: Efficient Algorithms for the Problem of ...reif/courses/alg... · Introduction: Efficient Algorithms for the Problem of Computing Fibonocci Numbers Prepared by John

Introduction: Efficient Algorithms for the Problem of Computing

Fibonocci Numbers

Prepared by

John Reif, Ph.D.

Analysis of Algorithms