5

how does bfs on directed and undirected graph differ in implementation.

i found the following pseudocode on web. i am ok with undirected graph. but can't figure out how to implement it for directed graph.

 frontier = new Queue()
  mark root visited (set root.distance = 0)
  frontier.push(root)
  while frontier not empty {
     Vertex v = frontier.pop()
    for each successor v' of v {
    if v' unvisited {
        frontier.push(v')
        mark v' visited (v'.distance = v.distance + 1)
    }
    }
  }
3
  • It's just the same.
    – Beta
    Oct 3, 2016 at 17:57
  • ahh got that already.. thanks for the response Oct 3, 2016 at 18:01
  • well..Thanks to that guy who up-voted my question Oct 3, 2016 at 18:46

1 Answer 1

2

The implementation in pseudocode is the same, except that the notion of successor would mean neighbor for an undirected graph but child (or similar) for a directed graph.

2
  • addNode(a, b); if(dir == "no") { addNode(b, a); } Oct 3, 2016 at 18:44
  • is the above setup correct?? if its directed one way access is provided. else 2 way access is provided Oct 3, 2016 at 18:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.