cs120: lecture 10 mp johnson hunter [email protected]

25
CS120: Lecture 10 MP Johnson Hunter [email protected]

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

CS120: Lecture 10

MP Johnson

Hunter

[email protected]

admin

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

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

• For Wed: bring questions!

Agenda

• Defs• Complexity ftns• Probs:

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

• Review: ord-of-mag est– 2^80/(10^10/s)

• Stable Marriage

• Programming– Algorithms programs– Pseudocode– JavaScript

Alg/log review

• Other egs:– Reading Knuth– Decbin

• Cmplxs of:– TSP greedy– TSP brute-force– Sequential search– Bin search

• Exp/log:– 8– 10– 16– 20– 30

20Q

• Best kind of Qs: searchspace/2

• Range

• Or bit values

Pseudocode

• Later will write programs– Must by syntactically correct

• This week: talking about algs– Syntax not important

• Will write algs in pseudocode– Can assign to vars– Can do math– If/then/else– While loop

• Do seq search, bin search in pseudocode

Bin search v. seq search

• Compute actual times for:– 8M people– Stored on disk– 10ms per access

• Estimate times

other times/cmplxs

Cmplx & big-O

• New topic: big-O - motiv• Idea 1: measure actual runtimes

– Prob: alg written for general case• Works on insts of many sizes• By any alg, sort (10,000) slower than sort (10)

• idea 2: measure runtime as ftn of input– Makes sense maybe for factoring nums– But what about sorting objects?

• Idea3 : measure runtime as ftn of input size– Prob: faster, slower computers

• Idea 4: measure #ops in abstract RAM model as ftn of input size

Canonical complexity • O(1) “constant” A[0], A[10000], 2+3, 2*3

• O(log n) bin search, 20 questions

• O(n) “linear” file download, seq search

• O(n^2) “quadratic” ins sort, TSP Greedy

• O(n^k)“polynomial” matrix mult

• 2^n “exponential” 3-col graph, bin packingprotein folding

• n! “factorial” TSP Brute

Complx ftn table

10 30 50 100

n 10^-9s 3*10^-9s 5*10^-9s 10^-8s

n^2 10^-8s 9*10^-8s 2.5*10^-7s 10^-6s

N^3 10^-7s 2.7*10^-6s 1.25*10^-5s .0001s

N^5 10^-5s .00243s .03125s 1s

2^n 10^-7s .107s 112,589s 1.3*10^20s = 10^13m

Basis for big-O• X^3 v nx^3 doesn’t matter

– Wait log1.5(n) years

• X^3 v. x^3 + 800x^2 doesn’t matter because:– Replace x^3 with 2x^3 (allowed above)– At first, second bigger than 2x^3– But after x=800, 2x^3 bigger

• 2^n v 2^(n+1) doesn’t matter– 2^(n+1) = 2*2^n = O(2^n)

New prob: gcd

• Input: Two integers x > 0 and y > 0.

• Output: The maximum integer z s.t. z|x, z|y: z = gcd(x, y)

• Examples:– 5 = gcd(5, 15)– 6 = gcd(12, 18)– 1 = gcd(13, 21)

Gcd Alg 1

• Idea: If z = gcd(x, y) then no divisors larger– But gcd is can’t be larger than x or y

• gcd(x, y):

• z := min {x, y} + 1

• repeat– z := z − 1

• until (x % z = 0) and (y % z = 0)

• Return z

Gcd Alg 2: better

• Idea: x>y every divisor of x and y will divide x-y and y (why?)– visualize with rectangles

• gcd2(x, y):

• if x = y then return x

• if x < y then switch x and y

• return gcd(x − y, y)

Gcd Alg 3: Euclid

• Idea: x>y every divisor of x and y will divide x%y and y (why?)

• gcd2(x, y):• if x = y then return x• if x < y then switch x and y• return gcd(x % y, y)

• Can show: time is log(max(x,y))

Larger eg: Gcd(1989,1590)

• Alg 1: decrement down from 1590– Answer is 3

• Alg 2: 1989-1590 = 399– 1590-399, …

• Alg 3 (Euclid):• 1989%1590 = 399• 1590%39 = 30• 39%30 = 9• 30%9 = 3

Mairson: Why study computers?• “We all know about computers, because they are

everywhere…. Everyone knows about word processors, spelling checkers, spreadsheets, laser printers…

• “Notice, though, that all of these electronic ``essentials'' are meant to relieve some kind of daily tedium: they don't help us do or think anything new, except in freeing us from some laborious activity. We could in principle type correctly and not misspell words, and balance checkbooks, but why not have computers manage these mundane tasks for us? … a computer balances a checkbook the same way that we do, only it doesn't make mistakes when it's adding and subtracting.

• “But this mundane activity, even if wrapped up in a chip the size of your thumbnail, is not worth inclusion in a university curriculum, as a counterpart to Thackeray, Thucydides, or thermodynamics. What is intellectually important about the computer is the idea of a computer, the variety of computational processes that inhabit it, and how these can make us think in new ways that we might have never before considered. Solving the problem of stable marriage is an example of such new thinking.”

Stable marriage problem

• Problem def:– given M and W, |M|=|W|=n, and a preference list for

each member,– pair them m’s and w’s, so the pairings are stable

• We can’t guarantee first choice to everyone (legal issues), but we at least want stability:

• If we have A-B and C-D, A and D won’t both prefer one another– No mutual adultery– 19th C lit would have been different with computers

Matchmaker alg

• Proceeds in rounds• 1st round: each m proposes to first-choice w• If a w gets >1 offers, she accepts her first choice

– Then they’re engaged

• 2nd round: each unengaged m proposes to his second-choice w

• if an unengaged w gets >1, picks first choice, as before

• If an engaged w gets get more proposals, accepts first choice, if better than curr fiancé

• And so on…

Example

• M:1. 23145

2. 32514

3. 23451

4. 32514

5. 32415

• W:1. 12345

2. 25341

3. 15234

4. 12345

5. 21345

Check alg• Each step clear? Y• Does it finish? Y

– In each round, men propose– Each M proposes <= n times

• Can anyone be left out? N– They get selected in pairs– Could we have some m,w both unpaired?– No: if w alone, no one proposed to her

• But eventually m must

• What is complexity? n^2

• Are the matches stable? Y

Stability?• Could we have A-B, C-D, s.t. AD?

– Al,Betty,Chuck,Denise• Suppose so• Then A proposed to D first

– Either D accept or not

• Suppose D accepted A– Then later she switched to T, so she prefers T

• Suppose D rejected A– Then she was engaged to someone better, before

switching to T, so she still prefers T

• QED• strategy: proof by cases

– Analysis again!

Fair?

• This alg is “male-optimal”

• E.g.: A->B, C->D, B->C, D->A AB,CD

• In residency assignments, hospitals play male role

• Students wanted to choose, but were denied

Abstraction & simulation

• Same logic with M-W and students-hospitals• Meaning doesn’t matter

– Computer only needs the logic/alg

• Computers can play chess, without “understanding” the game

• Computers can simulate the weather, without getting wet

• Computers can simulate intelligence, without being intelligent (?)– Chess, help with hw, make jokes, debate the movie

Programming

• Algorithms programs

• Can express in pseudocode

• But must code in some prog lang

• This week: lang = JavaScript in webpages