CS120: Lecture 10 MP Johnson Hunter

Slides:



Advertisements
Similar presentations
Blah blah blah. Zoes shark Thanks to the conference artist Phoebe.
Advertisements

Piyush Kumar (Lecture 3: Stable Marriage) Welcome to COT5405.
Naveen Garg, CSE, IIT Delhi
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Sorted list matching & Experimental run-Time COP 3502.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
1 CS120: Lecture 9 MP Johnson Hunter
CS120: Lecture 13 MP Johnson Hunter
CS120: Lecture 14 MP Johnson Hunter
CS120: Lecture 1 MP Johnson Hunter College
1 CSc100: Lecture 11 MP Johnson Hunter
Algorithms and Efficiency of Algorithms February 4th.
1 CS120: Lecture 4 MP Johnson Hunter College
Lecture 20: April 12 Introduction to Randomized Algorithms and the Probabilistic Method.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Binary Conversion In today’s lesson we will link together the binary and algorithm topics by looking at how to get the computer to: convert binary to decimal.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity.
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
The Stable Marriage Problem
1 Stable Matching Problem Goal. Given n men and n women, find a "suitable" matching. n Participants rate members of opposite sex. n Each man lists women.
Invitation to Computer Science, Java Version, Second Edition.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.
Stable Matchings a.k.a. the Stable Marriage Problem
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 What can a computer be commanded to do?
1 The Stable Marriage Problem Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij.
All Pair Shortest Path IOI/ACM ICPC Training June 2004.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Computer Science Theory & Introduction Week 1 Lecture Material – F'13 Revision Doug Hogan Penn State University CMPSC 201 – C++ Programming for Engineers.
CS 2601 Runtime Analysis Big O, Θ, some simple sums. (see section 1.2 for motivation) Notes, examples and code adapted from Data Structures and Other Objects.
Design & Co-design of Embedded Systems Final Project: “The Match Maker” Second Phase Oral Presentation Safa S. Mahmoodian.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
Vacaville USD September 5, AGENDA Problem Solving and Patterns Math Practice Standards and Effective Questions Word Problems Ratios and Proportions.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]
Some Computer Science Conceptual Background Material Doug Hogan This version for UIC Spring 2016 CS 107.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
CS321 Spring 2016 Lecture 3 Jan Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.
CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Vishnu Kotrajaras, PhD.1 Data Structures
Grade School Again: A Parallel Perspective CS Lecture 7.
Introduction to Algorithms
Cybernetica AS & Tartu University
Stable Matching.
Introduction to the Design and Analysis of Algorithms
May 17th – Comparison Sorts
Copyright © Zeph Grunschlag,
Copyright © Zeph Grunschlag,
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
CSE 421: Introduction to Algorithms
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Computational Thinking for KS3
Richard Anderson Autumn 2006 Lecture 1
1.6) Storing Integer:.
CSE 421: Introduction to Algorithms
Introduction to Algorithms
For loops Taken from notes by Dr. Neil Moore
Piyush Kumar (Lecture 3: Stable Marriage)
Chap 7. Advanced Control Statements in Java
Week 10 - Wednesday CS221.
1.6) Storing Integer: 1.7) storing fraction:
Presentation transcript:

CS120: Lecture 10 MP Johnson Hunter

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 –Dec  bin 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 packing protein folding n!“factorial”TSP Brute

Complx ftn table 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

Basis for big-O X^3 v nx^3 doesn’t matter –Wait log1.5(n) years X^3 v. x^ x^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: = 399 – , … Alg 3 (Euclid): 1989%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 –19 th C lit would have been different with computers

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…

Example M: W:

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

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

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