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