Dijkstra Simulator
Build interactive graphs and watch Dijkstra prioritize the lowest-cost paths to the target using a real-time priority queue.
Routing Table (Shortest Distances)
| Destination | Cost from Start (g) | Previous | Visited |
|---|---|---|---|
| AStart | 0 | - | 0 |
| B | ∞ | - | 0 |
| C | ∞ | - | 0 |
| D | ∞ | - | 0 |
| E | ∞ | - | 0 |
Explanation: Shortest Paths:
About This Simulation
This Dijkstra simulator lets you build a weighted graph, choose a source and target, and watch Dijkstra's algorithm pick the closest unvisited node from a priority queue at every step until the shortest path is found.
Features
- Draw nodes and weighted edges, or load an example graph
- Choose the source and target nodes
- Live priority queue and distance values for every node
- Edge relaxation highlighted step by step
- Final shortest path highlighted on the graph
Explanation
Dijkstra's algorithm finds the shortest path from one source node to every other node in a graph with non-negative edge weights. It is a greedy algorithm: at each step it permanently settles the unvisited node with the smallest known distance.
How it works
- Set the distance of the source to 0 and every other node to infinity. Put the source in a priority queue.
- Remove the node u with the smallest distance from the priority queue and mark it as visited.
- For each neighbour v of u, relax the edge: if dist[u] + w(u, v) < dist[v], update dist[v], set parent[v] = u and add v to the queue with its new distance.
- Repeat until the queue is empty or the target has been visited.
- Follow the parent links back from the target to read off the shortest path.
Why it matters
Dijkstra's algorithm is used in GPS navigation, network routing protocols such as OSPF, and games. Understanding relaxation and the priority queue is essential for CSE221 and for algorithms like A* and Prim's.
Frequently Asked Questions
What is the time complexity of Dijkstra's algorithm?
O((V + E) log V) with a binary heap priority queue and an adjacency list. A simple array implementation takes O(V²), which can be better for very dense graphs.
Why doesn't Dijkstra's algorithm work with negative edge weights?
Once a node is settled, Dijkstra assumes its distance can never get smaller. A negative edge found later could make a shorter path to an already-settled node, so the answer may be wrong. Use Bellman-Ford when edges can be negative.
What is edge relaxation?
Relaxing an edge (u, v) means checking whether going through u gives a shorter path to v: if dist[u] + w(u, v) < dist[v], then dist[v] is updated to the smaller value.
What is the difference between Dijkstra and Prim's algorithm?
Both use a priority queue, but Dijkstra picks the node with the smallest total distance from the source, while Prim picks the node connected by the cheapest single edge to the tree. Dijkstra builds a shortest-path tree; Prim builds a minimum spanning tree.
Topics covered
- Dijkstra's Algorithm
- Single-Source Shortest Path
- Weighted Graphs
- Priority Queue / Min-Heap
- Edge Relaxation
- Greedy Algorithms
Tags
- dijkstra
- shortest path
- weighted graph
- priority queue
- greedy
Related Simulations
Algorithms
Searching Algorithms
Run linear, binary, ternary and quaternary search on your own array and step through the code one line at a time.
Algorithms
Sorting Algorithm Visualiser
Visualise sorting algorithms
Algorithms
A* Algorithm
Show pathfinding using cost, heuristic, and total estimated distance.
Algorithms
BFS (Breadth First Search) Simulator
Build interactive graphs and watch BFS explore networks level-by-level using a visual queue.

