-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathAStar.cs
200 lines (163 loc) · 7.25 KB
/
AStar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;
using Advanced.Algorithms.DataStructures.Graph;
namespace Advanced.Algorithms.Graph;
/// <summary>
/// A* algorithm implementation using Fibonacci Heap.
/// </summary>
public class AStarShortestPath<T, TW> where TW : IComparable
{
private readonly IAStarHeuristic<T, TW> heuristic;
private readonly IShortestPathOperators<TW> @operator;
public AStarShortestPath(IShortestPathOperators<TW> @operator, IAStarHeuristic<T, TW> heuristic)
{
this.@operator = @operator;
this.heuristic = heuristic;
}
/// <summary>
/// Search path to target using the heuristic.
/// </summary>
public ShortestPathResult<T, TW> FindShortestPath(IGraph<T> graph, T source, T destination)
{
if (@operator == null)
throw new ArgumentException("Provide an operator implementation for generic type W during initialization.");
if (!graph.IsWeightedGraph)
if (@operator.DefaultValue.GetType() != typeof(int))
throw new ArgumentException("Edges of unweighted graphs are assigned an imaginary weight of one (1)." +
"Provide an appropriate IShortestPathOperators<int> operator implementation during initialization.");
//regular argument checks
if (graph?.GetVertex(source) == null || graph.GetVertex(destination) == null) throw new ArgumentException();
//track progress for distance to each Vertex from source
var progress = new Dictionary<T, TW>();
//trace our current path by mapping current vertex to its Parent
var parentMap = new Dictionary<T, T>();
//min heap to pick next closest vertex
var minHeap = new FibonacciHeap<AStarWrap<T, TW>>();
//keep references of heap Node for decrement key operation
var heapMapping = new Dictionary<T, AStarWrap<T, TW>>();
//add vertices to min heap and progress map
foreach (var vertex in graph.VerticesAsEnumberable)
{
//init parent
parentMap.Add(vertex.Key, default);
//init to max value
progress.Add(vertex.Key, @operator.MaxValue);
if (vertex.Key.Equals(source)) continue;
}
//start from source vertex as current
var current = new AStarWrap<T, TW>(heuristic, destination)
{
Distance = @operator.DefaultValue,
Vertex = source
};
//insert neighbour in heap
minHeap.Insert(current);
heapMapping[source] = current;
//until heap is empty
while (minHeap.Count > 0)
{
//next min vertex to visit
current = minHeap.Extract();
heapMapping.Remove(current.Vertex);
//no path exists, so return max value
if (current.Distance.Equals(@operator.MaxValue))
return new ShortestPathResult<T, TW>(null, @operator.MaxValue);
//visit neighbours of current
foreach (var neighbour in graph.GetVertex(current.Vertex).Edges
.Where(x => !x.TargetVertexKey.Equals(source)))
{
//new distance to neighbour
var newDistance = @operator.Sum(current.Distance,
graph.GetVertex(current.Vertex).GetEdge(neighbour.TargetVertex).Weight<TW>());
//current distance to neighbour
var existingDistance = progress[neighbour.TargetVertexKey];
//update distance if new is better
if (newDistance.CompareTo(existingDistance) < 0)
{
progress[neighbour.TargetVertexKey] = newDistance;
if (heapMapping.ContainsKey(neighbour.TargetVertexKey))
{
//decrement distance to neighbour in heap
var decremented = new AStarWrap<T, TW>(heuristic, destination)
{
Distance = newDistance,
Vertex = neighbour.TargetVertexKey
};
minHeap.UpdateKey(heapMapping[neighbour.TargetVertexKey], decremented);
heapMapping[neighbour.TargetVertexKey] = decremented;
}
else
{
//insert neighbour in heap
var discovered = new AStarWrap<T, TW>(heuristic, destination)
{
Distance = newDistance,
Vertex = neighbour.TargetVertexKey
};
minHeap.Insert(discovered);
heapMapping[neighbour.TargetVertexKey] = discovered;
}
//trace parent
parentMap[neighbour.TargetVertexKey] = current.Vertex;
}
}
}
return TracePath(graph, parentMap, source, destination);
}
/// <summary>
/// Trace back path from destination to source using parent map.
/// </summary>
private ShortestPathResult<T, TW> TracePath(IGraph<T> graph, Dictionary<T, T> parentMap, T source, T destination)
{
//trace the path
var pathStack = new Stack<T>();
pathStack.Push(destination);
var currentV = destination;
while (!Equals(currentV, default(T)) && !Equals(parentMap[currentV], default(T)))
{
pathStack.Push(parentMap[currentV]);
currentV = parentMap[currentV];
}
//return result
var resultPath = new List<T>();
var resultLength = @operator.DefaultValue;
while (pathStack.Count > 0) resultPath.Add(pathStack.Pop());
for (var i = 0; i < resultPath.Count - 1; i++)
resultLength = @operator.Sum(resultLength,
graph.GetVertex(resultPath[i]).GetEdge(graph.GetVertex(resultPath[i + 1])).Weight<TW>());
return new ShortestPathResult<T, TW>(resultPath, resultLength);
}
}
/// <summary>
/// Search heuristic used by A* search algorithm.
/// </summary>
public interface IAStarHeuristic<T, TW> where TW : IComparable
{
/// <summary>
/// Return the distance to target for given sourcevertex as computed by the hueristic used for A* search.
/// </summary>
TW HueristicDistanceToTarget(T sourceVertex, T targetVertex);
}
//Node for our Fibonacci heap
internal class AStarWrap<T, TW> : IComparable where TW : IComparable
{
private readonly T destinationVertex;
private readonly IAStarHeuristic<T, TW> heuristic;
internal AStarWrap(IAStarHeuristic<T, TW> heuristic, T destinationVertex)
{
this.heuristic = heuristic;
this.destinationVertex = destinationVertex;
}
internal T Vertex { get; set; }
internal TW Distance { get; set; }
//compare distance to target using the heuristic provided
public int CompareTo(object obj)
{
if (this == obj) return 0;
var result1 = heuristic.HueristicDistanceToTarget(Vertex, destinationVertex);
var result2 = heuristic.HueristicDistanceToTarget((obj as AStarWrap<T, TW>).Vertex, destinationVertex);
return result1.CompareTo(result2);
}
}