0% found this document useful (0 votes)
30 views14 pages

Shortest Path Algorithm Overview

This document describes the shortest path algorithm to find the shortest path between two nodes in a graph that may contain negative edge weights. It explains that Dijkstra's algorithm does not work in this case. It then presents a dynamic programming approach that works by computing the minimum cost of a path from each node to the target node using at most i edges. The algorithm runs in O(n3) time and also describes how to recover the shortest path using a pointer graph. It further explains how to detect if the graph contains a negative cost cycle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views14 pages

Shortest Path Algorithm Overview

This document describes the shortest path algorithm to find the shortest path between two nodes in a graph that may contain negative edge weights. It explains that Dijkstra's algorithm does not work in this case. It then presents a dynamic programming approach that works by computing the minimum cost of a path from each node to the target node using at most i edges. The algorithm runs in O(n3) time and also describes how to recover the shortest path using a pointer graph. It further explains how to detect if the graph contains a negative cost cycle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Shortest Path Algorithm

The Problem Statement


• Given a directed graph G=(V,E) such that each
edge is having a weight possibly negative but
no negative cycle, then find the shortest path
from a vertex s to sink vertex t.

• Dijkstra’s algorithm worked for Shortest Path


Problem when there are no negative costs.
Is negative edge required
• Yes

• In many applications the negative edge makes


sense.
• For example, the nodes may represent agents in a
financial setting and 𝑐𝑖𝑗 represents the cost of a
transaction in which we buy from agent 𝑖 and
immediately sell to agent 𝑗. So a negative weight
indicates profitable transactions.
Failure of Dijkstra’s Algorithm
• If we run Dijkstra’s algorithm, first u
visited vertex will be {s}. The next 2 3
unvisited vertex with least distance s t
from s is v.
1 v -6
• As per Dijkstra’s algorithm the
shortest path from s to v is 1 with the
single edge {s,v}.
• The path {s,v} is clearly the shortest to
v if there are no negative edge costs:
any other path from s to v would have
to start on an edge out of s that is at
least as expensive as edge (s,v).
Modify the cost of edge
• A natural idea is to increase 2
u
2
the cost of all edges by some s t
large constant M. Then all 3 3
v v
costs will be positive. -3

• This does not work. Add 3 to


all the edge weights and
check!
Dynamic Programming Approach
• Important Observation:

If G has no negative cycles, then there is a shortest path from s to t that is simple
(i.e. does not repeat nodes), and hence has at most n-1 edges.

Proof: Since every cycle has non-negative cost, the


shortest path P from s to t with the fewest number
of edges does not repeat any vertex v. For if P did
repeat a vertex, we could remove the portion of P
between consecutive visits to v, resulting in a path of
no greater cost and fewer edges.
Opt(i,v)
• Let 𝑂𝑃𝑇(𝑖, 𝑣) to denote the minimum cost of
a 𝑣 − 𝑡 path using at most 𝑖 edges.

• Thus we have to compute 𝑂𝑃𝑇(𝑛 − 1, 𝑠).


v t
w

• Let’s fix an optimal path P representing 𝑂𝑃𝑇 𝑖, 𝑣


as shown above.
• If the path P uses at most 𝑖 − 1 edges, then
𝑂𝑃𝑇 𝑖, 𝑣 = 𝑂𝑃𝑇 𝑖 − 1, 𝑣
• If the path P uses 𝑖 edges, and the first edge is
𝑣, 𝑤 , then 𝑂𝑃𝑇 𝑖, 𝑣 = 𝑐𝑣𝑤 + 𝑂𝑃𝑇(𝑖 − 1, 𝑤).
Recursion

If i>0 then
𝑂𝑃𝑇 𝑖, 𝑣 = min{𝑂𝑃𝑇 𝑖 − 1, 𝑣 , min(𝑐𝑣𝑤 + 𝑂𝑃𝑇(𝑖 − 1, 𝑤))}
Pseudo code
Shortest-Path (G,s,t)
n = number of nodes in G
Array M[0…n-1, V]
Define M[0,t] = 0 and M[0,v] = inf for all other vertices
For i = 1 to n-1
for 𝑣𝜖𝑉 in any order
Compute 𝑀[𝑖, 𝑣] using the given recurrence.
Endfor
Endfor
Return 𝑀[𝑛 − 1, 𝑠]
Running Time
• A loose analysis says that this algorithm runs in 𝑂(𝑛3 ) time
where there are quadratic entries and each entry requires
to compute the recurrence which is linear in time.
• 𝑀[ 𝑖, 𝑣 ] = min{𝑀[ 𝑖 − 1, 𝑣 ], min(𝑐𝑣𝑤 + 𝑀[(𝑖 − 1, 𝑤)]}
• Note that the inner minimum is computed only for
neighbors w of v. So it takes 𝑛𝑣 , where that is the number
of outgoing edges at 𝑣.
• So it takes 𝑂(𝑛𝑣 ) to compute the array entry 𝑀 𝑖, 𝑣 .
• We have to compute an entry for every v and every index
0 ≤ 𝑖 ≤ 𝑛 − 1, so running time bound is
• 𝑂 𝑛 σ𝑣𝜖𝑉 𝑛𝑣 = 𝑂(𝑚𝑛)
Pointer Graph
• To help with recovering the shortest path,
each node v maintains the first node (after
itself) on its path to the destination t;
• We will call this vector 𝑓𝑖𝑟𝑠𝑡[𝑣].
• Let P denote the directed “pointer graph”
whose nodes are V and whose edges are {(𝑣,
𝑓𝑖𝑟𝑠𝑡[𝑣])}.
Detecting a cycle
• If the pointer graph P contains a cycle C, then this
cycle must have negative cost.
• Proof: Notice that if 𝑓𝑖𝑟𝑠𝑡 𝑣 = w, then 𝑀 𝑣 ≥
𝑐𝑣𝑤 + 𝑀[𝑤].
• Let 𝑣1 , 𝑣2 , … , 𝑣𝑘 be the nodes along the cycle in
the pointer graph and assume that (𝑣𝑘 , 𝑣1 ) is the
last edge to have been added. Now 𝑀 𝑣𝑖 ≥
𝑐𝑣𝑖 𝑣𝑖+1 + 𝑀[𝑣𝑖+1 ] for all 𝑖 = 1, … , 𝑘 − 1, and also
have 𝑀 𝑣𝑘 ≥ 𝑐𝑣𝑘𝑣1 + 𝑀[𝑣1 ] . Adding all the
equations we get 0 > σ𝑘−1 𝑖=1 𝑐𝑣𝑖 𝑣𝑖+1 + 𝑐𝑣𝑘 𝑣1 . Thus
it is a negative cycle.
Reference
• Algorithm Design by Eva Tardos and Jon
Kleinberg.

You might also like