问题
My question is mostly algorithm-related and not specific to a specific programming language
Assuming we have a graph represented by a list of lists, where each internal list represents two nodes and a numbered edge, Is is possible to implement a recursive BFS (breadth-first search) function using ONLY the following 12 functions? Our bfs recursive function is supposed to take 5 arguments:
- the graph to be searched
- the element to find
- the search queue
- list of visited nodes
- current element we're looking at
An example of a graph:
e1
/ \
e2 e3
| \ |
e5 e4
((e1 1 e2) (e1 2 e3) (e2 3 e4) (e3 4 e4) (e2 5 e5))
in the following functions, I refer to each individual list in our graph as a graph element
here are the functions:
create-graph ; creates an empty list
is-graph-element: ; check if a list is of the format above (for graph elements)
element-contains-node ; check if a graph element contains an atom representing a node (e.g., a)
is-member ; check if a [generic] list contains an atom
push-unique ; gets a list and an atom and inserts it at the end of the list if it's not already there
remove-visited ; gets a graph (list of list), removing all the graph elements containing the specified atom
remove-all-visited ; same as above, but we can pass a list of atoms to be removed
del-from-list ; remove all occurrences of an atom from a list
del-all-from-list ; same as above, but we can pass a list of atoms to be removed
first-member ; return the first member of a graph element (e.g., for [a, 1, b], return a
third-member ; return third member of a graph element
graph-to-list ; receives a graph and returns a flat list of all first and third members listed in order
This is how I have implemented the recursive BFS function:
- I have actually two base cases for my recursive calls: when the queue is empty (which means that we couldn't reach the element we were searching for), and when the element to be popped from the queue is the element we were searching for
- in each call, I have defined a function to find the paths from the current node (to find the nodes we can go to), and then I push these nodes to the queue
- I then push the current node to the visited list and recur
my problem is that I have defined two functions (one to find the paths and one to push the target node of those paths to the search queue) that are not in the function list above.
I was wondering if it's possible to do the recursive BFS using ONLY those functions?
PS: The list we have for the graph is supposed to represent an undirected graph, but I'm not sure how that would change the problem
Any help of any kind is sincerely appreciated...
来源:https://stackoverflow.com/questions/33612960/breadth-first-search-bfs-using-only-a-list-of-available-functions