71 std::cout <<
"============================================================\n";
72 std::cout <<
" Scenario 1: Corporate Hierarchy — Security Clearance\n";
73 std::cout <<
"============================================================\n\n";
74 std::cout <<
" Find the highest security clearance on the path between\n";
75 std::cout <<
" two employees in the org chart.\n\n";
88 for (
size_t i = 0; i < 8; ++i)
99 constexpr std::array<const char *, 8>
names = {
100 "CEO",
"CTO",
"CFO",
"Eng",
"Data",
"Acct",
"Legal",
"ML"};
101 constexpr std::array<int, 8>
clearance = {5, 4, 3, 2, 3, 1, 2, 3};
106 struct Query {
size_t u, v; };
107 constexpr Query
queries[] = {{3, 7}, {5, 7}, {3, 6}, {7, 7}};
109 std::cout << std::left
110 << std::setw(20) <<
"Query(u,v)"
111 << std::setw(12) <<
"Max Clear."
112 << std::setw(12) <<
"Distance"
114 std::cout << std::string(54,
'-') <<
"\n";
118 const int mx =
hld.path_query(n(q.u), n(q.v));
119 const size_t dist =
hld.distance(n(q.u), n(q.v));
120 auto *
l =
hld.lca(n(q.u), n(q.v));
121 const auto lid =
static_cast<size_t>(
l->get_info());
123 std::cout << std::left
125 << (std::string(
names[q.u]) +
"," +
names[q.v])
126 << std::setw(12) <<
mx
127 << std::setw(12) << dist
131 std::cout <<
"\nHLD chains: " <<
hld.num_chains() <<
"\n\n";
148 std::cout <<
"============================================================\n";
149 std::cout <<
" Scenario 2: Network — Bandwidth Bottleneck\n";
150 std::cout <<
"============================================================\n\n";
151 std::cout <<
" Find the minimum bandwidth (bottleneck) on the path\n";
152 std::cout <<
" between two servers in a tree network.\n";
153 std::cout <<
" Edge weights stored on child nodes.\n\n";
166 for (
size_t i = 0; i < 7; ++i)
176 constexpr std::array<const char *, 7>
names = {
177 "Router",
"S1",
"S2",
"S3",
"S4",
"S5",
"S6"};
178 constexpr std::array<int, 7>
bandwidth = {1000, 100, 50, 80, 30, 60, 90};
183 struct Query {
size_t u, v; };
184 constexpr Query
queries[] = {{6, 5}, {4, 3}, {6, 3}, {1, 2}};
186 std::cout << std::left
187 << std::setw(16) <<
"Path"
188 << std::setw(18) <<
"Bottleneck(edge)"
190 std::cout << std::string(44,
'-') <<
"\n";
195 const int bw =
hld.path_query_edges(n(q.u), n(q.v));
196 const size_t dist =
hld.distance(n(q.u), n(q.v));
198 std::cout << std::left
200 << (std::string(
names[q.u]) +
" -> " +
names[q.v])
201 << std::setw(18) <<
bw
202 << dist <<
" hops\n";
223 std::cout <<
"============================================================\n";
224 std::cout <<
" Scenario 3: Tax Routes — Sum + Dynamic Updates\n";
225 std::cout <<
"============================================================\n\n";
226 std::cout <<
" Sum taxes on a trade route (path between cities).\n";
227 std::cout <<
" Update taxes after policy change.\n\n";
237 for (
size_t i = 0; i < 6; ++i)
246 constexpr std::array<const char *, 6>
names = {
247 "Capital",
"Port",
"Market",
"Farm",
"Mine",
"Workshop"};
248 constexpr std::array<int, 6>
taxes = {10, 5, 8, 3, 7, 6};
250 auto nv = [&
taxes](
Node * p) {
return taxes[
static_cast<size_t>(p->get_info())]; };
253 auto print_query = [&](
const char * label,
size_t u,
size_t v)
255 std::cout <<
" " << std::left << std::setw(25) << label
256 <<
"path_sum(" <<
names[u] <<
", " <<
names[v] <<
") = "
257 <<
hld.path_query(n(u), n(v)) <<
"\n";
260 std::cout <<
"Before policy change:\n";
265 std::cout <<
"\n subtree_sum(Port) = "
266 <<
hld.subtree_query(n(1)) <<
"\n";
267 std::cout <<
" subtree_sum(Capital) = "
268 <<
hld.subtree_query(n(0)) <<
"\n";
271 std::cout <<
"\nPolicy change: Capital tax 10 -> 20\n";
272 hld.point_update(n(0), 20);
274 std::cout <<
"\nAfter policy change:\n";
278 std::cout <<
"\n subtree_sum(Capital) = "
279 <<
hld.subtree_query(n(0)) <<
"\n";
282 std::cout <<
"\nNew tax-free zone at Port (5 -> 0):\n";
283 hld.point_update(n(1), 0);
286 std::cout <<
" subtree_sum(Port) = "
287 <<
hld.subtree_query(n(1)) <<
"\n";
305 std::cout <<
"==============================================================\n";
306 std::cout <<
" Heavy-Light Decomposition on Aleph Tree Graphs\n";
307 std::cout <<
"==============================================================\n\n";
313 std::cout <<
"All scenarios completed successfully.\n";
Heavy-Light Decomposition for tree path queries on Aleph graphs.
WeightedDigraph::Node Node
static Array create(size_t n)
Create an array with n logical elements.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
int main()
Entry point that runs HLD demonstration scenarios for Aleph tree graphs.
Main namespace for Aleph-w library functions.
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.
Arc of graph implemented with double-linked adjacency lists.
HLD with path/subtree max queries.
HLD with path/subtree min queries.
HLD with path/subtree sum queries.
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.