How to calculate the shortest path for a graph with weighted vertices?

George P. :

I’m trying to figure out, how to calculate the shortest path for a graph with weighted vertices. Classical algorithms like Dijkstra and Floyd–Warshall normally work with weighted edges and I'm not seeing a way how to apply them to my case (weighted vertices):

graph with weighted vertices

One of the ideas I had was to convert the graph to the more classical view with weighted edges. This is what I received:

graph with weighted edges

Here we have mono and bi-directional weighted edges, but I'm still not sure which algorithm would handle this in order to find the shortest path.

Matt Timmermans :

You can certainly do this by transforming the graph. The simplest way is to transform each edge into a vertex, and connect the new vertexes together with edges that have the same cost as the vertex that used to join them.

But you don't really need to bother with any of that...

Dijkstra's algorithm is very easy to adapt to vertex costs without using any such transformation. When you traverse an edge, instead of new_vertex_cost = old_vertex_cost + edge_weight, you just do new_vertex_cost = old_vertex_cost + new_vertex_weight.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88783&siteId=1
Recommended