|
7 | 7 | #include <algorithm> |
8 | 8 |
|
9 | 9 | 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; |
45 | 19 | } |
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 | + } |
61 | 48 | } |
62 | | - |
63 | | -} // namespace cp_algo::graph |
64 | 49 | #endif // CP_ALGO_GRAPH_TREE_DIAMETER_HPP |
0 commit comments