Geography and CS - PowerPoint PPT Presentation

About This Presentation
Title:

Geography and CS

Description:

Geography and CS Philip Chan – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 58
Provided by: pkc
Learn more at: https://cs.fit.edu
Category:

less

Transcript and Presenter's Notes

Title: Geography and CS


1
Geography and CS
  • Philip Chan

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

3
NavigationProblem 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
NavigationProblem 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 -gt vertices
  • Highways -gt edges
  • City-by-city route -gt 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

9
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?

10
Problem with Algorithm 1
  • What is the main problem?

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

12
Algorithm 2
  • Exhaustive algorithm
  • Explore/generate all possible paths
  • Not just the ones that look short
  • Compare all possible paths

13
Greedy vs Exhaustive
  • Greedy doesnt guarantee the shortest path

14
Greedy vs Exhaustive
  • Greedy doesnt guarantee the shortest path
  • Greedy is faster

15
Greedy vs Exhaustive
  • Greedy doesnt guarantee the shortest path
  • Greedy is faster
  • Greedy requires less memory

16
Algorithm 3
  • smart algorithm
  • Guarantees the shortest path
  • Faster than Exhaustive algorithm
  • Any ideas on what we can ignore?

17
Algorithm 3
  • Smart algorithm
  • Similar to Exhaustive
  • explore all alternatives from each city
  • Ignore alternative paths that are not shorter

18
Algorithm 4
  • Smarter algorithm
  • Dijkstras algorithm
  • Any ideas on additional alternatives that can be
    ignored?

19
Algorithm 4
  • Smarter algorithm
  • Dijkstras algorithm
  • Any ideas on additional alternatives that can be
    ignored?
  • Hint we can commit certain cities earlier
  • Cant have a shorter path to those cities
  • Ignore the committed cities later

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

21
Algorithm 4
  • Let the current city be the origin (I)
  • While the current city is not the destination(C)
  • Explore neighboring non-committed cities Xs 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)
  • Find the non-committed city that has the shortest
    path length
  • Commit that city
  • Update the current city to the committed city (U)

22
Algorithm 4
  • Why does it guarantee to find the shortest path?
  • The shortest path to city X is committed
  • When?

23
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

24
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

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

26
Comparing the Four Algorithms
  • Greedy
  • Commit to closest neighboring city
  • Exhaustive
  • Consider all possible paths, compare all paths
  • Smart
  • Consider all neighboring cities, compare old and
    new paths, ignore the longer one
  • Smarter (Dijkstras)
  • Consider only non-committed neighboring cities,
    compare old and new paths, ignore the worse one

27
Implementation of Dijkstras Algorithm
  • Keeping track of information needed by the
    algorithm

28
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)

29
What to keep track (data)?
30
What to keep track (data)?
  • Whether a city is committed

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

32
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?

33
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?
  • committedcity
  • pathLengthcity
  • aka shortestDistancecity

34
How to keep track (instructions)?
  • Sketching on whiteboard

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

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

37
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?

38
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?

39
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

40
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?
  • committedcity
  • pathLengthcity
  • aka shortestDistancecity

41
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?
  • committedcity
  • pathLengthcity
  • aka shortestDistancecity
  • parentcity
  • aka previousCitycity

42
Algorithm 4 with data tracking
  • Let the current city be the origin (I)
  • While the current city is not the destination(C)
  • Explore neighboring non-committed cities Xs of
    the current city
  • if new path to X is shorter than current path to
    X
  • Update current path to X (pathLengthX,
    parentX)
  • Find the non-committed city that has the shortest
    path length
  • Commit that city (committedcity)
  • Update the current city to the committed city (U)

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

44
How to keep track (instructions)?
  • Sketching on whiteboard

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

46
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

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

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

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

50
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

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

52
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?

53
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?

54
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

55
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

56
Shortest Path Algorithm
  • Dijkstras 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

57
Summary
  • Navigation problem
  • Turn-by-turn directions
  • Simplified city-by-city directions
  • Algorithms
  • Greedy might not yield shortest path
  • Dijkstras always yield shortest path
  • Reasons for guarantee
  • Data structures in implementation
  • Quadratic comparisons in of cities
Write a Comment
User Comments (0)
About PowerShow.com