0% found this document useful (0 votes)
9 views11 pages

Bellman Ford

Uploaded by

Random Swag
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)
9 views11 pages

Bellman Ford

Uploaded by

Random Swag
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
You are on page 1/ 11

CS 113: Data Structures and

Algorithms
Abhiram G. Ranade

Shortest paths in presence of negative edge


weights
Let us trade in currencies!
• Suppose we know currency trading rates from various places in the
world.
• Data = set of triples (i, j, rij) where i, j are currencies and rij = number
of units of currency i will buy 1 unit of currency j.
Example: (Rupee, $, 66)
• In actual currency exchange you may pay an additional commission
etc. which we ignore (or say it is included in the rate).
• Question: What is the minimum number of Rupees I need to pay in
order to buy one Tanzanian Shilling, assuming I can exchange
several times along the way?
– Possible solution could be: convert into Euro in Mumbai, then into Yen in
Stockholm, ..., into Tanzanian Shillings in Melbourne.
– Long indirect trades may actually give you a low rate!
Is there a graph in here?
• Vertices = Currencies
• Directed edge from i to j with weight rij = the number
of units of currency i needed to buy 1 unit of currency
j
– Find a path from i to j s.t. product of edge weights is as
small as possible
• Idea: make weight of edge ij be log rij
– Find shortest path in the graph from i to j
• Are weights always positive?
– rij can be less than 1. So log rij can be negative!
Are shortest paths even defined?
• Length of shortest path = min {Lengths of all paths}
• All paths = Simple and non simple paths.
• Set is infinite.
• Is it possible that lengths can be made as small as you want?
• Suppose we have a cycle whose total weight is negative.
• By including copies of that cycle in our path the path length can be
made as negative as you want.
• Assumption: Graph does not contain negative cycles.
• If we have an undirected graph in which weight of edge xy is
negative, then x-y-x is a negative cycle.
• So shortest paths not defined in undirected graphs if any edge
weight is negative.
Negative cycles in currency trading
• Negative cycle in currency trading: I can buy 1
rupee for less than a rupee!
• Such cycles actually exist occasionally –
“Arbitrage”
• But usually this does not happen.
Will Dijkstra’s algorithm not work if there
are negative cycles?
• Before iteration 1:
S={u,v,w,x}, d[u]=0, d[v]=d[x]=d[w]=∞
• After iteration 1:
u 5 v
S={v,w,x}, d[v]=5, d[x]=100
• After iteration 2:
S={w,x}, d[w]=10
100 5 5
• After iteration 3:
S={x}, no change in d
• After iteration 4:
x w
S={}, d[w]=-10 -110
Algorithm ends. d[v] should be -5.
Can we use any of the ideas from Dijkstra?

• Start with d[u] = 0, d[] = ∞ for others


• Key observation: d[v] is an upper bound on the length
of the shortest path from u to v.
• Basic operation: updating edge (v,w):
d[w] = min(d[w], d[v] + wvw )
• Observations:
d[v] + wvw ≥ length of a shortest path from u to v + wvw
≥ length of path from u to v to w
≥ length of shortest path from u to w.
• So basic update preserves the upper bound property.
What if we update all edges once?
Claim: If there is a shortest path from u to v having
one edge, the after updating all edges once we will
have d[v] = length of shortest path from u to v.
Proof: The path must clearly have length wuv.
The update for edge (u,v) will be enough:
d(v) = min(d(v),d(u)+wuv)
= min(∞, d(u) + wuv)
= wvw
k iterations of updating all edges once
Claim: If there is a path from u to v having k edges, d[v] =
shortest path length after the updates.
Proof: By induction over k.
Suppose x = vertex just before v on the shortest path P.
x has a shortest path P’ of length k-1
So after k-1 iterations d[x] = length of P’
In kth iteration we will get d[v] = min(d[v],d[x]+wxv)
≤ d[x]+wxv = Length of P, shortest path.
But d[v] is always an upper bound, i.e. it cannot go below the
length of shortest path, so it must equal it.
Final algorithm
Claim: It suffices to run n-1 iterations in each of
which we update every edge once.
Proof: Every shortest path must have length at
most n-1.

Time taken: O(m) per iteration, so total O(mn).


Exercises
• Run the algorithm on different graphs and check that it does
produce the correct distances.
• Modify the algorithm so that it also finds a value p[v] for each
vertex v which is the first vertex from v on a shortest path to
u.
– The tree produced by (v,p[v]) edges is called a shortest path tree.
• Suppose we run the algorithm for i = 1 to n, i.e. an extra
iteration. Show that some d[v] will decrease in this last
iteration iff the graph has a negative cycle.
– This is a way of detecting if a graph has a negative cycle.
– Hint for the proof: Suppose the weight does not decrease for any
vertex on the cycle..

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