How to check if a triangular cycle exists in a graph
C++
// C++ program to find out whether
// a given graph is Bipartite or not.
// It works for disconnected graph also.
#include <bits/stdc++.h>
using namespace std;
const int V = 4;
// This function returns true if
// graph G[V][V] is Bipartite, else false
bool isBipartiteUtil(int G[][V], int src, int colorArr[])
{
colorArr[src] = 1;
// Create a queue (FIFO) of vertex numbers a
// nd enqueue source vertex for BFS traversal
queue <int> q;
q.push(src);
// Run while there are vertices in queue (Similar to BFS)
while (!q.empty())
{
// Dequeue a vertex from queue ( Refer http://goo.gl/35oz8 )
int u = q.front();
q.pop();
// Return false if there is a self-loop
if (G[u][u] == 1)
return false;
// Find all non-colored adjacent vertices
for (int v = 0; v < V; ++v)
{
// An edge from u to v exists and
// destination v is not colored
if (G[u][v] && colorArr[v] == -1)
{
// Assign alternate color to this
// adjacent v of u
colorArr[v] = 1 - colorArr[u];
q.push(v);
}
// An edge from u to v exists and destination
// v is colored with same color as u
else if (G[u][v] && colorArr[v] == colorArr[u])
return false;
}
}
// If we reach here, then all adjacent vertices can
// be colored with alternate color
return true;
}
// Returns true if G[][] is Bipartite, else false
bool isBipartite(int G[][V])
{
// Create a color array to store colors assigned to all
// veritces. Vertex/ number is used as index in this
// array. The value '-1' of colorArr[i] is used to
// ndicate that no color is assigned to vertex 'i'.
// The value 1 is used to indicate first color is
// assigned and value 0 indicates second color is
// assigned.
int colorArr[V];
for (int i = 0; i < V; ++i)
colorArr[i] = -1;
// This code is to handle disconnected graoh
for (int i = 0; i < V; i++)
if (colorArr[i] == -1)
if (isBipartiteUtil(G, i, colorArr) == false)
return false;
return true;
}
// Driver program to test above function
int main()
{
int G[][V] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
};
isBipartite(G) ? cout << "Yes" : cout << "No";
return 0;
}
Also in C++:
- Title
- vprintf
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- c++ public inheritance not getting protected
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- c++ class member initialization
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- cpp how to create an object of template class
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- c++ lettura file
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++