1

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))

here are the functions:

create-gr         ; creates an empty list
is-graph-el:      ; check if a list is of the format above (for graph elements)
el-contains-n     ; check if a graph element contains an atom representing a node (e.g., a)
is-mem            ; check if a list contains an atom
push-unq          ; gets a list and an atom and inserts it at the end of the list if it's not already there
remove-vis        ; gets a graph (list of list), removing all the graph elements containing the specified atom
remove-all-vis    ; 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-el          ; return the first element of a node (e.g., for [a, 1, b], return a
third-el          ; return third element (similar to above)
convert-to-els    ; receives a graph and returns a flat list of all first and third elements listed in order

This is how I have implemented the 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 BFS using ONLY those functions?

Any help of any kind is sincerely appreciated...

Farzad
  • 1,770
  • 4
  • 26
  • 48
  • That's not a good question for Stackoverflow. See the help: **Focus on questions about an actual problem you have faced. Include details about what you have tried and exactly what you are trying to do.** You describe an assignment you've got, but not an actual programming problem and you don't describe what you tried so far. You didn't show your work. You posted no Lisp code which attempts to solve your assignment. The data you've listed does not even look like Lisp data... – Rainer Joswig Nov 07 '15 at 23:00
  • Actually, I tried to ask my question in a very general way. The functions are listed are just the names of the functions and a description of what they do. And to be honest, I couldn't try to do much because as I mentioned in the question, I'm lost and I don't know where to begin... I have some functions, but I don't know how to relate them to the problem... – Farzad Nov 07 '15 at 23:11
  • Stackoverflow is not a tutoring service to help with your assignments. It might help to solve the question in some other programming language (one you already know) first and then solve it in Lisp. You need to learn some Lisp first. Here is a Lisp introductory book, available as no-cost download: https://www.cs.cmu.edu/~dst/LispBook/index.html – Rainer Joswig Nov 07 '15 at 23:15
  • I know a little LISP. As I mentioned in the question, I have already implemented all the preliminary functions that are supposed to be enough to solve the problem. I was just looking for some insight, tip, or anything to help me head in the right direction. Also, for the sentence you mentioned from help, I'm not sure what I'm missing: it is a problem I have faced, I mentioned what I tried (in this case it's only just thoughts because I didn't know where to start), and also I mention what I'm trying to do (in this case, what is the problem supposed to solve, along with an example) – Farzad Nov 07 '15 at 23:30
  • And by the way, the problem here is to implement a BFS using ONLY the given function, which I think is a very legible problem. I'm not asking about how to implement functions in lisp or how to read lists etc. It's a general question. Maybe my mistake was to mention LISP here, but it's a general problem of: **using only the 12 functions we have, how to implement a recursive bfs function that accepts 5 arguments?** – Farzad Nov 08 '15 at 00:43
  • I posted [this question](http://stackoverflow.com/questions/33612960/breadth-first-search-bfs-using-only-a-list-of-available-functions?noredirect=1#comment55000415_33612960) with different tags to be sure it's not tied to a specific programming language... – Farzad Nov 09 '15 at 16:27

0 Answers0