Answers for "bipartite graph"

C++
1

bipartite graph in discrete mathematics

Bipartite Graph-
 

A bipartite graph is a special kind of graph with the following properties-

It consists of two sets of vertices X and Y.
The vertices of set X join only with the vertices of set Y.
The vertices within the same set do not join.
Posted by: Guest on December-31-2020
0

bipartite graph

//Bipartite Graph (BFS)/ Graph coloring
//Do check striver graph series
#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 bfsbipar(int source_node,vector<int>adj[],int color[])
{
    queue<int>q;
    q.push(source_node);
    color[source_node]=0;
    while(!q.empty())
    {
        int f=q.front();
        q.pop();
        for(auto i:adj[f])
        {
            if(color[i]==-1)
            {
                if(color[f]==1)
                {
                    color[i]=0;
                }
                else
                {
                    color[i]=1;
                }
                q.push(i);
            }
            else if(color[i]==color[f])
            {
                return false;
            }
        }
    }
    return true;
}
bool checkbipar(vector<int>adj[],int n)
{
    int color[n];
    for(int i=0;i<n;i++)
    {
        color[i]=-1;
    }
    for(int i=0;i<n;i++)
    {
        if(color[i]==-1)
        {
            if(!bfsbipar(i,adj,color))
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int n,v;
    cout<<"Enter the number of vertex, edges: ";
    cin>>n>>v;
    int a,b;
    vector<int>adj[n];
    cout<<"enter the link:"<<endl;
    for(int i=0;i<v;i++)
    {
        cin>>a>>b;
        addedge(adj,a,b);
    }
    if(checkbipar(adj,n))
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    return 0;
}
Posted by: Guest on August-15-2021

Browse Popular Code Answers by Language