Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
79fb528d
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 搜索 >>
未验证
提交
79fb528d
编写于
8月 27, 2020
作者:
F
Filip Hlasek
提交者:
GitHub
8月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: clang-format for graph/ (#1056)
* fix: clang-format for graph/ * remove graph.h
上级
3239fcc1
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
67 addition
and
60 deletion
+67
-60
graph/CMakeLists.txt
graph/CMakeLists.txt
+5
-5
graph/breadth_first_search.cpp
graph/breadth_first_search.cpp
+4
-3
graph/bridge_finding_with_tarjan_algorithm.cpp
graph/bridge_finding_with_tarjan_algorithm.cpp
+5
-4
graph/depth_first_search_with_stack.cpp
graph/depth_first_search_with_stack.cpp
+3
-3
graph/kosaraju.cpp
graph/kosaraju.cpp
+9
-8
graph/kruskal.cpp
graph/kruskal.cpp
+3
-3
graph/lowest_common_ancestor.cpp
graph/lowest_common_ancestor.cpp
+22
-23
graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp
graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp
+5
-3
graph/prim.cpp
graph/prim.cpp
+2
-2
graph/topological_sort.cpp
graph/topological_sort.cpp
+5
-3
graph/topological_sort_by_kahns_algo.cpp
graph/topological_sort_by_kahns_algo.cpp
+4
-3
未找到文件。
graph/CMakeLists.txt
浏览文件 @
79fb528d
#
If necessary, use the RELATIVE flag, otherwise each source file may be listed
#
with full pathname.
RELATIVE may makes it easier to extract an executable name
#
automatically.
file
(
GLOB APP_SOURCES RELATIVE
${
CMAKE_CURRENT_SOURCE_DIR
}
*.cpp
)
#
file(
GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
#If necessary, use the RELATIVE flag, otherwise each source file may be listed
#
with full pathname.
RELATIVE may makes it easier to extract an executable name
#automatically.
file
(
GLOB APP_SOURCES RELATIVE
${
CMAKE_CURRENT_SOURCE_DIR
}
*.cpp
)
#
file(
GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach
(
testsourcefile
${
APP_SOURCES
}
)
# I used a simple string replace, to cut off .cpp.
...
...
graph/breadth_first_search.cpp
浏览文件 @
79fb528d
...
...
@@ -89,11 +89,12 @@ void add_undirected_edge(std::vector<std::vector<int>> *graph, int u, int v) {
*
* @param graph Adjacency list representation of graph
* @param start vertex from where traversing starts
* @returns a binary vector indicating which vertices were visited during the search.
* @returns a binary vector indicating which vertices were visited during the
* search.
*
*/
std
::
vector
<
bool
>
breadth_first_search
(
const
std
::
vector
<
std
::
vector
<
int
>>
&
graph
,
int
start
)
{
std
::
vector
<
bool
>
breadth_first_search
(
const
std
::
vector
<
std
::
vector
<
int
>>
&
graph
,
int
start
)
{
/// vector to keep track of visited vertices
std
::
vector
<
bool
>
visited
(
graph
.
size
(),
false
);
/// a queue that stores vertices that need to be further explored
...
...
graph/bridge_finding_with_tarjan_algorithm.cpp
浏览文件 @
79fb528d
...
...
@@ -27,14 +27,14 @@ class Solution {
bridge
.
push_back
({
itr
,
current_node
});
}
}
out_time
[
current_node
]
=
std
::
min
(
out_time
[
current_node
],
out_time
[
itr
]);
out_time
[
current_node
]
=
std
::
min
(
out_time
[
current_node
],
out_time
[
itr
]);
}
}
public:
std
::
vector
<
std
::
vector
<
int
>>
search_bridges
(
int
n
,
const
std
::
vector
<
std
::
vector
<
int
>>&
connections
)
{
int
n
,
const
std
::
vector
<
std
::
vector
<
int
>>&
connections
)
{
timer
=
0
;
graph
.
resize
(
n
);
in_time
.
assign
(
n
,
0
);
...
...
@@ -73,7 +73,8 @@ int main() {
* I assumed that the graph is bi-directional and connected.
*
*/
std
::
vector
<
std
::
vector
<
int
>>
bridges
=
s1
.
search_bridges
(
number_of_node
,
node
);
std
::
vector
<
std
::
vector
<
int
>>
bridges
=
s1
.
search_bridges
(
number_of_node
,
node
);
std
::
cout
<<
bridges
.
size
()
<<
" bridges found!
\n
"
;
for
(
auto
&
itr
:
bridges
)
{
std
::
cout
<<
itr
[
0
]
<<
" --> "
<<
itr
[
1
]
<<
'\n'
;
...
...
graph/depth_first_search_with_stack.cpp
浏览文件 @
79fb528d
#include <iostream>
#include <list>
#include <vector>
#include <stack>
#include <vector>
constexpr
int
WHITE
=
0
;
constexpr
int
GREY
=
1
;
constexpr
int
BLACK
=
2
;
constexpr
int
INF
=
99999
;
void
dfs
(
const
std
::
vector
<
std
::
list
<
int
>
>
&
graph
,
int
start
)
{
void
dfs
(
const
std
::
vector
<
std
::
list
<
int
>
>
&
graph
,
int
start
)
{
std
::
vector
<
int
>
checked
(
graph
.
size
(),
WHITE
);
checked
[
start
]
=
GREY
;
std
::
stack
<
int
>
stack
;
...
...
@@ -33,7 +33,7 @@ void dfs(const std::vector< std::list<int> > &graph, int start) {
int
main
()
{
int
n
=
0
;
std
::
cin
>>
n
;
std
::
vector
<
std
::
list
<
int
>
>
graph
(
INF
);
std
::
vector
<
std
::
list
<
int
>
>
graph
(
INF
);
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
int
u
=
0
,
w
=
0
;
std
::
cin
>>
u
>>
w
;
...
...
graph/kosaraju.cpp
浏览文件 @
79fb528d
...
...
@@ -3,9 +3,8 @@
*/
#include <iostream>
#include <vector>
#include <stack>
#include <vector>
/**
* Iterative function/method to print graph:
...
...
@@ -13,7 +12,7 @@
* @param V number of vertices
* @return void
**/
void
print
(
const
std
::
vector
<
std
::
vector
<
int
>
>
&
a
,
int
V
)
{
void
print
(
const
std
::
vector
<
std
::
vector
<
int
>
>
&
a
,
int
V
)
{
for
(
int
i
=
0
;
i
<
V
;
i
++
)
{
if
(
!
a
[
i
].
empty
())
{
std
::
cout
<<
"i="
<<
i
<<
"-->"
;
...
...
@@ -35,7 +34,8 @@ void print(const std::vector< std::vector<int> > &a, int V) {
* @param adj adjacency list representation of the graph
* @return void
**/
void
push_vertex
(
int
v
,
std
::
stack
<
int
>
*
st
,
std
::
vector
<
bool
>
*
vis
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
void
push_vertex
(
int
v
,
std
::
stack
<
int
>
*
st
,
std
::
vector
<
bool
>
*
vis
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
(
*
vis
)[
v
]
=
true
;
for
(
auto
i
=
adj
[
v
].
begin
();
i
!=
adj
[
v
].
end
();
i
++
)
{
if
((
*
vis
)[
*
i
]
==
false
)
{
...
...
@@ -52,7 +52,8 @@ void push_vertex(int v, std::stack<int> *st, std::vector<bool> *vis, const std::
* @param grev graph with reversed edges
* @return void
**/
void
dfs
(
int
v
,
std
::
vector
<
bool
>
*
vis
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
grev
)
{
void
dfs
(
int
v
,
std
::
vector
<
bool
>
*
vis
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
grev
)
{
(
*
vis
)[
v
]
=
true
;
// cout<<v<<" ";
for
(
auto
i
=
grev
[
v
].
begin
();
i
!=
grev
[
v
].
end
();
i
++
)
{
...
...
@@ -72,7 +73,7 @@ no SCCs i.e. none(0) or there will be x no. of SCCs (x>0)) i.e. it returns the
count of (number of) strongly connected components (SCCs) in the graph.
(variable 'count_scc' within function)
**/
int
kosaraju
(
int
V
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
int
kosaraju
(
int
V
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
std
::
vector
<
bool
>
vis
(
V
,
false
);
std
::
stack
<
int
>
st
;
for
(
int
v
=
0
;
v
<
V
;
v
++
)
{
...
...
@@ -81,7 +82,7 @@ int kosaraju(int V, const std::vector< std::vector<int> > &adj) {
}
}
// making new graph (grev) with reverse edges as in adj[]:
std
::
vector
<
std
::
vector
<
int
>
>
grev
(
V
);
std
::
vector
<
std
::
vector
<
int
>
>
grev
(
V
);
for
(
int
i
=
0
;
i
<
V
+
1
;
i
++
)
{
for
(
auto
j
=
adj
[
i
].
begin
();
j
!=
adj
[
i
].
end
();
j
++
)
{
grev
[
*
j
].
push_back
(
i
);
...
...
@@ -114,7 +115,7 @@ int main() {
int
a
=
0
,
b
=
0
;
// a->number of nodes, b->directed edges.
std
::
cin
>>
a
>>
b
;
int
m
=
0
,
n
=
0
;
std
::
vector
<
std
::
vector
<
int
>
>
adj
(
a
+
1
);
std
::
vector
<
std
::
vector
<
int
>
>
adj
(
a
+
1
);
for
(
int
i
=
0
;
i
<
b
;
i
++
)
// take total b inputs of 2 vertices each
// required to form an edge.
{
...
...
graph/kruskal.cpp
浏览文件 @
79fb528d
#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
//#include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
const
int
mx
=
1e6
+
5
;
...
...
@@ -12,7 +12,7 @@ ll node, edge;
std
::
vector
<
std
::
pair
<
ll
,
std
::
pair
<
ll
,
ll
>>>
edges
;
void
initial
()
{
for
(
int
i
=
0
;
i
<
node
+
edge
;
++
i
)
{
parent
[
i
]
=
i
;
parent
[
i
]
=
i
;
}
}
...
...
graph/lowest_common_ancestor.cpp
浏览文件 @
79fb528d
...
...
@@ -9,7 +9,8 @@
* Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html
*
* Complexity:
* - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices in the tree
* - Precomputation: \f$O(N \log N)\f$ where \f$N\f$ is the number of vertices
* in the tree
* - Query: \f$O(\log N)\f$
* - Space: \f$O(N \log N)\f$
*
...
...
@@ -34,11 +35,11 @@
* lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x)
*/
#include <cassert>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#include <queue>
#include <cassert>
/**
* \namespace graph
...
...
@@ -50,7 +51,7 @@ namespace graph {
* Its vertices are indexed 0, 1, ..., N - 1.
*/
class
Graph
{
public:
public:
/**
* \brief Populate the adjacency list for each vertex in the graph.
* Assumes that evey edge is a pair of valid vertex indices.
...
...
@@ -58,7 +59,7 @@ class Graph {
* @param N number of vertices in the graph
* @param undirected_edges list of graph's undirected edges
*/
Graph
(
size_t
N
,
const
std
::
vector
<
std
::
pair
<
int
,
int
>
>
&
undirected_edges
)
{
Graph
(
size_t
N
,
const
std
::
vector
<
std
::
pair
<
int
,
int
>
>
&
undirected_edges
)
{
neighbors
.
resize
(
N
);
for
(
auto
&
edge
:
undirected_edges
)
{
neighbors
[
edge
.
first
].
push_back
(
edge
.
second
);
...
...
@@ -70,19 +71,18 @@ class Graph {
* Function to get the number of vertices in the graph
* @return the number of vertices in the graph.
*/
int
number_of_vertices
()
const
{
return
neighbors
.
size
();
}
int
number_of_vertices
()
const
{
return
neighbors
.
size
();
}
/** \brief for each vertex it stores a list indicies of its neighbors */
std
::
vector
<
std
::
vector
<
int
>
>
neighbors
;
std
::
vector
<
std
::
vector
<
int
>
>
neighbors
;
};
/**
* Representation of a rooted tree. For every vertex its parent is precalculated.
* Representation of a rooted tree. For every vertex its parent is
* precalculated.
*/
class
RootedTree
:
public
Graph
{
public:
public:
/**
* \brief Constructs the tree by calculating parent for every vertex.
* Assumes a valid description of a tree is provided.
...
...
@@ -90,7 +90,8 @@ class RootedTree : public Graph {
* @param undirected_edges list of graph's undirected edges
* @param root_ index of the root vertex
*/
RootedTree
(
const
std
::
vector
<
std
::
pair
<
int
,
int
>
>
&
undirected_edges
,
int
root_
)
RootedTree
(
const
std
::
vector
<
std
::
pair
<
int
,
int
>
>
&
undirected_edges
,
int
root_
)
:
Graph
(
undirected_edges
.
size
()
+
1
,
undirected_edges
),
root
(
root_
)
{
populate_parents
();
}
...
...
@@ -106,7 +107,7 @@ class RootedTree : public Graph {
/** \brief Index of the root vertex. */
int
root
;
protected:
protected:
/**
* \brief Calculate the parents for all the vertices in the tree.
* Implements the breadth first search algorithm starting from the root
...
...
@@ -135,7 +136,6 @@ class RootedTree : public Graph {
}
}
}
};
/**
...
...
@@ -143,13 +143,13 @@ class RootedTree : public Graph {
* queries of the lowest common ancestor of two given vertices in the tree.
*/
class
LowestCommonAncestor
{
public:
public:
/**
* \brief Stores the tree and precomputs "up lifts".
* @param tree_ rooted tree.
*/
explicit
LowestCommonAncestor
(
const
RootedTree
&
tree_
)
:
tree
(
tree_
)
{
populate_up
();
explicit
LowestCommonAncestor
(
const
RootedTree
&
tree_
)
:
tree
(
tree_
)
{
populate_up
();
}
/**
...
...
@@ -196,16 +196,16 @@ class LowestCommonAncestor {
}
/* \brief reference to the rooted tree this structure allows to query */
const
RootedTree
&
tree
;
const
RootedTree
&
tree
;
/**
* \brief for every vertex stores a list of its ancestors by powers of two
* For each vertex, the first element of the corresponding list contains
* the index of its parent. The i-th element of the list is an index of
* the (2^i)-th ancestor of the vertex.
*/
std
::
vector
<
std
::
vector
<
int
>
>
up
;
std
::
vector
<
std
::
vector
<
int
>
>
up
;
protected:
protected:
/**
* Populate the "up" structure. See above.
*/
...
...
@@ -241,9 +241,8 @@ static void tests() {
* |
* 9
*/
std
::
vector
<
std
::
pair
<
int
,
int
>
>
edges
=
{
{
7
,
1
},
{
1
,
5
},
{
1
,
3
},
{
3
,
6
},
{
6
,
2
},
{
2
,
9
},
{
6
,
8
},
{
4
,
3
},
{
0
,
4
}
};
std
::
vector
<
std
::
pair
<
int
,
int
>
>
edges
=
{
{
7
,
1
},
{
1
,
5
},
{
1
,
3
},
{
3
,
6
},
{
6
,
2
},
{
2
,
9
},
{
6
,
8
},
{
4
,
3
},
{
0
,
4
}};
graph
::
RootedTree
t
(
edges
,
3
);
graph
::
LowestCommonAncestor
lca
(
t
);
assert
(
lca
.
lowest_common_ancestor
(
7
,
4
)
==
3
);
...
...
graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp
浏览文件 @
79fb528d
...
...
@@ -16,7 +16,7 @@
// std::max capacity of node in graph
const
int
MAXN
=
505
;
class
Graph
{
std
::
vector
<
std
::
vector
<
int
>
>
residual_capacity
,
capacity
;
std
::
vector
<
std
::
vector
<
int
>
>
residual_capacity
,
capacity
;
int
total_nodes
=
0
;
int
total_edges
=
0
,
source
=
0
,
sink
=
0
;
std
::
vector
<
int
>
parent
;
...
...
@@ -50,8 +50,10 @@ class Graph {
void
set_graph
()
{
std
::
cin
>>
total_nodes
>>
total_edges
>>
source
>>
sink
;
parent
=
std
::
vector
<
int
>
(
total_nodes
,
-
1
);
capacity
=
residual_capacity
=
std
::
vector
<
std
::
vector
<
int
>
>
(
total_nodes
,
std
::
vector
<
int
>
(
total_nodes
));
for
(
int
start
=
0
,
destination
=
0
,
capacity_
=
0
,
i
=
0
;
i
<
total_edges
;
++
i
)
{
capacity
=
residual_capacity
=
std
::
vector
<
std
::
vector
<
int
>
>
(
total_nodes
,
std
::
vector
<
int
>
(
total_nodes
));
for
(
int
start
=
0
,
destination
=
0
,
capacity_
=
0
,
i
=
0
;
i
<
total_edges
;
++
i
)
{
std
::
cin
>>
start
>>
destination
>>
capacity_
;
residual_capacity
[
start
][
destination
]
=
capacity_
;
capacity
[
start
][
destination
]
=
capacity_
;
...
...
graph/prim.cpp
浏览文件 @
79fb528d
...
...
@@ -5,7 +5,7 @@
using
PII
=
std
::
pair
<
int
,
int
>
;
int
prim
(
int
x
,
const
std
::
vector
<
std
::
vector
<
PII
>
>
&
graph
)
{
int
prim
(
int
x
,
const
std
::
vector
<
std
::
vector
<
PII
>
>
&
graph
)
{
// priority queue to maintain edges with respect to weights
std
::
priority_queue
<
PII
,
std
::
vector
<
PII
>
,
std
::
greater
<
PII
>
>
Q
;
std
::
vector
<
bool
>
marked
(
graph
.
size
(),
false
);
...
...
@@ -40,7 +40,7 @@ int main() {
return
0
;
}
std
::
vector
<
std
::
vector
<
PII
>
>
graph
(
nodes
);
std
::
vector
<
std
::
vector
<
PII
>
>
graph
(
nodes
);
// Edges with their nodes & weight
for
(
int
i
=
0
;
i
<
edges
;
++
i
)
{
...
...
graph/topological_sort.cpp
浏览文件 @
79fb528d
...
...
@@ -2,7 +2,8 @@
#include <iostream>
#include <vector>
int
number_of_vertices
,
number_of_edges
;
// For number of Vertices (V) and number of edges (E)
int
number_of_vertices
,
number_of_edges
;
// For number of Vertices (V) and number of edges (E)
std
::
vector
<
std
::
vector
<
int
>>
graph
;
std
::
vector
<
bool
>
visited
;
std
::
vector
<
int
>
topological_order
;
...
...
@@ -28,7 +29,8 @@ void topological_sort() {
reverse
(
topological_order
.
begin
(),
topological_order
.
end
());
}
int
main
()
{
std
::
cout
<<
"Enter the number of vertices and the number of directed edges
\n
"
;
std
::
cout
<<
"Enter the number of vertices and the number of directed edges
\n
"
;
std
::
cin
>>
number_of_vertices
>>
number_of_edges
;
int
x
=
0
,
y
=
0
;
graph
.
resize
(
number_of_vertices
,
std
::
vector
<
int
>
());
...
...
@@ -41,7 +43,7 @@ int main() {
std
::
cout
<<
"Topological Order :
\n
"
;
for
(
int
v
:
topological_order
)
{
std
::
cout
<<
v
+
1
<<
' '
;
// converting zero based indexing back to one based.
<<
' '
;
// converting zero based indexing back to one based.
}
std
::
cout
<<
'\n'
;
return
0
;
...
...
graph/topological_sort_by_kahns_algo.cpp
浏览文件 @
79fb528d
...
...
@@ -4,7 +4,7 @@
#include <queue>
#include <vector>
std
::
vector
<
int
>
topoSortKahn
(
int
N
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
);
std
::
vector
<
int
>
topoSortKahn
(
int
N
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
);
int
main
()
{
int
nodes
=
0
,
edges
=
0
;
...
...
@@ -14,7 +14,7 @@ int main() {
}
int
u
=
0
,
v
=
0
;
std
::
vector
<
std
::
vector
<
int
>
>
graph
(
nodes
);
std
::
vector
<
std
::
vector
<
int
>
>
graph
(
nodes
);
// create graph
// example
// 6 6
...
...
@@ -32,7 +32,8 @@ int main() {
}
}
std
::
vector
<
int
>
topoSortKahn
(
int
V
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
std
::
vector
<
int
>
topoSortKahn
(
int
V
,
const
std
::
vector
<
std
::
vector
<
int
>
>
&
adj
)
{
std
::
vector
<
bool
>
vis
(
V
+
1
,
false
);
std
::
vector
<
int
>
deg
(
V
+
1
,
0
);
for
(
int
i
=
0
;
i
<
V
;
i
++
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录