DSA Section簡単

Dijkstra

Detailed guide and Python implementation for the 'Dijkstra' problem.

問題提起

簡単

Write a function dijkstra(graph, start) that calculates the shortest path from a starting node start to all other nodes in a weighted graph. The graph is represented as an adjacency list of dictionaries, where graph[u][v] is the weight of the directed edge from u to v. Return a dictionary of shortest distances.

制約
  • 1 <= V <= 1000
  • 0 <= E <= 5000
  • Weights are non-negative integers.

Example 1
Input
graph = {0: {1: 4, 2: 1}, 1: {3: 1}, 2: {1: 2, 3: 5}, 3: {}}, start = 0
Output
{0: 0, 1: 3, 2: 1, 3: 4}
Explanation

Shortest distance from 0 to 1 is 3 (via 0->2->1).

Need a Hint?
Consider using Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。