Search
 
SCRIPT & CODE EXAMPLE
 

CPP

bfs to detect cycle in undirected graph

// C++ program to detect cycle
// in an undirected graph
// using BFS.
#include <bits/stdc++.h>
using namespace std;
 
void addEdge(vector<int> adj[], int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
 
bool isCyclicConntected(vector<int> adj[], int s,
                        int V, vector<bool>& visited)
{
    // Set parent vertex for every vertex as -1.
    vector<int> parent(V, -1);
 
    // Create a queue for BFS
    queue<int> q;
 
    // Mark the current node as
    // visited and enqueue it
    visited[s] = true;
    q.push(s);
 
    while (!q.empty()) {
 
        // Dequeue a vertex from queue and print it
        int u = q.front();
        q.pop();
 
        // Get all adjacent vertices of the dequeued
        // vertex u. If a adjacent has not been visited,
        // then mark it visited and enqueue it. We also
        // mark parent so that parent is not considered
        // for cycle.
        for (auto v : adj[u]) {
            if (!visited[v]) {
                visited[v] = true;
                q.push(v);
                parent[v] = u;
            }
            else if (parent[u] != v)
                return true;
        }
    }
    return false;
}
 
bool isCyclicDisconntected(vector<int> adj[], int V)
{
    // Mark all the vertices as not visited
    vector<bool> visited(V, false);
 
    for (int i = 0; i < V; i++)
        if (!visited[i] && isCyclicConntected(adj, i,
                                         V, visited))
            return true;
    return false;
}
 
// Driver program to test methods of graph class
int main()
{
    int V = 4;
    vector<int> adj[V];
    addEdge(adj, 0, 1);
    addEdge(adj, 1, 2);
    addEdge(adj, 2, 0);
    addEdge(adj, 2, 3);
 
    if (isCyclicDisconntected(adj, V))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: run c++ program mac 
Cpp :: c++ math 
Cpp :: string comparison c++ 
Cpp :: vector of threads thread pool c++ 
Cpp :: overload subscript operator cpp 
Cpp :: standard template library in c++ 
Cpp :: take a function argument 
Cpp :: add matic mainnet to metamask mobile 
Cpp :: cyclic array rotation in cpp 
Cpp :: how to empty a std vector 
Cpp :: friend function in c++ 
Cpp :: print Colored text in C++ 
Cpp :: string erase 
Cpp :: Find duplicates in an array geeks for geeks solution in cpp 
Cpp :: programs using vectors in c++ 
Cpp :: remove comments c++ 
Cpp :: stoi in c++ 
Cpp :: c++ prime number 
Cpp :: C++ rename function 
Cpp :: c++ swap function 
Cpp :: time complexity of sorting algorithms 
Cpp :: hello world programming 
Cpp :: how we can write code to remove a character in c++ 
Cpp :: build a prefix array cpp 
Cpp :: even and odd numbers 1 to 100 
Cpp :: unordered_map in c++ 
Cpp :: Programming Languages codechef solution in c++ 
Cpp :: string class in c++ 
Cpp :: i++ i-- 
Cpp :: c++ error missing terminating character 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =