Solved

Recursive "Breadth first search" (BDF)

Posted on 2000-04-10
8
4,194 Views
Last Modified: 2008-03-17
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 ;)
Thanks!

(Sorry, forgot to mention the recursive-part)
0
Comment
Question by:tryso
8 Comments
 
LVL 2

Expert Comment

by:Serega
ID: 2699850
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:

tree_type
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)
        continue;
      if (predicate(guess) == TRUE)
        return guess;
      enqueue(left_child(guess), queue);
      enqueue(right_child(guess), queue);
    }
  return the_empty_tree;
}
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2699870
C in the Delphi area? ;-) just listening...
0
 

Author Comment

by:tryso
ID: 2699905
That is an iterative algorithm..
...I need a recursive one.
(as many childs as you want.. but that's no problem)
0
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.

 

Author Comment

by:tryso
ID: 2699909
Edited text of question.
0
 
LVL 2

Accepted Solution

by:
Serega earned 193 total points
ID: 2700257
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;
begin
  marks[nodenr] := true;
  if predicate(nodenr) then
    return nodenr;

  if( exist(neighbour(nodenr))
  begin
    neighbourNr := neighbour(nodenr);
    if not marks[neighbourNr] then
    begin
      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;
    end;
  end;  
  return 0;
end;
0
 

Author Comment

by:tryso
ID: 2701315
Um, might be just fine, but I'm using an adjacency List..
Linked lists instead of Arrays..
They are also weighted.
0
 

Expert Comment

by:johnstoned
ID: 2711344
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?

Dave.
0
 

Author Comment

by:tryso
ID: 2711464
Hi,
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 -

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question