1 |
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Problems.RoutePlanning.Graph;
|
---|
4 | namespace HeuristicLab.Algorithms.GraphRouting {
|
---|
5 | public class DijkstraAlgorithm : IRouter {
|
---|
6 | private Graph graph;
|
---|
7 |
|
---|
8 | public DijkstraAlgorithm(Graph graph) {
|
---|
9 | this.graph = graph;
|
---|
10 | }
|
---|
11 |
|
---|
12 | #region IRouter Members
|
---|
13 |
|
---|
14 | public long[] Calculate(long sourceNodeId, long targetNodeId) {
|
---|
15 | HashSet<long> visitedNodes = new HashSet<long>(); // visited
|
---|
16 | HashSet<long> unvisitedNodes = new HashSet<long>(); // unvisited
|
---|
17 | Dictionary<long, long> distances = new Dictionary<long, long>();
|
---|
18 | Dictionary<long, long> predecessors = new Dictionary<long, long>();
|
---|
19 |
|
---|
20 | long currentNode;
|
---|
21 | Dictionary<long, float> currentNeighbors = new Dictionary<long, float>();
|
---|
22 | List<long> resultRoute = new List<long>();
|
---|
23 |
|
---|
24 | if (sourceNodeId == targetNodeId) {
|
---|
25 | resultRoute.Add(sourceNodeId);
|
---|
26 | return resultRoute.ToArray();
|
---|
27 | }
|
---|
28 |
|
---|
29 | distances.Add(sourceNodeId, 0);
|
---|
30 | unvisitedNodes.Add(sourceNodeId);
|
---|
31 |
|
---|
32 | currentNode = sourceNodeId;
|
---|
33 | currentNeighbors = graph.GetNeighbors(currentNode);
|
---|
34 |
|
---|
35 | while (unvisitedNodes.Count > 0) {
|
---|
36 | //Vertex node = getMinimum(unSettledNodes);
|
---|
37 | //settledNodes.add(node);
|
---|
38 | //unSettledNodes.remove(node);
|
---|
39 | //findMinimalDistances(node);
|
---|
40 |
|
---|
41 | //currentNode = GetMinimum(unvisitedNodes);
|
---|
42 | visitedNodes.Add(currentNode);
|
---|
43 | unvisitedNodes.Remove(currentNode);
|
---|
44 |
|
---|
45 | if (currentNode == targetNodeId) {
|
---|
46 | break;
|
---|
47 | }
|
---|
48 |
|
---|
49 | currentNeighbors = graph.GetNeighbors(currentNode);
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (currentNode != targetNodeId) {
|
---|
54 | // target was not found
|
---|
55 | // maybe throw exception
|
---|
56 | }
|
---|
57 | return resultRoute.ToArray();
|
---|
58 | }
|
---|
59 |
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | //private long GetMinimum(HashSet<long> nodeIds) {
|
---|
63 | // long minimum = -1;
|
---|
64 | // foreach (long id in nodeIds) {
|
---|
65 | // if (minimum == -1) minimum = id;
|
---|
66 | // else {
|
---|
67 | // if (GetDistance(id) < GetDistance(minimum)) {
|
---|
68 | // minimum = id;
|
---|
69 | // }
|
---|
70 | // }
|
---|
71 | // }
|
---|
72 | //}
|
---|
73 | }
|
---|
74 | }
|
---|