Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
sparse_table.cpp File Reference

Implementation of Sparse Table data structure. More...

#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
Include dependency graph for sparse_table.cpp:

Namespaces

namespace  range_queries
 Algorithms and Data Structures that support range queries and updates.
 
namespace  sparse_table
 Functions for Implementation of Sparse Table
 

Functions

template<typename T >
std::vector< T > range_queries::sparse_table::computeLogs (const std::vector< T > &A)
 
template<typename T >
std::vector< std::vector< T > > range_queries::sparse_table::buildTable (const std::vector< T > &A, const std::vector< T > &logs)
 
template<typename T >
int range_queries::sparse_table::getMinimum (int beg, int end, const std::vector< T > &logs, const std::vector< std::vector< T > > &table)
 
int main ()
 

Detailed Description

Implementation of Sparse Table data structure.

Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in O(logn), but its true power is answering range minimum queries or equivalent range maximum queries). For those queries it can compute the answer in O(1) time.

  • Running Time Complexity
  • Build : O(NlogN)
  • Range Query : O(1)

Function Documentation

◆ buildTable()

template<typename T >
std::vector< std::vector< T > > range_queries::sparse_table::buildTable ( const std::vector< T > &  A,
const std::vector< T > &  logs 
)

This functions builds the primary data structure sparse table

Parameters
nvalue of the size of the input array
Aarray of the input integers
logsarray of the log table
Returns
created sparse table data structure
54 {
55 int n = A.size();
56 std::vector<std::vector<T> > table(20, std::vector<T>(n+5, 0));
57 int curLen = 0;
58 for (int i = 0 ; i <= logs[n] ; i++) {
59 curLen = 1 << i;
60 for (int j = 0 ; j + curLen < n ; j++) {
61 if (curLen == 1) {
62 table[i][j] = A[j];
63 }
64 else {
65 table[i][j] = std::min(table[i-1][j], table[i-1][j + curLen/2]);
66 }
67 }
68 }
69 return table;
70}
T min(T... args)
T size(T... args)
Here is the call graph for this function:

◆ computeLogs()

template<typename T >
std::vector< T > range_queries::sparse_table::computeLogs ( const std::vector< T > &  A)

This function precomputes intial log table for further use.

Parameters
nvalue of the size of the input array
Returns
corresponding vector of the log table
36 {
37 int n = A.size();
38 std::vector<T> logs(n);
39 logs[1] = 0;
40 for (int i = 2 ; i < n ; i++) {
41 logs[i] = logs[i/2] + 1;
42 }
43 return logs;
44}
Here is the call graph for this function:

◆ getMinimum()

template<typename T >
int range_queries::sparse_table::getMinimum ( int  beg,
int  end,
const std::vector< T > &  logs,
const std::vector< std::vector< T > > &  table 
)

This function is the query function to get the range minimum value

Parameters
begbeginning index of the query range
endending index of the query range
logsarray of the log table
tablesparse table data structure for the input array
Returns
minimum value for the [beg, end] range for the input array
81 {
82 int p = logs[end - beg + 1];
83 int pLen = 1 << p;
84 return std::min(table[p][beg], table[p][end - pLen + 1]);
85}
T end(T... args)
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function

92 {
93 std::vector<int> A{1, 2, 0, 3, 9};
96 assert(range_queries::sparse_table::getMinimum(0, 0, logs, table) == 1);
97 assert(range_queries::sparse_table::getMinimum(0, 4, logs, table) == 0);
98 assert(range_queries::sparse_table::getMinimum(2, 4, logs, table) == 0);
99 return 0;
100}
std::vector< T > computeLogs(const std::vector< T > &A)
Definition: sparse_table.cpp:36
std::vector< std::vector< T > > buildTable(const std::vector< T > &A, const std::vector< T > &logs)
Definition: sparse_table.cpp:54