Week2 Chap2 Advanced Data Structures
Week2 Chap2 Advanced Data Structures
APPLIED ALGORITHMS
Data structures and advanced techniques
Cumulative array, 2-pointing technique
3
CONTENTS
• Cumulative array
• 2-pointing technique
4
Cumulative array
• Illustrative exercise (P.02.02.01). Given the sequence a1, a2, …, an. Execute Q queries, each query is
characterized by a pair of indices (i, j) in which we need to calculate the sum ai + ai+1 + . . . + aj.
• Direct algorithm:
• For each query, we traverse the array from ai to aj to calculate the sum of the elements in this
subrange.
• The worst-case complexity of each query is O(n). sum(i, j){
• The worst-case complexity of Q queries is O(Qn). T = 0;
for k = i to j do
T = T + ak;
return T;
}
5
Cumulative array
• Illustrative exercise (P.02.02.01). Given the sequence a1, a2, …, an. Execute Q queries, each query is
characterized by a pair of indices (i, j) in which we need to calculate the sum ai + ai+1 + . . . + aj.
• Algorithm uses cumulative array:
• Calculate the sum of elements in cumulative array Sk = a1 + a2 + … + ak (k = 1, 2, …, n), S0 = 0
• Complexity O(n)
• The query (i, j) has the value of Sj - Si-1 Input a1, a2, …, an and Q;
for q = 1 to Q do {
Input i, j;
res = Sj – Si-1;
output(res);
}
6
Cumulative array
• Given 2D array a[1..n, 1..m], cumulative array S[1..n, 1..m] is defined as following:
• S[0, j] = 0, S[i, 0] = 0, i = 0, 1, …, n and j = 0, 1, . . ., m
𝑗
• S[i, j] = σ𝑖𝑘=1 σ𝑞=1 𝑎[𝑘, 𝑞], i = 1, …, n and j = 1, . . ., m
• Recursive formula : S[i, j] = S[i-1,j] + S[i, j-1] – S[i-1, j-1] + a[i,j]
• Algorithm to calculate cumulative array
• Complexity O(nm) for i = 0 to n do S[i,0] = 0;
for j = 0 to m do S[0,j] = 0;
for i = 1 to n do {
for j = 1 to n do {
S[i,j] = S[i-1,j] + S[i,j-1] –
S[i-1,j-1] + a[i,j];
}
}
7
Cumulative array
• Illustrative exercise (P.02.02.02). Given the 2D array a[1..n, 1..m]. Execute Q queries, each query is
characterized by a set of indices (a, b, c, d) and defined as follows:
query[a, b, c, d] = σ𝑐𝑘=𝑎 σ𝑑𝑞=𝑏 𝑎[𝑘, 𝑞]
• Direct algorithm:
• Each query, we perform 2 nested loops to traverse all elements and calculate the sum
• The worst-case complexity of each query is O(nm)
8
Cumulative array
• Illustrative exercise (P.02.02.02). Given the 2D array a[1..n, 1..m]. Execute Q queries, each query is
characterized by a set of indices (a, b, c, d) and defined as follows:
query[a, b, c, d] = σ𝑐𝑘=𝑎 σ𝑑𝑞=𝑏 𝑎[𝑘, 𝑞]
• Algorithm that uses cumulative algorithm:
• Formular: query[a, b, c, d] = S[c, d] – S[c, b-1] – S[a-1, d] + S[a-1, b-1]
• The worst-case complexity of each query is O(1)
9
2-pointing technique
• In many problems, we have to traverse a sequence a1, a2, …, an to search for objects characterized
by 2 indices (i, j) on the sequence (for example, a subsequence consists of consecutive elements or
pairs of 2 elements of a sequence) that satisfies some certain properties.
• Use 2 nested loops to traverse through all pairs of 2 indices (i, j): complexity O(n2)
• Use 2 pointers to move in 1 direction or to move in 2 opposite directions: complexity O(n)
10
2-pointing technique
• Illustrative exercise 2.1 (P.02.02.03). Given the sequence a[1], a[2], . . ., a[n] is sorted in ascending
order (distinct elements: no elements with the same value). Given the value Q, count the number of
pairs of 2 indices i and j such that a[i] + a[j] = Q.
11
2-pointing technique
• Illustrative exercise 2.1 (P.02.02.03). Given the sequence a[1], a[2], . . ., a[n] is sorted in ascending
order (distinct elements: no elements with the same value). Given the value Q, count the number of
pairs of 2 indices i and j such that a[i] + a[j] = Q.
12
2-pointing technique
• Illustrative exercise 2.2 (P.02.02.04). Given a sequence of non-negative numbers a[1], a[2], . . ., a[n].
Given the value Q, find the longest subsequence (consisting of a number of consecutive elements:
a[i], a[i+1],…,a[j]) whose sum is less than or equal to Q.
13
2-pointing technique
• Illustrative exercise 2.2 (P.02.02.04). Given a sequence of non-negative numbers a[1], a[2], . . ., a[n].
Given the value Q, find the longest subsequence (consisting of a number of consecutive elements:
a[i], a[i+1],…,a[j]) whose sum is less than or equal to Q.
14
THANK YOU !
15