弗洛伊德无向图的最短路径

时间:2019-01-14 08:51:41

标签: java floyd-warshall

我实现了一个程序,该程序可以通过基于用户输入的Floyd / Dijkstra算法来计算任何图形的最短路径。

两种算法都可以很好地用于有向图。 输出应该显示 1)从起始顶点开始的实际路径 2)要行驶的最短距离

但是当涉及到无向图时,我遇到了问题。

我的图不是由邻接矩阵表示的,而是具有Graph&Edge类,如下所示

class Graph
{
    private final int noOfVertices;
    private final ArrayList<Edge> edges = new ArrayList<Edge>();
    private boolean undirected = false;

    Graph(int vertexCount)
    {
        noOfVertices = vertexCount;
        edges.ensureCapacity(2 * (vertexCount - 1));
    }

    public int getWeight(int src,int dst)
    {
     int weight=90000;

     for(Edge edge:edges)
     {
      if(edge.src==src && edge.dst==dst)
      {
       weight=edge.weight;
       break;
      }
     }

     return weight;
    }

    public int getEdgeCount(){return edges.size();}
    public int getVertexCount(){return noOfVertices;}

    public static class Edge
    {
        public int src;
        public int dst;
        public int weight;

        Edge(int v1, int v2, int w)
        {
            src = v1;
            dst = v2;
            weight = w;
        }
    }
}

要实现无向图,这是下面使用的代码

void newEdge(int src,int dst,int weight)
{
    edges.add(new Edge(src,dst,weight));
    if(undirected){ edges.add(new Edge(dst,src,weight));}
}

现在Dijkstra的算法在这种设置下工作正常,但是当我使用Floyd的算法时,我开始得到错误的路径但正确的距离

这是我的弗洛伊德算法

public static Integer[] Floyd(Graph graph, int startVertex, int endVertex)
{
    ArrayList<Integer> pathInfo = new ArrayList<Integer>();
    int dist[][] = new int[graph.getVertexCount()][graph.getVertexCount()];
    int path[][] = new int[graph.getVertexCount()][graph.getVertexCount()];

    int V = graph.getVertexCount();
    for (int i = 0; i < V; i++)
    {
        for (int j = 0; j < V; j++)
        {
            if (i == j)
            {
                dist[i][j] = 0;
            }
            else
            {
                dist[i][j] = graph.getWeight(i, j);
            }/*Initialize with edge weight's between vertices i,j.If edge does not exist graph.getWeight() return's 90000 i.e simply an value somewhat close to infinite because when I use Integer.MAX_VALUE I get negative distance's when doing dist[i][k]+dist[k][j] so i avoid it*/
            path[i][j] = j;
        }
    }

    /*actual Floyd's algorithm*/
    for (int k = 0; k < V; k++)
    {
        for (int i = 0; i < V; i++)
        {
            for (int j = 0; j < V; j++)
            {
                if (dist[i][j] > dist[i][k] + dist[k][j])
                {
                    dist[i][j] = dist[i][k] + dist[k][j];
                    path[i][j] = k; /*if path exist's record the intermediate vertex*/
                }
            }
        }
    }

    int currentVertex=endVertex;  /*Start from last vertex*/
    int nextVertex=path[startVertex][endVertex];/*Find path*/

    pathInfo.add(currentVertex);/*Add Current Vertex*/
    while(currentVertex!=nextVertex)/*Backtrack until the vertex we are currently at and the next vertex we need to go to from there are the same which mean's we have reached our target*/
    {
      currentVertex=nextVertex;
      pathInfo.add(0,currentVertex);
      nextVertex=path[startVertex][nextVertex];
    }
    pathInfo.add(0,startVertex);/*Finally add the vertex we ended at*/

    pathInfo.add(dist[startVertex][endVertex]);/*Come's out correct in both cases*/

    return pathInfo.toArray(new Integer[]{});/*Convert info to array*/
}

这是下面给出的我的无向图。 点线表示沿两个方向延伸的边缘。

0--1--2--3

每个边缘的权重为2

现在,当我用起始顶点= 0和终止顶点= 3调用Floyd算法时 我得到正确的输出路径。

0,1,2,3

但是当我再次调用Floyd算法时,其起点为Vertex = 3,终点为Vertex = 0 输出路径是

3,2,0

缺少顶点1。

但是使用Dijkstra的算法,我在两种情况下都能得到正确的结果

这是上面计算出的路径矩阵。

 0   1   1   2   
 0   1   2   2   
 1   1   2   3   
 2   2   2   3    

两种情况下得出的距离都是正确的,但仅当Floyd算法颠倒顶点顺序时,路径是错误的。

大多数视频创意都通过此链接合并 https://www.bing.com/videos/search?q=floyd%27s+algorithm&&view=detail&mid=E17F409B3AB0B2307233E17F409B3AB0B2307233&&FORM=VRDGAR

关于我哪里出问题了吗?

0 个答案:

没有答案