8000 modernize connected component code + add description · cp-algorithms/cp-algorithms@a4d6447 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit a4d6447

Browse files
committed
modernize connected component code + add description
1 parent 0b045d9 commit a4d6447

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

src/graph/search-for-connected-components.md

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,53 @@ Given an undirected graph $G$ with $n$ nodes and $m$ edges. We are required to f
2020

2121
``` cpp
2222
int n;
23-
vector<int> g[MAXN] ;
24-
bool used[MAXN] ;
25-
vector<int> comp ;
23+
vector<vector<int>> adj;
24+
vector<bool> used;
25+
vector<int> comp;
2626

2727
void dfs(int v) {
2828
used[v] = true ;
2929
comp.push_back(v);
30-
for (size_t i = 0; i < (int) g[v].size(); ++i) {
31-
int to = g[v][i];
32-
if (!used[to])
33-
dfs(to);
30+
for (int u : adj[v]) {
31+
if (!used[u])
32+
dfs(u);
3433
}
3534
}
3635

3736
void find_comps() {
38-
for (int i = 0; i < n ; ++i)
39-
used [i] = false;
40-
for (int i = 0; i < n ; ++i)
37+
fill(used.begin(), used.end(), 0);
38+
for (int v = 0; v < n; ++v) {
4139
if (!used[i]) {
4240
comp.clear();
4341
dfs(i);
44-
cout << "Component:" ;
45-
for (size_t j = 0; j < comp.size(); ++j)
46-
cout << ' ' << comp[j];
42+
cout << "Component:" ;
43+
for (int u : comp)
44+
cout << ' ' << u;
4745
cout << endl ;
4846
}
47+
}
4948
}
5049
```
5150
51+
* The most important function that is used is `find_comps()` which finds and displays connected components of the graph.
52+
53+
* The graph is stored in adjacency list representation, i.e `adj[v]` contains a list of vertices that have edges from the vertex `v`.
54+
55+
* Vector `comp` contains a list of nodes in the current connected component.
56+
5257
## Iterative implementation of the code
53-
``` cpp
58+
59+
Deeply recursive functions are in general bad.
60+
Ever single recursive call will require a little bit of memory in the stack, and per default programs only have a limited amount of stack space.
61+
So when you do a recursive DFS over a connected graph with millions of nodes, you might run into stack overflows.
62+
63+
It is always possible to translate a recursive program into an iterative program, by manually maintaining a stack data structure.
64+
Since this data structure is allocated on the heap, no stack overflow will occur.
65+
66+
```cpp
5467
int n;
55-
vector<int> g[MAXN];
56-
bool used[MAXN];
68+
vector<vector<int>> adj;
69+
vector<bool> used;
5770
vector<int> comp;
5871
5972
void dfs(int v) {
@@ -66,38 +79,28 @@ void dfs(int v) {
6679
if (!used[curr]) {
6780
used[curr] = true;
6881
comp.push_back(curr);
69-
for (int i = g[curr].size() - 1; i >= 0; i--) {
70-
int to = g[curr][i];
71-
st.push(to);
82+
for (int i = adj[curr].size() - 1; i >= 0; i--) {
83+
st.push(adj[curr][i]);
7284
}
7385
}
7486
}
7587
}
7688
7789
void find_comps() {
78-
for (int i = 0; i < n ; ++i)
79-
used [i] = false;
80-
for (int i = 0; i < n ; ++i)
81-
if (!used[i]) {
90+
fill(used.begin(), used.end(), 0);
91+
for (int v = 0; v < n ; ++v) {
92+
if (!used[v]) {
8293
comp.clear();
83-
dfs(i);
94+
dfs(v);
8495
cout << "Component:" ;
85-
for (size_t j = 0; j < comp.size(); ++j)
86-
cout << ' ' << comp[j];
96+
for (int u : comp)
97+
cout << ' ' << u;
8798
cout << endl ;
8899
}
100+
}
89101
}
90-
91102
```
92103

93-
94-
95-
* The most important function that is used is `find_comps()` which finds and displays connected components of the graph.
96-
97-
* The graph is stored in adjacency list representation, i.e `g[i]` contains a list of vertices that have edges from the vertex `i`. The constant `MAXN` should be set equal to the maximum possible number of vertices in the graph.
98-
99-
* Vector `comp` contains a list of nodes in the current connected component.
100-
101104
## Practice Problems
102105
- [SPOJ: CCOMPS](http://www.spoj.com/problems/CCOMPS/)
103106
- [SPOJ: CT23E](http://www.spoj.com/problems/CT23E/)

0 commit comments

Comments
 (0)
0