# FindShortestPath with VertexWeight

I have a weighted graph on which I want to perform some FindShortestPath calculations. However, the problem is that my weights are defined for vertices with VertexWeight, but FindShortestPath can only use edge weights defined with EdgeWeight. For example, if I have a simple grid graph

g = GridGraph[{2, 2}, VertexWeight -> {1, 2, 1, 1}, VertexLabels -> "VertexWeight"]


and I compute the shortest path between the lower left and the upper right corner

FindShortestPath[g, 1, 4]

{1, 2, 4}


it would give me the path over the upper left vertex which is not what I want, because this vertex has a bigger weight than the vertex in the opposite corner. How can I calculate the shortest path with regard to vertex weights?

• Maybe you can construct the line graph of g to get a edge-weighted graph for FindShortestPath to work. Nov 27 '14 at 19:29
• @Silvia Can you explain how to do it with Mathematica? You can post it as an answer.
– shrx
Nov 27 '14 at 19:30
• Define your edge weights as the sums of the corresponding pairs of vertex weights. Nov 27 '14 at 20:18
• @shrx: Assuming your start and end vertices are known in advance, you can fix Daniel's proposal by subtracting half the initial vertex weight from all edges emanating from it, and subtract half the end vertex weight from all edges emanating from it. That way, the first and last vertex weights aren't counted. Nov 27 '14 at 21:07
• I do not see the issue. If you define edge weights by thatn summing, then every initial segment contains the initial vertex weight and so that part is a wash. Likewise for the terminating vertex weight and final segment. Another way to view it as this: every path will have weight equal to twice the weights of the intermediate vertices, plus the (constant) sum of the start and end weights. Nov 28 '14 at 4:30

Starting from the graph indicated.

vertexWeight={1, 2, 1, 1};
g = GridGraph[{2, 2}, VertexWeight -> vertextWeight,
VertexLabels -> "VertexWeight"]
{l1, l2} = Dimensions[m];
Table[m[[i, j]] (vertexWeight[[i]] + vertexWeight[[j]]), {i, l1}, {j,
l2}] /. Rule[0, \[Infinity]], VertexLabels -> "Name",
EdgeLabels -> "EdgeWeight", GraphLayout -> "SpringEmbedding"]
sp=FindShortestPath[h, 1, 4]
(*{1, 3, 4}*)
HighlightGraph[h, PathGraph[FindShortestPath[h, 1, 4]],
GraphHighlightStyle -> "Dashed"] • Thank you, with the illustration I can see now that @DanielLichtblau's suggestion will work even in those cases that looked problematic to me. I forgot to account that the vertex' weight would influence all edges connected to it.
– shrx
Nov 28 '14 at 14:12
• In line 2 there is the misspelling 'VertextWeight'. Dec 2 '14 at 0:36