Finding all edges that appear on at least one s-t path - algorithm

I am looking for an algorithm to solve the following problem:
Input: Directed Graph G, nodes s and t
Output: Set of all edges that are part of a simple st-path
A path is simple if no node is visited twice.
Does somebody have an idea how to do this?
The problem is easy on DAGs as all paths are simple. However, the input graph is not a DAG. I believe that by using a strongly connected component algorithm, the problem can be reduced to the case where G is strongly connected. Unfortunately, I do not know how to proceed further. I'm even unsure whether the problem is polynomially solvable.

I've managed to figure out a polynomial-time algorithm for the undirected case. For the directed case, I don't have a full answer, but for reasons I'll outline below I have a weak hunch that the problem is NP-hard.
The main insight I'm using is the following. Suppose you have a pair of nodes s and t and you want to determine whether the edge {u, v} appears on some simple path from s to v. This is equivalent to asking whether there are paths Psu and Pvt where Psu runs from s to u, Pvt runs from v to t, and Psu and Pvt have no nodes in common.
This is a special case of a problem called the 2-distinct path problem. In that problem, you're given pairs of nodes (w, x) and (y, z) and are asked whether there are node-disjoint paths from w to x and from y to z. This problem has been studied since the 1970s, and we know that
there exists a polynomial-time algorithm for this problem in the undirected case, but that
the directed version of the problem is NP-hard.
This means that, in the undirected case, we can solve your original problem as follows. Given nodes s and t, for each edge {u, v}, see whether there's a solution to the 2-distinct path problem from s to u and from v to t. If so, record that there is a simple path passing through {u, v}. At the end, report all edges you found this way. Using Tholey's algorithm as a black box solver for the 2-distinct path problem gives a total runtime of O(mn + m2 α(m, n)), where α(m, n) is the inverse Ackermann function.
This approach could also be used in the directed case, except that the 2-distinct path problem is NP-hard for directed graphs, even if the one of the terminal nodes has a direct edge to the other start node. That by itself doesn't prove that your original problem is NP-hard because solving your problem doesn't immediately seem to give a way to determine whether there are two disjoint paths from s to t. However, the fact that it's this difficult to determine whether a single edge is on some s-t path is weak heuristic evidence that this is a hard problem.

Related

Find a positive simple s-t Path on a Graph is NP-Complete?

I'm trying to find something on the literature to help me to solve this problem:
Given a graph with non negative edge weights, find a simple s-t positive path of any size, i.e., a path that goes from s to t with length greater than 0. I was trying to use dijkstra algorithm to find a positive shortest path by avoiding relax the edges that have cost zero, but this is wrong. I don't want to believe that this problem is NP-Complete =/. Sometimes it seems to be NP-Complete, because there may be a case where the only positive path is the longest path. But this is such a specific case. I think that on this kind of instance the longest path problem is polynomially solvable.
Can someone help me identifying this problem or showing that it is NP-Complete ?
Observations: The only requirement is to be some positive path (not necessarily the lowest or longest). In case of multiple positive paths it can be anyone. In case of non existence of such path, the algorithm should flag that the graph has no positive path.
Dijkstra's algorithm produces an answer in polynomial time (so in P rather than NP) and provides a shortest path between any 2 points on the graph.
Please refer to Dijkstra's Algorithm Wiki for further details.
You don't really need any more proof.
I'm not quite sure how "relax the edges that have cost 0" has any impact on this question at all.
The problem is NP-complete even with only one single edge having value 1, and all the others having value 0. Reduce from Two Directed Paths:
Two Directed Paths
Input: A directed graph G and two pairs of vertices (s1, t1) and (s2, t2)
Output: Two vertex-disjoint paths, one from s1 to t1 and from from s2 to t2.
Create a new instance with all edges having weight 0, and create an edge from t1 to s2 with weight 1.

Hamiltonian Cycle algorithm

I was looking for some hamiltonian cycle algorithms, but I can't find any implementations, not even a single pseudo-code ! I don't even need to output the cycle, just check if the graph has one. The input is a graph with V vertices and E edges. Also, I would like to have an algorithm to check if a graph has a hamiltonian path. I don't need to output the path, just check if it has one. Both should be in polynomial time.
The problem is one of the NP-Complete problems.
A brute force algorithm is just creating all permutations and checking if one of them is feasible solution.
Checking the feasibility:
let the current permutation be v1,v2,...,vn: if for each i there is an edge v_i -> v_(i+1) in the graph, and also v_n->v1 - then the solution is feasible.
An alternative is creating a graph G'=(V,E',w) where the new edges E' = VxV (all edges) and the weight function is:
w(u,v) = 1 if there is an edge (u,v) in the original graph
infinity otherwise.
Now you got yourself a Traveling-salesman problem, and can solve it with dynamic programming in O(n^2*2^n)
Unless P = NP, Hamiltonicity can not be decided for general graphs in polynomial time.
An online HCP heuristic exists at http://fhcp.edu.au/slhweb/ where you can upload a graph and test it, but if you want your own function you will either need to write it yourself, or splice in someone else's function. Andrew Chalaturnyk wrote a very good algorithm.

Reduction algorithm from the Hamiltonian cycle

I believe that the Hamiltonian cycle problem can be summed up as the following:
Given an undirected graph G = (V, E), a
Hamiltonian circuit is a tour in G passing through
every vertex of G once and only once.
Now, what I would like to do is reduce my problem to this. My problem is:
Given a weighted undirected graph G, integer k, and vertices u, v
both in G, is there a simple path in G from u to v
with total weight of AT LEAST k?
So knowing that the Hamiltonian cycle problem is NP-complete, by reducing this problem to the Hamiltonian, this problem is also proved NP-complete. My issue is the function reducing it to Hamiltonian.
The big issue is that the Hamiltonian problem does not deal with edge weights, so I must convert my graph to one that doesn't have any weights.
On top of that, this problem has a designated start and finish (u and v), whereas the Hamiltonian finds a cycle, so any start is the same as the finish.
For (1), I am thinking along the lines of passing a graph with all simple paths of total weight LESS THAN k taken out.
For (2), I am thinking that this is not really an issue, because if there is a Hamiltonian cycle, then the simple path from u to v can be sliced out of it.
So, my real questions are:
Is my solution going to give me the right answer?
If yes, then how can I take out the edges that will produce simple paths of total weight less than k WITHOUT affecting the possibility that one of those edges may be required for the actual solution? Because if an edge e is taken out because it produces a simple path of weight < k for a subset of E, it can still be used in a simple path with a different combination of edges to produce a path of weight >= k.
Thanks!
Your reduction is in the wrong direction. To show that your problem is NP-complete, you need to reduce Hamilton Circuit to it, and that is very easy. I.e. show that every Hamilton Circuit problem can be expressed in terms of your problem variant.
More of a hint than an answer:
A unweighted graph can be interpreted as a weighted graph where each edge has weight 1. What would the cost of a Hamiltonian cycle be in a graph like that?
The part about the mismatch between cycles and paths is correct and is a problem you need to solve. Basically you need to prove that the Hamiltonian Path problem is also NP complete (a relatively straightfoward reduction to the Cycle problem)
As for the other part, you are doing things in the wrong direction. You need to show that your more complicated problem can solve the Hamiltonian Path problem (and is thus NP-hard). To do this you just basically have to use the special case of unit-cost edges.

Optimal Graph path containing n nodes

Is there an established algorithm for finding a path from point A to point B in a directed-weighted graph which visits exactly N nodes, but not necessarily any nodes in particular?
This problem is known to be NP-hard via a reduction from Hamiltonian path. In particular, you can solve Hamiltonian path with a polynomial-time reduction to this problem as follows: for each possible pair of nodes (s, t) in a graph with n nodes, ask if there is a path from s to t that passes through exactly n nodes. This makes only polynomial calls to your solver, so any polynomial-time solution to your problem would result in a polynomial-time solution to the Hamiltonian path problem.
So in short, you shouldn't expect a polynomial-time algorithm for this problem unless P = NP.
I'm going to assume you're trying to find the shortest/longest weight with N nodes. This probably isn't optimal, but from your original graph, you could generate a state graph with 'N*(#Nodes)' nodes representing consisting of the original node and number of steps taken and run it thorough some shortest path algorithm like http://en.wikipedia.org/wiki/Dijkstra's_algorithm.
i.e.,
A->B->C
\___/
becomes
(A,0)->(B,1)->(C,2)
\>(C,1)
Your target node would then be node (B,N) - B with N steps. This approach would allow for loops in the original graph if it's not a DAG ( (X,0)->(Y,1)->(X,2) )
I am not sure if I undersand it correctly, can you do a depth first search (up to N-1 layers away from the source)?
If you can visit your destination in that layer. you can get a path down there.

What is the dynamic programming algorithm for finding a Hamiltonian cycle in a graph?

What is dynamic programming algorithm for finding a Hamiltonian cycle in an undirected graph?
I have seen somewhere that there exists an algorithm with O(n.2^n) time complexity.
There is indeed an O(n2n) dynamic-programming algorithm for finding Hamiltonian cycles. The idea, which is a general one that can reduce many O(n!) backtracking approaches to O(n22n) or O(n2n) (at the cost of using more memory), is to consider subproblems that are sets with specified "endpoints".
Here, since you want a cycle, you can start at any vertex. So fix one, call it x. The subproblems would be: “For a given set S and a vertex v in S, is there a path starting at x and going through all the vertices of S, ending at v?” Call this, say, poss[S][v].
As with most dynamic programming problems, once you define the subproblems the rest is obvious: Loop over all the 2n sets S of vertices in any "increasing" order, and for each v in each such S, you can compute poss[S][v] as:
poss[S][v] = (there exists some u in S such that poss[S−{v}][u] is True and an edge u->v exists)
Finally, there is a Hamiltonian cycle iff there is a vertex v such that an edge v->x exists and poss[S][v] is True, where S is the set of all vertices (other than x, depending on how you defined it).
If you want the actual Hamiltonian cycle instead of just deciding whether one exists or not, make poss[S][v] store the actual u that made it possible instead of just True or False; that way you can trace back a path at the end.
I can't pluck out that particular algorithm, but there is more about Hamiltonian Cycles on The Hamiltonian Page than you will likely ever need. :)
This page intends to be a
comprehensive listing of papers,
source code, preprints, technical
reports, etc, available on the
Internet about the Hamiltonian Cycle
and Hamiltonian Path Problems as well
as some associated problems.
(original URL, currently 404), (Internet Archive)

Resources