-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraAlgorithm.cs
49 lines (42 loc) · 1.22 KB
/
DijkstraAlgorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using DA.DataStructures.Tree;
using DA.Graphs;
namespace DA.Algorithms.TreeAlgorithms
{
static class DijkstraAlgorithm
{
public static void FindShortestPath (Graph graph, int source)
{
int[] previous = new int[graph.Count];
int[] distance = new int[graph.Count];
for (int i = 0; i < graph.Count; i++)
{
previous[i] = -1;
distance[i] = int.MaxValue;
}
previous[source] = -1;
distance[source] = 0;
PriorityQueue<Graph.Node> queue = new PriorityQueue<Graph.Node> ();
Graph.Node node = new Graph.Node (source, source, 0);
while (queue.Count != 0)
{
node = queue.Peek ();
queue.Dequeue ();
Graph.Node currentNode = graph.GetNode(node.destination);
while (currentNode != null)
{
int cost = currentNode.cost + distance[currentNode.source];
if (cost < distance[currentNode.destination])
{
distance[currentNode.destination] = cost;
previous[currentNode.destination] = currentNode.source;
node = new Graph.Node (currentNode.source, currentNode.destination, cost);
queue.Enqueue (node);
}
currentNode = currentNode.next;
}
}
}
}
}