Top Banner
Geography and CS Philip Chan
57

Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Dec 27, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Geography and CS

Philip Chan

Page 2: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

How do I get there?

Navigation

Which web sites can give you turn-by-turn directions?

Page 3: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 4: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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 …

Page 5: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Navigation[Problem Formulation] Given (input)

Map (cities and highways) Origin city Destination city

Find (output) City-by-city route between origin and

destination cities

Page 6: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Graph Problem

A graph has vertices and edges Cities -> vertices Highways -> edges City-by-city route -> shortest path

Page 7: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Shortest Path Problem

How would you solve the shortest path problem?

Page 8: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 1

Greedy algorithm1. Pick the closest city

2. Go to the city

3. Repeat until the destination city is reached

Page 9: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 1

Greedy algorithm1. Pick the closest city

2. Go to the city

3. Repeat until the destination city is reached

Does this always find the shortest path?

If not, what could be a counter example?

Page 10: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Problem with Algorithm 1

What is the main problem?

Page 11: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Problem with Algorithm 1

What is the main problem? Committing to the next city too soon

Any ideas for improvement?

Page 12: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 2

“Exhaustive” algorithm Explore/generate all possible paths

Not just the ones that look short Compare all possible paths

Page 13: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Greedy vs Exhaustive

Greedy doesn’t guarantee the shortest path

Page 14: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Greedy vs Exhaustive

Greedy doesn’t guarantee the shortest path Greedy is faster

Page 15: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Greedy vs Exhaustive

Greedy doesn’t guarantee the shortest path Greedy is faster Greedy requires less memory

Page 16: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 3

“smart” algorithm Guarantees the shortest path Faster than Exhaustive algorithm

Any ideas on what we can ignore?

Page 17: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 3

“Smart” algorithm Similar to Exhaustive

explore all alternatives from each city Ignore alternative paths that are not shorter

Page 18: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

“Smarter” algorithm Dijkstra’s algorithm

Any ideas on additional alternatives that can be ignored?

Page 19: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

“Smarter” algorithm Dijkstra’s algorithm

Any ideas on additional alternatives that can be ignored? Hint: we can commit certain cities earlier

Can’t have a shorter path to those cities Ignore the committed cities later

Page 20: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

Keep track of alternative paths Commit to the next city when

we are sure it is shortest no other paths are shorter

Page 21: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

Let the current city be the origin (I) While the current city is not the destination(C)

1. Explore neighboring non-committed cities X’s of the current city

if new path to X is shorter than current path to X Update current path to X (ie, ignore the longer path)

2. Find the non-committed city that has the shortest path length

3. Commit that city

4. Update the current city to the committed city (U)

Page 22: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

Why does it guarantee to find the shortest path? The shortest path to city X is committed

When?

Page 23: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

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

Page 24: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4

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

Page 25: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Dijkstra’s shortest path algorithm

Interesting applet to demonstrate the alg:

http://www.dgp.toronto.edu/people/JamesStewart/270/9798s/Laffra/DijkstraApplet.html

Page 26: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Comparing the Four Algorithms

1. “Greedy” Commit to closest neighboring city

2. “Exhaustive” Consider all possible paths, compare all paths

3. “Smart” Consider all neighboring cities, compare old and

new paths, ignore the longer one

4. “Smarter” (Dijkstra’s) Consider only non-committed neighboring cities,

compare old and new paths, ignore the worse one

Page 27: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Implementation of Dijkstra’s Algorithm Keeping track of information needed by the

algorithm

Page 28: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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)

Page 29: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

What to keep track (data)?

Page 30: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

What to keep track (data)?

Whether a city is committed

Page 31: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

What to keep track (data)?

Whether a city is committed What is the shortest distance so far for a city

Page 32: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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?

Page 33: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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] pathLength[city]

aka shortestDistance[city]

Page 34: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

How to keep track (instructions)?

Sketching on whiteboard

Page 35: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Implementation 2

We would like to know the intermediate cities as well the shortest path, not just the shortest

distance

Page 36: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

What to keep track (data)?

Whether a city is committed What is the shortest distance so far for a city What else?

Page 37: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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?

Page 38: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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?

Page 39: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 40: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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] pathLength[city]

aka shortestDistance[city]

Page 41: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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] pathLength[city]

aka shortestDistance[city] parent[city]

aka previousCity[city]

Page 42: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Algorithm 4 with data tracking

Let the current city be the origin (I) While the current city is not the destination(C)

1. Explore neighboring non-committed cities X’s of the current city

if new path to X is shorter than current path to X Update current path to X (pathLength[X], parent[X])

2. Find the non-committed city that has the shortest path length

3. Commit that city (committed[city])

4. Update the current city to the committed city (U)

Page 43: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Storing the map

How to store the distance between two cities so that, given two cities, we can find the

distance quickly?

Page 44: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

How to keep track (instructions)?

Sketching on whiteboard

Page 45: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Storing the Map

How to store the distance between two cities so that, given two cities, we can find the

distance quickly?

Page 46: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 47: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Number of comparisons (speed of algorithm) Comparing:

Shortest distance so far and Distance of an alternative path

For updating what?

Page 48: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Number of comparisons (speed of algorithm) Comparing:

Shortest distance so far and Distance of an alternative path

For updating what? Shortest distance so far

Page 49: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Number of comparisons (speed of algorithm) Worst –case scenario

When does it occur?

Page 50: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 51: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

Worst-case scenario (speed of algorithm) How many comparisons?

How many non-committed neighbors from the origin (in the first round)?

Page 52: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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?

Page 53: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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?

Page 54: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 55: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 56: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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

Page 57: Geography and CS Philip Chan. How do I get there? Navigation Which web sites can give you turn-by-turn directions?

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