22
1 CS120: Lecture 9 MP Johnson Hunter [email protected]

1 CS120: Lecture 9 MP Johnson Hunter [email protected]

  • View
    224

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

1

CS120: Lecture 9

MP Johnson

Hunter

[email protected]

Page 2: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

2

Agenda: Algs

• Next topic: algs– Hw2 due Wed– MT Thursday– No unexcused absences

• Next after that: JavaScript– Next week’s lab: JS &

advanced HTML

• For Tues: read handout on stable marriage

• Defs• Complexity ftns• Probs:

– Add nums– Mult nums– Gcd– Sorting– Binary search– Stable marriage– Prefix-sum– Poly eval– Towers of Hanoi

• Order-of-magn calcs

Page 3: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

3

Hist, motiv

• Def: ordered set of precise instructions, executable steps that defines a finite process

• You write programs, which implement some alg

• Etym: Persian math. Al-Khowarizmi Arabic al-khuwarizmi Medieval Latin

algorismus Middle English algorisme algorithm

• Also: title of book algebra

Page 4: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

4

def

1. Alg: idea behind program– seq of steps for solving some prob

• finite• well defined• each step tractable

2. (step 3 can’t be “compute Pi exactly” or “find meaning of life”)

3. alg is a mapping:– A:{instances} {outputs}– outputs could be bits/decisions: prime or not?

or larger structure: position of number; sorted seq

4. intuition: what stays the same when prog is ported from C to J to Basic to Assembly

Page 5: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

5

To be an alg

• Each step must be unambiguous– Can be converted to machine instructions

• Each step must be doable– Can’t say “calc Pi exactly” or “print meaning of

life”

• Must be finite– Description is finite– For each poss. input, must eventually stop

Page 6: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

6

Alg goals

1. ideally, alg should be:1. Correct2. efficient – or at least optimal

2. each can be difficult3. sometimes: trade-off between difficulty of each

– easy-to-understand alg may be slow– fast alg may be difficult to understand (or to find!)

4. not really surprising: “obvious” soln often very slow

Page 7: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

7

To be a good alg

• Also: want algorithm to be fast– In a precise sense…

• Should scale well on different inputs

• Shouldn’t take too much storage space

• Should be easy to understand/code

Page 8: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

8

Alg v. program

• A program implements an alg– Program v. alg v. process

• Book analogy:– Story:book :: alg:program

– Story can be translated between langs, printed in hardcover of paperback

– Alg can be translated between langs, run on Win or Mac

– The alg is what stays the same when it’s ported

Page 9: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

9

Algs you already know

1. Dial your phone number2. Combination lock3. Cooking4. Add numbers in memory:

• Fixed-length alg

5. Addition itself (of bitstrings)• Length depends on bitstring size

Page 10: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

10

Algs you already know6. Long additon, etc. (base-10 ops)

1. Let rightmost column be current2. Add the digits in the current col3. Write first dig of result as answer for that col4. If result >9, add 1 to previous digits (“carry”)5. Move “current column” to left6. Go to step 2

7. Processor’s alg:– Fetch an instruction– Decode it– Execute it– Go to step 1

Page 11: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

11

Problem-solving

• Programming can be hard (fixing bugs)• But deciding what to program (finding the alg)

can often be harder

• Given problem description, must find some (good) alg

• No general-purpose method that always works• Trial&error, experience• Would like: an alg to produce alg, but there isn’t

one

Page 12: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

12

TSP: Hilary motiv1. algs are designed to solve problems, in all cases

– should work correctly (quickly) on all possible inputs2. eg: say Hilary has to campaign in N cities

– maybe all state caps– maybe a subset

3. Hilary needs to save gas– no Air Force One– needs optimal route to: visit each stop, return to starting place

• Washington? Boston?

4. Hilary can look at the map– use geometric reasoning– intuition based on experience– very sophisticated– hard – see an ML class

5. Hilary’s intuition may work for small cases– for larger ones, very likely will be highly suboptimal

6. we want an alg: guaranteed to always return a best route– may not be unique

Page 13: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

13

TSP: Hilary motiv1. Where does she start?• NYC

2. Now what?• Obvious choice: jump to closest city• Say, Bos

3. Now what?• Again, jump to closest (unvisited) city• Thus ag:

4. Pick arbitrary p0;Set I = 1;While (I < n)

Set pi = closest new city to p(i-1)

Page 14: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

14

TSP: Greedy alg

1. We’ve stumbled onto the “greedy algorithm” for this prob– many other probs have “Greedy algs”: alg does maximal

optimization at each step– an. To short-term self interest v. long-term self-interest

2. this greedy alg has much to recommend it:– very easy to understand– very easy to code up– pretty efficient

• for each next move, just choose among those remaining

3. only problem: it’s wrong– i.e., the resulting path won’t always be the shortest one possible

Page 15: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

15

TSP: Greedy alg1. consider this case: (sequence of dots)2. greedy produces this 3. clearly optimal is that• eg:

– NYCBos 215– NYCDC 232

4. prob not just all in a line– Prob also occurs for T-shaped– Anytime something like this as a subgraph– just one example

5. what happened? Too short-sighted– cities that were next to each other were separated on the tour– should have been kept together

Page 16: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

16

TSP: Joining pairs

1. idea: join together near pairs, then do greedy on results

– maybe this will work?

2. Apply this strategy to line of dots produces optimal

3. What about to this eg: Grid of dots– wider than tall– suboptimal again!

4. Clearly better tours exist: …– This alg didn’t work either

• Intuition: writing algorithms = raising children

Page 17: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

17

TSP: Brute force

1. So what does work?– Let’s slow down– Recall: wanted best ordering– How many orderings? Only so many

2. Following must work: run through all possible, measure each one

3. Code:• Best = -1

Cost = infinityI = 0While (I < n)

If (best == -1 or cost[i] < cost[best])Best = I;

Return best;

Page 18: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

18

TSP: Brute force1. Correct?

– yes, must be

2. efficient?– NO, very inefficient– number of steps roughly n!– very bad: 5! = 120, 10! = 3,628,800, 20! = 10^18, 33! = 8^36

3. Q: can we do better?– Well, somewhat—basically clever software engineering, using info from

special cases, clever coding

4. But no essentially faster alg– There’s no fast alg (so far as anyone knows!)– No proof of its nonexistence, but would be a huge surprise– In other cases, we’ll have better luck

Page 19: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

19

Measuring running time

• Logn• N• N^2• N^k• 2^n• N!

• O(f(n)) – “big O”, Th(f(n)) – theta

• Big-O matters, coeffs don’t

N n^2 2^n51020

Page 20: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

20

Log review

• df: logbX = y b^y = x– intuit: Y is number of b’s multiplied together yield X– i.e., log is inverse of exponentiation function

• Log2(x)= y 2^y = x

• 2^10 = 1024 = k log1024• 2^8 = 256 …• 2^16 = 64k• 2^20 = M• 2^30 = G

Page 21: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

21

search

• Sequential search– Alg?– Cmplx?

• Binary search– Alg?– Analogy: phonebook– Cmplx?

• Q: how many times to /2?

• N logn• 1024• …

Page 22: 1 CS120: Lecture 9 MP Johnson Hunter mpjohnson@gmail.com

22

• Order of magnitude calculations?