1
votes

I want to find the largest distance between any two vertices of a weighted undirected graph using Floyd-warshall algorithm. For this i have made few changes:

  1. I add negative weights instead of positive.

  2. Then i find out the shortest path.

But it does not give me the correct output. Can someone point out the mistake i am making.

class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int testcases=sc.nextInt();
        for(int t=0;t<testcases;t++)
        {
            int nodes=sc.nextInt();
            int edges=sc.nextInt();
            int[][] dist_mat=new int[nodes][nodes];
            for(int i=0;i<nodes;i++)
            {
                for(int j=0;j<nodes;j++)
                {
                    if(i!=j)
                    {
                        dist_mat[i][j]=Integer.MAX_VALUE;
                    }
                }
            }
            for(int i=0;i<edges;i++)
            {
                int source=sc.nextInt();
                int dest=sc.nextInt();
                dist_mat[source-1][dest-1]=-sc.nextInt();
                dist_mat[dest-1][source-1]=dist_mat[source-1][dest-1];
            }

            for(int k=0;k<nodes;k++)
            {
                for(int i=0;i<nodes;i++)
                {
                    for(int j=0;j<nodes;j++)
                    {

                        if(i!=j && j!=k && i!=k && dist_mat[i][j]>dist_mat[i][k]+dist_mat[k][j])
                        {
                            if(dist_mat[i][k]<Integer.MAX_VALUE && dist_mat[k][j]<Integer.MAX_VALUE)
                                    dist_mat[i][j]=Integer.min(dist_mat[i][j],dist_mat[i][k]+dist_mat[k][j]);
                            if(dist_mat[j][k]<Integer.MAX_VALUE && dist_mat[k][i]<Integer.MAX_VALUE)
                                    dist_mat[j][i]=Integer.min(dist_mat[j][i],dist_mat[j][k]+dist_mat[k][i]);
                        }

                    }
                }
            }   
        }
    }

The same input is :-

1[number of test cases]

5 4 [number of nodes,number of edges]

1 2 4 [first node, second node, weight]

3 2 3 [first node, second node, weight]

2 5 2 [first node, second node, weight]

4 1 1 [first node, second node, weight]

2
Floyd-Warshall algorithm is an algorithm for finding shortest paths (not "longest distance") in a weighted graph. What are you trying to do here?avysk
I don't think you can adapt FW to compute largest distance. Indeed in case of a loop the largest distance may be infinite.hivert

2 Answers

2
votes

Floyd-Warshal should work. First notice that there is a confusion when people talk about longest distance problem and its NP-hardness.

From this link:

notice that there is a huge confusion when we talk about the longest path:

The longest path problem commonly means finding the longest simple path. The shortest path problem, however, focuses on finding the shortest (simple or non-simple) path.

If the original graph G does not have a positive cycle, then -G, the graph created from G by negating its edges, will not have negative edges, and you CAN use Floyd-Warshall to find the shortest path in -G, and hence the longest path in G. Therefore, Floyd-Warshall should work if your input graph does not have positive cycles. Also see here.

One possible issue with your code is that you initialize all distances to a MAX value: dist_mat[i][j]=Integer.MAX_VALUE, whereas I think in Floyd-Warshall you should initialize them to the edge weights of the graph.

0
votes

An algorithm which would be capable of finding a longest path between any two nodes could be used to decide the Hamiltonian path problem. However, the Hamiltonian path problem is NP-complete. The Floyd-Warshall algorithm yields a polynomial runtime bound, hence it is unlikey that a modification will result in an algorithm which determines the longest paths.