Changeset 8312
- Timestamp:
- 07/20/12 11:19:43 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/DijkstraAlgorithm.cs
r8308 r8312 1 1 2 using System; 2 3 using System.Collections.Generic; 3 4 using HeuristicLab.Problems.RoutePlanning.Graph; … … 5 6 public class DijkstraAlgorithm : IRouter { 6 7 private Graph graph; 8 9 private HashSet<long> visitedNodes; 10 private HashSet<long> unvisitedNodes; 11 private Dictionary<long, float> distances; 12 private Dictionary<long, long> predecessors; 7 13 8 14 public DijkstraAlgorithm(Graph graph) { … … 13 19 14 20 public long[] Calculate(long sourceNodeId, long targetNodeId) { 15 HashSet<long>visitedNodes = new HashSet<long>(); // visited16 HashSet<long>unvisitedNodes = new HashSet<long>(); // unvisited17 Dictionary<long, long> distances = new Dictionary<long, long>();18 Dictionary<long, long>predecessors = new Dictionary<long, long>();21 visitedNodes = new HashSet<long>(); // visited 22 unvisitedNodes = new HashSet<long>(); // unvisited 23 distances = new Dictionary<long, float>(); 24 predecessors = new Dictionary<long, long>(); 19 25 20 long currentNode;21 Dictionary<long, float> currentNeighbors = new Dictionary<long, float>();22 26 List<long> resultRoute = new List<long>(); 23 24 27 if (sourceNodeId == targetNodeId) { 25 28 resultRoute.Add(sourceNodeId); … … 27 30 } 28 31 29 distances.Add(sourceNodeId, 0); 30 unvisitedNodes.Add(sourceNodeId); 31 32 currentNode = sourceNodeId; 33 currentNeighbors = graph.GetNeighbors(currentNode); 32 long currentNode = sourceNodeId; 33 distances.Add(currentNode, 0); 34 unvisitedNodes.Add(currentNode); 34 35 35 36 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); 37 currentNode = GetMinimumDistance(unvisitedNodes); 42 38 visitedNodes.Add(currentNode); 43 39 unvisitedNodes.Remove(currentNode); 44 45 40 if (currentNode == targetNodeId) { 46 41 break; 47 42 } 48 49 currentNeighbors = graph.GetNeighbors(currentNode); 50 43 FindMinimalWeight(currentNode); 51 44 } 52 45 … … 54 47 // target was not found 55 48 // maybe throw exception 49 throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId)); 56 50 } 57 return resultRoute.ToArray();51 return GetRoute(targetNodeId).ToArray(); 58 52 } 59 53 60 54 #endregion 61 55 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 //} 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 } 73 106 } 74 107 }
Note: See TracChangeset
for help on using the changeset viewer.