Assignment-4
CSE 2218: DSA 2 Lab
Single Source Shortest Paths
Total Marks: 10
Assignment Instructions:
● Submit 2 files: 1 file containing the code for the problem and 1 pdf containing the explanation.
● Don’t submit a zip file.
● Rename the files as Student_Id_code and Student_Id_explanation.
Question 1: Dijkstra’s Algorithm
Real-Life Scenario: Fastest Food Delivery in a Neighborhood
Imagine you manage a local food delivery service that must calculate the fastest driving route to
different delivery points in a small neighborhood. Each intersection (A, B, C, D, E, F) is
connected by roads of varying lengths in kilometers. You need to find the shortest path from the
central kitchen (intersection A) to all other intersections using Dijkstra's algorithm.
1. Graph Description (Example Input)
○ Vertices (Intersections): A, B, C, D, E, F
○ Edges (Roads with distances in km):
1. A → B = 2
2. A → C = 4
3. B → C = 1
4. B → D = 7
5. C → E = 3
6. D → F = 2
7. E → F = 5
8. C → D = 2
2. You may assume these roads are bidirectional if you wish to consider travel in both
directions. (If so, list them as A–B, B–C, etc.)
3. Task
○ Use Dijkstra’s algorithm to determine the shortest distance from A to every
other intersection (B, C, D, E, F).
○ Show each step of the algorithm:
1. Initialize distances
2. Update neighbors
3. Settle final shortest paths
○ Provide the final shortest distance to each node, and reconstruct the actual
shortest path if necessary.
4. Example Output (Illustrative)
○ Shortest distance from A to B: 2 (Path: A → B)
○ Shortest distance from A to C: 3 (Path: A → B → C)
○ Shortest distance from A to D: 5 (Path: A → B → C → D)
○ Shortest distance from A to E: 6 (Path: A → B → C → E)
○ Shortest distance from A to F: 7 (Path: A → B → C → D → F)
(Note: The exact distances and paths might vary depending on the edges you choose to use or if
you treat them as one-way. The provided numbers are just an illustrative example.)
Question 2: Bellman-Ford Algorithm
Real-Life Scenario: Travel Planning with Discounts
You are planning a series of bus or train trips between cities where certain routes have
promotional discounts that can effectively reduce the total travel “cost.” In this scenario, some
edges might have negative weights to represent these special discounts. You want to calculate the
least costly route from a starting city to all other cities.
1. Graph Description (Example Input)
○ Vertices (Cities): A, B, C, D
○ Edges (Routes with costs):
1. A → B = 4
2. A → C = 2
3. B → C = -1 (this represents a special discount or credit)
4. B → D = 3
5. C → D = 2
2. Task
○ Use the Bellman-Ford algorithm to determine the shortest cost from A to all
other cities (B, C, D).
○ Show the relaxation steps for each edge, across the necessary iterations:
1. Initialization of distance array
2. Repeated relaxation
3. Final check for negative cycles (if any)
○ List the final shortest distances from A to each node, and trace the resulting
path (if required).
3. Example Output (Illustrative)
○ Shortest cost from A to B: 4 (Path: A → B)
○ Shortest cost from A to C: 3 (Path: A → B → C)
○ Shortest cost from A to D: 5 (Path: A → B → C → D)
(Again, the sample final numbers assume no changes to the routes. They illustrate the format of
the final answer.)