title: Boost Graph Lib add tree vertex during algorithm
body: <p>I would like to create an algorithm with Boost Graph Library that expands a branch on a tree graph "on the fly" while the algorithm is running. Depending on some conditions the algorithm would explore branches with lower costs. These costs should also be created while the tree gets expanded. The reason I want to avoid creating the tree before running an algorithm is to avoid unnecesary vertices. Each vertex has data attached to it that requires a computation intense prediction step which takes a lot of time.</p>
  
  <p>All the examples and in the documentation of boost show a given graph and then just apply an algorithm but none of them expands a graph while the algorithm is running.</p>
  
  <p>I found a similar question <a href="https://lists.boost.org/boost-users/2005/09/13782.php" rel="nofollow noreferrer">here</a> and the answer:</p>
  
  <blockquote>
    <p>Adding vertices and edges is a dangerous thing to do inside an
    algorithm visitor. This is because doing that invalidates
    iterators that the algorithm is using.  Also, the algorithm
    creates a mapping from vertices to colors (to mark
    which vertices have been processed), and adding vertices
    will mess up that mapping. Can you just record that
    you want to add vertices and do it after the BFS? </p>
  </blockquote>
  
  <p>I also thought of creating an empty tree structure and changing the data attached to vertices when they are explored using dijkstra_shortest_paths. However, as far as I understand I need a cost map for the edges before running the algorithm which I dont have either. The weights should be adapted when a new vertex is discovered because this newly discovered vertex must be assigned some cost or reward first. Therefore the cost map should also be created on the fly.</p>
  
  <p>Has someone tried adding vertices in an algorithm before or is there a better way to achieve such behavior? Maybe BGL is not the right library for this? </p>