Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/NaiveDijkstraAlgorithm.cs @ 12832

Last change on this file since 12832 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: 4.2 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.GraphRouting.Interfaces;
5using HeuristicLab.Problems.RoutePlanning.Interfaces;
6namespace HeuristicLab.Algorithms.GraphRouting {
7  // simple implemntation of disjkstra's algorithm
8  // based on: http://de.wikipedia.org/wiki/Dijkstra-Algorithmus
9  // (doesn't use heaps ... )
10  public class NaiveDijkstraAlgorithm : IRouter {
11    private IGraph graph;
12
13    private HashSet<long> visitedNodes;
14    private HashSet<long> unvisitedNodes;
15    private Dictionary<long, float> distances;
16    private Dictionary<long, long> predecessors;
17
18    public NaiveDijkstraAlgorithm(IGraph graph) {
19      this.graph = graph;
20    }
21
22    #region IRouter Members
23
24    public long[] Calculate(long sourceNodeId, long targetNodeId) {
25      visitedNodes = new HashSet<long>();        // visited
26      unvisitedNodes = new HashSet<long>();      // unvisited
27      distances = new Dictionary<long, float>();
28      predecessors = new Dictionary<long, long>();
29
30      List<long> resultRoute = new List<long>();
31      if (sourceNodeId == targetNodeId) {
32        resultRoute.Add(sourceNodeId);
33        return resultRoute.ToArray();
34      }
35
36      long currentNode = sourceNodeId;
37      distances.Add(currentNode, 0);
38      unvisitedNodes.Add(currentNode);
39
40      while (unvisitedNodes.Count > 0) {
41        currentNode = GetMinimumDistance(unvisitedNodes);
42        visitedNodes.Add(currentNode);
43        unvisitedNodes.Remove(currentNode);
44        if (currentNode == targetNodeId) {
45          break;
46        }
47        FindMinimalWeight(currentNode);
48      }
49
50      if (currentNode != targetNodeId) {
51        // target was not found
52        // maybe throw exception
53        throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
54      }
55      return GetRoute(targetNodeId).ToArray();
56    }
57
58    #endregion
59
60    public long GetNodeIdWithRank(long sourceNodeId, int rank) {
61      visitedNodes = new HashSet<long>();        // visited
62      unvisitedNodes = new HashSet<long>();      // unvisited
63      distances = new Dictionary<long, float>();
64      predecessors = new Dictionary<long, long>();
65      int currentRank = 0;
66
67
68      long currentNode = sourceNodeId;
69      distances.Add(currentNode, 0);
70      unvisitedNodes.Add(currentNode);
71
72      while (unvisitedNodes.Count > 0) {
73        currentNode = GetMinimumDistance(unvisitedNodes);
74        visitedNodes.Add(currentNode);
75        unvisitedNodes.Remove(currentNode);
76        currentRank++;
77        if (currentRank == rank) {
78          break;
79        }
80        FindMinimalWeight(currentNode);
81      }
82      return currentNode;
83    }
84
85    private long GetMinimumDistance(HashSet<long> nodeIds) {
86      long minimum = -1;
87      foreach (long id in nodeIds) {
88        if (minimum == -1) minimum = id;
89        else {
90          if (GetDistance(id) < GetDistance(minimum)) {
91            minimum = id;
92          }
93        }
94      }
95      return minimum;
96    }
97
98    private float GetDistance(long id) {
99      float d;
100      if (distances.TryGetValue(id, out d)) {
101        return d;
102      } else { return float.MaxValue; }
103
104    }
105
106    private void FindMinimalWeight(long nodeId) {
107      Dictionary<long, float> currentNeighbors = graph.GetNeighborsWithWeight(nodeId);
108      foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
109        // update relax
110        float totalWeight = GetDistance(nodeId) + neighbor.Value;
111        if (GetDistance(neighbor.Key) > totalWeight) {
112          distances[neighbor.Key] = totalWeight;
113          predecessors[neighbor.Key] = nodeId;
114          unvisitedNodes.Add(neighbor.Key);
115        }
116      }
117    }
118
119    private List<long> GetRoute(long nodeId) {
120      List<long> route = new List<long>();
121      long current = nodeId;
122      long next;
123
124      if (!predecessors.TryGetValue(current, out next)) {
125        return null;
126      }
127      route.Add(current);
128      while (predecessors.TryGetValue(current, out next)) {
129        current = predecessors[current];
130        route.Add(current);
131      }
132      route.Reverse();
133      return route;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.