📖 Scenario: You are working on a navigation system that finds the shortest path between cities. Some roads have toll discounts, which means the cost can be negative. You need to find the shortest path from a starting city to all other cities, even if some roads reduce the cost.
🎯 Goal: Build a program using the Bellman Ford algorithm to find the shortest distances from a starting city to all other cities in a graph that may have negative edge weights.
📋 What You'll Learn
Create a list of edges representing the graph with exact values
Add a variable for the number of vertices called V
Implement the Bellman Ford algorithm using the given edge list and V
Print the shortest distances from the source city after running the algorithm
💡 Why This Matters
🌍 Real World
Navigation systems and routing algorithms often need to handle roads with discounts or penalties, which can be modeled as negative weights.
💼 Career
Understanding Bellman Ford is important for software engineers working on graph algorithms, network routing, and optimization problems.
Progress0 / 4 steps
1
Create the graph edges
Create a vector of edges called edges with these exact entries: {0, 1, 5}, {1, 2, -2}, {0, 2, 4}, {2, 3, 3}, {3, 1, 1}. Each edge is a struct with src, dest, and weight.
DSA C++
Hint
Use a vector<Edge> and initialize it with the exact edges given.
2
Add the number of vertices
Add an integer variable called V and set it to 4 to represent the number of vertices in the graph.
DSA C++
Hint
Set V to the total number of vertices, which is 4.
3
Implement the Bellman Ford algorithm
Create a vector dist of size V initialized with INT_MAX. Set dist[0] to 0 as the source vertex. Use a for loop to relax all edges V - 1 times. Inside the loop, use a for loop to iterate over edges and update dist if a shorter path is found.
DSA C++
Hint
Initialize distances, then relax edges repeatedly to update shortest paths.
4
Print the shortest distances
Use a for loop with variable i from 0 to V - 1 to print the shortest distance to each vertex in the format: "Distance to vertex i is dist[i]".
DSA C++
Hint
Print each vertex number and its distance on a new line.
Practice
(1/5)
1. What is the main advantage of the Bellman Ford algorithm compared to Dijkstra's algorithm?
easy
A. It only works on directed acyclic graphs.
B. It runs faster on all graphs.
C. It uses less memory.
D. It can handle graphs with negative weight edges.
Solution
Step 1: Understand the limitation of Dijkstra's algorithm
Dijkstra's algorithm cannot correctly handle graphs with negative weight edges because it assumes that once a node's shortest path is found, it cannot be improved.
Step 2: Identify Bellman Ford's capability
Bellman Ford algorithm can handle negative weight edges and detect negative weight cycles, making it more versatile for such graphs.
Final Answer:
It can handle graphs with negative weight edges. -> Option D
Quick Check:
Negative weights handled = D [OK]
Hint: Bellman Ford works with negative edges, Dijkstra does not [OK]
Common Mistakes:
Thinking Bellman Ford is always faster than Dijkstra
Believing Bellman Ford cannot detect negative cycles
Assuming Bellman Ford only works on acyclic graphs
2. Which of the following is the correct way to initialize the distance array in Bellman Ford algorithm in C++?
easy
A. vector<int> dist(V, 0); dist[src] = INT_MAX;
B. vector<int> dist(V, INT_MAX); dist[src] = 0;
C. vector<int> dist(V, -1); dist[src] = 0;
D. vector<int> dist(V, INT_MIN); dist[src] = 0;
Solution
Step 1: Understand distance initialization
Distances to all vertices except the source should be set to a very large number (infinity equivalent), here INT_MAX, to represent unknown distance.
Step 2: Set source distance to zero
The source vertex distance is zero because the distance from source to itself is zero.
Final Answer:
vector<int> dist(V, INT_MAX); dist[src] = 0; -> Option B
Quick Check:
Initialize with INT_MAX and source zero = B [OK]
Hint: Initialize all distances to INT_MAX except source zero [OK]
Common Mistakes:
Setting all distances to zero
Using INT_MIN which is incorrect for distances
Not setting source distance to zero
3. Consider the following graph edges and weights: