Review - PowerPoint PPT Presentation

About This Presentation
Title:

Review

Description:

Shortest Paths. Minimum Hopping Flight. ORD. PVD. MCO. DFW. SFO. LAX. LGA. HNL. 1. 1. 1. 1. 1. 1 ... In a flight route graph, the weight of an edge represents ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 57
Provided by: csU73
Learn more at: http://www.cs.ucf.edu
Category:
Tags: flight | paths | review

less

Transcript and Presenter's Notes

Title: Review


1
Review
2
Topological Sorting of a DAG
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MCO
3
Topological Sorting of a DAG
PVD 3
ORD 4
SFO 8
0
0
LGA 2
0
0
HNL 7
LAX 6
DFW 5
0
MCO 1
0
0
0
4
Depth-First Search
5
Depth-First Search
Algorithm DFS(G, v) setLabel(v, VISITED) for all
e ? G.incidentEdges(v) if getLabel(e)
UNEXPLORED w ? opposite(v,e) if getLabel(w)
UNEXPLORED setLabel(e, DISCOVERY) DFS(G,
w) else setLabel(e, BACK)
6
Breadth-First Search
7
Breadth-First Search
  • Algorithm BFS(G, s)
  • Q ? new queue
  • Q.enQueue(s)
  • while ?Q.isEmpty()
  • v ? Q .deQueue()
  • for all e ? G.incidentEdges(v)
  • if getLabel(e) UNEXPLORED
  • w ? opposite(v,e)
  • if getLabel(w) UNEXPLORED
  • setLabel(e, DISCOVERY)
  • setLabel(w, VISITED)
  • Q.enQueue(w)
  • else
  • setLabel(e, CROSS)

8
Shortest Paths
9
Minimum Hopping Flight
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MCO
10
Minimum Hopping Flight
1
PVD
ORD
1
1
?
SFO
?
?
LGA
1
1
1
1
1
?
1
HNL
1
1
LAX
1
DFW
?
MCO
?
?
0
11
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
?
?
LGA
1
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
?
MCO
?
1
0
12
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
?
LGA
1
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
?
MCO
2
1
0
13
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
?
LGA
1
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
?
MCO
2
1
0
14
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
?
LGA
1
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
?
MCO
2
1
0
15
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
LGA
1
3
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
3
MCO
2
1
0
16
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
LGA
1
3
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
3
MCO
2
1
0
17
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
LGA
1
3
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
3
MCO
2
1
0
18
Minimum Hopping Flight
1
PVD
ORD
1
1
SFO
1
2
LGA
1
3
1
1
1
1
1
1
HNL
1
1
LAX
1
DFW
3
MCO
2
1
0
19
Shortest Path when each edge is equally important
  • Algorithm ShortestPath (G, s)
  • for all v ? G.vertices()
  • if v s
  • s.distance ? 0
  • else s.distance ? ?
  • v.parent ? null
  • Q ? new queue
  • Q.enQueue(s)
  • while ?Q.isEmpty()
  • v ? Q .deQueue()
  • for all e ? G.incidentEdges(v)
  • w ? opposite(v,e)
  • if w.distance ?
  • w.distance ? v.distance 1
  • w.parent ? v
  • Q.enQueue(v)

20
Weighted Graphs
  • In a weighted graph, each edge has an associated
    numerical value, called the weight of the edge
  • Edge weights may represent, distances, costs,
    etc.
  • Example
  • In a flight route graph, the weight of an edge
    represents the distance in miles between the
    endpoint airports

21
Shortest Path Problem
  • Given a weighted graph and two vertices u and v,
    we want to find a path of minimum total weight
    between u and v.
  • Length of a path is the sum of the weights of its
    edges.
  • Example
  • Shortest path between Orlando and San Francisco

22
Shortest Path Problem
  • Given a weighted graph and two vertices u and v,
    we want to find a path of minimum total weight
    between u and v.
  • Length of a path is the sum of the weights of its
    edges.
  • Example
  • Shortest path between Orlando and San Francisco

849
PVD
ORD
1843
142
SFO
802
LGA
1305
1743
337
1387
2555
HNL
1099
1233
LAX
1120
DFW
MCO
23
BFS strategy may not work!!
849
PVD
ORD
1843
142
SFO
LGA
802
1743
1305
337
1387
2555
HNL
1099
1233
LAX
1120
DFW
MCO
24
Dijkstras Algorithm
  • for Single source Shortest Path
  • i.e. shortest path between one vertex and all
    other vertices.
  • Extension of Breadth First Search
  • uses a Greedy algorithm.

25
Dijkstra Algorithm Strategy
849
PVD
ORD
1843
142
SFO
1305
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
MCO
1120
26
Dijkstra Algorithm Strategy
849
PVD
ORD
1843
142
SFO
1305
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
MCO
1120
27
Dijkstra Algorithm Strategy
849
PVD
ORD
1843
142
SFO
1243
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
MCO
1120
and so on
28
Dijkstras Algorithm
849
PVD
ORD
1843
142
?
SFO
?
?
LGA
802
1743
1305
337
1387
?
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
?
0
29
Dijkstras Algorithm
849
PVD
ORD
1843
142
?
SFO
?
?
LGA
802
1743
1305
337
1387
?
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
?
0
30
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1305
?
?
LGA
802
1743
1305
337
1387
2555
HNL
1099
1099
1233
LAX
1120
DFW
?
MCO
?
1120
0
31
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1305
?
?
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
1120
0
32
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
?
?
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
1120
0
33
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
?
?
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
1120
0
34
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
?
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
2353
1120
0
35
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
?
LGA
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
2353
1120
0
36
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
LGA
3765
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
2353
1120
0
37
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
LGA
2690
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
4908
MCO
2353
1120
0
38
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
LGA
2690
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
4908
MCO
2353
1120
0
39
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241
1922
LGA
2690
802
1743
1305
337
1387
1099
2555
HNL
1099
1233
LAX
1120
DFW
4908
MCO
2353
1120
0
40
Dijkstras Algorithm
  • How to Find shortest path and shortest path
    length?

41
Dijkstras Algorithm
849
PVD
ORD
1843
142
?,MCO
SFO
?,MCO
?,MCO
LGA
802
1743
1305
337
1387
?,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
?,MCO
?,MCO
0
42
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1305,MCO
?,MCO
?,MCO
LGA
802
1743
1305
337
1387
2555
HNL
1099,MCO
1099
1233
LAX
1120
DFW
?,MCO
MCO
?,MCO
1120,MCO
0
43
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1305,MCO
?,MCO
?,MCO
LGA
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
?,MCO
1120,MCO
0
44
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
?,MCO
?,MCO
LGA
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
?,MCO
1120,MCO
0
45
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
?,MCO
?,MCO
LGA
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
?,MCO
1120,MCO
0
46
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
?,MCO
LGA
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
2353,DFW
1120,MCO
0
47
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
?,MCO
LGA
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
2353,DFW
1120,MCO
0
48
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
LGA
3765,ORD
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
?,MCO
MCO
2353,DFW
1120,MCO
0
49
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
LGA
2690,LAX
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
4908,LAX
MCO
2353,DFW
1120,MCO
0
50
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
LGA
2690,LAX
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
4908,LAX
MCO
2353,DFW
1120,MCO
0
51
Dijkstras Algorithm
849
PVD
ORD
1843
142
SFO
1241,LGA
1922,DFW
LGA
2690,LAX
802
1743
1305
337
1387
1099,MCO
2555
HNL
1099
1233
LAX
1120
DFW
4908,LAX
MCO
2353,DFW
1120,MCO
0
52
Dijkstras Algorithm
  • Algorithm ShortestPath (G, s)
  • Q ? new priority queue
  • for all v ? G.vertices()
  • if v s
  • v.distance ? 0
  • else v.distance ? ?
  • v. parent ? s
  • Q.enQueue(v.distance, v)
  • while ?Q.isEmpty()
  • v ? Q .removeMin()
  • v.pathKnown ? true
  • for all e ? G.incidentEdges(v)
  • w ? opposite(v,e)
  • if ?w.pathKnown
  • if v.distance weight(e) lt w.distance
  • w.distance ? v.distance weight(e)
  • w. parent ? v
  • update key of w in Q

O(n log n)
O(n log n)
O(m log n)
O((nm)log n)
53
Analysis
  • Graph operations
  • Method incidentEdges is called once for each
    vertex
  • Label operations
  • We set/get the distance and locator labels of
    vertex w O(n) times
  • Setting/getting a label takes O(1) time
  • Priority queue operations
  • Each vertex is inserted once into and removed
    once from the priority queue, where each
    insertion or removal takes O(log n) time
  • The key of a vertex in the priority queue is
    modified at most deg(w) times, where each key
    change takes O(log n) time
  • Dijkstras algorithm runs in O((n m) log n)
    time provided the graph is represented by the
    adjacency list structure
  • Recall that Sv deg(v) 2m
  • The running time can also be expressed as O(m log
    n) since the graph is connected

54
Shortest Path Properties
  • Property 1 A subpath of a shortest path is
    itself a shortest path
  • Property 2 There is a tree of shortest paths
    from a start vertex to all the other vertices.

55
Single Source Shortest Path
  • Property 1 A subpath of a shortest path is
    itself a shortest path. Greedy Algorithm
  • Property 2 There is a tree of shortest paths
    from a start vertex to all the other vertices.
    Single source shortest path.

56
Why It Doesnt Work for Negative-Weight Edges
  • Dijkstras algorithm is based on the greedy
    method. It adds vertices by increasing distance.
  • If a node with a negative incident edge were to
    be added late to the vertex list for which
    decisions have been made, it could mess up
    distances for vertices already in the list.
Write a Comment
User Comments (0)
About PowerShow.com