From e8e9f941ac106b29ba4b84fa47a45f9ccaf17fbc Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Thu, 8 Feb 2018 14:46:07 -0200 Subject: [PATCH] Add lowpoint (articulations_bridges) Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- graph/articulations_bridges.cpp | 38 +++++++++++++++++++++++++++++++++ graph/bfs.cpp | 4 ++-- graph/dfs.cpp | 4 ++-- graph/dijkstra.cpp | 4 ++-- graph/lca.cpp | 1 - 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 graph/articulations_bridges.cpp diff --git a/graph/articulations_bridges.cpp b/graph/articulations_bridges.cpp new file mode 100644 index 0000000..a88d707 --- /dev/null +++ b/graph/articulations_bridges.cpp @@ -0,0 +1,38 @@ +/** + * Articulations and Bridges + * + * Complexity (Time): O(n + m) + * Complexity (Space): O(n + m) + */ + +vector<int> graph[MAX]; + +int cont[MAX], parent[MAX]; +int low[MAX], L[MAX]; + +// Find all articulations and bridges in the graph +void dfs(int x) { + int child = 0; + cont[x] = 1; + + for (auto i : graph[x]) { + if (!cont[i]) { + child++; + parent[i] = x; + + low[i] = L[i] = L[x] + 1; + dfs(i); + low[x] = min(low[x], low[i]); + + if ((parent[x] == -1 && child > 1) || (parent[x] != -1 && low[i] >= L[x])) + // CAUTION: may be executed more than once for the same vertex + // ==== x is an articulation point ==== + + if (low[i] > L[x]) + // ==== (x,i) is a bridge ==== + + } else if (parent[x] != i) { + low[x] = min(low[x], L[i]); + } + } +} diff --git a/graph/bfs.cpp b/graph/bfs.cpp index 5a7179a..e57da24 100644 --- a/graph/bfs.cpp +++ b/graph/bfs.cpp @@ -1,8 +1,8 @@ /** * Breadth First Search - BFS * - * Complexity (Time): O(|V| + |E|) - * Complexity (Space): O(|V| + |E|) + * Complexity (Time): O(n + m) + * Complexity (Space): O(n + m) */ bool cont[MAX]; diff --git a/graph/dfs.cpp b/graph/dfs.cpp index 8748a19..46373f8 100644 --- a/graph/dfs.cpp +++ b/graph/dfs.cpp @@ -1,8 +1,8 @@ /** * Depth First Search - DFS * - * Complexity (Time): O(|V| + |E|) - * Complexity (Space): O(|V| + |E|) + * Complexity (Time): O(n + m) + * Complexity (Space): O(n + m) */ bool cont[MAX]; diff --git a/graph/dijkstra.cpp b/graph/dijkstra.cpp index 86a350c..fcaee37 100644 --- a/graph/dijkstra.cpp +++ b/graph/dijkstra.cpp @@ -1,8 +1,8 @@ /** * Dijkstra * - * Complexity (Time): O(|E| + |V| log |V|) - * Complexity (Space): O(|E| + |V|) + * Complexity (Time): O(m + n log n) + * Complexity (Space): O(n + m) */ int dist[MAX]; diff --git a/graph/lca.cpp b/graph/lca.cpp index 79f835d..e66190c 100644 --- a/graph/lca.cpp +++ b/graph/lca.cpp @@ -5,7 +5,6 @@ * preprocess -> O(n log n) * query -> O(log n) * Complexity (Space): O(n + m + n log n) - * * * OBS: * = return sum path to LCA * ** = return max value on path to LCA -- GitLab