Shortest path algorithm with both vertex and edge costs - algorithm

This is a general algorithm question. I want to run some shortest path algorithm on an undirected graph where both edges and vertices have costs associated with them. Most of the shortest path finding algorithms do not take the vertex costs into account. Is there any method to compensate this problem?

Augment the graph by adding half the cost of the two vertices an edge connects to the cost of the edge (call that the augmented cost of the edge).
Then ignore the vertex costs and run an ordinary shortest path algorithm on the augmented graph.
For each path
v_0 -e_1-> v_1 -e_2-> v_2 -e_2-> ... -e_n-> v_n
the cost in the augmented graph is
(1/2*C(v_0) + C(e_1) + 1/2*C(v_1)) + (1/2*C(v_1) + C(e_2) + 1/2*C(v_2)) + ... + (1/2*C(v_(n-1)) + C(e_n) + 1/2*C(v_n))
= C(v_0) + C(e_1) + C(v_1) + C(e_2) + ... + C(e_n) + C(v_n) - 1/2*(C(v_0 + C(v_n))
so the cost of a path between two vertices a and b in the augmented graph is the cost of the same path in the original graph minus half the combined cost of the start and end vertices.
Thus a path is a shortest path in the original graph if and only it is a shortest path in the augmented graph.

Related

Finding paths of fixed cost in weighted undirected graph?

I have the following problem and I'm not quite sure how to solve it:
Given a graph G = (V;E) in which every edge e has a positive integer cost c_e and a starting vertex s\in V . Design an O(V + E) algorithm that marks all vertices reachable from s using a path (not necessarily a simple path) with the total cost of that path being multiples of 5.
How can I keep track of the total amount of cost of the path that I've already visited? I've been studying about BFS in undirected weighted graphs and made some attempts on using it here, but most of the BFS references focus on finding the shortest path (and not something like keep it multiple of 5).
What do you think about the next algorithm?
Let's consider new directed graph based on the source graph. For every vertex v from the source graph create 5 new vertexes v[0], v[1], ..., v[4] in the new graph corresponding to the modules from the division by 5. Then, if vertexes v and u were connected in the source graph by the edge with the weight w, add edge between v[i] and u[(i + w) % 5], u[j] and v[(j + w) % 5] in the new graph, where i = 0..4, j = 0..4. Then run BFS from the v[0], where v is the starting vertex in the source graph.
Consider vertexes with the index 0 like v[0]. Each of them corresponding to the path of the length multiple of 5 to the vertex v in the source graph. All of such vertexes marked after BFS as reachable from the starting vertex form the answer. Total complexity is linear.

Find the minimum weight cycle that contains a specific edge

given a graph:
- oriented,
- strongly connected and
- weighed (the weights are all positive)
I have to write a function that taken as input an edge (u, v), calculate the weight of the cycle of minimum weight that contains this edge.
I thought I'd do as follows but the procedure is inaccurate and incomplete:
- Starts a bfs visit starting from node u
- Keep in a variable the temporary weight
- When you arrive to v choose the minimum of the previous weights.
How do I keep track of previous weights? How to write the pseudocode?
I have no idea how to get started so someone help me?
Thanks
This can be reduced to Shortest Path Problem:
Find the shortest path from v to u, then the shortest cycle that contains (u,v), is v->...->u->v, where v->...->u is the shortest path from v to u as found by the shortest path algorithm.
It can be solved efficiently using Dijkstra's Algorithm, since there are no negative weights in the graph.
Correctness Proof:
Assume there is a cheaper cycle v1->v2->...vi->u->v->v_i+1->...->vn->v1. Let's say it weights x < d(v,u) + w(u,v)
Since it's a cycle, we can look at it as v->v_i+1->....->vn->v1->...->vi->u->v.
The weight of the cycle didn't change, and is still x. This means x=w(v,v_i+1) + ... + w(vn,v1) + ... + w(v_i-1,vi) + w(vi,u) + w(u,v)
But this gives us w(v,v_i+1) + ... + w(vn,v1) + ... + w(v_i-1,vi) + w(vi,u) + w(u,v) - w(u,v) < d(v,u), and we have found a shorter path from v to u, than d(v,u), which is contradicting correctness of Dijkstra's Algorithm.
QED

Number of lightest paths from single source vertex

Assume I have a directed, weighted graph with positive or negative weights, (with no zero or negative weighted loops).
The graph is Bellman-Ford analized, meaning each vertex holds the data of the lightest path to it from the source vertex, and its predecessor in the lightest path.
What is the most efficient way to store the number of different shortest paths from the source to each vertex?
I am willing to make it in linear time - O(V+E) if possible.
You can do it pretty efficiently if you have no negative edges as well.
Let the shortest path to node v be denoted as D(v)
sort vertices by distances - O(VlogV)
Denote P(v) - number of paths leading from the source to v.
Now, you can use DP to solve this relation (from first to last):
P(source) = 1
P(v) = sum { P(u) | (u,v) is an edge and D(u) + w(u,v) = D(v) }
Complexity of the algorithm is O(VlogV + E)
Correctness proof: by induction (guidelines):
Base clause for source, there is a single path (the empty path).
let us assume P(v) is correct for every v such that D(v) < D(u).
For every shortest path that ends with u, it must go through one of the vertices such that D(v) < D(u). Given a shortest path source->...->v->u, the path is counted in P(v). In addition, it is not counted for any other P(v'), so it is counted exactly once in sum { P(u) | (u,v) is an edge and D(u) + w(u,v) = D(v) }.
In addition, for any path which is not shortest path, from induction hypothesis, it is not counted for any v such that D(v)<D(u), so the path must be generated in the last step, but the restriction (u,v) is an edge and D(u) + w(u,v) = D(v) is preventing it, so we do not count any non-shortest path.
QED

Finding all shortest paths from source to all vertices in a digraph

We are given a directed graph G (possibly with cycles) with positive edge weights, and the minimum distance D[v] to every vertex v from a source s is also given (D is an array this way).
The problem is to find the array N[v] = number of paths of length D[v] from s to v,
in linear time.
Now this is a homework problem that I've been struggling with for quite long. I was working along the following thought : I'm trying to remove the cycles by suitably choosing an acyclic subgraph of G, and then try to find shortest paths from s to v in the subgraph.
But I cannot figure out explicitly what to do, so I'd appreciate any help, as in a qualitative idea on what to do.
You can use dynamic programming approach in here, and fill up the number of paths as you go, if D[u] + w(u,v) = D[v], something like:
N = [0,...,0]
N[s] = 1 //empty path
For each vertex v, in *ascending* order of `D[v]`:
for each edge (u,v) such that D[u] < D[v]:
if D[u] + w(u,v) = D[v]: //just found new shortest paths, using (u,v)!
N[v] += N[u]
Complexity is O(VlogV + E), assuming the graph is not sparsed, O(E) is dominanting.
Explanation:
If there is a shortest path v0->v1->...->v_(k-1)->v_k from v0 to v_k, then v0->...->v_(k-1) is a shortest path from v0 to v_k-1, thus - when iterating v_k - N[v_(k-1)] was already computed fully (remember, all edges have positive weights, and D[V_k-1] < D[v_k], and we are iterating by increasing value of D[v]).
Therefor, the path v0->...->v_(k-1) is counted in the number N[V_(k-1)] at this point.
Since v0->...->v_(k-1)-v_k is a shortest path - it means D[v_(k-1)] + w(v_k-1,v_k) = D[v_k] - thus the condition will hold, and we will add the count of this path to N[v_k].
Note that the proof for this algorithm will basically be induction that will follow the guidelines from this explanation more formally.

Finding the shortest path in a graph between 2 nodes that goes through a subset of nodes

I'm trying to find out an efficient way of finding the shortest path between 2 nodes in a graph with positive edge costs that goes trough a subset of nodes.
More formally:
Given a graph G = (U, V) where U is the set of all nodes in the graph and V is the set of all edges in the graph, a subset of U called U' and a cost function say:
f : UxU -> R+
f(x, y) = cost to travel from node x to node y if there exists an edge
between node x and node y or 0 otherwise,
I have to find the shortest path between a source node and a target node that goes trough all the nodes in U'.
The order in which I visit the nodes in U' doesn't matter and I am allowed to visit a node more than once.
My original idea was to make use of Roy-Floyd algorithm to generate the cost matrix.
Then, for each permutation of the nodes in U' I would be computing the cost between the source and the target like this: f(source_node, P1) + f(P1, P2) + ... + f(Pk, target) saving the configuration for the lowest cost and then reconstructing the path.
The complexity for this approach is O(n3 + k!) O(n3 + k*k!), where n is the number of nodes in the graph and k the number of nodes in the subset U', which is off limits since I'll have to deal with graphs with maximum n = 2000 nodes out of which maximum n - 2 nodes will be part of the U' subset.
This is a generalization of travelling salesman. If U' == U, you get exactly TSP.
You can use the O(n^2 * 2^n) TSP algorithm, where the exponential factor for full scale TSP (2^n) will reduce to k = |U'|, so you'd get O(n^2 * 2^k).
This has the DP solution to TSP.
http://www.lsi.upc.edu/~mjserna/docencia/algofib/P07/dynprog.pdf
Combine the source node s and target node t into a single node z, and define the node set U'' := U' union {z}, with the distances to and from z defined by f(z,x) := f(s,x) and f(x,z) := f(x,t) for all x in U \ {s, t}. Compute shortest paths between all nodes of U'' and let f'(x,y) be the shortest distances, or infinity when there is no appropriate path. Voila, you now have a travelling salesman problem on the complete directed graph with vertices U'' and edge weights f'.

Resources