Created
February 21, 2018 09:17
-
-
Save vardanator/d6f7beea2e69f389f56aa953015e6039 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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