I have a method “connection(int n)” which gives me all the cells number that have relation with cell number “n” now I want a method which gives me all the routes with a specific length “myLength” that start from cell number “start” and just in one direction (as it’s usual) I mean we are not allowed to pass some cells more than one time
thanks in advance for your help
P.S. I can’t use map tools, graph tools,… with basic tools please
I have a method connection(int n) which gives me all the cells number that
Share
You are looking for BFS.
Model your problem as a graph
G = (V,E)such thatV = {1,...,n}[all possible values] andE = { (u,v) | connection(u) returns v }[there is a connection between u and v using yourconnection()method]In addition to the standard BFS, you will need to add another stop condition when you reached the limited length.
EDIT:
Note that this solution assumes you are looking for a path up-to length, and not exactly length.
BFS doesn’t work here for the counter example of a clique if you want exactly of length.
To get all vertices that have a simple path of exactly length – You will probably need a DFS that avoids loops [can be done by maintaining a
setthat is modified each iteration], but can explore each vertex more then once.