Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
link_cut_tree_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
44#include <iostream>
45#include <tpl_link_cut_tree.H>
46
47using namespace std;
48using namespace Aleph;
49
50// ---------------------------------------------------------------
51// Example 1: Dynamic connectivity
52// ---------------------------------------------------------------
54{
55 cout << "=== Example 1: Dynamic connectivity ===" << endl;
56
57 // Build a forest of 6 nodes:
58 //
59 // 0 - 1 - 2 3 - 4
60 // |
61 // 5
62 Link_Cut_Tree lct;
63 auto * n0 = lct.make_vertex(0);
64 auto * n1 = lct.make_vertex(1);
65 auto * n2 = lct.make_vertex(2);
66 auto * n3 = lct.make_vertex(3);
67 auto * n4 = lct.make_vertex(4);
68 auto * n5 = lct.make_vertex(5);
69
70 lct.link(n0, n1);
71 lct.link(n1, n2);
72 lct.link(n3, n4);
73 lct.link(n5, n4);
74
75 cout << " connected(0, 2) = " << boolalpha << lct.connected(n0, n2) << endl;
76 cout << " connected(0, 3) = " << boolalpha << lct.connected(n0, n3) << endl;
77
78 // Merge the two components
79 cout << " linking node 2 to node 3..." << endl;
80 lct.link(n2, n3);
81 cout << " connected(0, 5) = " << boolalpha << lct.connected(n0, n5) << endl;
82
83 // Split them again
84 cout << " cutting edge (2, 3)..." << endl;
85 lct.cut(n2, n3);
86 cout << " connected(0, 5) = " << boolalpha << lct.connected(n0, n5) << endl;
87
88 cout << endl;
89}
90
91// ---------------------------------------------------------------
92// Example 2: Rerooting and find_root
93// ---------------------------------------------------------------
94static void example_rerooting()
95{
96 cout << "=== Example 2: Rerooting ===" << endl;
97
98 // Build a path: A - B - C - D - E
99 Link_Cut_Tree lct;
100 auto * a = lct.make_vertex(0);
101 auto * b = lct.make_vertex(1);
102 auto * c = lct.make_vertex(2);
103 auto * d = lct.make_vertex(3);
104 auto * e = lct.make_vertex(4);
105
106 lct.link(a, b);
107 lct.link(b, c);
108 lct.link(c, d);
109 lct.link(d, e);
110
111 cout << " Before reroot: find_root(E) = node "
112 << lct.get_val(lct.find_root(e)) << endl;
113
114 lct.make_root(c);
115 cout << " After make_root(C): find_root(A) = node "
116 << lct.get_val(lct.find_root(a)) << endl;
117 cout << " After make_root(C): find_root(E) = node "
118 << lct.get_val(lct.find_root(e)) << endl;
119
120 cout << " Path size from A to E = " << lct.path_size(a, e) << endl;
121
122 cout << endl;
123}
124
125// ---------------------------------------------------------------
126// Example 3: Lowest Common Ancestor
127// ---------------------------------------------------------------
128static void example_lca()
129{
130 cout << "=== Example 3: LCA ===" << endl;
131
132 // Build a tree:
133 //
134 // 0
135 // / \
136 // 1 2
137 // / \ \
138 // 3 4 5
139 //
140 Link_Cut_Tree lct;
141 auto * n0 = lct.make_vertex(0);
142 auto * n1 = lct.make_vertex(1);
143 auto * n2 = lct.make_vertex(2);
144 auto * n3 = lct.make_vertex(3);
145 auto * n4 = lct.make_vertex(4);
146 auto * n5 = lct.make_vertex(5);
147
148 lct.make_root(n0);
149 lct.link(n1, n0);
150 lct.link(n2, n0);
151 lct.link(n3, n1);
152 lct.link(n4, n1);
153 lct.link(n5, n2);
154
155 cout << " lca(3, 4) = " << lct.get_val(lct.lca(n3, n4)) << endl; // 1
156 cout << " lca(3, 5) = " << lct.get_val(lct.lca(n3, n5)) << endl; // 0
157 cout << " lca(4, 5) = " << lct.get_val(lct.lca(n4, n5)) << endl; // 0
158 cout << " lca(0, 4) = " << lct.get_val(lct.lca(n0, n4)) << endl; // 0
159
160 cout << endl;
161}
162
163// ---------------------------------------------------------------
164// Example 4: Path aggregates (sum / min / max)
165// ---------------------------------------------------------------
167{
168 cout << "=== Example 4: Path aggregates ===" << endl;
169
170 // Build a weighted path:
171 //
172 // node weights: [10, 20, 5, 30, 15]
173 // edges: 0 - 1 - 2 - 3 - 4
174
175 // --- Sum ---
176 {
178 auto * n0 = lct.make_vertex(10);
179 auto * n1 = lct.make_vertex(20);
180 auto * n2 = lct.make_vertex(5);
181 auto * n3 = lct.make_vertex(30);
182 auto * n4 = lct.make_vertex(15);
183
184 lct.link(n0, n1);
185 lct.link(n1, n2);
186 lct.link(n2, n3);
187 lct.link(n3, n4);
188
189 cout << " path_sum(0, 4) = " << lct.path_query(n0, n4) << endl; // 80
190 cout << " path_sum(1, 3) = " << lct.path_query(n1, n3) << endl; // 55
191
192 lct.set_val(n2, 100);
193 cout << " after set_val(2, 100): path_sum(0, 4) = "
194 << lct.path_query(n0, n4) << endl; // 175
195 }
196
197 // --- Min ---
198 {
200 auto * n0 = lct.make_vertex(10);
201 auto * n1 = lct.make_vertex(20);
202 auto * n2 = lct.make_vertex(5);
203 auto * n3 = lct.make_vertex(30);
204 auto * n4 = lct.make_vertex(15);
205
206 lct.link(n0, n1);
207 lct.link(n1, n2);
208 lct.link(n2, n3);
209 lct.link(n3, n4);
210
211 cout << " path_min(0, 4) = " << lct.path_query(n0, n4) << endl; // 5
212 cout << " path_min(3, 4) = " << lct.path_query(n3, n4) << endl; // 15
213 }
214
215 // --- Max ---
216 {
218 auto * n0 = lct.make_vertex(10);
219 auto * n1 = lct.make_vertex(20);
220 auto * n2 = lct.make_vertex(5);
221 auto * n3 = lct.make_vertex(30);
222 auto * n4 = lct.make_vertex(15);
223
224 lct.link(n0, n1);
225 lct.link(n1, n2);
226 lct.link(n2, n3);
227 lct.link(n3, n4);
228
229 cout << " path_max(0, 4) = " << lct.path_query(n0, n4) << endl; // 30
230 cout << " path_max(0, 1) = " << lct.path_query(n0, n1) << endl; // 20
231 }
232
233 cout << endl;
234}
235
236// ---------------------------------------------------------------
237// Example 5: Lazy path updates
238// ---------------------------------------------------------------
240{
241 cout << "=== Example 5: Lazy path updates ===" << endl;
242
243 // 5 nodes with initial value 0, path: 0-1-2-3-4
246 LCT lct;
247 auto * n0 = lct.make_vertex(0LL);
248 auto * n1 = lct.make_vertex(0LL);
249 auto * n2 = lct.make_vertex(0LL);
250 auto * n3 = lct.make_vertex(0LL);
251 auto * n4 = lct.make_vertex(0LL);
252
253 lct.link(n0, n1);
254 lct.link(n1, n2);
255 lct.link(n2, n3);
256 lct.link(n3, n4);
257
258 cout << " Initial path_sum(0, 4) = " << lct.path_query(n0, n4) << endl;
259
260 // Add 10 to every node on path 0..4
261 lct.path_apply(n0, n4, 10LL);
262 cout << " After +10 on path(0,4): sum = " << lct.path_query(n0, n4) << endl;
263
264 // Add 5 to sub-path 1..3
265 lct.path_apply(n1, n3, 5LL);
266 cout << " After +5 on path(1,3): sum(0,4) = "
267 << lct.path_query(n0, n4) << endl;
268 cout << " Sub-path sum(1,3) = " << lct.path_query(n1, n3) << endl;
269
270 cout << " Individual values: ";
271 for (auto * nd : {n0, n1, n2, n3, n4})
272 {
273 // reading after access materialises the lazy value
274 cout << lct.get_val(nd) << " ";
275 }
276 cout << endl;
277
278 cout << endl;
279}
280
281int main()
282{
285 example_lca();
288
289 return 0;
290}
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
#define LL
Definition ran_array.c:24
Additive lazy tag: adds a delta to every node on a path.