Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1894_RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/DijkstraAlgorithm.cs @ 16654

Last change on this file since 16654 was 8640, checked in by spimming, 12 years ago

#1894:

  • Included costCalculator in a star search
  • restructured and renamed Dijkstra algorithm
  • Added code comments to FibonacciHeap
File size: 3.6 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.GraphRouting.Interfaces;
5using HeuristicLab.Algorithms.GraphRouting.PriorityQueues;
6using HeuristicLab.Problems.RoutePlanning.Interfaces;
7namespace HeuristicLab.Algorithms.GraphRouting {
8  public class DijkstraAlgorithm : IRouter {
9    private IGraph graph;
10    private IHeap<float, long> unvisitedNodes;
11    private Dictionary<long, float> distances;
12    private Dictionary<long, long> predecessors;
13
14    public DijkstraAlgorithm(IGraph graph) {
15      this.graph = graph;
16    }
17
18    #region IRouter Members
19
20    public long[] Calculate(long sourceNodeId, long targetNodeId) {
21      unvisitedNodes = new BinaryHeap<float, long>(1000000);      // unvisited
22      //unvisitedNodes = new BinomialHeap<float, long>();    // unvisited
23      //unvisitedNodes = new FibonacciHeap<float, long>();   // unvisited
24      distances = new Dictionary<long, float>();
25      predecessors = new Dictionary<long, long>();
26
27      List<long> resultRoute = new List<long>();
28      if (sourceNodeId == targetNodeId) {
29        resultRoute.Add(sourceNodeId);
30        return resultRoute.ToArray();
31      }
32
33      long currentNode = sourceNodeId;
34      float currentWeight = 0;
35      unvisitedNodes.Insert(currentWeight, currentNode);
36
37      while (unvisitedNodes.Size > 0) {
38        KeyValuePair<float, long> data = unvisitedNodes.PeekMin();
39        unvisitedNodes.RemoveMin();
40        currentNode = data.Value;
41        currentWeight = data.Key;
42
43        //--------------
44        //Logger.LogToFile("cn = " + currentNode + " (" + currentWeight + ")");
45        //--------------
46
47        if (currentNode == targetNodeId) {
48          break;
49        }
50
51        distances[currentNode] = currentWeight;
52        RelaxNeighbors(currentNode, currentWeight);
53
54
55        //--------------
56        //Logger.LogToFile("");
57        //--------------
58      }
59
60      if (currentNode != targetNodeId) {
61        throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
62      }
63      return GetRoute(targetNodeId).ToArray();
64    }
65
66    #endregion
67
68    public void RelaxNeighbors(long nodeId, float weight) {
69      Dictionary<long, float> currentNeighbors = graph.GetNeighborsWithWeight(nodeId);
70      foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
71        float totalWeight = weight + neighbor.Value;
72        if (totalWeight < GetDistance(neighbor.Key)) {
73
74          if (GetDistance(neighbor.Key) == float.MaxValue) {
75            unvisitedNodes.Insert(totalWeight, neighbor.Key);
76          } else {
77            unvisitedNodes.DecreaseKey(totalWeight, neighbor.Key);
78          }
79
80          distances[neighbor.Key] = totalWeight;
81          predecessors[neighbor.Key] = nodeId;
82
83          //--------------
84          //Logger.LogToFile("    neigb:" + neighbor.Key + " (" + totalWeight + ")");
85          //--------------
86        }
87      }
88    }
89
90    private float GetDistance(long id) {
91      float d;
92      if (distances.TryGetValue(id, out d)) {
93        return d;
94      } else { return float.MaxValue; }
95    }
96
97    private List<long> GetRoute(long nodeId) {
98      List<long> route = new List<long>();
99      long current = nodeId;
100      long next;
101
102      if (!predecessors.TryGetValue(current, out next)) {
103        return null;
104      }
105      route.Add(current);
106      while (predecessors.TryGetValue(current, out next)) {
107        current = predecessors[current];
108        route.Add(current);
109      }
110      route.Reverse();
111      return route;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.