Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
sparse_table.cpp File Reference

Implementation of Sparse Table data structure. More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
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
57 {
58 int n = A.size();
59 std::vector<std::vector<T> > table(20, std::vector<T>(n + 5, 0));
60 int curLen = 0;
61 for (int i = 0; i <= logs[n]; i++) {
62 curLen = 1 << i;
63 for (int j = 0; j + curLen < n; j++) {
64 if (curLen == 1) {
65 table[i][j] = A[j];
66 } else {
67 table[i][j] =
68 std::min(table[i - 1][j], table[i - 1][j + curLen / 2]);
69 }
70 }
71 }
72 return table;
73}
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
38 {
39 int n = A.size();
40 std::vector<T> logs(n);
41 logs[1] = 0;
42 for (int i = 2; i < n; i++) {
43 logs[i] = logs[i / 2] + 1;
44 }
45 return logs;
46}
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
85 {
86 int p = logs[end - beg + 1];
87 int pLen = 1 << p;
88 return std::min(table[p][beg], table[p][end - pLen + 1]);
89}
T end(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function

96 {
97 std::vector<int> A{1, 2, 0, 3, 9};
101 assert(range_queries::sparse_table::getMinimum(0, 0, logs, table) == 1);
102 assert(range_queries::sparse_table::getMinimum(0, 4, logs, table) == 0);
103 assert(range_queries::sparse_table::getMinimum(2, 4, logs, table) == 0);
104 return 0;
105}
std::vector< T > computeLogs(const std::vector< T > &A)
Definition sparse_table.cpp:38
std::vector< std::vector< T > > buildTable(const std::vector< T > &A, const std::vector< T > &logs)
Definition sparse_table.cpp:56