Skip to content

Instantly share code, notes, and snippets.

@vardanator
Created February 21, 2018 09:17
Show Gist options
  • Select an option

  • Save vardanator/d6f7beea2e69f389f56aa953015e6039 to your computer and use it in GitHub Desktop.

Select an option

Save vardanator/d6f7beea2e69f389f56aa953015e6039 to your computer and use it in GitHub Desktop.
// Assuming graph is connected
// and a graph node is defined by this structure
// struct GraphNode {
// T item;
// vector<GraphNode*> children;
// }
// WARNING: untested code
void BreadthFirstSearch(GraphNode* node) // start node
{
if (!node) return;
queue<GraphNode*> q;
q.push(node);
while (!q.empty()) {
GraphNode* cur = q.front(); // doesn't pop
q.pop();
for (auto child : cur->children) {
q.push(child);
}
// do what you want with current node
cout << cur->item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment