Breadth-first search with set instead of queue <p>I'm using BFS to find connected components. I've decided to implement it using a set to track visited nodes. The problem with that approach is that one vertex may be added to queue twice. So I just changed queue to set. I don't care about visit order, all nodes are visited once and the algorithm works fine. Of course, this is not classical BFS anymore: the order is broken. </p> <p>Pseudocode:</p> <pre><code>Set visited; Set to_visit; visited.insert(start) to_visit.insert(start) while (to_visit is not empty){ current = to_visit.first to_visit.delete(current) for each neighbour of current { isNew = visited.insert(neighbour) if (isNew) { to_visit.insert(neighbour) } } } </code></pre> <p>I'm pretty sure I'm not the first one who "invented" it. I wonder: how does this I-dont-care-first search is called? </p>