Lecture 11: Weighted Shortest Path#
Overview#
BFS in Lecture 9 can find the shortest path in graph
G
from given sources
. But it only usable for unit-weighted graph, where the weights of all edges are 1. The distance of each vertexv
in terms ofs
is counted by the number of edges betweens
andv
.BFS is a
single source shortest-paths
problem
Variants#
in
shortest-paths problem
, we are given aweighted, directed
graph with weight functionw
mapping edges to real-valued weights.the weight of a path
p=(v0, v1, v2,..., vk)
asw(p)
is the sum of weights of its constituent edges:define the
shortest-path weight
from to bya
shortest path
from to is then defined as any pathp
with weight $w(p) = \delta(u,v).
single-destination shortest-paths problem
: find a shortest path to a given destination vertex from each vertex . We can reverse the directoin of each edges in the graph, and reduce this problem to asingle-source
problem.single-pair shortest-path problem
: Find a shortest path from to for given vertices and . If we solve the single-source problem with source vertex , we solve this problem also.all-pairs shortest-paths problem
: Find a shortest path from to for every pair of vertices and . Although we can solve this problem by running a single- source algorithm once from each vertex, we usually can solve it faster. (later in XXX)
Negative-weight edges#
Negative-weight cycles
:the weight of the cyclic path is negative
If the graph contains a negative-weight cycle reachable from
, however, shortest-path weights are not well defined.No path from
to a vertex on the cycle can be a shortest path—we can always find a path with lower weight by following the proposed “shortest” path and then traversing the negative-weight cycle.If there is a negative-weight cycle on some path from
to , we define .
Some shortest-paths algorithms, such as Dijkstra’s algorithm, assume that all edge weights in the input graph are nonnegative, as in the road-map example.
Others, such as the Bellman-Ford algorithm, allow negative-weight edges in the in- put graph and produce a correct answer as long as no negative-weight cycles are reachable from the source.
Typically, if there is such a negative-weight cycle, the algorithm can detect and report its existence.
Representing shortest paths#
similar to BFS trees
shortest paths are not necessarily unique, and neither are shortest-trees.
Relaxation#
for each vertex
, we maintain an attribute , which is an upper bound on the weight of a shortest path from source to .initialize the shortest-path estimates and predecessors in
time
def initialize_single_source(G,s):
for v in G.V:
v.d = infinity
v.p = None
s.d = 0
The process of relaxing an edge
consists of testing whether we can improve the shortest path to found so far by going through and, if so, updating and .
def relax(u,v,w):
if v.d > u.d + w(u,v):
v.d = u.d + w(u,v)
v.p = u
Single-source shortest paths in DAG (directed acyclic graphs)#
Find the shortest paths from source
to every vertex in a weighted DAGAlgorithm:
in aggregated analysis
def DAG_shortest_paths(G, s):
linked_list = topological_sort(G)
initialize_single_source(G,s)
for u in linked_list:
for v in G.Adj[u]:
relax(u,v,w)
Example
Follow up:
How to find the longest path in a DAG?
negate the edge weights and runing
DAG_shortest_paths(G,s)
or
modifyDAG_shortest_paths(G,s)
by replacing by during initialization and by in therelax
program.