This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

# Dijkstra's Algorithm

Graph traversal algorithms are used to process a graph of interconnected nodes, visiting each node by following a set behaviour. Dijkstra's algorithm is such an algorithm. It determines the shortest path from a start point to all other nodes in a graph.

### Testing the Calculator

We can test the algorithm by creating a graph and traversing it. The code below creates the graph pictured at the start of this article. The distances from the "G" node are calculated and the results outputted. Try changing the distances or the starting point and viewing the results.

Graph graph = new Graph();

//Nodes

//Connections

var calculator = new DistanceCalculator();
var distances = calculator.CalculateDistances(graph, "G");  // Start from "G"

foreach (var d in distances)
{
Console.WriteLine("{0}, {1}", d.Key, d.Value);
}

/* OUTPUT

A, 28
B, 22
C, 18
D, 27
E, 29
F, 8
G, 0
H, 18
I, 9
J, 19
Z, Infinity

*/
29 May 2011