Editing
Breadth-First Search
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example C++ code == Here is an example of implementing BFS in C++ using STL containers: <pre> #include <iostream> #include <vector> #include <queue> using namespace std; vector<vector<int>> adjList; vector<bool> visited; void bfs(int startNode) { queue<int> q; visited[startNode] = true; q.push(startNode); while (!q.empty()) { int currentNode = q.front(); q.pop(); cout << "Visiting node: " << currentNode << endl; for (int adjacent : adjList[currentNode]) { if (!visited[adjacent]) { visited[adjacent] = true; q.push(adjacent); } } } } int main() { // Initialize adjacency list and visited vector adjList = {{1, 3}, {0, 2}, {1, 3}, {0, 2}}; visited.resize(4, false); // Perform BFS from the first node bfs(0); return 0; } </pre> === Example USACO Silver Problems === Here are some example USACO Silver problems that utilize Breadth-First Search: [[USACO_2017_January_Silver_P3|2017 January Silver Problem 3: Cow Navigation]] [[USACO_2018_December_Silver_P2|2018 December Silver Problem 2: Mooyo Mooyo]] [[USACO_2020_February_Silver_P1|2020 February Silver Problem 1: Triangles]] === Example USACO Gold Problems === Here are some example USACO Gold problems that utilize Breadth-First Search: [[USACO_2015_December_Gold_P1|2015 December Gold Problem 1: Moocast]] [[USACO_2019_December_Gold_P2|2019 December Gold Problem 2: Cow Land]] [[USACO_2020_January_Gold_P2|2020 January Gold Problem 2: Milk Visits]] [[Category:Breadth-First Search]] [[Category:Algorithms]] [[Category:Graph]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information