• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4318
  • Last Modified:

Recursive "Breadth first search" (BDF)

Can anybody help me with the code for a recursive Breadth First Search  Traversal-routine for a graph?
I only need a Pseudocode.
My textbook says it's hard, but I'm too lazy to make that queue-ADT ;)

(Sorry, forgot to mention the recursive-part)
1 Solution
See http://yoda.cis.temple.edu:8080/UGAIWWW/books/shoham/chapter2/section2.5.html
or this:

Here is an implementation of breadth--first search. Notice that a queue is used instead of a stack; otherwise, this routine is virtually identical to the second implementation of depth--first search:

bfs_queue(tree_type tree, BOOL (*predicate)(tree_type))
  queue_type queue = create_queue();
  tree_type guess;

  enqueue(tree, queue);
  while (empty_(queue) == FALSE)
      guess = dequeue(queue);
      if (empty_tree(guess) == TRUE)
      if (predicate(guess) == TRUE)
        return guess;
      enqueue(left_child(guess), queue);
      enqueue(right_child(guess), queue);
  return the_empty_tree;
C in the Delphi area? ;-) just listening...
trysoAuthor Commented:
That is an iterative algorithm..
...I need a recursive one.
(as many childs as you want.. but that's no problem)
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

trysoAuthor Commented:
Edited text of question.
I don't see what language it was. But it pass for opcode. :)
Ok, I think recursive one by another kind of opcode will look like this:

marks: array[0..N] of boolean;
{marks := (FALSE,...,FALSE) }
function bfs_queue(nodenr: integer):integer;
  marks[nodenr] := true;
  if predicate(nodenr) then
    return nodenr;

  if( exist(neighbour(nodenr))
    neighbourNr := neighbour(nodenr);
    if not marks[neighbourNr] then
      if predicate(neighbourNr) then
        return neighbourNr;
      marks[neighbourNr] := true;
      res := bfs_queue(nodenr);
      if res > 0 then return res;
      marks[neighbourNr] := false;
      res := bfs_queue(neighbourNr);
      return res;
  return 0;
trysoAuthor Commented:
Um, might be just fine, but I'm using an adjacency List..
Linked lists instead of Arrays..
They are also weighted.
I don't think it's possible to do breadth first search recursivley.  I think if you tried, you would end up doing a depth first search.

I can give you a recursive algorithm for DFS if you want?

trysoAuthor Commented:
I've solved the problem semi-satisfactory with a DFS algorithm. I know that it is possible to do a recursive BFS-algorithm because my textbook leaves it to the reader to make a pseudocode for such an algo (although it stresses that it's a difficult task).
I'm not 100% sure wheter Serega solved the problem or not since I used an adjacancy list instead of an adjacancy matrix, but I'll look into it shortly, and think it's fair that he gets the points, taking into account that he at least came very close (and I guess there is no big task to convert from matrix to linked lists..).
Thank you all! :)
- T -

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now