Skip to content

Commit abc8579

Browse files
committedMar 27, 2023
Add bipartite graph check implmentation
1 parent c5e4e2e commit abc8579

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
+ [Floyd Warshall’s](algorithms/floyd_warshall.cpp)
4848
+ [Loop Detection](algorithms/topsort.cpp)
4949
+ [Topological Sort](algorithms/topsort.cpp)
50+
+ [Bipartite Graph Check](algorithms/bipartite_graph_check.cpp)
5051
+ [Strongly Connected Component (Kosaraju)](algorithms/scc.cpp)
5152
+ [Lowest Common Ancestor(sparse table)](algorithms/lca.cpp)
5253
+ [Articulation Point](algorithms/articulation_point.cpp)

‎algorithms/bipartite_graph_check.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector>
2+
#include <array>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
#define MAX 100000
8+
9+
vector<int> adj[MAX];
10+
array<int, MAX> color;
11+
array<bool, MAX> visited;
12+
13+
bool isGraphBipartite(int src) {
14+
color.fill(-1);
15+
visited.fill(false);
16+
queue<int> Q;
17+
Q.push(src);
18+
color[src] = 1;
19+
visited[src] = true;
20+
while (!Q.empty()) {
21+
int u = Q.front();
22+
Q.pop();
23+
for (int i = 0; i < (int)adj[u].size(); i++) {
24+
int v = adj[u][i];
25+
if (!visited[v]) {
26+
visited[v] = true;
27+
color[v] = 1 - color[u];
28+
Q.push(v);
29+
} else if (visited[v] and color[v] == color[u]) {
30+
return false;
31+
}
32+
}
33+
}
34+
return true;
35+
}
180 KB
Binary file not shown.

0 commit comments

Comments
 (0)