-
Notifications
You must be signed in to change notification settings - Fork 292
/
Dijikstra.cs
188 lines (155 loc) · 6.52 KB
/
Dijikstra.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
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;
using Advanced.Algorithms.DataStructures.Graph;
namespace Advanced.Algorithms.Graph;
/// <summary>
/// A dijikstra algorithm implementation using Fibonacci Heap.
/// </summary>
public class DijikstraShortestPath<T, TW> where TW : IComparable
{
private readonly IShortestPathOperators<TW> @operator;
public DijikstraShortestPath(IShortestPathOperators<TW> @operator)
{
this.@operator = @operator;
}
/// <summary>
/// Get shortest distance to target.
/// </summary>
public ShortestPathResult<T, TW> FindShortestPath(IGraph<T> graph, T source, T destination)
{
//regular argument checks
if (graph?.GetVertex(source) == null || graph.GetVertex(destination) == null) throw new ArgumentException();
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.");
//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<MinHeapWrap<T, TW>>();
//keep references of heap Node for decrement key operation
var heapMapping = new Dictionary<T, MinHeapWrap<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 MinHeapWrap<T, TW>
{
Distance = @operator.DefaultValue,
Vertex = source
};
minHeap.Insert(current);
heapMapping.Add(current.Vertex, 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))
{
var wrap = new MinHeapWrap<T, TW> { Distance = newDistance, Vertex = neighbour.TargetVertexKey };
minHeap.Insert(wrap);
heapMapping.Add(neighbour.TargetVertexKey, wrap);
}
else
{
//decrement distance to neighbour in heap
var decremented = new MinHeapWrap<T, TW>
{ Distance = newDistance, Vertex = neighbour.TargetVertexKey };
minHeap.UpdateKey(heapMapping[neighbour.TargetVertexKey], decremented);
heapMapping[neighbour.TargetVertexKey] = decremented;
}
//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>
/// Generic operators interface required by shorted path algorithms.
/// </summary>
public interface IShortestPathOperators<TW> where TW : IComparable
{
TW DefaultValue { get; }
TW MaxValue { get; }
TW Sum(TW a, TW b);
}
/// <summary>
/// Shortest path result object.
/// </summary>
public class ShortestPathResult<T, TW> where TW : IComparable
{
public ShortestPathResult(List<T> path, TW length)
{
Length = length;
Path = path;
}
public TW Length { get; internal set; }
public List<T> Path { get; }
}
/// <summary>
/// For fibornacci heap node.
/// </summary>
internal class MinHeapWrap<T, TW> : IComparable where TW : IComparable
{
internal T Vertex { get; set; }
internal TW Distance { get; set; }
public int CompareTo(object obj)
{
return Distance.CompareTo((obj as MinHeapWrap<T, TW>).Distance);
}
}