Download presentation
Presentation is loading. Please wait.
1
SINGLE-SOURCE SHORTEST PATHS
2
Shortest-Path Variants
Single-Source Single-Destination (1-1) No good solution that beats 1-M variant Thus, this problem is mapped to the 1-M variant Single-Source All-Destination (1-M) Need to be solved (several algorithms) We will study this one All-Sources Single-Destination (M-1) Reverse all edges in the graph Thus, it is mapped to the (1-M) variant All-Sources All-Destinations (M-M) We will study it (if time permits)
3
Shortest Path Shortest Path = Path of minimum weight d(u,v)=
min{ω(p) : u v}; if there is a path from u to v, otherwise.
4
Algorithms to Cover Dijkstra’s Algorithm Shortest Path is DAGs
5
Initialization Maintain d[v] for each v in V
d[v] is called shortest-path weight estimate and it is upper bound on δ(s,v) INIT(G, s) for each v V do d[v] ← ∞ π[v] ← NIL d[s] ← 0 Upper bound Parent node
6
Relaxation RELAX(u, v) if d[v] > d[u]+w(u,v) then
π[v] ← u When you find an edge (u,v) then check this condition and relax d[v] if possible u v u v 2 2 5 9 5 6 Change d[v] No change 5 7 5 6 2 2 u v u v
7
Algorithms to Cover Dijkstra’s Algorithm Shortest Path is DAGs
8
Dijkstra’s Algorithm For Shortest Paths
Non-negative edge weight Like BFS: If all edge weights are equal, then use BFS, otherwise use this algorithm Use Q = min-priority queue keyed on d[v] values
9
Dijkstra’s Algorithm For Shortest Paths
DIJKSTRA(G, s) INIT(G, s) S←Ø > set of discovered nodes Q←V[G] > put all vertices in a queue while Q ≠Ø do u←EXTRACT-MIN(Q) S←S U {u} for each v in Adj[u] do RELAX(u, v) > May cause > DECREASE-KEY(Q, v, d[v])
10
Example: Initialization Step
11
Example 10 5 s u v y x 1 3 9 4 6 7 2 2
12
Example 8 14 5 7 s y x 10 1 3 9 4 6 2 u v 2
13
Example 8 13 5 7 s u v y x 1 2 3 9 4 6 10
14
Example 8 u v 9 5 7 y x 10 1 2 3 4 6 s
15
Example u 8 9 5 7 v y x 10 1 2 3 4 6 s 5
16
Dijkstra’s Algorithm Analysis
O(V) O(V Log V) Total in the loop: O(V Log V) Total in the loop: O(E Log V) O(Log V) for each call Time Complexity: O (E Log V)
17
Algorithms to Cover Dijkstra’s Algorithm Shortest Path is DAGs
18
Key Property in DAGs If there are no cycles it is called a DAG
(Directed Acyclic Graph) In DAGs, nodes can be sorted in a linear order such that all edges are forward edges Topological sort
19
Single-Source Shortest Paths in DAGs
Shortest paths are always well-defined in DAGs no cycles => no negative-weight cycles even if there are negative-weight edges Idea: No backward edges Process nodes left to right. When reach node X its cost will not change any more because there are no backward edges from the coming nodes We will be done in 1 pass
20
Single-Source Shortest Paths in DAGs
DAG-SHORTEST PATHS(G, s) TOPOLOGICALLY-SORT the vertices of G INIT(G, s) for each vertex u taken in topologically sorted order do for each v in Adj[u] do RELAX(u, v)
21
Example 6 1 u r s t v w 5 2 7 –1 –2 4 3 2
22
Example 6 1 u r s t v w 5 2 7 –1 –2 4 3 2
4 3 2 Any nodes left from the source s will have cost ∞ (not reachable from s)
23
Example 6 1 u r s t v w 5 2 7 –1 –2 2 6 4 3 2
24
Example 6 1 u r s t v w 5 2 7 –1 –2 2 6 6 4 4 3 2
25
Example 6 1 u r s t v w 5 2 7 –1 –2 2 6 5 4 4 3 2
26
Example 6 1 u r s t v w 5 2 7 –1 –2 2 6 5 3 4 3 2
27
Example 6 1 u r s t v w 5 2 7 –1 –2 2 6 5 3 4 3 2
28
Single-Source Shortest Paths in DAGs: Analysis
O(V+E) DAG-SHORTEST PATHS(G, s) TOPOLOGICALLY-SORT the vertices of G INIT(G, s) for each vertex u taken in topologically sorted order do for each v in Adj[u] do RELAX(u, v) O(V) Total O(E) Time Complexity: O (V + E)
29
Shortest-Path Variants
Single-Source Single-Destination (1-1) No good solution that beats 1-M variant Thus, this problem is mapped to the 1-M variant Single-Source All-Destination (1-M) Need to be solved (several algorithms) We will study this one All-Sources Single-Destination (M-1) Reverse all edges in the graph Thus, it is mapped to the (1-M) variant All-Sources All-Destinations (M-M) We will study it (if time permits)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.