0% found this document useful (0 votes)
53 views8 pages

Lec17 Dijkstra and Bellman-Ford (Shortest Path)

Dijkstra's algorithm and the Bellman-Ford algorithm are used to find the shortest path between a source node and all other nodes in a graph. Dijkstra's algorithm works for graphs with non-negative edge weights, running in O(ElogV) time, while Bellman-Ford can handle graphs with negative edge weights, running in O(VE) time. Bellman-Ford works by iteratively relaxing all edges to improve path lengths over V-1 iterations, and can detect negative cycles by checking for further improvements on a final iteration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views8 pages

Lec17 Dijkstra and Bellman-Ford (Shortest Path)

Dijkstra's algorithm and the Bellman-Ford algorithm are used to find the shortest path between a source node and all other nodes in a graph. Dijkstra's algorithm works for graphs with non-negative edge weights, running in O(ElogV) time, while Bellman-Ford can handle graphs with negative edge weights, running in O(VE) time. Bellman-Ford works by iteratively relaxing all edges to improve path lengths over V-1 iterations, and can detect negative cycles by checking for further improvements on a final iteration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

11/2/20

Single Source Shortest Path


• Dijkstra’s Algorithm
• The Bellman-Ford Algorithm

Shortest Path Problem


• Shortest path network.
• Directed graph ! = ($, &, '). Consider the weights are distance

• Source *, destination +.
• Shortest path problem: find shortest directed path from s to t.
Total path weight = sum of edge weights in path

2 23 3
9
s 18
14
Path s-2-3-5-t
2 6 = 9 + 23 + 2 + 16
6
30 4 19 = 50.
11
15 5
5
6
20 16

7 t
44

1
11/2/20

Dijkstra's algorithm
This idea is similar to Prim’s MST algorithm.
Prim keeps track of minimum weight single
Dijkstra's algorithm. edge to attach, rather than total distance.

• Keep track of a growing blob of finalized nodes (nodes whose shortest


path distance has been determined)
• For all remaining nodes, keep track of the minimum total distance
currently known to reach that node
• Each iteration, choose the remaining node with cheapest total distance
• Add that node to the blob of finalized nodes;
• update remaining nodes by checking if newly finalized node offers an edge to
reach them that improves their minimum total distance

Dijkstra's algorithm
Dijkstra's algorithm.
• Maintain a set of finalized nodes ! for which we have determined the
shortest path distance "($) from & to $.
• Initialize ! = {&}, "(&) = 0.
• Repeatedly choose unfinalized node , which minimizes total distance
from currently known nodes to reach it.
NOTE: we are
π (v) = min d(u) + we , assumed non-negative
e = (u, v) : u∈ S

• add , to !, and update " , = p(v) shortest path to some u in


finalized part, followed by a
single edge (u, v)

2
11/2/20

Dijkstra's algorithm
Dijkstra ( G=(V,E,w), s )
1. Let H = V–{s};
2. For every vertex v do
3. dist[v]= ∞, parent[v]=null
4. dist[s]=0, parent[s] = none
5. Update(s)
6. For i=1 to n-1 do
7. u=extract vertex from H
of smallest weight
8. Update(u)
• Return dist[]

Update (u)
1. For every neighbor v of u (such that v in H)
2. If dist[v]>dist[u]+w(u,v) then
3. dist[v]=dist[u]+w(u,v)
4. parent[v]=u

Dijkstra's Algorithm: Proof of Correctness


Invariant. For each node u Î S, d(u) is the length of the shortest s-u
path.
Pf. (by induction on |S|)
• Base case: |"| = 1 is trivial.
• Inductive hypothesis: Assume true for " = % > 1.
• Inductive step:
• Let ' be next node added to ", and let ( − ' be the chosen edge.
• The * − ' path has length: p ' = d ( + -((, ').
• Consider any * − ' path 1. We'll see that it's no shorter than p(') P
• Let 2 − 3 be the first edge in 1 that leaves ",
x y
and let 1' be the sub-path to 2. P'

• 1 is already too long as soon as it leaves ". s

S u
v

3
11/2/20

Dijkstra – Complexity
Complexity Analysis for ! = ($, &), |$| = ), |&| = *
• We store minimum path weight for each vertex in a min-heap.
• Each heap operation requires +(log )) time
• ) − 1 iterations in which an EXTRACT_MIN is performed
• +() log ))
• Each edge can result in at most one CHANGE_KEY
• +(* log ))
• Overall complexity: +(* log ))
• (This is the same analysis as for Prim’s MST algorithm)

Dijkstra – Complexity
Complexity Analysis for ! = ($, &), |$| = ), |&| = *
• Alternatively, store edge information in an adjacency matrix and store
minimum path weight information for each vertex in a separate list
• ) iterations in which the vertex with minimum path weight is chosen:
• +()) per iteration, +()2) total
• In each iteration, neighbors of the added vertex may have their minimum
path weight updated:
• +()) per iteration, +()2) total
• Overall complexity: +()2)
• Note: this is better than using a binary heap in the case that ! is dense (* ≈ )2)
• (This is the same analysis is for Prim’s MST algorithm)

4
11/2/20

Dijkstra's algorithm
Dijkstra's algorithm:
• Can be specified for a single source,!, and single destination,".
• However, each iteration determines the shortest path from ! to some
vertex #.
• Dijkstra’s algorithm run for $ iterations will find the shortest path
from ! to all vertices.
• Dijkstra works for directed as well as undirected graphs.
• Requires non-negative weights.

Single Source Shortest Path


• Dijkstra’s Algorithm
• The Bellman-Ford Algorithm

5
11/2/20

Single Source Shortest Path:


The Bellman-Ford Algorithm
• Shortest path network.
• Directed graph ! = ($, &, ').
• Shortest path problem: given a starting vertex s, find shortest
directed path from s to all other vertices
• Option #1: if all edge weights are non-negative, use Dijkstra
• Greedy algorithm works in *(+2) or *(- log +)
• Option #2: if some edge weights are negative, use Bellman-Ford
• Runs in *(-+) time
• Also used to determine if there exists a negative-weight cycle

Bellman-Ford Algorithm
Basic Idea:
• Iterate over the number of vertices
• Keep track of current shortest path (distance and parent) for each vertex
• For each iteration, “relax” all edges
• Consider whether this edge can be used to improve the current shortest path of the
vertex at its endpoint
• After ! iterations, the first ! steps of any shortest path are correct and will
never change from that point (we’ll prove shortly)
• Because every shortest path is simple, we know that after at most " − 1
iterations, we’ll have every shortest path determined.

6
11/2/20

Bellman-Ford – Correctness
Correctness Argument assuming no negative weight cycles:
Proof by induction over the iterations of the algorithm
Claim: Consider any vertex ! and a shortest path " from # to !. Let " be defined as
$0$1$2 … $) , where $0 = # and $) = !. After + ≤ ) iterations of Bellman-Ford, all vertices
along the path $0$1 … $+ have had their shortest path computed.
• Base case: + = 0. Trivial.
• Inductive hypotheses: Suppose true for - iterations. Then the shortest path from # to
$- has been calculated.
• Inductive step: in iteration j+1, all edges are relaxed again, in particular edge . =
($-, $123 ), such that the shortest path is correctly computed for $123 .

Note that by definition of the Bellman-Ford algorithm, the shortest path to a vertex can
never increase from one iteration to the next, and it can never get lower than the true
shortest path.

Bellman-Ford – Correctness
Negative Cycles
• Assuming there are no negative cycles, every shortest path is simple and
contains at most ! − 1 edges
• Therefore the Bellman-Ford algorithm correctly identifies all shortest paths
from source vertex $ in at most ! − 1 iterations.
• As soon as you have an iteration in which nothing changes (no vertices
receive improved shortest paths) the algorithm is finished – nothing can
change from that point.
• So to check for cycles, after completing ! − 1 iterations of Bellman-Ford,
simply scan all edges one more time to see if there is a vertex that could
still be improved.
• If so, that implies a path longer than ! − 1 edges to achieve the shortest path, which
implies a negative cycle.

7
11/2/20

Bellman-Ford Algorithm
Bellman-Ford ( G=(V,E,w), s )

1. For every vertex v


Time Complexity: !(#$)
2. d[v] = ∞
3. d[s]=0 ! Iterate over the vertices
4. For i=1 to |V|-1 do ! Consider every edge during
5. For every edge (u,v) in E do each iteration
6. If d[v]>d[u]+w(u,v) then
7. d[v]=d[u]+w(u,v), parent[v] = u
8. For every edge (u,v) in E do
9. If d[v]>d[u]+w(u,v) then
10. Return NEGATIVE CYCLE
11.Return d[], parent[]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy