You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Optional featured image (relative to `static/img/` folder).
10
10
[header]
@@ -13,47 +13,51 @@ caption = ""
13
13
14
14
+++
15
15
16
-
## Perfect Distribution Based On GCD
16
+
## Perfect Distribution Based on GCD
17
17
18
-
We discuss an algorithm that has property of uniform distribution.
19
-
The examples below are Software Engineering used cases of this algorithm.
18
+
We discuss an algorithm that distributes $a$ ones among $n$ positions so that the gaps between consecutive ones differ by at most one—a **perfect distribution**. The examples below are software engineering use cases for this algorithm.
20
19
21
-
### Software Engineering Used Case 0
20
+
### Use Case 0: Text spacing
22
21
23
-
Given a string of characters $s$, and the given number $n$ that is greater then the length of $s$.
24
-
Extend the string $s$ to the length $n$ by inserting spaces between the words of $s$.
25
-
They key requirement is that the distances between two consecutive words will be uniform.
22
+
Given a string of characters $s$ and a number $n$ greater than the length of $s$,
23
+
extend the string $s$ to length $n$ by inserting spaces between the words.
24
+
The key requirement is that the distances between two consecutive words be uniform.
26
25
27
-
### Software Engineering Used Case 1
26
+
### Use Case 1: Stress testing
28
27
29
-
Assume we want to test a server for stress.
28
+
Assume we want to stress-test a server.
30
29
Our test contains positive and negative requests.
31
-
Assume we want to sends $1000$ request that $300$ of them are negative.
30
+
Suppose we want to send $1000$ requests, $300$ of which are negative.
32
31
Our test tool might send requests simultaneously or sequentially,
33
-
but what we required is that the negative requests will arrive the server with uniform distribution.
34
-
I.e., we want to prevent the following: sending 300 negative following by $700$ positive requests.
35
-
Using the algorithm, we simple distribute $300$ request by calling ${\cal PD}(300, 1000)$,
36
-
containing $1$ in cell $i$ corresponds to the request $i$ to be negative.
37
-
Then the test tool will use the array to sends request that now might be sent even sequentially.
38
-
39
-
### Software Engineering Used Case 2
40
-
41
-
Assume the test tool can also run performance tests, that is, creates stress on the server and measure performance -- the number of requests per second processed by the server.
42
-
We want to extend the test tool by profiling feature: to specify the number of requests being sent per second.
43
-
E.g. a performance test shows that the server performance is $1000$ requests per second.
44
-
We want test tool will send requests according to specified speed, say $500$, or $200$, or $20$, or even $0.2$ requests per second (i.e., one request per $5$ second).
45
-
Such profiling tool allows to explore the state of the server under various stress conditions: utility of CPU, Memory, Threads in depending on various number of requests received.
46
-
The test tool may divide $1$ seconds, say, on $100$ parts, i.e. every part is $20$ milliseconds.
47
-
Assume $230$ is specified, then during every part, $2.3$ request must be send.
48
-
In other words, the test tool must send 2 requests in most of parts and $3$ request in some parts.
49
-
Calling ${\cal PD}(30, 100)$ yields an array defining the parts in which 3 requests will be sent.
50
-
51
-
We present the general solution ${\cal PD}$ to the problem -- for any specified input in above examples, ${\cal PD}$ produces the exact solution.
52
-
Any probabilistic or approximation solutions do not achieve the required exactness and usually are much more complicated.
53
-
The algorithm ${\cal PD}$ is very simple, clear to prove correctness and to find time complexity.
54
-
Moreover, it has a nice relation to well-known Euclid's algorithm of computing Greatest Common Divisor, which is denoted here by ${\cal GCD}$.
55
-
56
-
Euqlid’s Algorithm $\cal GCD$
32
+
but we require that the negative requests arrive at the server with uniform distribution.
33
+
I.e., we want to avoid sending 300 negative followed by 700 positive requests.
34
+
Using the algorithm, we simply distribute the 300 negative requests by calling ${\cal PD}(300, 1000)$:
35
+
cell $i$ is $1$ if and only if request $i$ is negative.
36
+
The test tool then uses the array to send requests (possibly sequentially).
37
+
38
+
### Use Case 2: Rate limiting / profiling
39
+
40
+
Assume the test tool can run performance tests—creating stress and measuring throughput (requests per second).
41
+
We want to extend the test tool with a profiling feature: specify the number of requests sent per second.
42
+
E.g., a performance test shows the server handles $1000$ requests per second.
43
+
We want the test tool to send requests at a specified rate, say $500$, $200$, $20$, or even $0.2$ requests per second (one request every 5 seconds).
44
+
Such a profiling tool allows exploring the server state under various load conditions: CPU, memory, threads depending on the number of requests received.
45
+
46
+
The test tool may divide 1 second into $100$ parts, each $20$ milliseconds.
47
+
Suppose we specify $230$ requests per second: then during every part, $2.3$ requests must be sent on average.
48
+
In other words, the tool must send 2 requests in most parts and 3 in some parts.
49
+
We need to choose which $30$ parts get the extra request; ${\cal PD}(30, 100)$ yields that array (a $1$ in cell $i$ means part $i$ gets 3 requests).
50
+
51
+
We present the general solution ${\cal PD}$ to the problem—for any specified input in the above examples, ${\cal PD}$ produces the exact solution.
52
+
Any probabilistic or approximation solutions do not achieve the required exactness and are usually more complicated.
53
+
The algorithm ${\cal PD}$ is simple, has a clear correctness proof, and admits $O(n)$ time and space complexity.
54
+
Moreover, it has a direct relation to Euclid's algorithm for computing the Greatest Common Divisor, denoted here by ${\cal GCD}$.
55
+
56
+
### Notation
57
+
58
+
*POST* denotes a condition that the procedure guarantees will hold after the call.
59
+
60
+
### Euclid's Algorithm ${\cal GCD}$
57
61
58
62
* POST: $res = {\cal GCD}(a, n)$.
59
63
@@ -68,12 +72,14 @@ def gcd(a, n):
68
72
return res
69
73
```
70
74
71
-
Deterministic algorithm of perfect distribution $\cal PD$
75
+
### Deterministic algorithm of perfect distribution ${\cal PD}$
76
+
77
+
The recurrence mirrors Euclid's algorithm: ${\cal PD}(a, n)$ calls ${\cal PD}(n \bmod a, a)$, so the recursion depth is $O(\log \min(a, n))$ and total time is $O(n)$.
### Alternative formulation: subtraction-based (slow) versions
125
+
126
+
The same recurrence can be expressed using subtraction instead of modulo, which makes the symmetry with Euclid's algorithm more explicit. These versions have the same recursion structure but do more work per step.
0 commit comments