-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmos_algorithm.py
More file actions
405 lines (357 loc) · 12.9 KB
/
mos_algorithm.py
File metadata and controls
405 lines (357 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""
A method for answering a series of range queries on the same array.
Running time and space complexity of this method are:
* Preprocess - O(n)
* Answering m queries - O((n + m) * sqrt(n) * f) where f is the time complexity
of changing one of the range boundaries by +/- 1
* Space complexity is O(n + m)
For more explanations :
* https://www.hackerearth.com/practice/notes/mos-algorithm/
"""
import math
from abc import abstractmethod
from functools import cmp_to_key
from typing import List, Tuple, Union, Type
START = 0
END = 1
QUERY = 0
class MosOperation:
"""
A template class for operations used by Mo's algorithm.
It provides methods for moving both boundaries of the query's range
See examples of specific operations below
Parameters
---------
arr: list/tuple, array of items to perform queries on.
first_start_id: int, the id of the first query's left boundary
"""
def __init__(self, arr: Union[List, Tuple], first_start_id: int) -> None:
if first_start_id < 0 or first_start_id >= len(arr):
raise ValueError(f"Invalid first start id {first_start_id}")
self.arr = arr
self.len = len(arr)
self.cur_start = first_start_id
self.cur_end = self.cur_start
self.init_first_position()
@abstractmethod
def init_first_position(self) -> None:
"""
init first position
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = MosOperation(input_array, first_start_id=0)
>>> sir_operation.init_first_position()
"""
pass
@abstractmethod
def move_start(self, new_loc: int) -> None:
"""
Since it's a skelaton class only checks the validity of new_loc
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = MosOperation(input_array, first_start_id=2)
>>> sir_operation.move_start(0)
>>> sir_operation.move_start(100)
Traceback (most recent call last):
...
ValueError: Invalid location of start: 100
"""
if new_loc < 0 or new_loc >= self.len:
raise ValueError(f"Invalid location of start: {new_loc}")
@abstractmethod
def move_end(self, new_loc: int) -> None:
"""
Since it's a skelaton class only checks the validity of new_loc
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = MosOperation(input_array, first_start_id=2)
>>> sir_operation.move_end(4)
>>> sir_operation.move_end(100)
Traceback (most recent call last):
...
ValueError: Invalid location of end: 100
"""
if new_loc < 0 or new_loc >= self.len:
raise ValueError(f"Invalid location of end: {new_loc}")
class UniqueItemsInRange(MosOperation):
"""
An operation for calculating number of unique items in range.
This operation can be used in Mo's algorithm.
It provides methods for moving both boundaries of the query's range
See examples of specific operations below
Parameters
---------
arr: list/tuple, array of items to perform queries on.
first_start_id: int, the id of the first query's left boundary
"""
def __init__(self, arr: Union[List, Tuple], first_start_id: int) -> None:
self.num_unique_items = 0
self.unique_items_count = dict()
super().__init__(arr, first_start_id)
def _enlarge_range(self, added_id: int) -> None:
"""
process range enlarged by 1 element
Parameters
---------
added_id: int, the id of the added item
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = UniqueItemsInRange(input_array, first_start_id=0)
>>> sir_operation.move_end(3)
2
>>> sir_operation._enlarge_range(4)
>>> sir_operation.num_unique_items
3
"""
new_value = self.arr[added_id]
if new_value in self.unique_items_count.keys():
self.unique_items_count[new_value] += 1
else:
self.unique_items_count[new_value] = 1
if self.unique_items_count[new_value] == 1:
self.num_unique_items += 1
def _reduce_range(self, reduced_id: int) -> None:
"""
process range reduced by 1 elemnt
Parameters
---------
reduced_id: int, the id of the reduced item
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = UniqueItemsInRange(input_array, first_start_id=0)
>>> sir_operation.move_end(2)
2
>>> sir_operation._reduce_range(2)
>>> sir_operation.num_unique_items
1
"""
reduced_value = self.arr[reduced_id]
self.unique_items_count[reduced_value] -= 1
if self.unique_items_count[reduced_value] == 0:
self.num_unique_items -= 1
def init_first_position(self) -> None:
"""
init the first position
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = UniqueItemsInRange(input_array, first_start_id=4)
>>> sir_operation.cur_start
4
>>> sir_operation.cur_end
4
>>> sir_operation.num_unique_items
1
"""
self.num_unique_items = 1
first_value = self.arr[self.cur_start]
self.unique_items_count[first_value] = 1
def move_start(self, new_loc: int) -> int:
"""
set the range's left boundary
Parameters
---------
new_loc: int, the id of the new location of the range's left boundary
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = UniqueItemsInRange(input_array, first_start_id=2)
>>> sir_operation.move_start(new_loc=0)
2
>>> sir_operation.num_unique_items
2
>>> sir_operation.cur_start
0
"""
super().move_start(new_loc)
step = new_loc - self.cur_start
if step < 0:
for _ in range(-step):
self.cur_start -= 1
self._enlarge_range(self.cur_start)
elif step > 0:
for _ in range(step):
self._reduce_range(self.cur_start)
self.cur_start += 1
return self.num_unique_items
def move_end(self, new_loc: int) -> int:
"""
set the range's right boundary
Parameters
---------
new_loc: int, the id of the new location of the range's right boundary
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = UniqueItemsInRange(input_array, first_start_id=0)
>>> sir_operation.move_end(new_loc=2)
2
>>> sir_operation.num_unique_items
2
>>> sir_operation.cur_end
2
"""
super().move_end(new_loc)
step = new_loc - self.cur_end
if step < 0:
for _ in range(-step):
self._reduce_range(self.cur_end)
self.cur_end -= 1
elif step > 0:
for _ in range(step):
self.cur_end += 1
self._enlarge_range(self.cur_end)
return self.num_unique_items
class SumInRange(MosOperation):
"""
An operation for sum elements in range.
This operation can be used in Mo's algorithm.
It provides methods for moving both boundaries of the query's range
See examples of specific operations below
Parameters
---------
arr: list/tuple, array of items to perform queries on.
first_start_id: int, the id of the first query's left boundary
"""
def __init__(self, arr: Union[List, Tuple], first_start_id: int) -> None:
self.cur_sum = 0
super().__init__(arr, first_start_id)
def init_first_position(self) -> None:
"""
init the first position
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = SumInRange(input_array, first_start_id=4)
>>> sir_operation.cur_start
4
>>> sir_operation.cur_end
4
>>> sir_operation.cur_sum
3
"""
self.cur_sum = self.arr[self.cur_start]
def move_start(self, new_loc: int) -> int:
"""
set the range's left boundary
Parameters
---------
new_loc: int, the id of the new location of the range's left boundary
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = SumInRange(input_array, first_start_id=2)
>>> sir_operation.move_start(new_loc=0)
4
>>> sir_operation.cur_sum
4
>>> sir_operation.cur_start
0
"""
super().move_start(new_loc)
step = new_loc - self.cur_start
if step < 0:
for _ in range(-step):
self.cur_start -= 1
self.cur_sum += self.arr[self.cur_start]
elif step > 0:
for _ in range(step):
self.cur_sum -= self.arr[self.cur_start]
self.cur_start += 1
return self.cur_sum
def move_end(self, new_loc: int) -> int:
"""
set the range's right boundary
Parameters
---------
new_loc: int, the id of the new location of the range's right boundary
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> sir_operation = SumInRange(input_array, first_start_id=0)
>>> sir_operation.move_end(new_loc=2)
4
>>> sir_operation.cur_sum
4
>>> sir_operation.cur_end
2
"""
super().move_end(new_loc)
step = new_loc - self.cur_end
if step < 0:
for _ in range(-step):
self.cur_sum -= self.arr[self.cur_end]
self.cur_end -= 1
elif step > 0:
for _ in range(step):
self.cur_end += 1
self.cur_sum += self.arr[self.cur_end]
return self.cur_sum
class MosAlgorithm:
"""
A generic algorithm/idea for answering multiple range queries on the same
array efficiently. It does so by performing the queries in a way which
maximizes the relevance of each query to the one answered right after it.
Parameters
---------
arr: list/tuple, array of items to perform queries on.
queries: tuple of ints, (start, end) defines ranges to query on
Examples
-------
>>> input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
>>> queries = [(0, 4), (1, 3), (2, 4)]
>>> case1 = MosAlgorithm(input_array, queries)
>>> case1.run_queries(UniqueItemsInRange)
[3, 2, 3]
>>> case1.run_queries(SumInRange)
[8, 4, 6]
"""
def __init__(self, arr: Union[List, Tuple], queries: List[Tuple[int, int]]) -> None:
self.len, self.len_sqrt = len(arr), int(math.ceil(math.sqrt(len(arr))))
self.num_queries = len(queries)
self.arr = arr
if any(
[
(q[START] < 0) or (q[END] >= self.len) or (q[START] > q[END])
for q in queries
]
):
raise ValueError("At least one of the queries is invalid")
self.comparator = cmp_to_key(self._compartor_processed)
# process queries to maintain original order mapping which will
# be used to return results in the original order of the queries
processed_queries = [[queries[i], i] for i in range(self.num_queries)]
processed_queries = sorted(processed_queries, key=self.comparator)
self.sorted_queries, self.inverse_map = list(zip(*processed_queries))
def _compartor_processed(self, left: List, right: List) -> int:
left_start_block = math.floor(left[QUERY][START] / self.len_sqrt)
right_start_block = math.floor(right[QUERY][START] / self.len_sqrt)
if left_start_block == right_start_block:
if left[QUERY][END] > right[QUERY][END]:
return 1
elif left[QUERY][END] < right[QUERY][END]:
return -1
else:
if left[QUERY][START] == right[QUERY][START]:
return 0
elif left[QUERY][START] < right[QUERY][START]:
return -1
else:
return 1
elif left_start_block > right_start_block:
return 1
else:
return -1
def run_queries(self, operation: Type[MosOperation]) -> List:
first_start = self.sorted_queries[0][START]
op = operation(self.arr, first_start)
results = [None for _ in range(self.num_queries)]
for i, cur_query in enumerate(self.sorted_queries):
op.move_end(cur_query[END])
results[self.inverse_map[i]] = op.move_start(cur_query[START])
return results