0

I'm trying to implement a bfs breadth first search algorithm in R. I know about the graph::bfs function and do_bfs from DiaGrammer. I think my problem is in the "for" of the bfs function.

The input would be a graph as the following

             1
       2          3
   4      5     6   7

The output should be the path. in this case, if i start from 1, 1,2,3,4,5,6,7


library (igraph)
library(foreach)
library(flifo)
library(digest)
# devtools::install_github("rdpeng/queue")

This packages seemed useful for the implementation, especially the queue one.

t<-make_tree(7, children = 2, mode ="out")

plot.igraph(t)

bfsg(t, 1)

bfsg<- function (g, n) {

  m <- c(replicate(length(V(t)), 0))
  q<-flifo::fifo ()
  m[n]<- 1
  push (q, n)
  pr <- c(replicate(length(V(t)), 0))
}



at this point, 1 should be in the queue, afrter this, got printed and popped out of the queue. After the pop, the algorithm should go to 2 and 3


while (size(q)!=0){
  print (n)
  pop(q)
}
for (i in unlist(adjacent_vertices(g, n, mode = "out"))){
  if (m[i] == 0){
    push(q,i)
    m[i]=2
  }
  
}
Anthony Ebert
  • 675
  • 14
  • 25
  • 1
    You need to describe in words what your question is here. What is the output you expect? What is the problem with your current code? The `igraph` package you have loaded already has `bfs` function. What exactly are you trying to accomplish by re-implementing one? – MrFlick Feb 14 '20 at 17:02
  • With the tree like graph that is in there, im trying to get all the vertex in order, in this case, i want 1,2,3,4,5,6,7 printed on screen. The problem with my code is that i'm not getting any print. I know that igraph alreade has a bfs function, i need to implement one myself in R and couldn't find the source code of that function. – Nicolás Taha Feb 14 '20 at 17:31

0 Answers0