Chapter 6 · Simulation

Shortest-Path Algorithms

← Chapter 6

Ready.

Shortest-Path Algorithms

A connected, weighted graph is generated; Dijkstra, Bellman-Ford and Floyd-Warshall are compared on the path from node A to the last node.

How to use

  • Choose the number of nodes (3–10) and press Generate to get a new random connected graph.
  • Run one of the three algorithms: the current node is highlighted, relaxed edges light up, and tentative distances appear above the nodes and in the side table.
  • When the algorithm finishes, the shortest path from A to the last node is drawn and its rows in the table are marked.
  • Drag nodes to rearrange the drawing (only while no algorithm is running). Cancel stops the run.

Dijkstra

Correct only on graphs whose edge weights are non-negative, so the simulator generates positive weights only.

Bellman-Ford

Relaxes all edges repeatedly. It can also handle negative edges, but this shared comparison graph uses positive weights.

Floyd-Warshall

Computes shortest paths between all pairs of nodes by dynamic programming in O(V³) time. The table shows the row of source A after each intermediate node k.

Complexity

AlgorithmTimeNote
Dijkstra (array)O(V²)O((V + E) log V) with a binary heap
Bellman-FordO(V·E)Detects negative cycles
Floyd-WarshallO(V³)All pairs, O(V²) memory