I have a directed graph and I want to find a path that visits every node exactly one time. I want to do this with a good complexity. Is this possible? And if yes, how?
You are searching for a Hamiltonian path, which is a simple open path that contains each node exactly once.
Finding a Hamiltonian path in a given graph is NP-complete. In fact, determining whether a given (directed or undirected) graph contains a Hamiltonian path is already NP-complete (proven via reduction from e.g. the vertex cover problem).
If you still want to code it, here is an implementation on github. If you want a fast solution, maybe a heuristic is sufficient (for instance inspired by DNA molecules, or a solution that works fast on a subset of graphs. For instance, if you have a DAG, you can do a topological sort and then check if successive vertices are connected. If so, the topological sort gives a Hamiltonian path.
Related
I want an algorithm to be able to find an optimal path between two vertices on a graph (with positive int weights).The thing is my graph is relatively big (up to 100 vertices). I have considered the dijkstra algorithm but as I searched the net most implementions use the adjacency matrix which in my case will be 100x100.
If you could recommend me a certain source to read and learn from , or even better provide me with a c++ implementaion it will be great.
PS: The algorithm needs to output the required route and not just the shortest distance between two points.
Thank you for your time.
Have you looked into A*?
Here's a good article to start reading: http://www.redblobgames.com/pathfinding/a-star/introduction.html
I'm testing various TSP models/algorithms. Right now I'm using a full adjacency matrix, filled with random values from 1 to 100, which represents a complete directed graph.
I'm searching for a more rigorous approach that would allow me to try different kinds of random graphs, like Erdos-Renyi, small world networks and scale free networks.
I know I may have to switch to adjacency lists for the new graphs.
My approach would be generating a random graph and then ensuring there is the Hamiltonian path necessary for the problem to be a valid TSP instance. Is it possible, or is it cheaper to just try and solve the unsolvable instance (assuming all methods will terminate on such instance)?
BTW I was thinking of using the Boost Graph Library, but I'm not familiar with it, and maybe there's something more appropriate. Suggestions for alternatives are welcome, but should not be considered the main scope of this question.
I don't need a TSP solver, I need something to aid in the generation of acceptable problems.
Thanks.
You can try a monotone gray code, a.k.a a hilbert curve. It can help find a hamiltonian path:http://en.m.wikipedia.org/wiki/Gray_code.
I'm searching for a more rigorous approach that would allow me to try
different kinds of random graphs,
Check Mathematica. It has a built-in predicate to test whether a given graph has hamiltonian path or not. It also, has a predicate to generate random Hamiltonian Graphs.
In addition, If you have not try it yet, TSBLIB contains (hard and easy) instances that you may find them useful.
This is driving me mad...
I have a directed graph with weighted edges between the nodes.
I need to find the shortest distance from node A and back to node A.
I've tried Djikstras algorithm and Floyd-Warshall but neither quite seem to do the trick. I'm not strong on the maths and most resources I found quickly get overly complex without explaining what I really need to do. Once I understand the steps I can code it, just struggling to find an understandable approach...
Can anyone help?
You want to find the shortest non-trivial cycle containing a particular point. You could do this by taking successive A^n, where A is the adjacency matrix, and finding the first nonzero value on the matrix diagonal corresponding to your point. This is illustrative, but a breadth-first search will work faster. I don't think there is a better way than the BF search, performance wise, though I can't prove that.
Here is some code that implements the search.
http://algs4.cs.princeton.edu/42directed/BreadthFirstDirectedPaths.java.html
You'll have to modify the termination condition, I think.
Alternatively, you could use Dijkstra's and "lift" the point in question into two different start/end points, but this isn't going to be faster for computing just one path, and makes a mess of the graph if you want many paths (point pairs).
I need to generate all paths that are lesser than or equal to a specified length in a graph (the graph is undirected and it's possible to have cycles). I tried using BFS while keeping track of the distance already traversed but I'm not sure how I would ensure that every path is different.
Note: I know this probably has a very high computational complexity, but I'm not worrying about that for now.
Using BFS is kind of the right way to do so.
But you additionally have to keep track of already found nodes.
There is a simple algorithm from Dijkstra solving this for you
the depth-first algorithm implemented in the boost library visits each vertex just one time.
Is there any work around to deactivate this option. I want that the vertexes can be visited whenever there is a branch in any vertex.
any suggestion...
EDIT: The graph is acyclic.
If you want to enumerate all paths in an acyclic graph, then I don't think you can easily modify depth-first search to do that. There are algorithms specifically designed for this purpose, in particular: Rubin, F.; , "Enumerating all simple paths in a graph," Circuits and Systems, IEEE Transactions on , vol.25, no.8, pp. 641- 642, Aug 1978.
If you know the Floyd-Warshall algorithm, you can easily modify it to compute a list of paths in each element of the matrix, instead of min distance, which will do the job. The above article uses some bit operations to make this run a bit faster.
want that the vertexes can be visited
whenever there is a branch in any
vertex.
What do you propose that an iterator do when it reaches a branch at a vertex?
Depth first search is just one answer this question. Here are some others.
But you have to choose something. It's not a matter of turning off DFS.
I think that is impossible by design. Because if your graph contains cycles (and you have them there, when you say, that the vertex can be visited more than once), the algorithm will end up in endless loop.