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.
Related
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.
I am trying to do c++ program.I am trying to do problem in which i have numbers of points. Now i need to find the path that goes through all the points. This is not actually TSP because as per my knowledge in TSP it is possible to travel from all points to every other points. But in my case the path network between the points is fixed and i just need to find the suitable path that goes through all the points provided that all points may not have connection to every other point..so what algorithm am i supposed to follow.
It seems you are looking for a way to traverse a graph? If so have you tried Breadth first search http://en.wikipedia.org/wiki/Breadth-first_search or Depth first search http://en.wikipedia.org/wiki/Depth-first_search to traverse your graph.
You want to find a Hamiltonian path for a graph.
In the mathematical field of graph theory, a Hamiltonian path (or
traceable path) is a path in an undirected graph that visits each
vertex exactly once. A Hamiltonian cycle (or Hamiltonian circuit) is a
Hamiltonian path that is a cycle. Determining whether such paths and
cycles exist in graphs is the Hamiltonian path problem, which is
NP-complete.
Some techniques that exist :
There are n! different sequences of vertices that might be Hamiltonian
paths in a given n-vertex graph (and are, in a complete graph), so a
brute force search algorithm that tests all possible sequences would
be very slow. There are several faster approaches. A search procedure
by Frank Rubin divides the edges of the graph into three classes:
those that must be in the path, those that cannot be in the path, and
undecided. As the search proceeds, a set of decision rules classifies
the undecided edges, and determines whether to halt or continue the
search. The algorithm divides the graph into components that can be
solved separately. Also, a dynamic programming algorithm of Bellman,
Held, and Karp can be used to solve the problem in time O(n2 2n). In
this method, one determines, for each set S of vertices and each
vertex v in S, whether there is a path that covers exactly the
vertices in S and ends at v. For each choice of S and v, a path exists
for (S,v) if and only if v has a neighbor w such that a path exists
for (S − v,w), which can be looked up from already-computed
information in the dynamic program.
Andreas Björklund provided an alternative approach using the
inclusion–exclusion principle to reduce the problem of counting the
number of Hamiltonian cycles to a simpler counting problem, of
counting cycle covers, which can be solved by computing certain matrix
determinants. Using this method, he showed how to solve the
Hamiltonian cycle problem in arbitrary n-vertex graphs by a Monte
Carlo algorithm in time O(1.657n); for bipartite graphs this algorithm
can be further improved to time O(1.414n).
For graphs of maximum degree three, a careful backtracking search can
find a Hamiltonian cycle (if one exists) in time O(1.251n).
So I've been thinking, without resorting to another algorithm, what modifications could you make to a graph to allow Dijkstra's algorithm to work on it, and still get the correct answer at the end of the day? If it's even possible at all?
I first thought of adding a constant equal to the most negative weight to all weights, but I found that that will mess up everthing and change the original single source path.
Then, I thought of traversing through the graph, putting all weights that are less than zero into an array or somwthing of the sort and then multiplying it by -1. I think his would work (disregarding running time constraints) but maybe I'm looking at the wrong way.
EDIT:
Another idea. How about permanently setting all negative weights to infinity. that way ensuring that they are ignored?
So I just want to hear some opinions on this; what do you guys think?
Seems you looking for something similar to Johnson's algorithm:
First, a new node q is added to the graph, connected by zero-weight edges to each of the other nodes.
Second, the Bellman–Ford algorithm is used, starting from the new vertex q, to find for each vertex v the minimum weight h(v) of a path
from q to v. If this step detects a negative cycle, the algorithm is
terminated.
Next the edges of the original graph are reweighted using the values computed by the Bellman–Ford algorithm: an edge from u to v,
having length w(u,v), is given the new length w(u,v) + h(u) − h(v).
Finally, q is removed, and Dijkstra's algorithm is used to find the shortest paths from each node s to every other vertex in the
reweighted graph.
By any algorithm, you should check for negative cycles, and if there isn't negative cycle, find the shortest path.
In your case you need to run Dijkstra's algorithm one time. Also note that in Johnson's algorithm semi Bellman–Ford algorithm runs just for new added node. (not all vertices).
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.
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)