Given a graph, I want to find the shortest distance between each pair of vertices:
g = Graph[{4 <-> 5, 2 <-> 3, 4 <-> 6, 2 <-> 4, 3 <-> 4, 1 <-> 2},
VertexLabels -> "Name"];
This is what the graph looks like:
It is obvious that the distance of shortest path from 1 to 6 is 3. However, when I compute the shortest distance in this way:
m = GraphDistanceMatrix[g];
m[[1, 6]]
The output is
2
It tells that the distance from 1 to 6 is 2. This is incorrect. I noticed from its documentation that "the vertices are assumed to be in the order given by VertexList[g]
".
So, I called VertexList[g]
:
VertexList[g]
And this is the vertex order:
{4, 5, 2, 3, 6, 1}
This actually gives troubles to my application. Is there a way to get the shortest distance according to the numerical order rather than vertex order?