Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
3e355d0f
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 搜索 >>
提交
3e355d0f
编写于
8月 15, 2020
作者:
A
AkVaya
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Performed the requested changes
上级
ab261831
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
82 addition
and
66 deletion
+82
-66
graph/is_graph_bipartite.cpp
graph/is_graph_bipartite.cpp
+82
-66
未找到文件。
graph/is_graph_bipartite.cpp
浏览文件 @
3e355d0f
...
@@ -31,93 +31,109 @@
...
@@ -31,93 +31,109 @@
#include <vector>
#include <vector>
#include <queue>
#include <queue>
const
int
nax
=
5e5
+
1
;
/**
/**
* Class for representing graph as an adjacency list.
* Class for representing graph as an adjacency list.
*/
*/
class
graph
{
/**
private:
* @namespace graph
int
n
;
/// size of the graph
* @brief Graph algorithms
*/
namespace
graph
{
/**
* @namespace is_graph_bipartite
* @brief Functions for checking whether a graph is bipartite or not
*/
namespace
is_graph_bipartite
{
std
::
vector
<
std
::
vector
<
int
>
>
adj
;
/// adj stores the graph as an adjacency list
class
Graph
{
private:
int
n
;
/// size of the graph
std
::
vector
<
int
>
side
;
///stores the side of the vertex
std
::
vector
<
std
::
vector
<
int
>
>
adj
;
/// adj stores the graph as an adjacency list
public:
std
::
vector
<
int
>
side
;
///stores the side of the vertex
/**
* @brief Constructor that initializes the graph on creation
*/
graph
(
int
size
=
nax
){
n
=
size
;
adj
.
resize
(
n
);
side
.
resize
(
n
,
-
1
);
}
void
addEdge
(
int
u
,
int
v
);
/// function to add edges to our graph
static
const
int
nax
=
5e5
+
1
;
bool
is_bipartite
();
/// function to check whether the graph is bipartite or not
};
public:
/**
/**
* @brief Function that add an edge between two nodes or vertices of graph
* @brief Constructor that initializes the graph on creation
*
*/
* @param u is a node or vertex of graph
explicit
Graph
(
int
size
=
nax
){
* @param v is a node or vertex of graph
n
=
size
;
*/
adj
.
resize
(
n
);
void
graph
::
addEdge
(
int
u
,
int
v
)
{
side
.
resize
(
n
,
-
1
);
adj
[
u
-
1
].
push_back
(
v
-
1
);
}
adj
[
v
-
1
].
push_back
(
u
-
1
);
}
void
addEdge
(
int
u
,
int
v
);
/// function to add edges to our graph
/**
* @brief function that checks whether the graph is bipartite or not
bool
is_bipartite
();
/// function to check 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
};
*
/**
* @details
* @brief Function that add an edge between two nodes or vertices of graph
* Here, side refers to the two disjoint subsets of the bipartite graph.
*
* Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph.
* @param u is a node or vertex of graph
* If the current edge has no side assigned to it, then a Breadth First Search operation is performed.
* @param v is a node or vertex of graph
* If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false.
*/
* If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite.
void
Graph
::
addEdge
(
int
u
,
int
v
)
{
*
adj
[
u
-
1
].
push_back
(
v
-
1
);
*/
adj
[
v
-
1
].
push_back
(
u
-
1
);
bool
graph
::
is_bipartite
(){
}
bool
check
=
true
;
/**
std
::
queue
<
int
>
q
;
* @brief function that checks whether the graph is bipartite or not
for
(
int
current_edge
=
0
;
current_edge
<
n
;
++
current_edge
)
* the function returns true if the graph is a bipartite graph
{
* the function returns false if the graph is not a bipartite graph
if
(
side
[
current_edge
]
==
-
1
){
*
q
.
push
(
current_edge
);
* @details
side
[
current_edge
]
=
0
;
* Here, side refers to the two disjoint subsets of the bipartite graph.
while
(
q
.
size
()){
* Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph.
int
current
=
q
.
front
();
* If the current edge has no side assigned to it, then a Breadth First Search operation is performed.
q
.
pop
();
* If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false.
for
(
auto
neighbour
:
adj
[
current
]){
* If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite.
if
(
side
[
neighbour
]
==
-
1
){
*
side
[
neighbour
]
=
(
1
^
side
[
current
]);
*/
q
.
push
(
neighbour
);
bool
Graph
::
is_bipartite
(){
}
bool
check
=
true
;
else
{
std
::
queue
<
int
>
q
;
check
&=
(
side
[
neighbour
]
!=
side
[
current
]);
for
(
int
current_edge
=
0
;
current_edge
<
n
;
++
current_edge
)
{
if
(
side
[
current_edge
]
==
-
1
){
q
.
push
(
current_edge
);
side
[
current_edge
]
=
0
;
while
(
q
.
size
()){
int
current
=
q
.
front
();
q
.
pop
();
for
(
auto
neighbour
:
adj
[
current
]){
if
(
side
[
neighbour
]
==
-
1
){
side
[
neighbour
]
=
(
1
^
side
[
current
]);
q
.
push
(
neighbour
);
}
else
{
check
&=
(
side
[
neighbour
]
!=
side
[
current
]);
}
}
}
}
}
}
}
}
return
check
;
}
}
}
}
/// namespace is_graph_bipartite
return
check
;
}
/// namespace graph
}
/**
/**
* Function to test the above algorithm
* Function to test the above algorithm
* @returns none
*/
*/
void
test
(){
static
void
test
(){
graph
G1
(
5
);
/// creating graph G1 with 5 vertices
graph
::
is_graph_bipartite
::
Graph
G1
(
5
);
/// creating graph G1 with 5 vertices
/// adding edges to the graphs as per the illustrated example
/// adding edges to the graphs as per the illustrated example
G1
.
addEdge
(
1
,
2
);
G1
.
addEdge
(
1
,
2
);
G1
.
addEdge
(
1
,
3
);
G1
.
addEdge
(
1
,
3
);
G1
.
addEdge
(
3
,
4
);
G1
.
addEdge
(
3
,
4
);
G1
.
addEdge
(
4
,
5
);
G1
.
addEdge
(
4
,
5
);
graph
G2
(
3
);
/// creating graph G2 with 3 vertices
graph
::
is_graph_bipartite
::
Graph
G2
(
3
);
/// creating graph G2 with 3 vertices
/// adding edges to the graphs as per the illustrated example
/// adding edges to the graphs as per the illustrated example
G2
.
addEdge
(
1
,
2
);
G2
.
addEdge
(
1
,
2
);
G2
.
addEdge
(
1
,
3
);
G2
.
addEdge
(
1
,
3
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录