CS With NehalCS With Nehal

Dijkstra Simulator

Build interactive graphs and watch Dijkstra prioritize the lowest-cost paths to the target using a real-time priority queue.

Start:A▼
Target:E▼
Graph Type: Undirected
Nodes (V): 5
Edges (E): 7
Classic Shortest Path
A standard weighted graph. Watch Dijkstra prioritize paths with the smallest total weight to reach the target.
Priority Queue (Min Dist)
Empty
42158102
A
d:0
Start
B
d:∞
C
d:∞
D
d:∞
E
d:∞
End
Animation Speed — 1000ms

Routing Table (Shortest Distances)

Destination Cost from Start (g) PreviousVisited
AStart0-0
B∞-0
C∞-0
D∞-0
E∞-0
Final Path:
Pending calculation...

Explanation: Shortest Paths:

A → A = 0: Source node itself, so distance is 0.

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

  1. Set the distance of the source to 0 and every other node to infinity. Put the source in a priority queue.
  2. Remove the node u with the smallest distance from the priority queue and mark it as visited.
  3. 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.
  4. Repeat until the queue is empty or the target has been visited.
  5. 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

Acknowledgements

The Person Behind This Simulation

This simulation would not have been possible without his valuable contributions and support. We sincerely appreciate his time, effort, and dedication to the project.

Tahsin Uddin Labib

Tahsin Uddin Labib

  • 4th Year CS Undergraduate Student | BRAC University