0

When I use a BFS algorithm on a graph, I try to obtain the maximum depth of the graph.

But I don't know where to put my incrementation in this algorithm :

FUNCTION BFS(G,s) 
    BEGIN 
        FOR any vertex v of G 
        DO Mark[v] ← False 
        END FOR Mark[s] ← True 
    F ← Empty Queue 
    enqueue(s,F) 
        WHILE F is not empty 
        DO  v ← dequeue(F) 
            FOR any vertex w neighbor of v 
            DO
                IF Mark[w] = False THEN 
                Mark[w] ← True; 
                enqueue(w,F) 
                END IF  
            FOR END 
        WHILE END

I tried to put an incrementation of a number after the END FOR but it gives an number superior than the real max depth of the graph.

Please can anyone help me.

Thank You.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Raj
  • 197
  • 1
  • 2
  • 11

2 Answers2

1

Running BFS on a graph gives you a tree and what you want actually is the depth of the tree. Putting an incrementation after the END FOR will absolutely give you a number greater than the depth of tree (depending on your code). Imagine this case: all neighbors of the current vertex 'v' are visited (all of them marked as true), this means that vertex v is leaf but your incrementation will still increase by one, which is not correct.

Solution: your incrementation should only increase when for current v there is at least one neighbor w which is not yet visited ( marked as false) and you have not yet increased the depth for any other vertex at the same level (vertices at the same distance from the root 's') as w. So what you missing in your code is you should keep track of the level in your code to know when to increase.

This question has already been asked for a tree, so you might have to look to this How to keep track of depth in breadth first search? just to let you know how to update your code to keep track of the level. Simply the idea is to push a NULL to the queue at the end of each level. The root itself is at level0, so after pushing root to the queue, push also a NULL. After that, each time you encounter NULL, push NULL to the queue for the next level and check if the top of the queue is not NULL increase the depth by one and continue else break.

FUNCTION BFS(G,s) 
   BEGIN 
      FOR any vertex v of G DO 
          Mark[v] ← False 
      END FOR 
      Mark[s] ← True 
      F ← Empty Queue
      depth=0 
      enqueue(s,F), enqueue(NULL,F) // this is the level 0
      WHILE F is not empty DO    
         v ← dequeue(F)
         IF v=NULL THEN
             enqueue(NULL,F)
             IF(F.peek()==NULL) THEN
               BREAK
             ELSE
               depth++
               Continue
         FOR any vertex w neighbor of v DO
            IF Mark[w] = False THEN 
              Mark[w] ← True; 
              enqueue(w,F) 
            END IF  
         FOR END 
      WHILE END
Community
  • 1
  • 1
Abdulhakeem
  • 153
  • 7
  • Thank you very much for your answer. I also tried one different solution very similar to yours and it works – Raj Apr 25 '17 at 21:31
0

With my friend we found this solution :

FUNCTION BFS(G,s) 
   BEGIN 
      FOR any vertex v of G 
      DO Mark[v] ← False 
      END FOR Mark[s] ← True 
   F ← Empty Queue 
   DIST [] ← Empty Tab
   COUNTER = 0 
   enqueue(s,F) 
      WHILE F is not empty 
      DO    v ← dequeue(F)
            DIST[v] ← 0 
           FOR any vertex w neighbor of v 
           DO
              IF Mark[w] = False THEN 
              Mark[w] ← True; 
              enqueue(w,F)
              DIST[w] ← DIST[v] + 1 
                  IF [COUNTER < DIST[w]] THEN
                  COUNTER = DIST[w];
                  END IF
              END IF  
           FOR END 
      WHILE END
   RETURN (COUNTER);
END FUNCTION

I editied this answer after the comment of Abdulhakeem Apr 26 at 3:36, because previsouly I posted the wrong one.

Raj
  • 197
  • 1
  • 2
  • 11
  • That is not what you have asked. This is the mere BFS algorithm which finds the distance of all vertices of graph G from a source vertex. DIST is an array of values, you need to do one extra O(V) (V is the number of vertices in graph G) to find the max value from DIST which will be the depth of the tree. So if you want to find the depth of tree in just O(V+E), I would say the other approach is more efficient. – Abdulhakeem Apr 26 '17 at 03:36
  • Hello, Thank you for your comment, i just corrected my answer, i posted the wrong one. Now its correct normally. – Raj May 06 '17 at 10:55