forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DijkstraShortestPaths.cs
167 lines (137 loc) · 5.73 KB
/
DijkstraShortestPaths.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
using DataStructures.Graphs;
using DataStructures.Heaps;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Graphs
{
/// <summary>
/// Computes Dijkstra's Shortest-Paths for Directed Weighted Graphs from a single-source to all destinations.
/// </summary>
public class DijkstraShortestPaths<TGraph, TVertex>
where TGraph : IGraph<TVertex>, IWeightedGraph<TVertex>
where TVertex : IComparable<TVertex>
{
private const long Infinity = long.MaxValue;
private const int NilPredecessor = -1;
private long[] _distances;
private int[] _predecessors;
private Dictionary<TVertex, int> _nodesToIndices;
private Dictionary<int, TVertex> _indicesToNodes;
private MinPriorityQueue<TVertex, long> _minPriorityQueue;
private readonly TGraph _graph;
private readonly TVertex _source;
public DijkstraShortestPaths(TGraph graph, TVertex source)
{
if (graph == null)
throw new ArgumentNullException(nameof(graph));
if (source == null)
throw new ArgumentNullException(nameof(source));
if (!graph.HasVertex(source))
throw new ArgumentException("The source vertex doesn't belong to graph.");
if (graph.Edges.Any(edge => edge.Weight < 0))
throw new ArgumentException("Negative edge weight detected.");
_graph = graph;
_source = source;
_initialize();
_dijkstra();
}
/// <summary>
/// The Dijkstra's algorithm.
/// </summary>
private void _dijkstra()
{
while (!_minPriorityQueue.IsEmpty)
{
var currentVertex = _minPriorityQueue.DequeueMin();
var currentVertexIndex = _nodesToIndices[currentVertex];
var outgoingEdges = _graph.OutgoingEdges(currentVertex);
foreach (var outgoingEdge in outgoingEdges)
{
var adjacentIndex = _nodesToIndices[outgoingEdge.Destination];
var delta = _distances[currentVertexIndex] != Infinity ? _distances[currentVertexIndex] + outgoingEdge.Weight : Infinity;
if (delta < _distances[adjacentIndex])
{
_distances[adjacentIndex] = delta;
_predecessors[adjacentIndex] = currentVertexIndex;
if (_minPriorityQueue.Contains(outgoingEdge.Destination))
{
_minPriorityQueue.UpdatePriority(outgoingEdge.Destination, delta);
}
else
{
_minPriorityQueue.Enqueue(outgoingEdge.Destination, delta);
}
}
}
}
}
private void _initialize()
{
var verticesCount = _graph.VerticesCount;
_distances = new long[verticesCount];
_predecessors = new int[verticesCount];
_nodesToIndices = new Dictionary<TVertex, int>();
_indicesToNodes = new Dictionary<int, TVertex>();
_minPriorityQueue = new MinPriorityQueue<TVertex, long>((uint)verticesCount);
var vertices = _graph.Vertices.ToList();
for (int i = 0; i < verticesCount; i++)
{
if (_source.Equals(vertices[i]))
{
_distances[i] = 0;
_predecessors[i] = 0;
}
else
{
_distances[i] = Infinity;
_predecessors[i] = NilPredecessor;
}
_minPriorityQueue.Enqueue(vertices[i], _distances[i]);
_nodesToIndices.Add(vertices[i], i);
_indicesToNodes.Add(i, vertices[i]);
}
}
/// <summary>
/// Determines whether there is a path from the source vertex to this specified vertex.
/// </summary>
public bool HasPathTo(TVertex destination)
{
if (!_nodesToIndices.ContainsKey(destination))
throw new ArgumentException("Graph doesn't have the specified vertex.");
var index = _nodesToIndices[destination];
return _distances[index] != Infinity;
}
/// <summary>
/// Returns the distance between the source vertex and the specified vertex.
/// </summary>
public long DistanceTo(TVertex destination)
{
if (!_nodesToIndices.ContainsKey(destination))
throw new ArgumentException("Graph doesn't have the specified vertex.");
var index = _nodesToIndices[destination];
return _distances[index];
}
/// <summary>
/// Returns an enumerable collection of nodes that specify the shortest path from the source vertex to the destination vertex.
/// </summary>
public IEnumerable<TVertex> ShortestPathTo(TVertex destination)
{
if (!_nodesToIndices.ContainsKey(destination))
throw new ArgumentException("Graph doesn't have the specified vertex.");
if (!HasPathTo(destination))
{
return null;
}
var dstIndex = _nodesToIndices[destination];
var stack = new Stack<TVertex>();
int index;
for (index = dstIndex; _distances[index] != 0; index = _predecessors[index])
{
stack.Push(_indicesToNodes[index]);
}
stack.Push(_indicesToNodes[index]);
return stack;
}
}
}