Skip to content

Commit dd0f116

Browse files
committed
Remove documentations
1 parent 1be5abc commit dd0f116

File tree

2 files changed

+59
-90
lines changed

2 files changed

+59
-90
lines changed

cp-algo/graph/tree_diameter.hpp

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,43 @@
77
#include <algorithm>
88

99
namespace cp_algo::graph {
10-
11-
/**
12-
* @brief Finds the diameter of a weighted tree
13-
*
14-
* Returns the maximum distance between any two nodes in the tree.
15-
* Uses XOR DFS to traverse the tree while tracking path distances.
16-
*
17-
* @param g The input weighted tree
18-
* @return Tuple of (diameter_length, starting_node, path_edges)
19-
* The path is represented as a sequence of edge indices from start to end
20-
*
21-
* Time complexity: O(n)
22-
* Space complexity: O(n)
23-
*/
24-
template<weighted_undirected_graph_type graph>
25-
std::tuple<int64_t, node_index, std::basic_string<edge_index>> tree_diameter(graph const& g) {
26-
struct up_path {
27-
int64_t length = 0;
28-
node_index start;
29-
};
30-
std::vector<up_path> up(g.n());
31-
for(auto v: g.nodes()) {
32-
up[v].start = v;
33-
}
34-
up_path s, t;
35-
auto parents = xor_dfs(g, [&](node_index v, edge_index ep) {
36-
if (ep == edge_index(-1)) return;
37-
node_index u = g.edge(ep).traverse(v);
38-
up[v].length += g.edge(ep).w;
39-
if (up[v].length + up[u].length > s.length + t.length) {
40-
s = up[v];
41-
t = up[u];
42-
}
43-
if (up[v].length > up[u].length) {
44-
up[u] = up[v];
10+
template<weighted_undirected_graph_type graph>
11+
std::tuple<int64_t, node_index, std::basic_string<edge_index>> tree_diameter(graph const& g) {
12+
struct up_path {
13+
int64_t length = 0;
14+
node_index start;
15+
};
16+
std::vector<up_path> up(g.n());
17+
for(auto v: g.nodes()) {
18+
up[v].start = v;
4519
}
46-
});
47-
auto collect = [&](up_path v) {
48-
std::basic_string<edge_index> path;
49-
while(v.length) {
50-
edge_index ep = parents[v.start];
51-
path.push_back(ep);
52-
v.length -= g.edge(ep).w;
53-
v.start = g.edge(ep).traverse(v.start);
54-
}
55-
return path;
56-
};
57-
auto paths = collect(s);
58-
auto patht = collect(t);
59-
std::ranges::reverse(patht);
60-
return {s.length + t.length, s.start, paths += patht};
20+
up_path s, t;
21+
auto parents = xor_dfs(g, [&](node_index v, edge_index ep) {
22+
if (ep == edge_index(-1)) return;
23+
node_index u = g.edge(ep).traverse(v);
24+
up[v].length += g.edge(ep).w;
25+
if (up[v].length + up[u].length > s.length + t.length) {
26+
s = up[v];
27+
t = up[u];
28+
}
29+
if (up[v].length > up[u].length) {
30+
up[u] = up[v];
31+
}
32+
});
33+
auto collect = [&](up_path v) {
34+
std::basic_string<edge_index> path;
35+
while(v.length) {
36+
edge_index ep = parents[v.start];
37+
path.push_back(ep);
38+
v.length -= g.edge(ep).w;
39+
v.start = g.edge(ep).traverse(v.start);
40+
}
41+
return path;
42+
};
43+
auto paths = collect(s);
44+
auto patht = collect(t);
45+
std::ranges::reverse(patht);
46+
return {s.length + t.length, s.start, paths += patht};
47+
}
6148
}
62-
63-
} // namespace cp_algo::graph
6449
#endif // CP_ALGO_GRAPH_TREE_DIAMETER_HPP

cp-algo/graph/xor_dfs.hpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,30 @@
44
#include <vector>
55

66
namespace cp_algo::graph {
7-
8-
/**
9-
* @brief XOR DFS on trees - processes leaves to root using XOR trick
10-
*
11-
* Uses XOR to track remaining edges instead of storing parent pointers.
12-
* Calls callback for each leaf being processed, passing the leaf node and its parent edge.
13-
*
14-
* @param tree The input tree (undirected graph)
15-
* @param callback Function called for each leaf with (node, edge) parameters
16-
* @param root Root node (default 0)
17-
* @return Vector where result[v] = XOR of all incident edge indices at node v
18-
*
19-
* Time complexity: O(n)
20-
* Space complexity: O(n)
21-
*/
22-
template<undirected_graph_type graph>
23-
auto xor_dfs(graph const& tree, auto &&callback, node_index root = 0) {
24-
std::vector<int> degree(tree.n());
25-
std::vector<edge_index> neig_xor(tree.n());
26-
for(auto v: tree.nodes()) {
27-
for(auto e: tree.outgoing(v)) {
28-
degree[v]++;
29-
neig_xor[v] ^= e;
7+
template<undirected_graph_type graph>
8+
auto xor_dfs(graph const& tree, auto &&callback, node_index root = 0) {
9+
std::vector<int> degree(tree.n());
10+
std::vector<edge_index> neig_xor(tree.n());
11+
for(auto v: tree.nodes()) {
12+
for(auto e: tree.outgoing(v)) {
13+
degree[v]++;
14+
neig_xor[v] ^= e;
15+
}
3016
}
31-
}
32-
neig_xor[root] ^= edge_index(-1);
33-
degree[root]++;
34-
for(auto v: tree.nodes()) {
35-
while(degree[v] == 1) {
36-
edge_index ep = neig_xor[v];
37-
callback(v, ep);
38-
degree[v]--;
39-
if (v == root) break;
40-
v = tree.edge(ep).traverse(v);
41-
degree[v]--;
42-
neig_xor[v] ^= ep;
17+
neig_xor[root] ^= edge_index(-1);
18+
degree[root]++;
19+
for(auto v: tree.nodes()) {
20+
while(degree[v] == 1) {
21+
edge_index ep = neig_xor[v];
22+
callback(v, ep);
23+
degree[v]--;
24+
if (v == root) break;
25+
v = tree.edge(ep).traverse(v);
26+
degree[v]--;
27+
neig_xor[v] ^= ep;
28+
}
4329
}
30+
return neig_xor;
4431
}
45-
return neig_xor;
4632
}
47-
48-
} // namespace cp_algo::graph
4933
#endif // CP_ALGO_GRAPH_XOR_DFS_HPP

0 commit comments

Comments
 (0)