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
β’ 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
β’ New edges via
β’ Path queries via
Output: Return the minimum cost of the shortest path, or
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
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!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code