1 CS120: Lecture 9 MP Johnson Hunter

Slides:



Advertisements
Similar presentations
Announcements You survived midterm 2! No Class / No Office hours Friday.
Advertisements

CS420 lecture one Problems, algorithms, decidability, tractability.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
CS4HS at Marquette University. a) Find the sum of 4 and 7 b) Sort a list of words in alphabetical order c) State the most beautiful phrase in the English.
Infinite Horizon Problems
Chapter 3 Growth of Functions
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS120: Lecture 10 MP Johnson Hunter
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 21 Instructor: Paul Beame.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
CS120: Lecture 13 MP Johnson Hunter
Analysis of Algorithms CS 477/677
CS120: Lecture 1 MP Johnson Hunter College
On Time Versus Input Size Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 15March 2, 2004Carnegie Mellon University.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Instructor: Paul Beame TA: Gidon Shavit.
CS10 The Beauty and Joy of Computing Lecture #23 : Limits of Computing Thanks to the success of the Kinect, researchers all over the world believe.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Algorithm Analysis (Big O)
1 Complexity Lecture Ref. Handout p
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Week 2 CS 361: Advanced Data Structures and Algorithms
Scott Perryman Jordan Williams.  NP-completeness is a class of unsolved decision problems in Computer Science.  A decision problem is a YES or NO answer.
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
Analysis of Algorithms
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
1 Markov Decision Processes Infinite Horizon Problems Alan Fern * * Based in part on slides by Craig Boutilier and Daniel Weld.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Thursday, May 9 Heuristic Search: methods for solving difficult optimization problems Handouts: Lecture Notes See the introduction to the paper.
CSE373: Data Structures & Algorithms Lecture 22: The P vs. NP question, NP-Completeness Lauren Milne Summer 2015.
1 Markov Decision Processes Infinite Horizon Problems Alan Fern * * Based in part on slides by Craig Boutilier and Daniel Weld.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CS10: The Beauty and Joy of Computing Lecture #22 Limits of Computing Warning sign posted at Stern Hall. Also, Apple releases new operating.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem.
The Beauty and Joy of Computing Lecture #23 Limits of Computing Researchers at Facebook and the University of Milan found that the avg # of “friends” separating.
CSE 373: Data Structures and Algorithms
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
CompSci On the Limits of Computing  Reasons for Failure 1. Runs too long o Real time requirements o Predicting yesterday's weather 2. Non-computable.
Searching Topics Sequential Search Binary Search.
Lecture. Today Problem set 9 out (due next Thursday) Topics: –Complexity Theory –Optimization versus Decision Problems –P and NP –Efficient Verification.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Advanced Algorithms Analysis and Design
Algorithms and Problem Solving
BackTracking CS255.
Introduction Algorithms Order Analysis of Algorithm
Week 2 - Friday CS221.
Algorithms Furqan Majeed.
Objective of This Course
Algorithms and Problem Solving
Algorithms Step-by-step instructions that tell a computing agent how to solve some problem using only finite resources Resources Memory CPU cycles Time/Space.
Algorithms Step-by-step instructions that tell a computing agent how to solve some problem using only finite resources Resources Memory CPU cycles Time/Space.
Time Complexity and the divide and conquer strategy
Presentation transcript:

1 CS120: Lecture 9 MP Johnson Hunter

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

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

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

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

6 Alg goals 1.ideally, alg should be: 1.Correct 2.efficient –or at least optimal 2.each can be difficult 3.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

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

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

9 Algs you already know 1.Dial your phone number 2.Combination lock 3.Cooking 4.Add numbers in memory: Fixed-length alg 5.Addition itself (of bitstrings) Length depends on bitstring size

10 Algs you already know 6.Long additon, etc. (base-10 ops) 1.Let rightmost column be current 2.Add the digits in the current col 3.Write first dig of result as answer for that col 4.If result >9, add 1 to previous digits (“carry”) 5.Move “current column” to left 6.Go to step 2 7.Processor’s alg: –Fetch an instruction –Decode it –Execute it –Go to step 1

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

12 TSP: Hilary motiv 1.algs are designed to solve problems, in all cases –should work correctly (quickly) on all possible inputs 2.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

13 TSP: Hilary motiv 1.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)

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

15 TSP: Greedy alg 1.consider this case:(sequence of dots) 2.greedy produces this 3.clearly optimal is that eg: –NYC  Bos 215 –NYC  DC 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

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

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 = infinity I = 0 While (I < n) If (best == -1 or cost[i] < cost[best]) Best = I; Return best;

18 TSP: Brute force 1.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

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 Nn^22^n

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 = klog1024 2^8 = 256… 2^16 = 64k 2^20 = M 2^30 = G

21 search Sequential search –Alg? –Cmplx? Binary search –Alg? –Analogy: phonebook –Cmplx? Q: how many times to /2? Nlogn 1024 …

22 Order of magnitude calculations?