Workspace An Khang - Buổi 28 - Đồ thị - Mở đầu BFS

BFS

vector<vector<int>> adj;
vector<bool> visited;

void bfs(int s) {
  queue<int> q;
  q.push(s);
  is_visited[s] = true;
  while (!q.empty()) {
    int u = q.front(); // Get the first element
    q.pop(); // Remove the first element
    // Do something with u
    for (int v : adj[u]) 
      if (!is_visited[v]) {
        // Do something with v
        q.push(v);
        is_visited[v] = true;
      }
  }
}
Nhận xét Tham gia thảo luận bên dưới.

Không có ý kiến tại thời điểm này.