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
Related
So I'm trying to create a program that finds the shortest path from nodeA to nodeB, however, I want to block certain nodes so that it would find another path. I'm not really aiming for an optimal code here I'm just trying things out, exploring etc.
Is it still considered a BFS if I modify it a little?
Yes, it still is a BFS, with just a few constraints. The essence of BFS algorithm is the way it explores the graph, you are just exploring a subgraph (through filtering out a bit of it).
I am given a map that looks like so:
Every node is connected with its horizontal and vertical (not diagonal) neighbors and all connections have the same cost.
I want to find the best path, from one location to a set of end locations (the one which can be reached quickest). Also note that every node has the ability to be blocked and and thus not be used as a node in the path.
What would be the most efficient algorithm to solve this problem with?
I have thought of maybe A* but find it difficult to apply the multiple endpoint rule.
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.
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:
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.