How to find the shortest path in a weighted graph using Dijkstra's algorithm?
I am studying graph theory and I need to understand Dijkstra's algorithm for finding the shortest path from a source vertex to all other vertices in a weighted graph.
I know the basic idea:
- Set distance to source = 0, all others =
- Mark all vertices as unvisited
- For the current vertex, consider all unvisited neighbors and update their distances
- Mark current as visited, select the unvisited vertex with smallest distance
- Repeat until all visited
But I have specific questions:
- Why does Dijkstra's algorithm fail with negative edge weights?
- What is the time complexity with different data structures?
- Can someone walk through the algorithm step-by-step on a concrete graph?
For example, find shortest paths from A on:
- A→B: 4, A→C: 2
- B→C: 1, B→D: 5
- C→D: 8, C→E: 10
- D→E: 2, D→Z: 6
- E→Z: 3
1 answers314 views
Dijkstra step-by-step with the table is the clearest explanation I've seen.