4

How to determine the shortest path between 2 nodes, given the shortest distance matrix between the nodes of a graph?

For example, I have 4 nodes and the shortest distance matrix(m).

0 4 5 8
4 0 6 3
5 6 0 2
8 3 2 0

m(i,j) is the distance of the path between node i and node j, it need not be edge between node i and node j.

Could someone guide on how this can be done? Thank you in advance.

aries
  • 849
  • 3
  • 11
  • 24
  • i guess the question would have been worded `how to determine the shortest path....`. – guido Nov 02 '15 at 00:16
  • Ya right, corrected it. – aries Nov 02 '15 at 00:39
  • 1
    I'm not sure if the matrix you've given is correct. If shortest b/w 2/4 is 3 and b/w 4/3 is 2, then how can shortest dist b/w 2/3 be 6. Using the path 2-4-3, cost would be 5. Correct me if I'm wrong. – vish4071 Nov 02 '15 at 12:53

3 Answers3

4

Note: The actual links in the original network are all present in this distance matrix unless there is a shorter path between those two nodes via another node. And if there is a shorter path then this longer distance link can be ignored for the purpose of solving this problem.

So ...

I would start with the shortest distance. This must represent an actual path between two nodes. Create a graph with just those two nodes and the one link between them.

Now take the next shortest distance, between nodes X and Y.

  • Is there a path in the existing network between X and Y that is equal in distance to it? If so the link is not needed (it may represent a real link, or it may not, either way you don't need it).
  • Is it < the shortest path in the existing network between X and Y, good add it to the network, there must be a real-link here that you haven't seen yet.
  • Is it > the shortest path in the existing network between X and Y - error - it wasn't the shortest distance between these two nodes so the original distance matrix was wrong.

Keep going until you've used all of the distances.

You now have one possible network that is a sub-network of the original and it contains the links necessary to calculate every shortest path between any pair of nodes. Now you can calculate the shortest paths using a standard shortest-path algorithm.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • Ok, I will try to implement and post it if I get it right. Thanks. – aries Nov 02 '15 at 13:15
  • The third condition (Is it > the shortest path in the existing network ) will always be true isn't it? Cause the next shortest distance will always be greater than the initial one. May be I am getting it wrong, could you please clarify this? – aries Nov 02 '15 at 17:38
  • That should always be false otherwise the distance matrix you began with was wrong: If the distance matrix says the shortest path between two nodes is X but you have found a shorter path Y already, then whoever made the original matrix made a mistake. I'll clarify. – Ian Mercer Nov 02 '15 at 18:49
  • 1
    It works fine :) Thank you. I also found another solution. The idea is that you get a Minimum spanning tree using Kruskal's algorithm and then use DFS to get the path between the two points. – aries Nov 03 '15 at 22:55
0

Since the edge weights (which are the distances in this case) are positive, you can use Dijkstra's algorithm: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm.

An implementation in C: http://www.ccodechamp.com/c-program-to-find-shortest-path-using-dijkstras-algorithm/.

  • 2
    Suppose network is `A`--2-->`B`--2-->`C`. A to C is 4 in the distance matrix but is not connected. Dijkstra's algorithm might think that's a path when it's not since it could take either step when going to distance 4. The OP has a shortest distance matrix not an edge matrix, it contains links that may not exist in the real-world because they are via other nodes. – Ian Mercer Nov 02 '15 at 01:36
0

You can build the minimum distance matrix (another matrix that contains all minimum distance until the cell [i,j]) then return the last cell. Building this only takes O(n). n is the number of the items in your matrix.

Here is C# implementation of building that minimum distance matrix.

public static int[,] mindi_loop(int[,] original_mat)
{
    int[,] mindi_mat = new int[original_mat.GetLength(0), original_mat.GetLength(1)];
    for (int i = 0; i < original_mat.GetLength(0); i++)
    {
        for (int j = 0; j < original_mat.GetLength(1); j++)
        {
            if (i > 0 && j > 0)
            {
                mindi_mat[i, j] = Math.Min(mindi_mat[i - 1, j], mindi_mat[i, j - 1]) + original_mat[i, j];         
            }
            else if (i > 0 && j < 1)
            {
                mindi_mat[i, j] = mindi_mat[i - 1, j] + original_mat[i, j];
            }
            else if (j > 0 && i < 1)
            {
                mindi_mat[i, j] = mindi_mat[i, j - 1] + original_mat[i, j];
            }
            else if (i==0 && j == 0)
            {
                mindi_mat[i, j] = original_mat[i, j];
            }
        }
    }
    return mindi_mat;
}   
aerin
  • 20,607
  • 28
  • 102
  • 140