Title: Review
1Review
2Topological Sorting of a DAG
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MCO
3Topological 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
4Depth-First Search
5Depth-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)
6Breadth-First Search
7Breadth-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)
8Shortest Paths
9Minimum Hopping Flight
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MCO
10Minimum Hopping Flight
1
PVD
ORD
1
1
?
SFO
?
?
LGA
1
1
1
1
1
?
1
HNL
1
1
LAX
1
DFW
?
MCO
?
?
0
11Minimum 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
12Minimum 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
13Minimum 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
14Minimum 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
15Minimum 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
16Minimum 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
17Minimum 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
18Minimum 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
19Shortest 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)
20Weighted 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
21Shortest 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
22Shortest 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
23BFS strategy may not work!!
849
PVD
ORD
1843
142
SFO
LGA
802
1743
1305
337
1387
2555
HNL
1099
1233
LAX
1120
DFW
MCO
24Dijkstras 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.
25Dijkstra 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
26Dijkstra 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
27Dijkstra 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
28Dijkstras Algorithm
849
PVD
ORD
1843
142
?
SFO
?
?
LGA
802
1743
1305
337
1387
?
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
?
0
29Dijkstras Algorithm
849
PVD
ORD
1843
142
?
SFO
?
?
LGA
802
1743
1305
337
1387
?
2555
HNL
1099
1233
LAX
1120
DFW
?
MCO
?
?
0
30Dijkstras 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
31Dijkstras 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
32Dijkstras 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
33Dijkstras 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
34Dijkstras 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
35Dijkstras 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
36Dijkstras 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
37Dijkstras 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
38Dijkstras 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
39Dijkstras 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
40Dijkstras Algorithm
- How to Find shortest path and shortest path
length?
41Dijkstras 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
42Dijkstras 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
43Dijkstras 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
44Dijkstras 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
45Dijkstras 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
46Dijkstras 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
47Dijkstras 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
48Dijkstras 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
49Dijkstras 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
50Dijkstras 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
51Dijkstras 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
52Dijkstras 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)
53Analysis
- 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
54Shortest 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.
55Single 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.
56Why 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.