-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DijkstraAlgorithm.cs
123 lines (95 loc) · 3.84 KB
/
DijkstraAlgorithm.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
using System;
using System.Collections.Generic;
using System.Linq;
using DataStructures.Graph;
namespace Algorithms.Graph.Dijkstra;
public static class DijkstraAlgorithm
{
/// <summary>
/// Implementation of the Dijkstra shortest path algorithm for cyclic graphs.
/// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm.
/// </summary>
/// <param name="graph">Graph instance.</param>
/// <param name="startVertex">Starting vertex instance.</param>
/// <typeparam name="T">Generic Parameter.</typeparam>
/// <returns>List of distances from current vertex to all other vertices.</returns>
/// <exception cref="InvalidOperationException">Exception thrown in case when graph is null or start
/// vertex does not belong to graph instance.</exception>
public static DistanceModel<T>[] GenerateShortestPath<T>(DirectedWeightedGraph<T> graph, Vertex<T> startVertex)
{
ValidateGraphAndStartVertex(graph, startVertex);
var visitedVertices = new List<Vertex<T>>();
var distanceArray = InitializeDistanceArray(graph, startVertex);
var currentVertex = startVertex;
var currentPath = 0d;
while (true)
{
visitedVertices.Add(currentVertex);
var neighborVertices = graph
.GetNeighbors(currentVertex)
.Where(x => x != null && !visitedVertices.Contains(x))
.ToList();
foreach (var vertex in neighborVertices)
{
var adjacentDistance = graph.AdjacentDistance(currentVertex, vertex!);
var distance = distanceArray[vertex!.Index];
if (distance.Distance <= currentPath + adjacentDistance)
{
continue;
}
distance.Distance = currentPath + adjacentDistance;
distance.PreviousVertex = currentVertex;
}
var minimalAdjacentVertex = GetMinimalUnvisitedAdjacentVertex(graph, currentVertex, neighborVertices);
if (neighborVertices.Count == 0 || minimalAdjacentVertex is null)
{
break;
}
currentPath += graph.AdjacentDistance(currentVertex, minimalAdjacentVertex);
currentVertex = minimalAdjacentVertex;
}
return distanceArray;
}
private static DistanceModel<T>[] InitializeDistanceArray<T>(
IDirectedWeightedGraph<T> graph,
Vertex<T> startVertex)
{
var distArray = new DistanceModel<T>[graph.Count];
distArray[startVertex.Index] = new DistanceModel<T>(startVertex, startVertex, 0);
foreach (var vertex in graph.Vertices.Where(x => x != null && !x.Equals(startVertex)))
{
distArray[vertex!.Index] = new DistanceModel<T>(vertex, null, double.MaxValue);
}
return distArray;
}
private static void ValidateGraphAndStartVertex<T>(DirectedWeightedGraph<T> graph, Vertex<T> startVertex)
{
if (graph is null)
{
throw new ArgumentNullException(nameof(graph));
}
if (startVertex.Graph != null && !startVertex.Graph.Equals(graph))
{
throw new ArgumentNullException(nameof(graph));
}
}
private static Vertex<T>? GetMinimalUnvisitedAdjacentVertex<T>(
IDirectedWeightedGraph<T> graph,
Vertex<T> startVertex,
IEnumerable<Vertex<T>?> adjacentVertices)
{
var minDistance = double.MaxValue;
Vertex<T>? minVertex = default;
foreach (var vertex in adjacentVertices)
{
var currentDistance = graph.AdjacentDistance(startVertex, vertex!);
if (minDistance <= currentDistance)
{
continue;
}
minDistance = currentDistance;
minVertex = vertex;
}
return minVertex;
}
}