1

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]

hivert
  • 10,579
  • 3
  • 31
  • 56
ddwivedy
  • 23
  • 1
  • 4
  • 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 Feb 28 '17 at 06:04
  • 1
    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 Feb 28 '17 at 08:11
  • The problem is bi-directionality (or being undirected). The fact that you can go back and forth between a pair of nodes creates an infinite loop in the longest distance problem. In your example, the longest path from 1 to 3 would be 1-2-1-2-1-2 ... 2-3, which is not what you want. The negative weight approach works if you have a purely directed graph with no loops at all. – FNia Aug 03 '22 at 18:12

2 Answers2

2

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.

Ari
  • 7,251
  • 11
  • 40
  • 70
0

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.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    The graph contains no negative cycles. The point that i was trying to make is can't we use negative weights in case of positive one(given that all the edge weights are positive) and then find out the shortest path, which would give us the longest path reversing the sign. If it is not possible can someone explain why? The example and the test case stated above should work. – ddwivedy Feb 28 '17 at 17:18