1 / 45

Geography and CS

This article discusses the problem of finding the shortest route from the origin to the destination using navigation systems. It explores different algorithms, such as greedy algorithm and Dijkstra's shortest path algorithm, and their implementation.

gmolina
Download Presentation

Geography and CS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Geography and CS Philip Chan

  2. How do I get there? • Navigation • Which web sites can give you turn-by-turn directions?

  3. Navigation[Problem understanding] • Finding a route from the origin to the destination • “Static” directions • Mapquest, Google maps • “Dynamic” on-board directions • GPS navigation • if the car deviates from the route, it finds a new route

  4. Consider a Simpler Problem • A national map with only • Cities and • Highways • That is, ignoring • smaller streets and intersections in a city • small roads between cities • …

  5. Navigation[Problem Formulation] • Given (input) • Map (cities and highways) • Origin city • Destination city • Find (output) • City-by-city route between origin and destination cities

  6. Graph Problem • A graph has vertices and edges • Cities -> vertices • Highways -> edges • City-by-city route -> shortest path

  7. Shortest Path Problem • How would you solve the shortest path problem?

  8. Algorithm 1 • Greedy algorithm • Pick the closest city • Go to the city • Repeat until the destination city is reached • Does this always find the shortest path? • If not, what could be a counter example?

  9. Problem with Algorithm 1 • What is the main problem?

  10. Problem with Algorithm 1 • What is the main problem? • Committing to the next city too soon • Any ideas for improvement?

  11. Algorithm 2 • Keep track of alternative paths • Commit to the next city when • we are sure it is shortest • no other paths are shorter

  12. Algorithm 2 • Let the current city be the origin • While the current city is not the destination • Explore the neighboring non-committed cities of the current city • Compare alternative path to current path • Update if alternative path is shorter • Find the non-committed city that has the shortest total distance from origin • Commit that city • Update the current city to that city

  13. Algorithm 2 • Why does it guarantee to find the shortest path? • The shortest path to city X is committed • When?

  14. Algorithm 2 • Why does it guarantee to find the shortest path? • The shortest path to city X is committed • when every path to the “non-committed” cities is longer

  15. Algorithm 2 • Why does it guarantee to find the shortest path? • The shortest path to city X is committed • when every path to the “non-committed” cities is longer • no way to get to city X with a shorter path via “non-committed” cities

  16. Dijkstra’s shortest path algorithm • Interesting applet to demonstrate the alg: • http://www.dgp.toronto.edu/people/JamesStewart/270/9798s/Laffra/DijkstraApplet.html

  17. Implementation 1 • Simplified problem • What is the shortest distance between origin and destination? • We will worry about the intermediate cities later. • Consider • what we need to keep track (data) • how to keep track (instructions)

  18. What to keep track (data)?

  19. What to keep track (data)? • Whether a city is committed

  20. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city

  21. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • How to implement the data storage?

  22. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • How to implement the data storage? • committed[city] • shortestDistance[city]

  23. How to keep track (instructions)? • Sketching on whiteboard • Java in HW6

  24. Implementation 2 • We would like to know the intermediate cities as well • the shortest path, not just the shortest distance

  25. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What else?

  26. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What else? • What do you notice for each of the intermediate city?

  27. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What else? • What do you notice for each of the intermediate city? • Each was committed • What do you notice when we commit a city and update the shortest distance?

  28. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What else? • What do you notice for each of the intermediate city? • Each was committed • What do you notice when we commit a city and update the shortest distance? • We know the previous city

  29. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What is the previous city • How to implement the data storage? • committed[city] • shortestDistance[city]

  30. What to keep track (data)? • Whether a city is committed • What is the shortest distance so far for a city • What is the previous city • How to implement the data storage? • committed[city] • shortestDistance[city] • previousCity[city]

  31. Storing the map • How to store the distance between two cities • so that, given two cities, we can find the distance quickly?

  32. How to keep track (instructions)? • Sketching on whiteboard • Java in HW6

  33. Storing the Map • How to store the distance between two cities • so that, given two cities, we can find the distance quickly?

  34. Storing the Map (graph) • How to store the distance between two cities • so that, given two cities, we can find the distance quickly? • Adjacency matrix • Table (2D array) • Rows and columns are cities • Cells have distance

  35. Number of comparisons (speed of algorithm) • Comparing: • Shortest distance so far and • Distance of an alternative path • For updating what?

  36. Number of comparisons (speed of algorithm) • Comparing: • Shortest distance so far and • Distance of an alternative path • For updating what? • Shortest distance so far

  37. Number of comparisons (speed of algorithm) • Worst –case scenario • When does it occur?

  38. Number of comparisons (speed of algorithm) • N is the number of cities • Worst –case scenario • When does it occur? • Every city is connected to every city • Maximum numbers of neighbors to explore

  39. Worst-case scenario (speed of algorithm) • How many comparisons? • How many non-committed neighbors from the origin (in the first round)?

  40. Worst-case scenario (speed of algorithm) • How many comparisons? • How many non-committed neighbors from the origin (in the first round)? • N – 1 comparisons • How many in the second round?

  41. Worst-case scenario (speed of algorithm) • How many comparisons? • How many non-committed neighbors from the origin (in the first round)? • N – 1 comparisons • How many in the second round? • N – 2 comparisons • ... • How many in total?

  42. Worst-case scenario (speed of algorithm) • How many comparisons? • How many non-committed neighbors from the origin (in the first round)? • N – 1 comparisons • How many in the second round? • N – 2 comparisons • ... • How many in total? • (N-1) + (N-2) + … + 1

  43. Worst-case scenario (speed of algorithm) • How many comparisons? • How many non-committed neighbors from the origin (in the first round)? • N – 1 comparisons • How many in the second round? • N – 2 comparisons • ... • How many in total • (N-1) + (N-2) + … + 1 • (N-1)N/2 = (N2 – N)/2

  44. Shortest Path Algorithm • Dijkstra’s Algorithm • In terms of vertices (cities) and edges (highways) in a graph • 1959 • more than 50 years ago • Navigation optimization • Cheapest (tolls) route? • Least traffic route? • Many applications

  45. Summary • Navigation problem • Turn-by-turn directions • Simplified: city-by-city directions • Algorithms: • Greedy: might not yield shortest path • Dijkstra’s: always yield shortest path • Reasons for guarantee • Data structures in implementation • Quadratic comparisons in # of cities

More Related