-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cs
55 lines (52 loc) · 2.02 KB
/
Dijkstra.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
using System;
using System.Collections;
using System.Collections.Generic;
using static MolaDirectedGraph;
using System.Linq;
namespace Mola
{
public class Dijkstra
{
public float[] DistanceToAll(MolaDirectedGraph graph, int start)
{
float[] distances = new float[graph.NodesCount()];
int[] candidates = new int[graph.NodesCount()];
int[] nextCandidates = new int[graph.NodesCount()];
candidates[0] = start;
distances = Enumerable.Repeat((float)10000, graph.NodesCount()).ToArray();
distances[start] = 0;
int amountOfCandidates = 1;
while (amountOfCandidates > 0)
{
int nextAmountOfCandidates = 0;
//Debug.Log("amountOfCandidates: " + amountOfCandidates);
for (int i = 0; i < amountOfCandidates; i++)
{
int candidate = candidates[i];
float currentWeight = distances[candidate];
IEnumerable<DirectedEdge> cNbs = graph.GetDirectedEdges(candidate);
foreach (DirectedEdge edge in cNbs)
{
int nb = edge.n2;
if (nb >= 0)
{
float nextCost = edge.weight + currentWeight;
if (nextCost < distances[nb])
{
distances[nb] = nextCost;
nextCandidates[nextAmountOfCandidates] = nb;
nextAmountOfCandidates++;
}
}
}
}
for (int i = 0; i < nextAmountOfCandidates; i++)
{
candidates[i] = nextCandidates[i];
}
amountOfCandidates = nextAmountOfCandidates;
}
return distances;
}
}
}