0% found this document useful (0 votes)
16 views

Week2 Chap2 Advanced Data Structures

Uploaded by

Ha Minh Duc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week2 Chap2 Advanced Data Structures

Uploaded by

Ha Minh Duc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

APPLIED ALGORITHMS

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;

• Complexity of each query: O(1) S0 = 0;


for k = 1 to n do
• Complexity of Q queries: O(n) + O(Q)
Sk = Sk-1 + ak;

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.

• Direct algorithm res = 0;


• Use 2 nested loops to browse through all pairs (i, j) for i = 1 to n do {
and check the condition a[i] + a[j] = Q for j = i+1 to n do {
• Complexity O(n2) if a[i] + a[j] = Q then
res = res + 1;
}
}
Output(res);

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.

• Algorithm that uses 2-pointing technique res = 0;


• Variable i moves from left to right and variable j moves i = 1; j = n;
from right to left through the sequence while i < j do {
• Complexity O(n) if a[i] + a[j] = Q then {
res = res + 1; i = i + 1; j = j – 1;
}else if a[i] + a[j] < Q then
i = i + 1;
else
j = j – 1;
}
Output(res);

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.

• Direct algorithm res = 0;


• Use 2 nested loops to consider all starting and ending for i = 1 to n do {
positions of a subsequence and check whether the S = 0;
sum is less than or equal to Q? for j = i to n do {
• Complexity O(n2) S = S + a[j];
if S <= Q then {
res = max(res, j – i + 1);
}
}
}
Output(res);

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.

• Algorithm that uses 2-pointing technique res = 0; S = 0;


• Variable L moves from left to right and variable R L = 1;
moves from right to left through the sequence for R = 1 to n do {
• Complexity O(n) S = S + a[R];
while S > Q do {
S = S – a[L]; L = L + 1;
}
res = max(res, R – L + 1);
}
Output(res);

14
THANK YOU !

15

You might also like