50
CS 161: Design and Analysis of Algorithms Mark Zhandry

CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

CS 161: Design and Analysis of Algorithms

Mark Zhandry

Page 2: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Outline

• Why study algorithms? • What makes a good algorithm? • What We’ll See

– Example: Multiplication

• Asymptotics review • Course information

Page 3: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Why Study Algorithms?

• Required for CS majors • Algorithms are everywhere in CS

– OS (CS 140) – Compilers (CS 143) – Crypto (CS 155) – Etc.

• Algorithms important in other fields – Economics (Game Theory) – Biology

• Exciting!

Page 4: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

What Makes a Good Algorithm

• Computes desired result – Always? – With high probability? – On real-world inputs?

• Uses resources efficiently – Time? – Space? – Disk I/O? – Programmer Effort?

Page 5: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

CS 161 Concepts

• Data Structures • Graph Algorithms • Greedy Algorithms • Divide & Conquer • Dynamic Programming • Linear Programming • NP-Completeness

Page 6: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Algorithms We’ll See

• Integer/Matrix Multiplication • Fast Fourier Transform (FFT) • Shortest Paths • Sequence Alignment • Minimum Spanning Trees • Maximum Flows

Page 7: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Integer Multiplication

• Grade-school algorithm 3764

X 689 33876

301120 + 2258400

2593396

3764 X 9 33876

3764 X 8 30112

3764 X 6 22584

Can we do better? Yes!

Page 8: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Matrix Multiplication

=

×

nnnn

n

n

nnnn

n

n

nnnn

n

n

ccc

cccccc

bbb

bbbbbb

aaa

aaaaaa

,2,1,

,22,21,2

,12,11,1

,2,1,

,22,21,2

,12,11,1

,2,1,

,22,21,2

,12,11,1

∑==

n

kjkkiji bac

1,,,•Computing each term uses n

multiplications and (n-1) additions •Total: ~2n3 operations

Can we do better? Yes!

Page 9: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Discrete Fourier Transform (DFT)

• Given a sequence a=(a1, a2, …, an), the DFT of a is defined as A=(A1, A2, …, An) where

• Important in signal processing, solving differential equations, polynomial multiplication, and integer multiplication!

∑=

=n

i

iknik aA

Page 10: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Discrete Fourier Transform (DFT)

• Appears to require ~n2 additions and

multiplications.

∑=

=n

i

iknik aA

Can we do better? Yes!

Page 11: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Shortest Paths

How do we determine the shortest path without exploring all paths?

Page 12: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Sequence Alignment

• How close is “snowy” to “sunny”? – Cost: 3 modifications:

• Applications:

– Spell-checkers: If I have a misspelled word, what did I mean?

– Biology: Identify sections of DNA that are similar

S – N O W Y S U N N – Y

Page 13: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Minimum Spanning Trees

Page 14: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Minimum Spanning Trees

• Applications to network design, approximation algorithms, and more.

Page 15: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Maximum Flows

Page 16: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Maximum Flows

• Used in solutions for a host of different problems

Page 17: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 1: Grade-school algorithm 3764

X 689 33876

301120 + 2258400

2593396

3764 X 9 33876

3764 X 8 30112

3764 X 6 22584

Page 18: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 1: Grade-school algorithm – For simplicity, assume running time dominated by

single-digit multiplications – Number of single-digit multiplications: n2 – Can we do better?

Page 19: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 2: Recursive algorithm – Write n-digit number x as 10n/2 x1 + x2

• x1, x2 are n/2-digit numbers

– Write y as 10n/2 y1 + y2

– xy = 10n x1y1 + 10n/2(x1y2 + x2y1) + x2y2 – Can multiply n-digit numbers by performing 4

multiplications of n/2-digit numbers.

Page 20: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 2: Recursive algorithm – xy = 10n x1y1 + 10n/2(x1y2 + x2y1) + x2y2 – Claim: the number of single-digit multiplications

for algorithm 2 is n2. • True for n=1 • Assume true for n/2. Then #(multiplications for n-bits)

= 4 #(multiplications for n/2 bits) = 4 (n/2)2 = n2.

Page 21: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Which algorithm is better? – Both require n2 single-digit multiplications, so

running time almost the same

Page 22: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 3: Another Recursive Algorithm – We need the quantities x1y1, (x1y2 + x2y1), and x2y2 – Can we compute x1y2 + x2y1 using one multiplication? – Gauss: x1y2 + x2y1 = (x1 + x2) (y1 + y2) – x1y1 – x2y2

• Already have x1y1 and x2y2!

Page 23: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 3: Another Recursive Algorithm – Compute P = x1y1, Q = x2y2. – Let x3 = x1 + x2, y3 = y1 + y2. – Compute R = x3y3

– Let S = R – P – Q. – xy = 10n P + 10n/2 S + Q

Page 24: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Algorithm 3: Another Recursive Algorithm – Replaced 4 multiplications with 3, seems like it

should be faster. – Not so simple:

• One of the multiplications is slightly larger. • Added some extra overhead (additions and

subtractions)

– Possible to show < 4n1.6 single-digit multiplications

Page 25: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Example: Integer Multiplication

• Which algorithm is faster? – 4n1.6 > n2 for n < 32. – 4n1.6 < n2 for n > 32.

• Algorithm 3 slower for n < 32, faster for n>32. • How do we compare?

Page 26: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

How to Compare Algorithms

• Say we want to compute a function f(n) – Algorithm 1 runs in 100,000n seconds – Algorithm 2 runs in n2 seconds

• Which algorithm is better? – Algorithm 2 runs faster if n<100,000 – Algorithm 1 runs faster if n>100,000 – The ratio of run times goes to infinity

Page 27: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

How to Compare Algorithms

• Say we want to compute a function g(n) – Algorithm 1 runs in 20n seconds – Algorithm 2 runs in 10n+30 seconds

• Which algorithm is better? – Algorithm 2 is faster than Algorithm 1 when n>3 – However, it is only twice as fast

Page 28: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Asymptotics/Big Oh

• We say Algorithm 1 is “at least as fast as” Algorithm 2 if, for large enough inputs, Algorithm 1 is at most a constant factor slower than Algorithm 2.

• For this class, we will compare algorithms using “at least as fast as”.

Page 29: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh

• So g(n) ∈ O(f(n)) if – There are c and n0 such that – g(n) ≤ c f(n) for all n ≥ n0.

• Sometimes write g(n)=O(f(n)).

such that ,:)())((

00 nnc f(n) g(n)ncngnfO

≥∀≤∃=

Page 30: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Example 1

• f(n) = 10n+30, g(n) = 20n • f(n) ∈ O(g(n))

– Proof: Let c = 1, n0 = 3. – If n ≥ n0, then f(n)=10n+30 ≤ 20n =c g(n).

• g(n) ∈ O(f(n)) – Proof: Let c = 2, n0 = 1. – If n ≥ n0, then g(n)=20n ≤ 2 x (10n+30) =c f(n).

Page 31: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Example 1

Page 32: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Example 2

• f(n) = 100,000n, g(n) = n2. • f(n) ∈ O(g(n))

– Proof: Let c = 1, n0 = 100,000. – If n ≥ n0, then f(n)=100,000n ≤ n2 =c g(n).

• g(n) ∈ O(f(n))?

Page 33: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Example 2

• f(n) = 100,000n, g(n) = n2. • If g(n) ∈ O(f(n)), then

– There are c, n0 such that – g(n) ≤ c f(n) for all n ≥ n0.

• But, for any c, suppose n > 100,000c. – Then g(n)= n2 > 100,000c n=c f(n)

• So g(n) ∉ O(f(n)).

Page 34: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Example 2

Page 35: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Facts

• Big Oh allows us to ignore constant factors: – For any constant c, c f(n)∈ O(f(n))

• Big Oh allows us to ignore lower order terms: – If f(n)∈ O(g(n)), then g(n)+f(n) ∈O(g(n))

Page 36: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Facts

• If a ≤ b, then na ∈ O(nb) – Proof: na ≤ nb for all n ≥ 1.

• If 1<a ≤ b, then an ∈ O(bn) – Proof: an ≤ bn for all n > 0.

• For any a,b, loga(n) ∈ O(logb(n)) – Proof: loga(n)=logb(n) loga(b) for all n > 0.

We can usually ignore the base for logarithms. We will use log(n) to denote log2(n)

Page 37: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Facts

• For any a>0, b>1, na ∈ O(bn) – We’ll see a proof in a bit

• For any a>0, log n ∈ O(na) – We’ll see a proof in a bit

Page 38: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Facts

• If f(n) ∈ O(g(n)), and g(n) ∈ O(h(n)), then f(n) ∈ O(h(n))

– Proof: There are c, n0 such that f(n) ≤ c g(n) for n ≥ n0. – There are c’, n0’ such that g(n) ≤ c’ f(n) for n ≥ n0’. – Let c”=c c’, n0”=Max(n0, n0‘). – If n ≥ n0”, then f(n) ≤ c g(n) ≤ c c’ h(n) =c” h(n).

• f(n) ∈ O(f(n)): let c=1, n0=1.

Big Oh inclusion is a sort of “≤” on functions

Page 39: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Omega

• So g(n) ∈ Ω(f(n)) if

– There are c and n0 such that – g(n) ≥ c f(n) for all n ≥ n0.

• Sometimes write g(n)=Ω (f(n)).

such that ,:)())((

00 nnc f(n) g(n)ncngnf

≥∀≥∃=Ω

Page 40: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Omega

• g(n) ∈ Ω(f(n)) is equivalent to f(n) ∈ O(g(n)) – Proof: f(n) ∈ O(g(n)) means there is c, n0 such that f(n) ≤ c g(n) for n ≥ n0. – Then g(n) ≥ (1/c) f(n) for n ≥ n0. – Similar proof for other direction.

Big Omega inclusion is a sort of “≥” on functions

Page 41: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Theta

• g(n) ∈ Θ(f(n)) if: – g(n) ∈ O(f(n)) and – g(n) ∈ Ω(f(n)) (or equivalently f(n) ∈ O(g(n)) )

• Note: the constants c, n0 may be different for showing g(n) ∈ O(f(n)) and g(n) ∈ Ω(f(n))

• g(n) ∈ Θ(f(n)) is equivalent to f(n) ∈ Θ(g(n))

Big Theta inclusion is a sort of “=” on functions

Page 42: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Little o, omega

• f(n) ∈ o(g(n)) if, for all c, there is an n0: f(n)<c g(n) for all n ≥ n0. • f(n) ∈ ω(g(n)) if, for all c, there is an n0: f(n)>c g(n) for all n ≥ n0. • f(n) ∈ o(g(n)) is equivalent to g(n) ∈ ω(f(n))

Little o and omega are a sort of “<” and “>” for functions

Page 43: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Big Oh Is Not Total!

• It is not true that f(n) ∈ O(g(n)) or g(n) ∈ O(f(n)) – f(n) = n1+Cos(πn), g(n) = n

• Similarly, f(n) ∉ O(g(n)) does not imply that f(n) ∈ ω(g(n))

Page 44: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Limit Test

• Let c = limn∞ f(n)/g(n) – If c = 0, then f(n) ∈ o(g(n)) – If 0 < c < ∞, then f(n) ∈ Θ(g(n)). – If c = ∞, then f(n) ∈ ω(g(n))

Page 45: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Limit Test

• For any a>0, log n ∈ o(na) – Proof: Let c = limn∞ log n/na – L'Hôpital's rule: c = limn∞(1/n)/(a na-1)= limn∞1/(a na) = 0.

Page 46: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Limit Test

• For any a>0, b>1, na ∈ o(bn) – Proof: Let c = limn∞ na/bn

– L'Hôpital's rule: c = limn∞ (a na-1)/(bn ln b)=(a/ln b) limn∞ na-1/bn – Repeat until a ≤ 0 – We have that c = const x limn∞ na’/bn for a’ ≤ 0

• Numerator goes to 0, denominator goes to infinity • Thus c = 0.

Page 47: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Other Notation

• f(n) ∈ g(n)+o(h(n)) means f(n)-g(n) ∈ o(h(n)) – Common example: f(n) ∈ g(n)+o(1) means f(n)

converges to g(n).

• f(n) ∈ g(n)O(h(n)) means there is some function h’(n) ∈ O(h(n)) such that f(n)=g(n)h’(n). – Common example: f(n) ∈ nO(1) means f(n) ∈ O(na)

for some a.

Page 48: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Course Information

• Lectures: WMF 2:15-3:30 • Text: Algorithm Design by Kleinberg and Tardos • Webpage: http://cs161.stanford.edu

• Piazza: http://piazza.com/class#summer2012/cs161

• Staff Email: [email protected]

Page 49: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Grading

• Homeworks: 50% – Five weekly homeworks

• Midterm: 20% – Wednesday, July 25th in class

• Final: 30% – Friday, August 17th, Location: TBA

Page 50: CS 161: Design and Analysis of Algorithmsmzhandry/2012-Summer-CS161/docs/lectures/... · CS 161: Design and Analysis of Algorithms Mark Zhandry . Outline • Why study algorithms?

Homework

• Assigned each Wednesday, due following Friday – Exceptions: no homework due week of midterm or final – Due at beginning of class (2:15PM)

• May work in groups of up to three – Write up solutions individually

• One 72-hour extension • Can submit digitally to

[email protected] • See website for policies