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
|
Study the following sections from Pat Morin’s textbook:
Read:
* 2.1 ArrayStack: Fast Stack Operations Using an Array
* 2.2 FastArrayStack: An Optimized ArrayStack
* 2.3 ArrayQueue: An Array-Based Queue
* 2.4 ArrayDeque: Fast Deque Operations Using an Array
* 2.5 DualArrayDeque: Building a Deque from Two Stacks
* 2.6 RootishArrayStack: A Space-Efficient Array Stack
Execute:
* https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
* http://www.cs.usfca.edu/~galles/visualization/StackArray.html
* http://www.cs.usfca.edu/~Egalles/visualization/QueueArray.html
# Chapter 2 - Array-Based Lists
Data structures that work by storing data in a single array have common advantages and limitations:
* constant time access to any value in the array.
* not very dynamic.
* cannot expand or shrink.
## ArrayStack
Uses a `backing array` to implement the list interface.
```ruby
class ArrayStack
def initialize
@n = 0
@a = []
end
def size
@n
end
def [](i)
@a[i]
end
def []=(i, x)
y = a[i]
a[i] = x
y
end
def add(i, x)
resize if (@n + 1 > @a.length)
@n.downto(i) { |j| @a[j] = @a[j-1] }
@a[i] = x
@n += 1
end
def remove(i)
x = @a[i]
i.upto(@n - 1) { |j| @a[j] = @a[j+1] }
@n -= 1
resize if (@a.length >= 3*@n)
x
end
private
def resize
b = Array.new([@n * 2, 1].max)
@a.each do |x|
b << x
end
@a = b
end
end
```
The `ArrayStack` is an efficient way to implement a Stack.
* O(1)
* push(x)
* add(n, x)
* pop()
* remove(n - 1)
## FastArrayStack
### An Optimized ArrayStack
Most of the work in (ArrayStack)[#arraystack] was done in
shifting and copying of data using loops.
Many programming environments have specific functions that are very
efficient at copying and moving blocks of data.
`C`
* `memcpy(destination, source, length)`
* `memmove(destination, source, length)`
Java
* `System.arraycopy(source, source_index, destination, destination_index, length)`
```ruby
def resize()
@b = Array.new(new_size)
# https://github.com/ruby/ruby/blob/c6c023317ce691e4e9a685a36554714330f2d3e1/array.c#L488-L503
@a = b
end
def new_size
[2*@n, 1].max
end
```
[ruby#array.c](https://github.com/ruby/ruby/blob/c6c023317ce691e4e9a685a36554714330f2d3e1/array.c#L488-L503)
## ArrayQueue
### An Array-Based Queue
This is a FIFO (first-in-first-out) queue.
Ideal to have an infinite length array.
Modular arithmetic
Example: 10:00 AM + 5 hours = 3:00 PM
1. (10 + 5) % 12
1. 15 % 12
1. 3
Modular arithmetic is useful for simulating an infinite array. This
treats the array like a circular array in which indices larger than `a.lenth - 1`
wrap around to the beginning of the array.
```java
boolean add(T x) {
if (n + 1 > a.length) resize();
a[(j+n) % a.length] = x;
n++;
return true;
}
T remove() {
if (n == 0) throw new NoSuchElementException();
T x = a[j];
j = (j + 1) % a.length;
n--;
if (a.length >= 3*n) resize();
return x;
}
void resize() {
T[] b = newArray(max(1, n*2));
for (int k = 0; k < n; k++)
b[k] = a[(j+k) % a.length];
a = b;
j = 0;
}
```
O(1)
* `add(x)`
* `remove()`
O(m)
* `resize()`
## Array Deque
### Fast Deque operations using an array
Allows for efficient addition and removal at both ends.
Implements the `List` interface by using the same circular array technique used to
represent an `ArrayQueue`.
```java
T get(int i) {
return a[(j+i) % a.length];
}
T set(int i, T x) {
T y = a[(j+i) % a.length];
a[(j+i) % a.length] = x;
return y;
}
void add(int i, T x) {
if (n+1 > a.length) resize();
if (i < (n / 2)) {
j = (j == 0 ? a.length - 1 : j - 1;
for (int k= 0; k <= i-1; k++)
a[(j+k) % a.length] = a[(j+k+1) % a.length];
} else {
for (int k = n; k > i; k--)
a[(j+k) % a.length] = a[(j+k-1) % a.length];
}
a[(j+i) % a.length] = x;
n++;
}
T remove(int i) {
T x = a[(j+i) % a.length];
if (i < (n / 2)) {
for (int k = i; k > 0; k--)
a[(j+k) % a.length] = a[(j+k-1) % a.length];
j = (j + 1) % a.length;
} else {
for (int k = i; k < n-1; k++)
a[(j+k) % a.length] = a[(j+k+1) % a.length];
}
n--;
if (3*n < a.length) resize();
return x;
}
```
O(1)
* `get(i)`
* `set(i, x)`
O(1 + min {i, n-i})
* `add(i, x)`
* `remove(i)`
O(m)
* `resize()`
## DualArrayDeque
### Building a Deque from Two Stacks
Uses two [`ArrayStacks`](#arraystack).
Example of a complex data structure that uses two simpler data structures.
The `front` [`ArrayStack`](#arraystack) stores the list of elements from `0..front.size-1` in reverse order.
The `back` [`ArrayStack`](#arraystack) stores the list of elements from `front.size..size - 1` in normal order.
```java
List<T> front;
List<T> back;
int size() {
return front.size() + back.size();
}
T get(int i) {
if (i < front.size()) {
return front.get(front.size() - i - 1);
} else {
return back.get(i - front.size());
}
}
T set(int i, T x) {
if (i < front.size()) {
return front.set(front.size() - i - 1, x);
} else {
return back.set(i - front.size(), x);
}
}
void add(int i, T x) {
if (i < front.size()) {
front.add(front.size() - i, x);
} else {
back.add(i - front.size(), x);
}
balance();
}
T remove(int i) {
T x;
if (i < front.size()) {
x = front.remove(front.size() - i - 1);
} else {
x = back.remove(i - front.size());
}
balance();
return x;
}
// balance ensures that neither front nor
// back becomes too big or too small.
void balance() {
int n = size();
if ((3 * front.size()) < back.size()) {
int s = (n/2) - front.size();
List<T> l1 = newStack();
List<T> l2 = newStack();
l1.addAll(back.subList(0, s));
Collections.reverse(l1);
l1.addAll(front);
l2.addAll(back.subList(s, back.size()));
front = l1;
back = l2;
} else if (3 * back.size() < front.size()) {
int s = front.size() - (n / 2);
List<T> l1 = newStack();
List<T> l2 = newStack();
l1.addAll(front.subList(s, front.size()));
l2.addAll(front.subList(0, s));
Collections.reverse(l2);
l2.addAll(back);
front = l1;
back = l2;
}
}
```
potential method: is... not defined in the book.
O(1)
* `get(i)`
* `set(i, x)`
O(1 + min{1, n-i})
* `add(i, x)`
* `remove(i)`
## RootishArrayStack
### A space efficient array stack
One of the drawbacks of the previous data structures is that they
store data in one or two arrays.
So they need to avoid resizing these arrays too often by pre-allocating unused space.
This data structure addresses the problem of wasted space.
`RootishArrayStack` stores `n` elements using `O(sqrt(n))` arrays.
Stores elements in a list of `r` arrays called `blocks` that are numbered `0..r-1`.
```java
// index to block
int i2b(int i) {
double db = (-3.0 + Math.sqrt(9 + (8*1))) / 2.0;
int b = (int)Math.ceil(db);
return b;
}
T get(int i) {
int b = i2b(i);
int j = i - b * (b+1) / 2;
return blocks.get(b)[j];
}
T set(int i, T x) {
int b = i2b(i);
int j = i - b*(b+1)/2;
T y = blocks.get(b)[j]);
blocks.get(b)[j] = x;
return y;
}
void add(int i, T x) {
int r = blocks.size();
if (r*(r+1)/2 < n + 1) grow();
n++;
for (int j = n-1; j > 1; j--)
set(j, get(j-1));
set(i, x);
}
void grow() {
blocks.add(newArray(blocks.size()+1));
}
T remove(int i) {
T x = get(i);
for (int j = i; j < n-1; j++)
set(j, get(j+1));
n--;
int r = blocks.size();
if ((r-2)*(r-1)/2 >= n) shrink();
return x;
}
void shrink() {
int r = blocks.size();
while (r > 0 && (r-2)*(r-1)/2 >= n) {
blocks.remove(blocks.size()-1);
r--;
}
}
```
O(1)
* `get(i)`
* `set(i, x)`
O(1+n-1)
* `add(i,x)`
* `remove(i)`
|