Skip to content

Commit

Permalink
🧑‍💻 makes the 2-edge connected component accept 'complex' graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Mar 7, 2024
1 parent 9870ecc commit 5c4d880
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
32 changes: 24 additions & 8 deletions algorithms/graphs/find-bridge-tree-components.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
const int maxn(3'00'000);
int tin[maxn], low[maxn], comp[maxn], qtdcomps, clk;
vi g[maxn], stck;
int n;
const int MAXN(3'00'000);
vi g[MAXN], vi stck;
int tin[MAXN], low[MAXN], comp[MAXN], qtdcomps, clk;
void dfsb(int u, int p) {
void dfs(int u, int p = -1) {
low[u] = tin[u] = ++clk;
stck.emplace_back(u);

bool flagParent = false;
for (auto v : g[u]) {
if (!tin[v]) {
dfsb(v, u);
dfs(v, u);
low[u] = min(low[u], low[v]);
} else if (v != p) {
low[u] = min(low[u], tin[v]);
} else {
if (v != p)
low[u] = min(low[u], tin[v]);
else {
if (flagParent)
low[u] = min(low[u], tin[v]);
else
flagParent = true;
}
}
}

if (low[u] == tin[u]) {
qtdcomps++;
int v2;
do {
v2 = stck.back();
comp[v2] = qtdcomps;
stck.pop_back();
} while (v2 != u);
qtdcomps++;
}
}

void label2CC() {
memset(comp, -1, sizeof(int) * n);

for (int i = 0; i < n; i++) {
if (comp[i] == -1) dfs(i);
}
}
5 changes: 4 additions & 1 deletion algorithms/graphs/find-bridge-tree-components.tex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
\subsection{Find Bridge Tree Components}

$dfsb(u, p)$ finds the component of the connected coponent of u.
$label2CC(u, p)$ finds the 2-edge connected component of every node.

notes: 0 indexed, it also works with not simple graphs.


time: $O(n + m)$

0 comments on commit 5c4d880

Please sign in to comment.