How to determine the path between two nodes, given the shortest matrix of distances between nodes?

How to determine the shortest path between two nodes, given the shortest matrix of distances between nodes of the 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, this should not be the border between node i and node j.

Can someone explain how to do this? Thank you in advance.

+4
source share
3 answers

. , node. , .

...

. . .

X Y.

  • X Y, ? , ( , , , ).
  • & ​​lt; X Y, , , .
  • > X Y - , .

, .

, , , . , .

+3

( , [i, j]), . O (n). n - .

# .

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;
}   
0

Source: https://habr.com/ru/post/1614075/


All Articles