C# Dijkstra算法:如果有两个图是不连通的,它应该工作吗?

C# Dijkstra算法:如果有两个图是不连通的,它应该工作吗?,c#,algorithm,graph,graph-algorithm,dijkstra,C#,Algorithm,Graph,Graph Algorithm,Dijkstra,我目前已经实现了Dijkstra的算法,但是当我用这样一个图测试我的算法时,问题来了: 试着从C到B,我知道为什么它不起作用。但是我想知道如果有这样一个图,正常的实现是否会工作 internal static Stack<string> Dijkstra(string sourcePoint, string targetPoint, Graph graph) { List<string> verticesStringList = graph.

我目前已经实现了Dijkstra的算法,但是当我用这样一个图测试我的算法时,问题来了:

试着从C到B,我知道为什么它不起作用。但是我想知道如果有这样一个图,正常的实现是否会工作

  internal static Stack<string> Dijkstra(string sourcePoint, string targetPoint, Graph graph)
    {
        List<string> verticesStringList = graph.GetAllVertices();
        Dictionary<string, Vertex> verticesDictionary = new Dictionary<string, Vertex>();
        InitializeVerticesDictionary(sourcePoint, verticesStringList, verticesDictionary);

        while (verticesDictionary.Values.ToList().Any(x => x.IsVisited == false))
        {
            KeyValuePair<string, Vertex> keyValuePair = verticesDictionary.Where(x => x.Value.IsVisited == false).ToList().Min();
            string vertexKey = keyValuePair.Key;
            Vertex currentVertex = keyValuePair.Value;
            List<string> neighbourVertices = graph.GetNeighbourVerticesSorted(keyValuePair.Key);
            foreach (string neighbourVertexString in neighbourVertices)
            {
                Vertex neighbourVertex = verticesDictionary[neighbourVertexString];
                int newDistanceFromStartVertex = currentVertex.ShortestDistanceFromTarget + graph.GetEdgeWeight(keyValuePair.Key, neighbourVertexString);
                if (newDistanceFromStartVertex < neighbourVertex.ShortestDistanceFromTarget)
                {
                    verticesDictionary[neighbourVertexString].ShortestDistanceFromTarget = newDistanceFromStartVertex;
                    verticesDictionary[neighbourVertexString].PreviousVertex = keyValuePair.Key;
                }
            }
            verticesDictionary[vertexKey].IsVisited = true;
        }

        return FormShortestPath(targetPoint, verticesDictionary);

    }

    private static Stack<string> FormShortestPath(string targetPoint, Dictionary<string, Vertex> verticesDictionary)
    {
        Stack<string> traverseStack = new Stack<string>();
        KeyValuePair<string, Vertex> vertex = verticesDictionary.Where(x => x.Key == targetPoint).FirstOrDefault();
        while (vertex.Value.PreviousVertex != null)
        {
            traverseStack.Push(vertex.Value.PreviousVertex + " Goes To " + vertex.Key); //the end edge
            vertex = verticesDictionary.Where(x => x.Key == vertex.Value.PreviousVertex).FirstOrDefault();
        }
        return traverseStack;
    }



private static void InitializeVerticesDictionary(string sourcePoint, List<string> verticesStringList, Dictionary<string, Vertex> verticesDictionary)
    {
        foreach (string vertexString in verticesStringList)
        {
            Vertex vertex = new Vertex
            {
                ShortestDistanceFromTarget = int.MaxValue
            };

            if (vertexString == sourcePoint)
            {
                vertex.ShortestDistanceFromTarget = 0;
            }

            verticesDictionary.Add(vertexString, vertex);
        }
    }
内部静态堆栈Dijkstra(字符串源点、字符串目标点、图形)
{
List verticesStringList=graph.GetAllVertices();
Dictionary verticesDictionary=新字典();
初始化verticesDictionary(sourcePoint、VerticesTringlist、verticesDictionary);
while(verticesDictionary.Values.ToList().Any(x=>x.isvisted==false))
{
KeyValuePair KeyValuePair=verticesDictionary.Where(x=>x.Value.IsVisited==false).ToList().Min();
字符串vertexKey=keyValuePair.Key;
顶点currentVertex=keyValuePair.Value;
List neighbourtexts=graph.GetNeighbourVerticesSorted(keyValuePair.Key);
foreach(字符串邻接顶点中的字符串邻接顶点)
{
顶点邻接顶点=verticesDictionary[邻接顶点字符串];
int newDistanceFromStartVertex=currentVertex.ShortestDistanceFromTarget+graph.GetEdgeWight(keyValuePair.Key,NeigurVertexString);
if(newDistanceFromStartVertexx.Key==targetPoint).FirstOrDefault();
while(vertex.Value.PreviousVertex!=null)
{
traverseStack.Push(vertex.Value.PreviousVertex+“转到”+顶点.Key);//结束边
vertex=verticesDictionary.Where(x=>x.Key==vertex.Value.PreviousVertex.FirstOrDefault();
}
返回遍历堆栈;
}
私有静态void InitializeVerticesDictionary(字符串源点、列表VerticesTringList、字典verticesDictionary)
{
foreach(verticesStringList中的字符串vertexString)
{
顶点=新顶点
{
最短距离fromTarget=int.MaxValue
};
if(vertexString==sourcePoint)
{
vertex.ShortestDistanceFromTarget=0;
}
添加(vertexString,vertex);
}
}

更新:我将我的条件更改为verticesDictionary.Values.ToList().Any(x=>x.IsVisited==false&&x.ShortestDistanceFromTarget!=int.MaxValue),现在我没有收到我在评论中提到的溢出。

IsVisited这里有点误导,因为您实际上可以访问从源节点无法访问的节点。我会将其重命名为
isProcessed
。要检查是否可以从源节点到达另一个节点,需要检查其距离是否为
int.maxVal


为避免溢出,当currentVertex.ShortestDistanceFromTarget为
int.maxVal
时,不要迭代邻居,因为它已经是源节点中无法访问的节点。

什么意思不起作用?如果在处理目标节点时使用while(true)和breaking,这就是问题所在。将while true更改为while(有一些东西需要处理)可以修复it@juvian我实际上在使用你提到的第二种方法。节点的实际处理不是问题所在。当我回溯已处理的节点时,问题就出现了。您应该只在实际到达该节点时回溯,因为如果您没有回溯,则没有路径`t@juvian目前,伪代码中的代码是while(集合包含未访问的节点){//logic currentNode.isvisted=true}。。。循环结束后,我开始从终点到起点走自己的路。那么,我应该如何知道是否没有到它的路径呢?检查目标节点是否已访问为true?