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
Initial Graph State01234732After Adding Edge [1,3,1]012347321Path ComparisonOriginal Shortest Path (0→3)Route: 0 → 3 (direct)Cost: 7New Shortest Path (0→3)Route: 0 → 1 → 3Cost: 4 + 1 = 5
Understanding the Visualization
1
Initial Graph
Start with nodes and initial edges
2
Query Path
Use Dijkstra's algorithm to find shortest path
3
Add New Edge
Dynamically add edge to adjacency list
4
Updated Path
New shortest path discovered after graph update
Key Takeaway
🎯 Key Insight: Dynamic graphs benefit from adjacency lists for O(1) edge additions combined with on-demand shortest path algorithms like Dijkstra's for efficient queries!
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