Design Graph With Shortest Path Calculator - Problem
Design a Dynamic Graph with Shortest Path Calculator

You need to design and implement a smart graph data structure that can handle a directed weighted graph with dynamic edge additions and efficient shortest path queries.

🎯 Your Mission:
Build a Graph class that starts with n nodes (numbered 0 to n-1) and initial edges, then supports:
Dynamic Edge Addition: Add new weighted edges at runtime
Shortest Path Queries: Find minimum cost path between any two nodes

Key Challenge: The graph changes over time as new edges are added, so you need to efficiently handle both updates and queries. Think about real-world scenarios like GPS navigation systems that need to calculate shortest routes while road conditions and new routes are constantly being updated!

Input Format:
• Initial edges as edges[i] = [from, to, cost]
• New edges via addEdge([from, to, cost])
• Path queries via shortestPath(node1, node2)

Output: Return the minimum cost of the shortest path, or -1 if no path exists.

Input & Output

example_1.py — Basic Operations
$ Input: Graph(4, [[0,1,4],[0,3,7],[1,2,3],[2,3,2]]) shortestPath(0, 3) → 7 addEdge([1,3,1]) shortestPath(0, 3) → 5
Output: 7 5
💡 Note: Initially, shortest path from 0→3 is direct edge with cost 7. After adding edge 1→3 with cost 1, the new shortest path becomes 0→1→3 with total cost 4+1=5.
example_2.py — No Path Case
$ Input: Graph(3, [[0,1,2]]) shortestPath(1, 2) → -1 addEdge([1,2,5]) shortestPath(1, 2) → 5
Output: -1 5
💡 Note: Initially no path exists from node 1 to node 2, so return -1. After adding edge 1→2, the shortest (and only) path has cost 5.
example_3.py — Same Node Query
$ Input: Graph(2, [[0,1,3]]) shortestPath(0, 0) → 0 shortestPath(1, 1) → 0
Output: 0 0
💡 Note: Distance from any node to itself is always 0, regardless of the graph structure.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ edges.length ≤ n * (n - 1)
  • edges[i].length == 3
  • 0 ≤ fromi, toi < n
  • fromi ≠ toi
  • 1 ≤ edgeCosti ≤ 106
  • At most 100 calls will be made to addEdge
  • At most 100 calls will be made to shortestPath

Visualization

Tap to expand
Dynamic Graph with Shortest Path Calculator INPUT 0 1 2 3 4 7 3 2 1 (new) n=4, edges=[[0,1,4],[0,3,7], [1,2,3],[2,3,2]] addEdge([1,3,1]) Queries: shortestPath(0, 3) shortestPath(0, 3) after add ALGORITHM STEPS 1 Initialize Graph Store adjacency list 2 Dijkstra's Algorithm Use min-heap priority queue 3 Process Nodes Relax edges, update dist[] 4 Return Result dist[target] or -1 Min-Heap Priority Queue (0,0) (4,1) (7,3) (7,2) (cost, node) pairs sorted by cost Distance Array d[0] 0 d[1] 4 d[2] 7 d[3] 5 after add FINAL RESULT Query 1: shortestPath(0, 3) 0 Direct: 7 3 7 After addEdge([1,3,1]) Query 2: shortestPath(0, 3) 0 4 1 1 3 0 --4--> 1 --1--> 3 = 5 5 OK - New edge creates shorter path! Key Insight: Dijkstra's algorithm runs fresh for each query (O((V+E)logV)) since the graph is dynamic. Adding an edge is O(1) - just append to adjacency list. No need to precompute all-pairs shortest paths because edge additions would invalidate them. Perfect for GPS systems with real-time route updates! TutorialsPoint - Design Graph With Shortest Path Calculator | Dijkstra's Algorithm Approach
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
32.5K Views
High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen