Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
d87a6685
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d87a6685
编写于
8月 14, 2020
作者:
A
AkVaya
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Performed the requested changes
上级
97f3e1a0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
9 deletion
+16
-9
graph/is_graph_bipartite.cpp
graph/is_graph_bipartite.cpp
+16
-9
未找到文件。
graph/is_graph_bipartite.cpp
浏览文件 @
d87a6685
...
...
@@ -2,13 +2,14 @@
* @file is_graph_bipartite
*
* @brief Algorithm to check whether a graph is bipartite
* (https://en.wikipedia.org/wiki/Bipartite_graph)
*
* @details
* A graph is a collection of nodes also called vertices and these vertices
* are connected by edges.A bipartite graph is a graph whose vertices can be
* divided into two disjoint and independent sets U and V such that every edge
* connects a vertex in U to one in V.
*
(https://en.wikipedia.org/wiki/Bipartite_graph)
*
* The given Algorithm will determine whether the given graph is bipartite or not
*
*
...
...
@@ -32,9 +33,6 @@
#include <vector>
#include <queue>
using
std
::
vector
;
using
std
::
queue
;
const
int
nax
=
5e5
+
1
;
/**
* Class for representing graph as an adjacency list.
...
...
@@ -43,9 +41,9 @@ class graph {
private:
int
n
;
/// size of the graph
vector
<
vector
<
int
>
>
adj
;
/// adj stores the graph as an adjacency list
std
::
vector
<
std
::
vector
<
int
>
>
adj
;
/// adj stores the graph as an adjacency list
vector
<
int
>
side
;
///stores the side of the vertex
std
::
vector
<
int
>
side
;
///stores the side of the vertex
public:
/**
...
...
@@ -74,12 +72,14 @@ void graph::addEdge(int u, int v) {
}
/**
* @brief function that checks whether the graph is bipartite or not
* the function returns true if the graph is a bipartite graph
* the function returns false if the graph is not a bipartite graph
*/
bool
graph
::
is_bipartite
(){
n
=
adj
.
size
();
side
.
resize
(
n
,
-
1
);
bool
check
=
true
;
queue
<
int
>
q
;
std
::
queue
<
int
>
q
;
for
(
int
current_edge
=
0
;
current_edge
<
n
;
++
current_edge
)
{
if
(
side
[
current_edge
]
==
-
1
){
...
...
@@ -103,9 +103,9 @@ bool graph::is_bipartite(){
return
check
;
}
/**
*
Main funtion
*
Function to test the above algorithm
*/
int
main
(){
void
test
(){
graph
G1
(
5
);
/// creating graph G1 with 5 vertices
/// adding edges to the graphs as per the illustrated example
G1
.
addEdge
(
1
,
2
);
...
...
@@ -118,6 +118,7 @@ int main(){
G2
.
addEdge
(
1
,
2
);
G2
.
addEdge
(
1
,
3
);
G2
.
addEdge
(
2
,
3
);
/// checking whether the graphs are bipartite or not
if
(
G1
.
is_bipartite
()){
std
::
cout
<<
"The given graph G1 is a bipartite graph
\n
"
;
...
...
@@ -131,5 +132,11 @@ int main(){
else
{
std
::
cout
<<
"The given graph G2 is not a bipartite graph
\n
"
;
}
}
/**
* Main function
*/
int
main
(){
test
();
///Testing
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录