I have been searching for a shortest path algorithm with some conditions for 2 days but I'm not finding it. The algorithm should take 3 parameters, a beginning point, an ending point, a graph and should return the minimum time used to go from beginning point to ending point. But Djikstra's algorithm can't be used I think because I want the algorithm to use waiting nodes. Could someone give me some advice ?
PS: Sorry for my English I'm French :/
Illustration to my problem
You can split each node with an internal delay into an input and output node, pictured blue and red, with a connection between them and use Dijkstra's algorithm:
Related
In my unweighed graph, I need from a source vertex to reach an imposed vertex and come back to source.
All vertex may be visited at most once.
(There may be cycles in that graph.)
I want the length of the shortest such path.
Looking for a "fast" algorithm:
I have already made several attempts in Python but not fast enough. ;-)
Latest for instance : generator of all paths from source to imposed point (in order of increasing length), every time I get a new path, compare it with all those already calculated and stop if disjoint.
Good result, but too slow / memory expensive.
Earlier (same issue) : consider sub states with information : position, already been to imposed point or not, set of already visited vertexes... good result too but too slow / memory expensive.
DP solution welcome.
Could not find something with google. Point me to if you know where there is something.
Thanks.
--- example ---
e...
#!!#
#!.#
...s
This is a maze; you enter at 'e' and need to reach the sword 's' and back to the entrance 'e'.
'#' are unpassable. '!' are traps, when you walk on these, you trigger them and cannot pass again.
I have turned this into a graph with 5 vertices in this case : e,s, (2,2), (3,2) and (2,3)
--- case where Suurballe algo yields two not disjoined paths ----
.....
##!!#
#!!!#
#!!##
.....
For unweighted graph shortest path BFS in both directions might be the most efficient solution.
You can use BFS to determine how far every node is from your source.
after you find the path, you can remove every node beside the source and target so you will not repeat a node on the path more than once, and then use BFS from the target back to your source.
The total complexity is linear O(V+E)
more on that:
https://www.geeksforgeeks.org/shortest-path-unweighted-graph/
If I understood your question correctly, then I think what you're looking for are node disjoint paths. The classic algorithm to uncover them was proposed by Suurballe. Another algorithm based on network flow is implemented in networkx: networkx.algorithms.connectivity.disjoint_paths.node_disjoint_paths.
Hopefully these pointers solve the problem in your graphs with sufficient speed.
So given a graph that has a cycle from vertex S to E as it goes through every vertex then ends on E. My goal here is to remove all extra edges so that there is just a path from S to E. To help me with this I have a function called check(node) I'm not given the code for it but it returns True or False if there still exist a path from S to E such that all nodes were visited only once until we end at E.
Example:
The plan is to remove a edge from vertex a to b and then run check(node) on the mutated graph and see if it still returns True so we know its safe to remove that edge, and if it returns False then add it back. Do that for every edge so only the needed edge remains, however I have no idea how to iterate through the edges.
I stored the graphs in a dictionary
Usually the approach to an Algorithms problem like this is you first figure out what algorithmic tools you can use. Most basic problems can be solved with an existing algorithm. Your first objective is to see if you can modify the problem set (ie the given graph) in such a way that you don't need to modify the algorithm, because modifying the algorithm lends to difficulties in assessing Big-O for the problem. If the graph can't be modified in any way that makes running a black boxed Algorithm easy, then you modify the algorithm. The last resort is to come up wit your own algorithm to solve the problem.
If my Algorithms recollection is correct, in short this is the Travelling Salesman Problem. If I'm understanding your question correctly, you want the shortest path possible that visits every node. You don't even need to modify your given graph in order to use the algorithm. It should theoretically find you the desired path. Only after the algorithm has run do you need to reduce the graph to its desired state. So I suggest finding some way to implement TSP to your specifications, and remove all edges that aren't part of the solution.
Here is some sample code from GeeksForGeeks that could help you get started
I am reading `Grokking algorithms" and understand Dijkstran and greedy algorithms,
but, when the author compare them with NP-complete problem
But it’s hard to tell if a problem you’re working on is NP-complete. Usually there’s a very small difference between a problem that’s easy to solve and an NP-complete problem. For example, in the previous chapters, I talked a lot about shortest paths. You know how to calculate the shortest way to get from point A to point B.
But if you want to find the shortest path that connects several points, that’s the traveling-salesperson problem, which is NP-complete. The short answer: there’s no easy way to tell if the problem you’re working on is NP-complete. Here are some giveaways:
The sentence:
But if you want to find the shortest path that connects several points,
What are "the several points"?
I cannot figure out any difference with a basic Dijkstan's algorithms problem.
He means a path through a subset of all the nodes of a graph, I think. (Think the worst case of 'several points')
Note, that for any fixed number of points, say k = 3 or k = 3000 on a graph of n nodes, the problem would be of the same complexity as for two points. While some people may think that several is never greater than seven, or may be seven dozens or seven billion, it is neither a matter of fact nor an exact science.
Less likely he meant the usual formulation of the Traveling salesman problem (all the nodes/points on a connected graph), though a possibility. NP complete any way.
Suppose I have the following possible paths of maze.
I found the possible paths implementing dead end removal process with image processing.
I wanted to know if there is any equation to find the number of possible paths from the number of available junctions? I am thinking of implementing it in python.
Any other method / algorithm for finding number of possible paths can be suggested.
It'll be great if you could be as specific as possible.
Help will be appreciated.
I have a graph that contains edges that must be visited, as well as edges that are optional. The edges have varying weights and can be traveled in either direction and as many times as required. I am trying to determine the route that minimises the total weight.
As I understand it, the Chinese Postman Problem deals with a graphs where every edge of a graph must be visited at least once. Can anyone tell me if the variant described above has a 'name' or point me in the direction of algorithms that might deal with solving this type of graph?
I am attempting to program a solution in Python so any solutions that use that would be great, otherwise I'm sure I will be able to work through a solution.