1. Given an undirected weighted graph and two node s, t. How one can print any simple cycle that passes through both s and t or say no such cycle exist?
2. If more than one such cycle exist then How one can print cycle with minimum weight among them??
Is it possible to solve these problem in linear time?? Please suggest me optimal solution for these problem...It will be much helpful if one can provide code also.
This Greedy Algorithm will not work in this graph..
(1, 2) weight = 1
(2, 4) weight = 100
(1, 3) weight = 100
(3, 4) weight = 1
(2, 3) weight = 1
where (x, y) represent edge;
and s = 1, t = 4,
Edit This answer is incorrect because I missed the fact that the cycle must contain both s and t.
this finds a simple cycle but not necessary one that contains $$$s$$$ or $$$t$$$
Ah yes. I missed that fact. Sorry.
2 The best that I can think of is a quadratic time algorithm. Run MST algorithm. Then try adding an arbitrary edge to the MST. There will be a cycle for sure. Now, you can use DFS to check the weight of this cycle. Do this for every edge.
Edit This answer is incorrect because I missed the fact that the cycle must contain both s and t.
This does not work. A minimum cycle can need more than one edge which is not in the MST. example graph:
I missed the point that the cycle must contain s and t so my answer is incorrect for your counterexample.
On a side note, I believe it should be correct if we remove that restriction (and assume non-negative weights). Because there is no way we can add more than 1 edge to the MST to create a minimum cycle unless there are negative weights.
that should be right, yes. I believe with negative weights this becomes NP hard and it can be reduced to a hamilton cicle problem (just set every weight to $$$-1$$$, than there is a hamilton circle iff the weight of the minimal circle is $$$-n$$$).
For problem one we can use Menger's theorem which tells us that there are two vertex disjoint path (those form a simply cycle) between $$$s$$$ and $$$t$$$ iff there is no single vertex which seperates them. This is equivalent to they are in the same biconnected component (vertex connectivity) which can be found in $$$\mathcal{O}(n)$$$. Then those two paths can be found with a bfs.
problem two with negative weights is NP hard... best Solution for problem two with positive weights i can think of right now is to use a min cost max flow based approch...
"This is equivalent to they are in the same biconnected component (vertex connectivity) which can be found in O(n)."
May you suggest how i can find a biconnected component consist of vertex s and t.
I think you can just compute biconnected components of the graph (there are well-known algorithms that you can find online). Then check if s and t are in the same biconnected component.
Can't you just find two vertex-disjoint paths with two dfs ?
The first dfs starts from s and ends to t and finds a path.
The second dfs starts from s and ends to t but uses only vertices that first path did not use.
The union of these paths is the cycle you need. Isn't that right and simpler ?
Here Again, This will be counterexample
(1, 2) weight = 1
(2, 4) weight = 100
(1, 3) weight = 100
(3, 4) weight = 1
(2, 3) weight = 1
where (x, y) represent edge;
and s = 1, t = 4,
what if first dfs find path 1-2-3-4??
Opps, I am sorry. Max-flow is used for vertex-disjoint paths. It's not linear though as you want. My bad !
Maybe we could use max-flow algorithm in linear time though, because we just need to iterate the graph twice only. So what if you build a new graph with the "back" edges as well and run the max flow algorithm twice ?
https://en.wikipedia.org/wiki/Suurballe%27s_algorithm ?
I was not aware of that algorithm. I did not read it very carefully but I think it does the same job. Also we should replace dijkstra with simple bfs here.
How can we count the total number of simple cycles in an undirected graph?