我需要找到图的 2 个顶点之间的最短路径。我有一个矩阵,其中包含所有权重。我该怎么做?目前,我有以下代码:
private int[] Dijkstra(int start, int end)
{
bool[] done = new bool[8];
int[] parent = new int[8];
for (int i = 0; i < parent.Length; i++)
parent[i] = -1;
int[] distances = new int[8];
for (int i = 0; i < distances.Length; i++)
distances[i] = int.MaxValue;
distances[start] = 0;
int current = start;
while (!done[current])
{
done[current] = true;
for (int i = 0; i < 8; i++)
{
if (graph[current, i] != int.MaxValue)
{
int dist = graph[current, i] + distances[current];
if (dist < distances[i])
{
distances[i] = dist;
parent[i] = current;
}
}
}
int min = int.MaxValue;
for (int i = 0; i < 8; i++)
{
if (distances[i] < min&&!done[i])
{
current = i;
min = distances[i];
}
}
}
return parent;
}
它可以工作,但是,但是我不知道如何让它找到例如 1 和 3 之间的最短路线,并返回 1=>4=>2=>3 之类的路线。
提前致谢。