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.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

Heuristic Search techniques
NP-Hard Nattee Niparnan.
Data Structures and Functional Programming Computability Ramin Zabih Cornell University Fall 2012.
Lecture 24 Coping with NPC and Unsolvable problems. When a problem is unsolvable, that's generally very bad news: it means there is no general algorithm.
Computability & Complexity. Scenario I can’t write this program because I’m too dumb.
Week 7 - Wednesday.  What did we talk about last time?  Set proofs and disproofs  Russell’s paradox.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey  Class variables  Big Oh notation.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Complexity 15-1 Complexity Andrei Bulatov Hierarchy Theorem.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Week 12: Running Time and Performance 1.  Most of the problems you have written in this class run in a few seconds or less  Some kinds of programs can.
CSE332: Data Abstractions Lecture 27: A Few Words on NP Dan Grossman Spring 2010.
Complexity Theory CSE 331 Section 2 James Daly. Reminders Project 4 is out Due Friday Dynamic programming project Homework 6 is out Due next week (on.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
The Theory of NP-Completeness
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
Quicksort.
Analysis of Algorithms CS 477/677
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Halting Problem. Background - Halting Problem Common error: Program goes into an infinite loop. Wouldn’t it be nice to have a tool that would warn us.
1 Complexity Lecture Ref. Handout p
Programming & Data Structures
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.
1 Growth of Functions CS 202 Epp, section ??? Aaron Bloomfield.
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
Complexity Classes (Ch. 34) The class P: class of problems that can be solved in time that is polynomial in the size of the input, n. if input size is.
1 Excursions in Modern Mathematics Sixth Edition Peter Tannenbaum.
Algorithms and their Applications CS2004 ( ) Dr Stephen Swift 4.1 Time Complexity and Asymptotic Notation.
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
CSC 413/513: Intro to Algorithms NP Completeness.
CSC 172 P, NP, Etc. “Computer Science is a science of abstraction – creating the right model for thinking about a problem and devising the appropriate.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 What can a computer be commanded to do?
Cliff Shaffer Computer Science Computational Complexity.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CSE373: Data Structures & Algorithms Lecture 22: The P vs. NP question, NP-Completeness Lauren Milne Summer 2015.
Erdal Kose CC30.10 These slides are based of Prof. N. Yanofsky Lecture notes.
Beauty and Joy of Computing Limits of Computing Ivona Bezáková CS10: UC Berkeley, April 14, 2014 (Slides inspired by Dan Garcia’s slides.)
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity.
CS10: The Beauty and Joy of Computing Lecture #22 Limits of Computing Warning sign posted at Stern Hall. Also, Apple releases new operating.
Limits to Computation How do you analyze a new algorithm? –Put it in the form of existing algorithms that you know the analysis. –For example, given 2.
CS 3343: Analysis of Algorithms Lecture 25: P and NP Some slides courtesy of Carola Wenk.
Computability Heap exercise. The class P. The class NP. Verifiers. Homework: Review RELPRIME proof. Find examples of problems in NP.
CIRCUITS, PATHS, AND SCHEDULES Euler and Königsberg.
Complexity & Computability. Limitations of computer science  Major reasons useful calculations cannot be done:  execution time of program is too long.
Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem.
Easy, Hard, and Impossible Elaine Rich. Easy Tic Tac Toe.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
CS6045: Advanced Algorithms NP Completeness. NP-Completeness Some problems are intractable: as they grow large, we are unable to solve them in reasonable.
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.
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.
Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Lecture. Today Problem set 9 out (due next Thursday) Topics: –Complexity Theory –Optimization versus Decision Problems –P and NP –Efficient Verification.
I can describe the differences between Hamilton and Euler circuits and find efficient Hamilton circuits in graphs. Hamilton Circuits I can compare and.
Week 7 - Wednesday.  What did we talk about last time?  Proving the subset relationship  Proving set equality  Set counterexamples  Laws of set algebra.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Excursions in Modern Mathematics Sixth Edition
Week 9 - Monday CS 113.
Week 10 - Monday CS 113.
Week 13: Searching and Sorting
Week 11 - Monday CS221.
Unsolvable Problems December 4, 2017.
Week 11 - Wednesday CS221.
Presentation transcript:

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 language d) Name the smallest even number greater than 2 that cannot be written as the sum of two primes

 A problem is a relationship between input data and output data  For every set of input values, there is only one correct set of output  A specific set of input and output is called an instance  Adding 4 and 7 is an instance of the general problem of adding integers

 An algorithm is a solution to a problem  It gives a finite series of steps that you can follow from any legal input data to reach the correct output data  Can you give an example?

 How do you measure how long an algorithm takes?  We can describe the number of steps the algorithm takes as a function of the size of input data (usually abbreviated as n)  How does the number of steps needed to solve an instance grow as the input grows?

 Let's say an algorithm takes 34n 3 – 5n n steps for an input of size n  Ugh.  Since we care about growth when n gets big, the biggest term is what matters  Since different computers are better at different operations, we ignore constant coefficients  We say 34n 3 – 5n n is O(n 3 )  Cubic growth is what matters

 How long does it take to do addition by hand?  Let’s assume the numbers have n digits  n additions + n carries  Running time: O(n)

 How long does it take to do multiplication by hand? 123 x _492__  Let’s assume the numbers have n digits  (n multiplications + n carries) x n digits + (n + 1 digits) x n additions  Running time: O(n 2 )

 I've got a list of n numbers  You want to look at my list to see if your favorite number (42) is on it  How many numbers do you have to look at to see if my list has your number?  How long does that take in Big-Oh notation?

 Is there any way to go smaller than O(n)?  What if the numbers were processed in some way beforehand?  What if you knew that the numbers were sorted in ascending order?

 Repeatedly divide the search space in half  We play a high-low game  Again, let's say we're looking for Check the middle (Too high) Check the middle (Too low) Check the middle (Too low) Check the middle (Found it!) 42

 This is a classic interview question asked by Microsoft, Amazon, and similar companies  Imagine that you have 9 red balls  One of them is just slightly heavier than the others, but so slightly that you can’t feel it  You have a very accurate two pan balance you can use to compare balls  Find the heaviest ball in the smallest number of weighings

 It’s got to be 8 or fewer  We could easily test one ball against every other ball  There must be some cleverer way to divide them up  Something that is related somehow to binary search

 We can divide the balls in half each time  If those all balance, it must be the one we left out to begin with

 How?  They key is that you can actually cut the number of balls into three parts each time  We weigh 3 against 3, if they balance, then we know the 3 left out have the heavy ball  When it’s down to 3, weigh 1 against 1, again knowing that it’s the one left out that’s heavy if they balance

 The cool thing is…  Yes, this is “cool” in the CS sense, not in the real sense  Anyway, the cool thing is that we are trisecting the search space each time  This means that it takes log 3 n weighings to find the heaviest ball  We could do 27 balls in 3 weighings, 81 balls in 4 weighings, etc.

 CS people have written hundreds of papers just about how to sort things better  Bubble sort is a very simple sorting algorithm  It is not the fastest algorithm  Make a number of passes  In each pass, swap each contiguous pair of items if they're out of order  Keep making passes until no swaps are necessary

 Run through the whole list, swapping any entries that are out of order No swap Swap 45 0 No swap Swap No swap Swap

 Everyone stand up  We're going to use the bubble sort algorithm to sort us all by birth month and day (you can keep the year to yourself)  In each pass, we sequentially swap pairs that are out of order  We keep making passes until no one needs swapping  We are guaranteed that the last person is put in the right place after each pass

 How bad could it be?  What if the array was in reverse- sorted order?  One pass would only move the largest number to the bottom  We would need n – 1 passes to sort the whole array  What's the total time for bubble sort?

 How does Google Maps find the shortest route from Philadelphia to Milwaukee?  It simplifies all the streets into a graph  A graph has nodes and edges  Nodes represent locations  Edges are (parts of) streets

 We can write a number next to each edge which gives its weight  Weight can represent time, distance, cost: anything, really  The shortest path (lowest total weight) is not always obvious A A B B D D C C E E 2 8 6

A B D C E D E C B A  Take a moment and try to find the shortest path from A to E.  The shortest path has length

 On a graph of that size, it isn’t hard to find the shortest path  A Google Maps graph has millions and millions of nodes  How can we come up with an algorithm that will always find the shortest path from one node to another?  Dijkstra's algorithm adds the closest node we can find, expanding our set of stuff we know the best way to get to

NotationMeaning sStarting node d(v)d(v) The best distance from s to v found so far d(u, v) The direct distance between nodes u and v S A set which contains the nodes for which we know the shortest path from s V A set which contains the nodes for which we do not yet know the shortest path from s

1. Start with two sets, S and V:  S has the starting node in it  V has everything else 2. Set the distance to all nodes in V to ∞ 3. Find the node u in V that is closest to a node in S 4. For every neighbor v of u in V  If d(v) > d(u) + d(u,v)  Set d(v) = d(u) + d(u,v) 5. Move u from V to S 6. If V is not empty, go back to Step 2

Sets SV AB, C, D, E Sets SV A, BC, D, E Sets SV A, B, CD, E Sets SV A, B, C, DE Sets SV A, B, C, D, E Noded(u)d(u) A0 B5 C8 D∞ E A A B B D D C C E E Noded(u)d(u) A0 B5 C7 D11 E16 Noded(u)d(u) A0 B5 C7 D10 E16 Noded(u)d(u) A0 B5 C7 D10 E16 Noded(u)d(u) A0 B5 C7 D10 E14 Finding the shortest distance from A to all other nodes

A A B B D D C C E E A B C D E  The traveling salesman problem (TSP) also wants to find the shortest path, but it adds two requirements  You have to return to the point where you start  You have to visit all the cities exactly once  Like a UPS guy who wants to make all his dropoffs and return to the shipping center

 TSP seems easy!  There must be some way we can adapt Dijkstra's algorithm to work  Suggestions?  The greedy approach always adds the nearest neighbor  A brute force approach tries all possible tours and sees which is shortest

 We are tempted to always take the closest neighbor, but there are pitfalls GreedyOptimal

 In a completely connected graph, we can try any sequence of nodes  If there are n nodes, there are (n – 1)! tours  For 30 locations, 29! =  If we can check 1,000,000,000 tours per second, it will only take about 20,000 times the age of the universe to check them all  We will (eventually) get the best answer!

 TSP is just one NP-complete problem  These problems are pretty useful (scheduling, optimizing resource usage, packing boxes)  No running-times for NP-complete problems are known that are better than exponential O(2 n )  All these problems can easily be converted into each other  If you could solve one efficiently, you could solve them all efficiently  The Clay Mathematics Institute has a $1 million prize for such an algorithm  You get the same prize if you could prove that no such algorithm is possible

 A Turing machine is a model of computation  It consists of a head, an infinitely long tape, a set of possible states, and an alphabet of characters that can be written on the tape  A finite list of rules says what it should write and whether it should move left or right given the current symbol and state A A

 Computational power means the ability to solve a problem  The ability to run an algorithm  Speed is not related to computational power  Turing machines have as much computational power as any known computer  Any algorithm that can be run on any computer can be run on a Turing machine

 There are some computational problems that you cannot design an algorithm for  No computer can ever solve the general problem (though sometimes you can solve instances of the problem)  Examples  It's impossible to write a program that can tell if any program will run to completion

 Douglas Hofstadter uses the metaphor of turntables  Imagine that evil people design records that will shake turntables apart when they're played  Maybe turntable A can play record A and turntable B can play record B  However, if turntable A plays record B, it will shatter

 Turing machines can perform all possible computations  It's possible to encode the way a Turing machine works such that another Turing machine can read it  It's easy to make a slight change to a Turing machine so that it gives back the opposite answer (or goes into an infinite loop)

 You've got a Turing machine M with encoding m  You want to see if M will halt on input x  Assume there is a machine H that can take encoding m and input x  H(m,x) is YES if it halts  H(m,x) is NO if it loops forever  We create (evil) machine E that takes description m and runs H(m,m)  If H(m,m) is YES, E loops forever  If H(m,m) is NO, E returns YES H H mxmx YES NO H H YES NO E m Loop forever YES

 Let's say that e is the description of E  What happens if you feed description e into E?  E(e) says what E will do with itself as input  If it returns YES, that means that E on input e loops forever  But it can't, because it just returned YES  If it loops forever, then E on input e would return YES  But it can't, because it's looping forever!  Our assumption that machine H exists must be wrong