Download presentation

Presentation is loading. Please wait.

1
**Single Source Shortest Paths**

Definition Algorithms Bellman Ford DAG shortest path algorithm Dijkstra

2
**Definition Input Task Weighted, connected directed graph G=(V,E)**

Weight (length) function w on each edge e in E Source node s in V Task Compute a shortest path from s to all nodes in V

3
**Comments If edges are not weighted, then BFS works**

Optimal substructure A shortest path between s and t contains other shortest paths within it No known algorithm is better at finding a shortest path from s to a specific destination node t in G than finding the shortest path from s to all nodes in V

4
Negative weight edges Negative weight edges can be allowed as long as there are no negative weight cycles If there are negative weight cycles, then there cannot be a shortest path from s to any node t (why?) If we disallow negative weight cycles, then there always is a shortest path that contains no cycles

5
Relaxation technique For each vertex v, we maintain an upper bound d[v] on the weight of shortest path from s to v d[v] initialized to infinity Relaxing an edge (u,v) Can we improve the shortest path to v by going through u? If d[v] > d[u] + w(u,v), d[v] = d[u] + w(u,v) This can be done in O(1) time

6
**Bellman-Ford Algorithm**

Bellman-Ford (G, w, s) Initialize-Single-Source(G,s) for (i=1 to V-1) for each edge (u,v) in E relax(u,v); if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE

7
**Running Time for (i=1 to V-1) The above takes (V-1)O(E) = O(VE) time**

for each edge (u,v) in E relax(u,v); The above takes (V-1)O(E) = O(VE) time if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE The above takes O(E) time

8
Proof of Correctness If there is a shortest path from s to any node v, then d[v] will have this weight at end Let p = (e1, e2, …, ek) = (v1, v2, v3, …, v,+1) be a shortest path from s to v s = v1, v = vk+1, ei = (vi, vi+1) At beginning of ith iteration, during which we relax edge ei, d[vi] must be correct, so at end of ith iteration, d[vi+1] will e correct Iteration is the full sweep of relaxing all edges Proof by induction

9
**Negative weight cycle for each edge (u,v) in E**

if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE If no negative cycle, d[v] <= d[u] + w(u,v) for all edges (u,v) If there is a negative weight cycle, for some edge (u,v) on the cycle, it must be the case that d[v] > d[u] + (u,v).

10
**DAG shortest path algorithm**

DAG-SP (G, w, s) Initialize-Single-Source(G,s) Topologically sort vertices in G for each vertex u, taken in sorted order for each edge (u,v) in E relax(u,v);

11
**Running time Improvement**

for each vertex u, taken in sorted order for each edge (u,v) in E relax(u,v); We only do 1 relaxation for each edge: O(E) time In addition, O(V+E) for the sorting and we get O(V+E) running time overall

12
Proof of Correctness If there is a shortest path from s to any node v, then d[v] will have this weight at end Let p = (e1, e2, …, ek) = (v1, v2, v3, …, v,+1) be a shortest path from s to v s = v1, v = vk+1, ei = (vi, vi+1) Since we sort edges in topological order, we will process node vi (and edge ei) before processing later edges in the path.

13
**Dijkstra’s Algorithm Dijkstra (G, w, s)**

/* Assumption: all edge weights non-negative */ Initialize-Single-Source(G,s) Completed = {}; ToBeCompleted = V; While ToBeCompleted is not empty u =EXTRACT-MIN(ToBeCompleted); Completed += {u}; for each edge (u,v) relax(u,v);

14
**Running Time Analysis While ToBeCompleted is not empty**

u =EXTRACT-MIN(ToBeCompleted); Completed += {u}; for each edge (u,v) relax(u,v); Each edge relaxed once: O(E) Need to decrease-key potentially once per edge Need to extract-min once per node

15
**Running Time Analysis cont’d**

O(E) edge relaxations Priority Queue operations O(E) decrease key operations O(V) extract-min operations Three implementations of priority queues Array: O(V2) time decrease-key is O(1) and extract-min is O(V) Binary heap: O(E log V) time assuming E >= V decrease-key and extract-min are O(log V) Fibonacci heap: O(V log V + E) time decrease-key is O(1) amortized time and extract-min is O(log V)

16
**Proof of Correctness s u v Only nodes in S**

Assume that Dijkstra’s algorithm fails to compute length of all shortest paths from s Let v be the first node whose shortest path length is computed incorrectly Let S be the set of nodes whose shortest paths were computed correctly by Dijkstra prior to adding v to the processed set of nodes. Dijkstra’s algorithm has used the shortest path from s to v using only nodes in S when it added v to S. The shortest path to v must include one node not in S Let u be the first such node. s u v Only nodes in S

17
Proof of Correctness The length of the shortest path to u must be at least that of the length of the path computed to v. Why? The length of the path from u to v must be < 0. No path can have negative length since all edge weights are non-negative, and thus we have a contradiction. s u v Only nodes in S

18
**Computing paths (not just distance)**

Maintain for each node v a predecessor node p(v) p(v) is initialized to be null Whenever an edge (u,v) is relaxed such that d(v) improves, then p(v) can be set to be u Paths can be generated from this data structure

Similar presentations

OK

Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.

Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Musculoskeletal system anatomy and physiology ppt on cells Ppt on duty roster army Ppt on coalition government examples Performance linkedin pay ppt online Ppt on motivational skills English 8 unit 12 read ppt online Ppt on wild animals in india Ppt on digital marketing Ppt on eddy current loss Ppt on brand equity