/
oop_nust_misis
/
lecture_2
Обзор
Документация
Войти
/
oop_nust_misis
/
lecture_2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
master
algorithms/bfs.cpp
30 строк
691 B
Stanislav Kiselev
Add Code For Lecture 2
20 фев 2025, 09:28
20 фев 2025, 09:28
8f596f2
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <queue> void bfs(int start, const std::vector<std::vector<int>>& graph) { std::vector<bool> visited(graph.size(), false); std::queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); std::cout << node << " "; for (int neighbor : graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { std::vector<std::vector<int>> graph = { {1, 2}, {0, 3, 4}, {0, 4}, {1, 5}, {1, 2, 5}, {3, 4} }; bfs(0, graph); }