3. What will be the output of the following code snippet detecting cycle in an undirected graph?
int main() {
vector graph = {{1,2}, {0,2}, {0,1,3}, {2}};
vector<bool> visited(4, false);
bool hasCycle = false;
function dfs = [&](int node, int parent) {
visited[node] = true;
for (int adj : graph[node]) {
if (!visited[adj]) {
if (dfs(adj, node)) return true;
} else if (adj != parent) {
return true;
}
}
return false;
};
for (int i = 0; i < 4; i++) {
if (!visited[i]) {
if (dfs(i, -1)) {
hasCycle = true;
break;
}
}
}
cout << (hasCycle ? "Cycle Found" : "No Cycle") << endl;
return 0;
}