Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
5aa074c8
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 搜索 >>
未验证
提交
5aa074c8
编写于
8月 16, 2020
作者:
D
David Leal
提交者:
GitHub
8月 16, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1034 from fhlasek/connected_components
fix: linter for connected_components.
上级
327a4f57
2c41598e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
93 addition
and
67 deletion
+93
-67
graph/connected_components.cpp
graph/connected_components.cpp
+93
-67
未找到文件。
graph/connected_components.cpp
浏览文件 @
5aa074c8
...
...
@@ -15,9 +15,9 @@
* <pre>
* Example - Here is graph with 3 connected components
*
*
3 9 6
8
*
1 4 5
8
* / \ / / \ / \
* 2---
4 2 7 3 7
* 2---
3 6 7 9 10
*
* first second third
* component component component
...
...
@@ -26,97 +26,123 @@
*/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using
std
::
vector
;
/**
* Class for representing graph as a adjacency list.
* @namespace graph
* @brief Graph Algorithms
*/
class
graph
{
private:
/** \brief adj stores adjacency list representation of graph */
vector
<
vector
<
int
>>
adj
;
/** \brief keep track of connected components */
int
connected_components
;
void
depth_first_search
();
void
explore
(
int
,
vector
<
bool
>
&
);
public:
/**
* \brief Constructor that intiliazes the graph on creation and set
* the connected components to 0
*/
explicit
graph
(
int
n
)
:
adj
(
n
,
vector
<
int
>
())
{
connected_components
=
0
;
}
void
addEdge
(
int
,
int
);
/**
* \brief Function the calculates the connected compoents in the graph
* by performing the depth first search on graph
*
* @return connected_components total connected components in graph
*/
int
getConnectedComponents
()
{
depth_first_search
();
return
connected_components
;
}
};
namespace
graph
{
/**
*
\
brief Function that add edge between two nodes or vertices of graph
*
@
brief Function that add edge between two nodes or vertices of graph
*
* @param u any node or vertex of graph
* @param v any node or vertex of graph
* @param adj adjacency list of graph.
* @param u any node or vertex of graph.
* @param v any node or vertex of graph.
*/
void
graph
::
addEdge
(
int
u
,
int
v
)
{
adj
[
u
-
1
].
push_back
(
v
-
1
);
adj
[
v
-
1
].
push_back
(
u
-
1
);
void
addEdge
(
std
::
vector
<
std
::
vector
<
int
>>
*
adj
,
int
u
,
int
v
)
{
(
*
adj
)
[
u
-
1
].
push_back
(
v
-
1
);
(
*
adj
)
[
v
-
1
].
push_back
(
u
-
1
);
}
/**
* \brief Function that perfoms depth first search algorithm on graph
* @brief Utility function for depth first seach algorithm
* this function explores the vertex which is passed into.
*
* @param adj adjacency list of graph.
* @param u vertex or node to be explored.
* @param visited already visited vertices.
*/
void
graph
::
depth_first_search
()
{
int
n
=
adj
.
size
();
vector
<
bool
>
visited
(
n
,
false
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
!
visited
[
i
])
{
explore
(
i
,
visited
);
connected_components
++
;
void
explore
(
const
std
::
vector
<
std
::
vector
<
int
>>
*
adj
,
int
u
,
std
::
vector
<
bool
>
*
visited
)
{
(
*
visited
)[
u
]
=
true
;
for
(
auto
v
:
(
*
adj
)[
u
])
{
if
(
!
(
*
visited
)[
v
])
{
explore
(
adj
,
v
,
visited
);
}
}
}
/**
* \brief Utility function for depth first seach algorithm
* this function explores the vertex which is passed into.
* @brief Function that perfoms depth first search algorithm on graph
* and calculated the number of connected components.
*
* @param adj adjacency list of graph.
*
* @param u vertex or node to be explored
* @param visited already visited vertex
* @return connected_components number of connected components in graph.
*/
void
graph
::
explore
(
int
u
,
vector
<
bool
>
&
visited
)
{
visited
[
u
]
=
true
;
for
(
auto
v
:
adj
[
u
])
{
if
(
!
visited
[
v
])
{
explore
(
v
,
visited
);
int
getConnectedComponents
(
const
std
::
vector
<
std
::
vector
<
int
>>
*
adj
)
{
int
n
=
adj
->
size
();
int
connected_components
=
0
;
std
::
vector
<
bool
>
visited
(
n
,
false
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
!
visited
[
i
])
{
explore
(
adj
,
i
,
&
visited
);
connected_components
++
;
}
}
return
connected_components
;
}
}
// namespace graph
/** Function to test the algorithm */
void
tests
()
{
std
::
cout
<<
"Running predefined tests..."
<<
std
::
endl
;
std
::
cout
<<
"Initiating Test 1..."
<<
std
::
endl
;
std
::
vector
<
std
::
vector
<
int
>>
adj1
(
9
,
std
::
vector
<
int
>
());
graph
::
addEdge
(
&
adj1
,
1
,
2
);
graph
::
addEdge
(
&
adj1
,
1
,
3
);
graph
::
addEdge
(
&
adj1
,
3
,
4
);
graph
::
addEdge
(
&
adj1
,
5
,
7
);
graph
::
addEdge
(
&
adj1
,
5
,
6
);
graph
::
addEdge
(
&
adj1
,
8
,
9
);
assert
(
graph
::
getConnectedComponents
(
&
adj1
)
==
3
);
std
::
cout
<<
"Test 1 Passed..."
<<
std
::
endl
;
std
::
cout
<<
"Innitiating Test 2..."
<<
std
::
endl
;
std
::
vector
<
std
::
vector
<
int
>>
adj2
(
10
,
std
::
vector
<
int
>
());
graph
::
addEdge
(
&
adj2
,
1
,
2
);
graph
::
addEdge
(
&
adj2
,
1
,
3
);
graph
::
addEdge
(
&
adj2
,
1
,
4
);
graph
::
addEdge
(
&
adj2
,
2
,
3
);
graph
::
addEdge
(
&
adj2
,
3
,
4
);
graph
::
addEdge
(
&
adj2
,
4
,
8
);
graph
::
addEdge
(
&
adj2
,
4
,
10
);
graph
::
addEdge
(
&
adj2
,
8
,
10
);
graph
::
addEdge
(
&
adj2
,
8
,
9
);
graph
::
addEdge
(
&
adj2
,
5
,
7
);
graph
::
addEdge
(
&
adj2
,
5
,
6
);
graph
::
addEdge
(
&
adj2
,
6
,
7
);
assert
(
graph
::
getConnectedComponents
(
&
adj2
)
==
2
);
std
::
cout
<<
"Test 2 Passed..."
<<
std
::
endl
;
}
/** Main function */
int
main
()
{
///
creating a graph with 4 vertex
graph
g
(
4
);
///
running predefined tests
tests
(
);
/// Adding edges between vertices
g
.
addEdge
(
1
,
2
);
g
.
addEdge
(
3
,
2
);
int
vertices
=
int
(),
edges
=
int
();
std
::
cout
<<
"Enter the number of vertices : "
;
std
::
cin
>>
vertices
;
std
::
cout
<<
"Enter the number of edges : "
;
std
::
cin
>>
edges
;
std
::
vector
<
std
::
vector
<
int
>>
adj
(
vertices
,
std
::
vector
<
int
>
());
int
u
=
int
(),
v
=
int
();
while
(
edges
--
)
{
std
::
cin
>>
u
>>
v
;
graph
::
addEdge
(
&
adj
,
u
,
v
);
}
/// printing the connected components
std
::
cout
<<
g
.
getConnectedComponents
()
;
int
cc
=
graph
::
getConnectedComponents
(
&
adj
);
std
::
cout
<<
cc
<<
std
::
endl
;
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录