Find the minimum weight cycle that contains a specific edge - algorithm

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

Related

Lowest cost edge where edge weights are between 0 and 1

I am given a graph G = (V, E). Each edge has probability of failure which is equal to its edge weight. Each edge weight is between 0 and 1. I need to find the path of the lowest probability of failure. This means a path where the total weight is 1 - ((1 - w(e1))(1 - w(e2))...).
To me, this doesn't seem like a shortest path problem because the shortest path may have the highest weights. I thought that a modified Dijkstra's algorithm would work where instead of checking if d[v] > d[u] + w(u, v) I would check if d[v] < d[u] + w(u, v) but that can't possibly be it. I am stuck. Can anyone offer advice?

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.

Shortest path algorithm with both vertex and edge costs

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.

graph - Shortest path with Vertex Weight

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?
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'
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.

Resources