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 342 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 441 of file heavy_light_decomposition.cpp.

441 : Tree<X>(nodes), SG<X>(nodes) {
442 /* Initialization and resize vectors */
443 label = 0;
444 h_label.assign(Tree<X>::t_nodes, -1);
445 h_heavychlid.assign(Tree<X>::t_nodes, -1);
447 iota(h_parent.begin(), h_parent.end(), 0);
448 }
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 415 of file heavy_light_decomposition.cpp.

415 {
418 std::swap(a, b);
419 }
420 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
421 int l = h_label[h_parent[a]];
422 int r = h_label[a];
425 }
427 a = Tree<X>::t_par[h_parent[a]][0];
428 if (a == -1) {
429 break;
430 }
431 }
432 return ret;
433 }
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.

◆ 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 356 of file heavy_light_decomposition.cpp.

356 {
357 int hc_size = -1, hc_id = -1;
358 for (const int &v : Tree<X>::t_adj[u]) {
359 if (v ^ p) {
360 dfs_hc(v, u);
361 if (Tree<X>::t_size[v] > hc_size) {
363 hc_id = v;
364 }
365 }
366 }
368 }
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf 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 396 of file heavy_light_decomposition.cpp.

396 {
397 h_label[u] = label++;
398 if (h_heavychlid[u] != -1) {
400 }
401 for (const int &v : Tree<X>::t_adj[u]) {
402 if (v ^ p and v ^ h_heavychlid[u]) {
403 dfs_labels(v, u);
404 }
405 }
406 }
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 377 of file heavy_light_decomposition.cpp.

377 {
378 if (h_heavychlid[u] != -1) {
381 }
382 for (const int &v : Tree<X>::t_adj[u]) {
383 if (v ^ p and v ^ h_heavychlid[u]) {
384 dfs_par(v, u);
385 }
386 }
387 }
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 456 of file heavy_light_decomposition.cpp.

456 {
458
459 // Fill the heavy child, greatest parent, and labels
460 label = 0;
464
465 // Segment Tree Initialization
466 for (int i = 0; i < Tree<X>::t_nodes; i++) {
468 }
469 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
471 SG<X>::s_tree[i << 1 | 1]);
472 }
473 }
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 495 of file heavy_light_decomposition.cpp.

495 {
496 int lc = Tree<X>::lca(a, b);
498 assert(lc != -1);
499 ret += chain_query(a, lc);
500 ret += chain_query(b, lc);
501 return ret - Tree<X>::t_val[lc];
502 }
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 481 of file heavy_light_decomposition.cpp.

481 {
485 }
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 346 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 345 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 347 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 344 of file heavy_light_decomposition.cpp.


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