#include <bits/stdc++.h> using namespace std; void bfs(vector<vector<int>> &adj, int…
- User
#include <bits/stdc++.h> using namespace std;
void bfs(vector<vector> &adj, int start, vector &vis) { queue myqueue; vis[start] = 1; myqueue.push(start);
codewhile (!myqueue.empty()) { int curr = myqueue.front(); for (auto it : adj[curr]) { if (!vis[it]) { vis[it] = 1; myqueue.push(it); } } }}
void dfs(vector<vector> &adj, int start, vector &vis) { vis[start] = 1; for (auto it : adj[start]) { if (!vis[it]) { vis[it]=1; dfs(adj, it, vis); } } } what is wrong
- Assistant
Your
bfsfunction is missing the key step of removing the current node from the queue after processing it. This will cause an infinite loop because the same node will be processed over and over without ever being removed from the queue. Here's the corrected version of thebfsfunction:cpp - User
#include <bits/stdc++.h> using namespace std;
void bfs(vector<vector> &adj, int start, vector &vis) { queue myqueue; vis[start] = 1; myqueue.push(start);
codewhile (!myqueue.empty()) { int curr = myqueue.front(); myqueue.pop(); for (auto it : adj[curr]) { if (!vis[it]) { vis[it] = 1; myqueue.push(it); } } }}
void dfs(vector<vector> &adj, int start, vector &vis) { vis[start] = 1; for (auto it : adj[start]) { if (!vis[it]) {
codedfs(adj, it, vis); } }}
- Assistant