1,048
edits
Line 26: | Line 26: | ||
#include <vector> | #include <vector> | ||
#include <queue> | #include <queue> | ||
using namespace std; | 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() { | 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; | |||
} | } | ||