Here's a C# implementation of Dijkstra's algorithm using an adjacency list to represent the graph:
main.cs1722 chars51 lines
To use this implementation, you would create an adjacency list representing your graph, where the keys are the nodes and the values are lists of tuples representing that node's neighbors and edge weights. For example, the following code creates a graph with 5 nodes and the edges (0,1), (0,2), (1,2), (1,3), (2,3), and (2,4):
main.cs479 chars7 lines
You could then call the ShortestPaths method with the graph and start node to get the shortest distances to every other node:
main.cs166 chars6 lines
gistlibby LogSnag