Search code examples
cmatlablinear-algebraadjacency-matrixdistance-matrix

Convert adjacency matrix to a distance or hop matrix


Is it possible to convert an adjacency matrix of ones and zeros as defined here into a distance matrix as defined here where each link would be of unit length 1?


Solution

  • An adjacency matrix of ones and zeros is simply a representation of an undirected graph. To get the distances between any two vertices of an unweighted graph, you can use breadth first search.

    Assuming you have an n by n matrix:

    for each vertex i:
        initialize an nxn matrix M
        run breadth-first search starting at i
        copy distances into row i of M
        return M