Basically, I need to have a shortest path in a graph that covers all of the vertices and returns to the source. Repetition of any vertex is ok so long as it is the shortest path.
My algorithm starts from the source. I run a dijkstra algorithm to find the shortest path. Then I choose the smallest weighted unreached vertex and run the dijkstra again as the chosen vertex as the source and keep doing it until all vertices are done. Then from the last vertex use dijkstra again to find the shortest path back to the original source.
I tried, but it seems to fail and I cannot find the reason.
Google the travelling salesperson problem,that finds the shortest cycle starting from the source covering all the vertices.The code for travelling salesperson is also not difficult to implement.
Since any vertex can be visited more that once you are basically looking for the shortest closed walk in a graph. The problem with your approach is that dikstra will only find the shortest path from the chosen node back to the source . This will produce a star type solution, where you have multiple paths coming out of the source vertex. Which could be longer then a walk.
This is an NP problem, as Vincent stated, but you could try implementing it using a random walk algorithm. Or look at the Traveling Salesman Walk Problem (http://dspace.mit.edu/handle/1721.1/33668)
Related
Pathfinding algorithms such as A* 100% guarantees the shortest path. But can we can manually analyze if a given path in the graph is the shortest path?
Suppose that we have this weighted graph
Weighted Graph
Let's say that our starting node is NODE 1 and we want to go to NODE 2. The path that we took is node 1 -> node 2. Obviously this is the shortest path. How can we manually prove or determine whether this path is the shortest route without any computers?
One can go with Dijkstra Algorithm to find the shortest path.
I have a weighted directed graph. I'm defining a "spanning walk" here as a walk through the graph that visits each vertex at least once, with no limitations on what edges need to be traversed. I'm trying to find the shortest of such walks, if one exists.
Here's an image of an example graph, whose shortest spanning walk is c->d->b->a, with a length of 6
I tried looking into Hamiltonian paths and the Traveling Salesman Problem and such, but as I'm allowed to visit a vertex multiple times in my problem, I don't know if I can make much use of it. I'm almost completely convinced the problem is NP-hard, but wondering if there's anything to make a search not take centuries for larger graphs.
I have a huge graph of network nodes. I would like to know what is the most efficient algo or technique to obtain the shortest path between any two nodes in the graph?
I have studied an algorithm of "All pair shortest path" but the time complexity is O(V^3) which is worse in my case as the number of nodes is large. How to reduce this complexity?
How can I find the shortest cycle in a simple (not directed ) bipartite graph using Breadth First Search ?
In a bipartite graph, the shortest possible circle is at least 4 edges long. Since you are using breadth first search you will find the optimum quite fast as long as you increase your travel distance accordingly. All possible 4 edges long path, all possible 5 edges long paths, and so on. The moment you find a path that is a circle, you are done, it is the shortest possible or at least tied for that prize.
Keep all paths explored as to only increase these by 1 more edge each cycle of the search. And use BFS to make sure you have explored all paths.
I am given a directed graph with a weight function and a vertex s.
My goal is to find for any other vertex v, the shortest path from s to v that goes through exactly one negative edge. The time complexity of the algorithm should be O(|E| + |V|*log|V|), so guess I need to use Dijkstra's algorithm somehow.
I am guessing that I need to translate my given graph somehow to a new graph with non-negative weights that a shortest path from s to v in this graph will be equivalent to the required shortest path in the given graph.. or maybe I need to modify Dijkstra's algorithm somehow??
I tried to think about it a little, don't have any ideas right now... :(