CS-310 Algorithms Homework: Bellman-Ford
Algorithm
Hasan Farooq
23-03-0016
Problem 1: Why Does Bellman-Ford Require V − 1
Iterations?
Concept:
In a graph with V vertices, the longest simple path (without revisiting vertices) between
any two nodes consists of at most V − 1 edges. This is because adding a V -th edge would
create a cycle.
The Bellman-Ford algorithm uses relaxation to compute shortest paths. Relaxation
updates the shortest path estimate for each vertex iteratively. Since the longest valid
path involves V − 1 edges, V − 1 iterations are sufficient to guarantee that all shortest
paths are computed.
Relaxation Formula:
d(v) = min(d(v), d(u) + w(u, v)),
where d(u) is the shortest known distance to vertex u, w(u, v) is the edge weight, and
d(v) is the current shortest distance to vertex v.
Differences from Dijkstra:
• Negative Weights: Bellman-Ford supports negative weights, whereas Dijkstra
does not.
• Approach: Bellman-Ford relaxes all edges iteratively, while Dijkstra uses a greedy
priority queue.
• Complexity: Bellman-Ford has a time complexity of O(V × E), while Dijkstra
runs in O((V + E) log V ) with a priority queue.
Problem 2: Concept of Relaxation
What is Relaxation?
Relaxation updates the shortest distance to a vertex if a shorter path is found through
another vertex.
d(v) = min(d(v), d(u) + w(u, v)).
Why is Repeated Relaxation Necessary?
1
• Shortest paths may require multiple edges to traverse.
• Each iteration accounts for paths with one more edge than the previous iteration.
• After V − 1 iterations, all possible shortest paths are evaluated.
Problem 3: Detecting Negative Weight Cycles
How Bellman-Ford Detects Negative Weight Cycles:
After V − 1 iterations, a V -th iteration is performed. If any edge can still be relaxed,
a negative weight cycle exists. This is because distances can be reduced indefinitely by
looping through the negative cycle.
Why Are Negative Cycles Problematic?
• Shortest path calculations lose meaning because distances can decrease indefinitely.
• In applications (e.g., routing), this can lead to incorrect or undefined behavior.
Problem 4: Comparison of Bellman-Ford and Dijkstra
When is Bellman-Ford Preferable?
• Graphs contain negative weights.
• Negative weight cycle detection is required.
Key Differences:
• Negative Weights: Bellman-Ford supports them, Dijkstra does not.
• Cycle Detection: Bellman-Ford detects negative cycles, Dijkstra cannot.
• Efficiency: Bellman-Ford has O(V ×E) complexity; Dijkstra is faster with O((V +
E) log V ) when using a heap.
Problem 5: Efficiency in Dense and Sparse Graphs
Efficiency Comparison:
Bellman-Ford: O(V × E), Dijkstra: O((V + E) log V ).
• Sparse Graphs (E ≪ V 2 ): Dijkstra is faster, but Bellman-Ford is manageable.
• Dense Graphs (E ≈ V 2 ): Bellman-Ford becomes slow due to the number of
edges, while Dijkstra remains efficient.
2
Problem 6: Bellman-Ford on a Small Graph
Graph Details:
• A → B (4)
• A → C (5)
• B → C (-3)
• C → D (2)
• B → D (4)
Step-by-Step Execution:
1. Initialization:
d(A) = 0, d(B) = ∞, d(C) = ∞, d(D) = ∞.
2. Iteration 1:
d(B) = 0 + 4 = 4, d(C) = min(∞, 0 + 5) = 5, d(D) = min(∞, 5 + 2) = 3.
3. Iterations 2 and 3: No further updates.
Final Distances:
d(A) = 0, d(B) = 4, d(C) = 1, d(D) = 3.
Problem 7: Negative Weight Cycle Detection
Graph Details:
• A → B (3), A → C (4)
• B → C (-2), C → D (3)
• D → E (-1), B → E (5)
Relaxation is performed for V − 1 iterations, followed by a check for further updates
in the V -th iteration to detect cycles.
Problem 8: Currency Arbitrage Detection
Representation:
• Vertices represent currencies.
• Edges represent exchange rates, with weights as − log(rate).
Why Negative Logarithms?
Logarithms convert multiplication into addition, and a negative weight cycle corresponds
to a profitable arbitrage opportunity.
3
Problem 9: Energy-Efficient Wireless Networks
Representation:
• Vertices: Sensor nodes.
• Edges: Energy costs, including possible negative weights.
Negative Cycles: Represent inefficiencies or perpetual energy gains.
Problem 10: Supply Chain Optimization
Representation:
• Vertices: Supply chain locations.
• Edges: Dynamic costs, including subsidies (negative weights).
Negative Cycles: Indicate inefficiencies or profitable loops.
Problem 11: Financial Network Risk Management
Representation:
• Vertices: Financial institutions.
• Edges: Transaction risks (weights).
Negative Cycles: Highlight low-risk opportunities or inefficiencies in the network.