Skip to content

Commit 19f7aa1

Browse files
lrvideckisweb-flow
andauthored
Templatized graph algs (#119)
* saving progress * [auto-verifier] verify commit 31974ee * changes * a fix * fix compile error * fix * more fixes * final fix * remove old readme * add templatized versions of graphs * some more fixes * final fix * more fixes * actually let's not use this * final fixes * some fixes * supress --------- Co-authored-by: GitHub <noreply@github.com>
1 parent c411499 commit 19f7aa1

34 files changed

+127
-222
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
"tests/library_checker_aizu_tests/flow/dinic_lib_checker.test.cpp": "2024-11-17 14:04:03 -0600",
5050
"tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600",
5151
"tests/library_checker_aizu_tests/flow/min_cost_max_flow.test.cpp": "2024-12-05 10:41:42 -0600",
52-
"tests/library_checker_aizu_tests/graphs/biconnected_components.test.cpp": "2024-12-14 15:47:13 -0600",
53-
"tests/library_checker_aizu_tests/graphs/connected_components_of_complement_graph.test.cpp": "2024-12-14 15:47:13 -0600",
54-
"tests/library_checker_aizu_tests/graphs/dijkstra_aizu.test.cpp": "2024-12-14 15:47:13 -0600",
55-
"tests/library_checker_aizu_tests/graphs/dijkstra_lib_checker.test.cpp": "2024-12-14 15:47:13 -0600",
56-
"tests/library_checker_aizu_tests/graphs/directed_cycle.test.cpp": "2024-12-14 15:47:13 -0600",
57-
"tests/library_checker_aizu_tests/graphs/enumerate_triangles.test.cpp": "2024-11-17 14:04:03 -0600",
58-
"tests/library_checker_aizu_tests/graphs/hopcroft_karp_aizu.test.cpp": "2024-12-14 15:47:13 -0600",
59-
"tests/library_checker_aizu_tests/graphs/hopcroft_karp_lib_checker.test.cpp": "2024-12-14 15:47:13 -0600",
6052
"tests/library_checker_aizu_tests/graphs/mst.test.cpp": "2024-11-17 14:04:03 -0600",
6153
"tests/library_checker_aizu_tests/graphs/offline_incremental_scc.test.cpp": "2024-12-14 15:47:13 -0600",
6254
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_aizu.test.cpp": "2024-12-14 15:47:13 -0600",
@@ -68,18 +60,10 @@
6860
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2024-12-14 15:47:13 -0600",
6961
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2024-11-17 14:04:03 -0600",
7062
"tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2024-12-05 10:41:42 -0600",
71-
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-12-14 15:47:13 -0600",
72-
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2024-12-05 10:41:42 -0600",
7363
"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2024-11-19 08:31:51 -0600",
7464
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2024-11-17 14:04:03 -0600",
7565
"tests/library_checker_aizu_tests/handmade_tests/mod_int.test.cpp": "2024-11-17 14:04:03 -0600",
7666
"tests/library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp": "2024-11-17 14:04:03 -0600",
77-
"tests/library_checker_aizu_tests/handmade_tests/permutation_tree_small.test.cpp": "2024-12-14 15:47:13 -0600",
78-
"tests/library_checker_aizu_tests/handmade_tests/rmq_small_n.test.cpp": "2024-12-14 15:47:13 -0600",
79-
"tests/library_checker_aizu_tests/handmade_tests/sa_find_subarray.test.cpp": "2024-12-14 15:47:13 -0600",
80-
"tests/library_checker_aizu_tests/handmade_tests/seg_tree_find.test.cpp": "2024-12-14 15:47:13 -0600",
81-
"tests/library_checker_aizu_tests/handmade_tests/seg_tree_find_small.test.cpp": "2024-12-14 15:47:13 -0600",
82-
"tests/library_checker_aizu_tests/handmade_tests/string_with_vector.test.cpp": "2024-12-14 15:47:13 -0600",
8367
"tests/library_checker_aizu_tests/loops/chooses.test.cpp": "2024-11-17 14:04:03 -0600",
8468
"tests/library_checker_aizu_tests/loops/quotients.test.cpp": "2024-11-17 14:04:03 -0600",
8569
"tests/library_checker_aizu_tests/loops/submasks.test.cpp": "2024-11-17 14:04:03 -0600",
@@ -101,28 +85,6 @@
10185
"tests/library_checker_aizu_tests/math/solve_linear_mod.test.cpp": "2024-12-14 15:47:13 -0600",
10286
"tests/library_checker_aizu_tests/math/tetration.test.cpp": "2024-11-17 14:04:03 -0600",
10387
"tests/library_checker_aizu_tests/math/totient.test.cpp": "2024-11-17 14:04:03 -0600",
104-
"tests/library_checker_aizu_tests/math/xor_basis.test.cpp": "2024-12-14 15:47:13 -0600",
105-
"tests/library_checker_aizu_tests/math/xor_basis_intersection.test.cpp": "2024-12-14 15:47:13 -0600",
106-
"tests/library_checker_aizu_tests/monotonic_stack_related/cartesian_binary_tree.test.cpp": "2024-12-14 15:47:13 -0600",
107-
"tests/library_checker_aizu_tests/monotonic_stack_related/cartesian_k_ary_tree.test.cpp": "2024-12-14 15:47:13 -0600",
108-
"tests/library_checker_aizu_tests/monotonic_stack_related/count_rectangles.test.cpp": "2024-12-14 15:47:13 -0600",
109-
"tests/library_checker_aizu_tests/monotonic_stack_related/max_rect_histogram.test.cpp": "2024-12-14 15:47:13 -0600",
110-
"tests/library_checker_aizu_tests/strings/kmp.test.cpp": "2024-12-05 10:41:42 -0600",
111-
"tests/library_checker_aizu_tests/strings/lcp_array.test.cpp": "2024-12-14 15:47:13 -0600",
112-
"tests/library_checker_aizu_tests/strings/lcp_query_palindrome.test.cpp": "2024-12-14 15:47:13 -0600",
113-
"tests/library_checker_aizu_tests/strings/lcp_query_zfunc.test.cpp": "2024-12-14 15:47:13 -0600",
114-
"tests/library_checker_aizu_tests/strings/lcs_dp.test.cpp": "2024-12-05 10:41:42 -0600",
115-
"tests/library_checker_aizu_tests/strings/lcs_queries.test.cpp": "2024-12-05 10:41:42 -0600",
116-
"tests/library_checker_aizu_tests/strings/lcs_queries_merge_sort_tree.test.cpp": "2024-12-05 10:41:42 -0600",
117-
"tests/library_checker_aizu_tests/strings/longest_common_substring.test.cpp": "2024-12-14 15:47:13 -0600",
118-
"tests/library_checker_aizu_tests/strings/manacher.test.cpp": "2024-11-17 14:04:03 -0600",
119-
"tests/library_checker_aizu_tests/strings/multi_matching_bs.test.cpp": "2024-12-14 15:47:13 -0600",
120-
"tests/library_checker_aizu_tests/strings/prefix_function.test.cpp": "2024-11-17 14:04:03 -0600",
121-
"tests/library_checker_aizu_tests/strings/sa_cmp.test.cpp": "2024-12-14 15:47:13 -0600",
122-
"tests/library_checker_aizu_tests/strings/sa_sort_pairs.test.cpp": "2024-12-14 15:47:13 -0600",
123-
"tests/library_checker_aizu_tests/strings/single_matching_bs.test.cpp": "2024-12-14 15:47:13 -0600",
124-
"tests/library_checker_aizu_tests/strings/suffix_array.test.cpp": "2024-12-14 15:47:13 -0600",
125-
"tests/library_checker_aizu_tests/strings/suffix_array_short.test.cpp": "2024-12-14 15:47:13 -0600",
12688
"tests/library_checker_aizu_tests/strings/trie.test.cpp": "2024-12-05 10:41:42 -0600",
12789
"tests/library_checker_aizu_tests/strings/wildcard_pattern_matching.test.cpp": "2024-11-17 14:04:03 -0600",
12890
"tests/library_checker_aizu_tests/trees/cd_jump_on_tree.test.cpp": "2024-12-14 15:47:13 -0600",

library/graphs/bridges_cuts/block_vertex_tree.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//! vector<vector<pii>> adj(n);
55
//! cuts cc(adj, m);
66
//! vector<vi> bvt = block_vertex_tree(adj, cc);
7+
//! vector<basic_string<array<int, 2>>> adj1(n);
8+
//! cuts cc1(adj1, m);
9+
//! vector<vi> bvt1 = block_vertex_tree(adj1, cc1);
710
//! //to loop over each unique bcc containing a node u:
811
//! for (int bccid : bvt[v]) {
912
//! bccid -= n;
@@ -15,8 +18,9 @@
1518
//! [n, n + num_bccs) are BCC nodes
1619
//! @time O(n + m)
1720
//! @time O(n)
18-
vector<vi> block_vertex_tree(
19-
const vector<vector<pii>>& adj, const cuts& cc) {
21+
template<class G>
22+
vector<vi> block_vertex_tree(const G& adj,
23+
const cuts<G>& cc) {
2024
int n = sz(adj);
2125
vector<vi> bvt(n + cc.num_bccs);
2226
vector<bool> vis(cc.num_bccs);

library/graphs/bridges_cuts/bridge_tree.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
//! vector<vector<pii>> adj(n);
55
//! bridges br(adj, m);
66
//! vector<vi> bt = bridge_tree(adj, br);
7+
//! vector<basic_string<array<int, 2>>> adj1(n);
8+
//! bridges br1(adj1, m);
9+
//! vector<vi> bt1 = bridge_tree(adj1, br1);
710
//! @endcode
811
//! @time O(n + m)
912
//! @space O(n)
10-
vector<vi> bridge_tree(const vector<vector<pii>>& adj,
11-
const bridges& br) {
13+
template<class G>
14+
vector<vi> bridge_tree(const G& adj,
15+
const bridges<G>& br) {
1216
vector<vi> tree(br.num_ccs);
1317
rep(i, 0, sz(adj)) for (auto [u, e_id] : adj[i]) if (
1418
br.is_bridge[e_id]) tree[br.br_id[i]]

library/graphs/bridges_cuts/bridges.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
//! adj[v].emplace_back(u, i);
1111
//! }
1212
//! auto [num_ccs, is_bridge, br_id] = bridges(adj, m);
13+
//! vector<basic_string<array<int,2>>> adj1(n);
14+
//! auto [num_ccs1, is_bridge1, br_id1] = bridges(adj1,
15+
//! m);
1316
//! @endcode
1417
//! is_bridge[edge id] = 1 iff bridge edge
1518
//! br_id[v] = id, 0<=id<num_ccs
1619
//! @time O(n + m)
1720
//! @space O(n + m)
18-
struct bridges {
21+
template<class G> struct bridges {
1922
int num_ccs = 0;
2023
vector<bool> is_bridge;
2124
vi br_id;
22-
bridges(const vector<vector<pii>>& adj, int m):
25+
bridges(const G& adj, int m):
2326
is_bridge(m), br_id(sz(adj), -1) {
2427
int n = sz(adj), timer = 1;
2528
vi tin(n), st;

library/graphs/bridges_cuts/cuts.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
//! adj[v].emplace_back(u, i);
1212
//! }
1313
//! auto [num_bccs, is_cut, bcc_id] = cuts(adj, m);
14+
//! vector<basic_string<array<int,2>>> adj1(n);
15+
//! auto [num_bccs1, is_cut1, bcc_id1] = cuts(adj1, m);
1416
//! @endcode
1517
//! is_cut[v] = 1 iff cut node
1618
//! bcc_id[edge id] = id, 0<=id<num_bccs
1719
//! @time O(n + m)
1820
//! @space O(n + m)
19-
struct cuts {
21+
template<class G> struct cuts {
2022
int num_bccs = 0;
2123
vector<bool> is_cut;
2224
vi bcc_id;
23-
cuts(const vector<vector<pii>>& adj, int m):
25+
cuts(const G& adj, int m):
2426
is_cut(sz(adj)), bcc_id(m, -1) {
2527
int n = sz(adj), timer = 1;
2628
vi tin(n), st;

library/graphs/complement_graph_ccs.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#pragma once
22
//! @code
33
//! vector<vi> adj(n);
4-
//! auto cc_id = get_complement_graph_ccs(adj);
4+
//! vi cc_id = get_complement_graph_ccs(adj);
5+
//! vector<basic_string<int>> adj1;
6+
//! vi cc_id2 = get_complement_graph_ccs(adj1);
57
//! @endcode
68
//! 0<=cc_id[v]<number of connected components
79
//! in the complement graph
810
//! cc_id[u] == cc_id[v] if u,v are in the same
911
//! cc in the compliment graph
1012
//! @time O(n + m)
1113
//! @space O(n)
12-
vi get_complement_graph_ccs(const vector<vi> &adj) {
14+
template<class G>
15+
vi get_complement_graph_ccs(const G& adj) {
1316
int n = sz(adj);
1417
vi cc_id(n), unseen(n);
1518
iota(all(unseen), 0);

library/graphs/dijkstra.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
//! @code
33
//! vector<vector<pair<int, ll>>> adj(n);
44
//! auto d = dijkstra(adj, source);
5+
//! vector<basic_string<array<int, 2>>> adj1(n);
6+
//! auto d1 = dijkstra(adj1, source);
57
//! @endcode
68
//! d[v] = min dist from source->..->v
79
//! @time O(n + (m log m))
810
//! @space O(n + m)
9-
vector<ll> dijkstra(
10-
const vector<vector<pair<int, ll>>>& adj, int s) {
11+
template<class G>
12+
vector<ll> dijkstra(const G& adj, int s) {
1113
using p = pair<ll, int>;
1214
priority_queue<p, vector<p>, greater<>> pq;
1315
pq.emplace(0, s);

library/graphs/hopcroft_karp.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! adj[l].push_back(r); // add edge l <-> r
66
//! auto [matching_size, to_r, to_l,
77
//! mvc_l, mvc_r] = hopcroft_karp(adj, rsz);
8+
//! vector<basic_string<int>> adj1(lsz);
9+
//! hopcroft_karp match(adj1, rsz);
810
//! @endcode
911
//! 0<=l<lsz; 0<=r<rsz
1012
//! l <-> to_r[l] in matching if to_r[l]!=-1
@@ -13,11 +15,11 @@
1315
//! mvc_r[r] is 1 if r in Min Vertex Cover
1416
//! @time O(n + m * sqrt(n)) n = lsz + rsz
1517
//! @space O(n)
16-
struct hopcroft_karp {
18+
template<class G> struct hopcroft_karp {
1719
int m_sz = 0;
1820
vi to_r, to_l;
1921
vector<bool> mvc_l, mvc_r;
20-
hopcroft_karp(const vector<vi>& adj, int rsz):
22+
hopcroft_karp(const G& adj, int rsz):
2123
to_r(sz(adj), -1), to_l(rsz, -1) {
2224
int lsz = sz(adj);
2325
while (1) {

library/graphs/strongly_connected_components/scc.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
//! @code
44
//! vector<vi> adj(n);
55
//! auto [num_sccs, scc_id] = sccs(adj);
6+
//! vector<basic_string<int>> adj1(n);
7+
//! auto [num_sccs1, scc_id1] = sccs(adj1);
68
//! @endcode
79
//! scc_id[v] = id, 0<=id<num_sccs
810
//! for each edge u -> v: scc_id[u] >= scc_id[v]
911
//! @time O(n + m)
1012
//! @space O(n)
11-
struct sccs {
13+
template<class G> struct sccs {
1214
int num_sccs = 0;
1315
vi scc_id;
14-
sccs(const vector<vi>& adj): scc_id(sz(adj), -1) {
16+
sccs(const G& adj): scc_id(sz(adj), -1) {
1517
int n = sz(adj), timer = 1;
1618
vi tin(n), st;
1719
auto dfs = [&](auto&& self, int v) -> int {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#pragma once
22
#include "prefix_function.hpp"
33
//! @code
4+
//! string s,t;
45
//! KMP kmp(t);
56
//! auto match = kmp.find_str(s);
7+
//! vi s_vec,t_vec;
8+
//! KMP kmp1(t_vec);
9+
//! auto match2 = kmp1.find_str(s_vec);
610
//! @endcode
711
//! if match[i] == 1 then s[i,sz(t)) == t
812
//! @time O(|s| + |t|)

0 commit comments

Comments
 (0)