简体   繁体   English

如何计算具有加权顶点的图的最短路径?

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

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): Dijkstra 和 Floyd-Warshall 等经典算法通常使用加权边,我没有看到如何将它们应用于我的案例(加权顶点):

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.这里我们有单向和双向加权边,但我仍然不确定哪种算法会处理这个以找到最短路径。

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. Dijkstra 的算法很容易适应顶点成本,而无需使用任何此类转换。 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 .当你穿越边缘,而不是new_vertex_cost = old_vertex_cost + edge_weight,你只是做new_vertex_cost = old_vertex_cost + new_vertex_weight。

You can reduce the problem to the classical shortest path problem and use Dijkstra, Bellman-Ford, or Floyd-Warshal as it suits the purpose.您可以将问题简化为经典的最短路径问题,并根据需要使用 Dijkstra、Bellman-Ford 或 Floyd-Warshal。 For the sake of simplicity, in what follows, I assume all weights are non-negative.为简单起见,在下文中,我假设所有权重都是非负的。 I consider such an assumption reasonable since the question mentions using Dijkstra's algorithm to solve the problem.我认为这样的假设是合理的,因为问题提到使用 Dijkstra 算法来解决问题。 In the end, this assumption can be removed with care.最后,可以小心地删除这个假设。

Consider most general form of the problem: Assume G = <V, E> is a directed weighted graph with weights on both edges and vertices.考虑问题的最一般形式:假设G = <V, E>是一个有向加权图,在边和顶点上都有权重。 Construct a graph H = <V', E'> , with weights only on edges, as follows: For any node v in G , create two nodes v_in and v_out in H;构造一个图H = <V', E'> ,权重仅在边上,如下所示: 对于G任何节点v ,在 H 中创建两个节点v_inv_out add an edge (v_in -> v_out) with weight equal to the weight of node v in G .添加一条边(v_in -> v_out) ,其权重等于G中节点v的权重。 Also, for any edge (u -> w) in G , add an edge (u_out -> w_in) in H (new edge carries the same weight as the original edge).此外,对于G任何边(u -> w)G中添加一条边(u_out -> w_in) (新边与原始边具有相同的权重)。

To summarize, for any vertex in the original graph add two vertices in H , one dedicated to ingoing edges, and the other dedicated to the outgoing edges (also, connect the new correlated nodes in H based on the weight of their corresponding vertex in G ).总而言之,对于原始图中的任何顶点,在H添加两个顶点,一个专用于传入边,另一个专用于传出边(此外,根据G相应顶点的权重连接H的新相关节点)。

Now, you have a directed weighted graph H with no weight on vertices, but only on edges.现在,您有一个有向加权图H ,顶点没有权重,只有边权重。 It is easy to prove that the shortest path between (s_in, t_out) in H is the same as the shortest path between (s,t) in the original graph G .很容易证明H (s_in, t_out)之间的最短路径与原始图G (s,t)之间的最短路径相同。

The proof is based on the fact that any such path goes through the edge (v_in, v_out) in H if and only if the corresponding path in G goes through node v .证明基于这样一个事实,即当且仅当G的相应路径通过节点v ,任何此类路径都经过H的边(v_in, v_out)

As far as the analysis goes, we have |V'| = 2|V|就分析而言,我们有|V'| = 2|V| |V'| = 2|V| , and |E'| = |E| + |V| , 和|E'| = |E| + |V| |E'| = |E| + |V| . . So the reduction does not affect the asymptotic behavior of the employed algorithm for finding shortest paths.因此,减少不会影响用于寻找最短路径的算法的渐近行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算无向加权图中一组顶点之间的最短路径 - Calculate the shortest path between a set of vertices in an undirected weighted graph 如何计算具有给定参数的Graph中两个顶点之间的最短路径? - How to calculate the shortest path between two vertices in Graph with given parameters? 如何在具有加权节点和顶点的图形中找到最佳路径 - How to find the best path in graph with weighted nodes and vertices 如何计算最短路径? - How to calculate the shortest path? 如何用Java中的文本文件为Dijkstra最短路径算法生成加权图的图? - How to generate a map of weighted graph for Dijkstra’s Shortest Path Algorithm from a text file in Java? 以图形方式显示图形中两个顶点之间的最短路径 - Graphically display the shortest path between two vertices in a graph 非加权图中邻接表中的最短路径 - Shortest path in an adjacency list in non-weighted graph 图中两个顶点之间的最短方式 - Shortest way between 2 vertices in graph 具有最大顶点数的最短路径 - Shortest path with a maximum number of vertices 对于通过实加权无向图的单对最短路径,最简单的算法/解决方案是什么? - What's the simplest algorithm/solution for a single pair shortest path through a real-weighted undirected graph?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM