Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graph - Shortest path with Vertex Weight

Tags:

Here is an excise:

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.


Here is my answer:

(a) use normal BFS;

(b) Use dijkstra’s algorithm, but replace weight with vertex weight;

(c)

Also use dijkstra’s algorithm

If only considering about edge weight, then for the key part of dijkstra's algorithm, we have:

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

Now, by considering about vertex weight, we have:

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

Am I right?

I guess my answer to (c) is too simple, is it?

like image 501
Jackson Tale Avatar asked May 04 '12 16:05

Jackson Tale


People also ask

How do you find the shortest path in a weighted graph?

One common way to find the shortest path in a weighted graph is using Dijkstra's Algorithm. Dijkstra's algorithm finds the shortest path between two vertices in a graph. It can also be used to generate a Shortest Path Tree - which will be the shortest path to all vertices in the graph (from a given source vertex).

Can DFS find shortest path in weighted graph?

And so, the only possible way for BFS (or DFS) to find the shortest path in a weighted graph is to search the entire graph and keep recording the minimum distance from source to the destination vertex.

What is a vertex weighted graph?

An edge-weighted graph is a graph in which each edge has been assigned a weight. Similarly, a vertex-weighted graph is a graph in which each vertex has been assigned a weight. In such graphs, the quantity represented by a weight depends on the application.

Can BFS be used to find shortest path in weighted graph?

So if all edges are of same weight, we can use BFS to find the shortest path. For this problem, we can modify the graph and split all edges of weight 2 into two edges of weight 1 each. We can use BFS to find the shortest path in the modified graph.


2 Answers

You are on the right track, and the solution is very simple.

In both B,C, Reduce the problem to normal dijkstra, which assumes no weights on the vertices.
For this, you will need to define w':E->R, a new weight function for edges.

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

in (b) w(u,v) = 0 (or const), and the solution is robust to fit (c) as well!

The idea behind it is using an edge cost you the weight of the edge, and the cost of reaching the target vertice. The cost of the source was already paid, so you disregard it1.

Reducing a problem, instead of changing an algorithm is usually much simpler to use, prove and analyze!.


(1) In this solution you "miss" the weight of the source, so the shortest path from s to t will be: dijkstra(s,t,w') + vertex_weight(s)_ [where dijkstra(s,t,w') is the distance from s to t using out w'

like image 133
amit Avatar answered Sep 24 '22 00:09

amit


The vertex weight can be removed by slicing every vertex a in two vertices a1 and a2 with an edge from a1 to a2 with the weight of a.

I think you are right for the adaptation of dijkstra’s algorithm.

like image 22
Thomash Avatar answered Sep 24 '22 00:09

Thomash