1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Problems.RoutePlanning.Graph;
|
---|
5 | namespace HeuristicLab.Algorithms.GraphRouting {
|
---|
6 | public class DijkstraAlgorithm : IRouter {
|
---|
7 | private OsmGraph graph;
|
---|
8 |
|
---|
9 | private HashSet<long> visitedNodes;
|
---|
10 | private HashSet<long> unvisitedNodes;
|
---|
11 | private Dictionary<long, float> distances;
|
---|
12 | private Dictionary<long, long> predecessors;
|
---|
13 |
|
---|
14 | public DijkstraAlgorithm(OsmGraph graph) {
|
---|
15 | this.graph = graph;
|
---|
16 | }
|
---|
17 |
|
---|
18 | #region IRouter Members
|
---|
19 |
|
---|
20 | public long[] Calculate(long sourceNodeId, long targetNodeId) {
|
---|
21 | visitedNodes = new HashSet<long>(); // visited
|
---|
22 | unvisitedNodes = new HashSet<long>(); // unvisited
|
---|
23 | distances = new Dictionary<long, float>();
|
---|
24 | predecessors = new Dictionary<long, long>();
|
---|
25 |
|
---|
26 | List<long> resultRoute = new List<long>();
|
---|
27 | if (sourceNodeId == targetNodeId) {
|
---|
28 | resultRoute.Add(sourceNodeId);
|
---|
29 | return resultRoute.ToArray();
|
---|
30 | }
|
---|
31 |
|
---|
32 | long currentNode = sourceNodeId;
|
---|
33 | distances.Add(currentNode, 0);
|
---|
34 | unvisitedNodes.Add(currentNode);
|
---|
35 |
|
---|
36 | while (unvisitedNodes.Count > 0) {
|
---|
37 | currentNode = GetMinimumDistance(unvisitedNodes);
|
---|
38 | visitedNodes.Add(currentNode);
|
---|
39 | unvisitedNodes.Remove(currentNode);
|
---|
40 | if (currentNode == targetNodeId) {
|
---|
41 | break;
|
---|
42 | }
|
---|
43 | FindMinimalWeight(currentNode);
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (currentNode != targetNodeId) {
|
---|
47 | // target was not found
|
---|
48 | // maybe throw exception
|
---|
49 | throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
|
---|
50 | }
|
---|
51 | return GetRoute(targetNodeId).ToArray();
|
---|
52 | }
|
---|
53 |
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | private long GetMinimumDistance(HashSet<long> nodeIds) {
|
---|
57 | long minimum = -1;
|
---|
58 | foreach (long id in nodeIds) {
|
---|
59 | if (minimum == -1) minimum = id;
|
---|
60 | else {
|
---|
61 | if (GetDistance(id) < GetDistance(minimum)) {
|
---|
62 | minimum = id;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return minimum;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private float GetDistance(long id) {
|
---|
70 | float d;
|
---|
71 | if (distances.TryGetValue(id, out d)) {
|
---|
72 | return d;
|
---|
73 | } else { return float.MaxValue; }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void FindMinimalWeight(long nodeId) {
|
---|
78 | Dictionary<long, float> currentNeighbors = graph.GetNeighbors(nodeId);
|
---|
79 | foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
|
---|
80 | // update relax
|
---|
81 | float totalWeight = GetDistance(nodeId) + neighbor.Value;
|
---|
82 | if (GetDistance(neighbor.Key) > totalWeight) {
|
---|
83 | distances[neighbor.Key] = totalWeight;
|
---|
84 | predecessors[neighbor.Key] = nodeId;
|
---|
85 | unvisitedNodes.Add(neighbor.Key);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private List<long> GetRoute(long nodeId) {
|
---|
91 | List<long> route = new List<long>();
|
---|
92 | long current = nodeId;
|
---|
93 | long next;
|
---|
94 |
|
---|
95 | if (!predecessors.TryGetValue(current, out next)) {
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 | route.Add(current);
|
---|
99 | while (predecessors.TryGetValue(current, out next)) {
|
---|
100 | current = predecessors[current];
|
---|
101 | route.Add(current);
|
---|
102 | }
|
---|
103 | route.Reverse();
|
---|
104 | return route;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|