Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS120: Lecture 10 MP Johnson Hunter

Similar presentations


Presentation on theme: "CS120: Lecture 10 MP Johnson Hunter"— Presentation transcript:

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

2 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!

3 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

4 Alg/log review Other egs: –Reading Knuth –Dec  bin Cmplxs of: –TSP greedy –TSP brute-force –Sequential search –Bin search Exp/log: –8 –10 –16 –20 –30

5 20Q Best kind of Qs: searchspace/2 Range Or bit values

6 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

7 Bin search v. seq search Compute actual times for: –8M people –Stored on disk –10ms per access Estimate times  other times/cmplxs

8 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

9 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 packing protein folding n!“factorial”TSP Brute

10 Complx ftn table 103050100 n10^-9s3*10^-9s5*10^-9s10^-8s n^210^-8s9*10^-8s2.5*10^-7s10^-6s N^310^-7s2.7*10^-6s1.25*10^-5s.0001s N^510^-5s.00243s.03125s1s 2^n10^-7s.107s112,589s1.3*10^20s = 10^13m

11 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)

12 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)

13 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

14 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)

15 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))

16 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

17 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.”

18 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 –19 th C lit would have been different with computers

19 Matchmaker alg Proceeds in rounds 1 st round: each m proposes to first-choice w If a w gets >1 offers, she accepts her first choice –Then they’re engaged 2 nd 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…

20 Example M: 1.23145 2.32514 3.23451 4.32514 5.32415 W: 1.12345 2.25341 3.15234 4.12345 5.21345

21 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

22 Stability? Could we have A-B, C-D, s.t. A  D? –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!

23 Fair? This alg is “male-optimal” E.g.: A->B, C->D, B->C, D->A  A  B,C  D In residency assignments, hospitals play male role Students wanted to choose, but were denied

24 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

25 Programming Algorithms  programs Can express in pseudocode But must code in some prog lang This week: lang = JavaScript in webpages


Download ppt "CS120: Lecture 10 MP Johnson Hunter"

Similar presentations


Ads by Google