算法 – 图 – 具有顶点权重的最短路径

这是一个消费税:

In certain graph problems, vertices have can have weights instead of
or in addi- tion to the weights of edges. Let Cv be the cost of vertex
v, and C(x,y) the cost of the edge (x, y). This problem is concerned
with finding the cheapest path between vertices a and b in a graph G.
The cost of a path is the sum of the costs of the edges and vertices
encountered on the path.

(a) Suppose that each edge in the graph has a weight of zero (while
non-edges have a cost of ∞).Assume that Cv =1 for all vertices 1≤v≤n
(i.e.,all vertices have the same cost). Give an efficient algorithm to
find the cheapest path from a to b and its time complexity.

(b) Now suppose that the vertex costs are not constant (but are all
positive) and the edge costs remain as above. Give an efficient
algorithm to find the cheapest path from a to b and its time
complexity.

(c) Now suppose that both the edge and vertex costs are not constant
(but are all positive). Give an efficient algorithm to find the
cheapest path from a to b and its time complexity.

这是我的答案:

(a)使用正常的BFS

(b)使用dijkstra的算法,但用顶点重量代替重量

(C)

还可以使用dijkstra的算法

如果只考虑边缘权重,那么对于dijkstra算法的关键部分,我们有:

if (distance[y] > distance[v]+weight) {
    distance[y] = distance[v]+weight; // weight is between v and y
}

现在通过考虑顶点重量,我们有:

if (distance[y] > distance[v] + weight + vertexWeight[y]) {
   distance[y] > distance[v] + weight + vertexWeight[y]; // weight is between v and y
}

我对吗?

我猜我对(c)的答案太简单了,是吗?

你在正确的轨道上,解决方案非常简单.

在B,C中,将问题减少到正常的dijkstra,它假定顶点上没有权重.
为此,您将需要定义w’:E> R,一个边缘的新的权重函数.

w'(u,v) = w(u,v) + vertex_weight(v)

在(b)w(u,v)= 0(或const)中,解决方案也是稳健的,以适应(c)!

它背后的想法是使用边缘花费你边缘的重量,以及达到目标顶点的成本.来源的费用已经支付了,所以你忽略它1.

减少一个问题,而不是改变算法通常要简单得多使用,证明和分析!

(1)在这个解决方案中,你“错过”源的权重,所以从s到t的最短路径将是:dijkstra(s,t,w’)vertex_weight(s)_ [其中dijkstra(s,t,w ‘)是从s到t的距离,使用w’

翻译自:https://stackoverflow.com/questions/10453053/graph-shortest-path-with-vertex-weight

转载注明原文:算法 – 图 – 具有顶点权重的最短路径