0

for example, I want to bfs for only k steps, or called k level. How can I do it? Now I know only a little about how to terminate it from Is it possible to change breadth first search termination condition in BGL?.

I use boost::record_distance solved the problem, but I think it's not the perfect answer.

Brbara
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 26 '22 at 07:51

2 Answers2

0

Maintain a count of levels and when level equals k steps, you can terminate the bfs.

kehsihba19
  • 83
  • 6
  • but I don't know the bfs level, because I didn't have the queue that used in bfs. Or is there any other way to get the level ? – Brbara Sep 27 '22 at 02:34
0

Wrap the first call the BFS in a try .. catch structure

try {
  ... call the BFS routine ...
}
catch 
{
   std::cout << "max level reached\n";
{

In the visitor contructor, initialize a level counter.

In the visitor, increment the level and throw an exception when the maximkum is reached.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • but I don't know the bfs level, because I didn't have the queue that used in bfs. Or is there any other way to get the level ? – Brbara Sep 27 '22 at 02:34