diff --git a/graph/articulations_bridges.cpp b/graph/articulations_bridges.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a88d707e7f5df0d04c8d9e242a08813790a55ddd --- /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 5a7179a325d1d7f79ffde88dcf1413c8b363a32b..e57da24dc01301b5923384ea4e69a91483a99c67 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 8748a19a6e8a4fc0748d15deefc74e499f113c04..46373f84b92b225b26717b358a98eeb16f8b8ebb 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 86a350c1cac6e1537827525e27a7cd97878e67c7..fcaee37ceb60d0af505b012426617e4439708188 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 79f835dc898f4a580b9f547ae59b0679babbdbf1..e66190c184a9ae10dc52ed4305098c195782fb9b 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