Problems and Algorithms Introduction to: Problem Algorithms Complexity
Problem: Get to NYC Need to travel from the Jersey Shore to New York City How would you do this?
Algorithm (Step-by-Step Solution) Drive in a car from Jersey shore to Miami Drive from Miami to Los Angeles Drive from Los Angeles to San Francisco Drive From San Francisco to New York Analysis Will this algorithm solve the problem? Is this a good algorithm to use? Best way? Gas? Toll? Mileage? Time?
Problem: Go from Jersey to San Fran Some choices for algorithms Walk the entire way Drive by car the entire way Drive to the nearest airport, fly to the nearest airport to San Fran, the rent a car and drive again
Problem: Go from class to the Gym Go from this classroom to the Gym
Algorithm: To get to the Gym Drive a car? Take a plane? Walk? Analysis: If planes are faster than cars, should you solve all travel problems by using a plane? Know how to use planes, cars, bicycles, and walk and use your head.
Problem: Find Max Instance: A collection of numbers Problem: Print out the maximum number of all numbers
Algorithm for Find Max Algorithm: Sort all the numbers Print out A[n-1] Analysis of the algorithm: How do we sort the numbers? How long does this take. Printing out the answer is simply the time it takes to print.
Algorithm for Find Max (cont) Suppose we sort the number by repeatedly Taking all permutations of the numbers Check if this is correctly sorted Stop if it is sorted Analysis of the algorithm How many permutations are there? N! 20! = 2.43 X 10^18 On a 1 gig processor would take approx. 76 years.
Better algorithm for Find Max? Suggestions? Best possible algorithm (a.k.a. Completeness) How do you know someone else will not come along later and come up with a better algorithm
Problem: Skyline Silhouette Instance: A collection of rectangular building (heights and widths) Problem: Print out the silhouette of the skyline from one point of view.
Skyline input and output building one left 3 right 7 height 4 building two left 11 right 20 height 8 building three left 15 right 29 height 3 ********** * * * * * * ***** * * * * * *************** * * * * * * * * * * * * =============================== 3 7 11 15 20 29 <---- left/right edge positions The output should be the points (3, 0) (3, 4) (7, 4) (7, 0) (11, 0) (11, 8) (20, 8) (20, 3) (29, 3) (29, 0)
Algorithm for Skyline problem Hints: Sort the building in some order Move buildings from one data structure to another Have a data structure that can always find the max and anytime