TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
range_queries::heavy_light_decomposition::HLD< X > Class Template Reference

The Heavy-Light Decomposition class. More...

Inheritance diagram for range_queries::heavy_light_decomposition::HLD< X >:
[legend]
Collaboration diagram for range_queries::heavy_light_decomposition::HLD< X >:
[legend]

Public Member Functions

 HLD (int nodes)
 Class parameterized constructor. Resizes the and initilizes the data members.
 
void init ()
 This function must be called after the tree adjacency list and node values are populated The function initializes the required parametes, and populates the segment tree.
 
void update (int node, X val)
 This function updates the value at node with val.
 
query (int a, int b)
 This function returns the sum of node values in the simple path from from node_1 to node_2.
 
- Public Member Functions inherited from range_queries::heavy_light_decomposition::Tree< X >
 Tree (int nodes)
 Class parameterized constructor, resizes the and initializes the data members.
 
void add_edge (const int u, const int v)
 Adds an undirected edge from node u to node v in the tree.
 
void change_root (int new_root)
 Set the root for the tree.
 
void set_node_val (const std::vector< X > &node_val)
 Set the values for all the nodes.
 
void init ()
 This function must be called after the tree adjacency list and node values are populated The function initializes the required parameters, and populates the segment tree.
 
void lift (int *const p, int dist)
 The function lifts a node, k units up the tree. The lifting is done in place, and the result is stored in the address pointed by p.
 
int kth_ancestor (int p, const int &dist)
 The function returns the kth ancestor of a node.
 
int lca (int a, int b)
 The function returns the least common ancestor of two nodes.
 

Private Member Functions

void dfs_hc (int u, int p=-1)
 Utility function to assign heavy child to each node (-1 for a leaf node)
 
void dfs_par (int u, int p=-1)
 Utility function to assign highest parent that can be reached though heavy chains.
 
void dfs_labels (int u, int p=-1)
 Utility function to lable the nodes so that heavy chains have a contigous lable.
 
chain_query (int a, int b)
 Utility function to break down a path query into two chain queries.
 

Private Attributes

int label
 utility member to assign labels in dfs_labels()
 
std::vector< int > h_label
 stores the label of a node
 
std::vector< int > h_heavychlid
 stores the heavy child of a node
 
std::vector< int > h_parent
 stores the top of the heavy chain from a node
 

Detailed Description

template<typename X>
class range_queries::heavy_light_decomposition::HLD< X >

The Heavy-Light Decomposition class.

Template Parameters
thedata type of the values stored in the tree nodes

Definition at line 336 of file heavy_light_decomposition.cpp.

Constructor & Destructor Documentation

◆ HLD()

template<typename X >
range_queries::heavy_light_decomposition::HLD< X >::HLD ( int nodes)
inlineexplicit

Class parameterized constructor. Resizes the and initilizes the data members.

Parameters
nodesthe total number of nodes in the tree

Definition at line 409 of file heavy_light_decomposition.cpp.

435 : Tree<X>(nodes), SG<X>(nodes) {
436 /* Initialization and resize vectors */
437 label = 0;
438 h_label.assign(Tree<X>::t_nodes, -1);
439 h_heavychlid.assign(Tree<X>::t_nodes, -1);
441 iota(h_parent.begin(), h_parent.end(), 0);
442 }
std::vector< int > h_parent
stores the top of the heavy chain from a node
int label
utility member to assign labels in dfs_labels()
std::vector< int > h_heavychlid
stores the heavy child of a node
std::vector< int > h_label
stores the label of a node
SG(int size)
Class parameterized constructor. Resizes the and initilizes the data members.
Tree(int nodes)
Class parameterized constructor, resizes the and initializes the data members.

Member Function Documentation

◆ chain_query()

template<typename X >
X range_queries::heavy_light_decomposition::HLD< X >::chain_query ( int a,
int b )
inlineprivate

Utility function to break down a path query into two chain queries.

Parameters
anode where the path starts
bnode where the path ends a and b must belong to a single root to leaf chain
Returns
the sum of ndoe values in the simple path from a to b

Definition at line 409 of file heavy_light_decomposition.cpp.

409 {
410 X ret = SG<X>::sret_init;
412 std::swap(a, b);
413 }
414 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
415 int l = h_label[h_parent[a]];
416 int r = h_label[a];
419 }
420 ret = SG<X>::combine(ret, SG<X>::query(l, r));
421 a = Tree<X>::t_par[h_parent[a]][0];
422 if (a == -1) {
423 break;
424 }
425 }
426 return ret;
427 }
X query(int l, int r)
Make a range query from node label l to node label r.
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
std::vector< int > t_depth
a vector to store the depth of a node,
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
double l(double x)
Another test function.

◆ dfs_hc()

template<typename X >
void range_queries::heavy_light_decomposition::HLD< X >::dfs_hc ( int u,
int p = -1 )
inlineprivate

Utility function to assign heavy child to each node (-1 for a leaf node)

Parameters
ucurrent dfs node
pthe parent of node u
Returns
void

Definition at line 350 of file heavy_light_decomposition.cpp.

350 {
351 int hc_size = -1, hc_id = -1;
352 for (const int &v : Tree<X>::t_adj[u]) {
353 if (v ^ p) {
354 dfs_hc(v, u);
355 if (Tree<X>::t_size[v] > hc_size) {
356 hc_size = Tree<X>::t_size[v];
357 hc_id = v;
358 }
359 }
360 }
361 h_heavychlid[u] = hc_id;
362 }
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf node)
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
std::vector< int > t_size
a vector to store the subtree size rooted at node

◆ dfs_labels()

template<typename X >
void range_queries::heavy_light_decomposition::HLD< X >::dfs_labels ( int u,
int p = -1 )
inlineprivate

Utility function to lable the nodes so that heavy chains have a contigous lable.

Parameters
ucurrent dfs node
pthe parent of node u
Returns
void

Definition at line 390 of file heavy_light_decomposition.cpp.

390 {
391 h_label[u] = label++;
392 if (h_heavychlid[u] != -1) {
394 }
395 for (const int &v : Tree<X>::t_adj[u]) {
396 if (v ^ p and v ^ h_heavychlid[u]) {
397 dfs_labels(v, u);
398 }
399 }
400 }
void dfs_labels(int u, int p=-1)
Utility function to lable the nodes so that heavy chains have a contigous lable.

◆ dfs_par()

template<typename X >
void range_queries::heavy_light_decomposition::HLD< X >::dfs_par ( int u,
int p = -1 )
inlineprivate

Utility function to assign highest parent that can be reached though heavy chains.

Parameters
ucurrent dfs node
pthe parent of node u
Returns
void

Definition at line 371 of file heavy_light_decomposition.cpp.

371 {
372 if (h_heavychlid[u] != -1) {
374 dfs_par(h_heavychlid[u], u);
375 }
376 for (const int &v : Tree<X>::t_adj[u]) {
377 if (v ^ p and v ^ h_heavychlid[u]) {
378 dfs_par(v, u);
379 }
380 }
381 }
void dfs_par(int u, int p=-1)
Utility function to assign highest parent that can be reached though heavy chains.

◆ init()

template<typename X >
void range_queries::heavy_light_decomposition::HLD< X >::init ( )
inline

This function must be called after the tree adjacency list and node values are populated The function initializes the required parametes, and populates the segment tree.

Returns
void

Definition at line 450 of file heavy_light_decomposition.cpp.

450 {
452
453 // Fill the heavy child, greatest parent, and labels
454 label = 0;
458
459 // Segment Tree Initialization
460 for (int i = 0; i < Tree<X>::t_nodes; i++) {
462 }
463 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
464 SG<X>::s_tree[i] =
465 SG<X>::combine(SG<X>::s_tree[i << 1], SG<X>::s_tree[i << 1 | 1]);
466 }
467 }
std::vector< X > s_tree
Everything here is private, and can only be accessed through the methods, in the derived class (HLD)
void init()
This function must be called after the tree adjacency list and node values are populated The function...

◆ query()

template<typename X >
X range_queries::heavy_light_decomposition::HLD< X >::query ( int a,
int b )
inline

This function returns the sum of node values in the simple path from from node_1 to node_2.

Parameters
athe node where the simple path starts
bthe node where the simple path ends (parameters are interchangeable, i.e., the function is commutative)
Returns
the sum of node values in the simple path from a to b

Definition at line 489 of file heavy_light_decomposition.cpp.

489 {
490 int lc = Tree<X>::lca(a, b);
491 X ret = SG<X>::sret_init;
492 assert(lc != -1);
493 ret += chain_query(a, lc);
494 ret += chain_query(b, lc);
495 return ret - Tree<X>::t_val[lc];
496 }
X chain_query(int a, int b)
Utility function to break down a path query into two chain queries.
int lca(int a, int b)
The function returns the least common ancestor of two nodes.

◆ update()

template<typename X >
void range_queries::heavy_light_decomposition::HLD< X >::update ( int node,
X val )
inline

This function updates the value at node with val.

Parameters
nodethe node where the update is done
valthe value that is being updated
Returns
void

Definition at line 475 of file heavy_light_decomposition.cpp.

475 {
476 X diff = val - Tree<X>::t_val[node];
477 SG<X>::update(h_label[node], diff);
478 Tree<X>::t_val[node] = val;
479 }
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
void update(int p, X v)
Update the value at a node.

Member Data Documentation

◆ h_heavychlid

template<typename X >
std::vector<int> range_queries::heavy_light_decomposition::HLD< X >::h_heavychlid
private

stores the heavy child of a node

Definition at line 340 of file heavy_light_decomposition.cpp.

◆ h_label

template<typename X >
std::vector<int> range_queries::heavy_light_decomposition::HLD< X >::h_label
private

stores the label of a node

Definition at line 339 of file heavy_light_decomposition.cpp.

◆ h_parent

template<typename X >
std::vector<int> range_queries::heavy_light_decomposition::HLD< X >::h_parent
private

stores the top of the heavy chain from a node

Definition at line 341 of file heavy_light_decomposition.cpp.

◆ label

template<typename X >
int range_queries::heavy_light_decomposition::HLD< X >::label
private

utility member to assign labels in dfs_labels()

Definition at line 338 of file heavy_light_decomposition.cpp.


The documentation for this class was generated from the following file: