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
<h2id="linear-time-randomized-algorithms">Linear time randomized algorithms<aclass="headerlink" href="#linear-time-randomized-algorithms" title="Permanent link">¶</a></h2>
6876
-
<h3id="a-linear-time-with-high-probability-algorithm">A linear time (with high probability) algorithm<aclass="headerlink" href="#a-linear-time-with-high-probability-algorithm" title="Permanent link">¶</a></h3>
6877
-
<p>An alternative method arises from a very simple idea to heuristically improve the runtime: We can divide the plane into a grid of <spanclass="arithmatex">$d \times d$</span> squares, then it is only required to test distances between same-block or adjacent-block points (unless all squares are disconnected from each other, we will avoid this by design), since any other pair has larger distance that the two points in the same square.</p>
6876
+
<h3id="a-randomized-algorithm-with-linear-expected-time">A randomized algorithm with linear expected time<aclass="headerlink" href="#a-randomized-algorithm-with-linear-expected-time" title="Permanent link">¶</a></h3>
6877
+
<p>An alternative method arises from a very simple idea to heuristically improve the runtime: We can divide the plane into a grid of <spanclass="arithmatex">$d \times d$</span> squares, then it is only required to test distances between same-block or adjacent-block points (unless all squares are disconnected from each other, but we will avoid this by design), since any other pair has larger distance that the two points in the same square.</p>
6878
6878
<divstyle="text-align: center;">
6879
6879
<imgsrc="nearest_points_blocks_example.png" alt="Example of the squares strategy" height="300px">
6880
6880
</div>
@@ -6884,7 +6884,15 @@ <h3 id="a-linear-time-with-high-probability-algorithm">A linear time (with high
6884
6884
<p>Now we need to decide on how to set <spanclass="arithmatex">$d$</span> so that it minimizes <spanclass="arithmatex">$\Theta(\sum_{i=1}^{k} n_i^2)$</span>.</p>
<p>We need <spanclass="arithmatex">$d$</span> to be an approximation of the minimum distance <spanclass="arithmatex">$d$</span>, and the trick is to just sample <spanclass="arithmatex">$n$</span> distances randomly and choose <spanclass="arithmatex">$d$</span> to be the smallest of these distances. We now prove that with high probability this has linear cost.</p>
6887
-
<p><strong>Proof.</strong> Assume with a particular choice of <spanclass="arithmatex">$d$</span>, the resulting squares have <spanclass="arithmatex">$C \coloneqq \sum_{i=1}^{k} n_i^2 = \lambda n$</span>. What is the probability that such <spanclass="arithmatex">$d$</span> survives the sampling of <spanclass="arithmatex">$n$</span> independent distances? If a single pair among the sampled ones has distance smaller than <spanclass="arithmatex">$d$</span>, this arrangement is not possible. Inside a square, at least half of the pairs would raise a smaller distance, so we have <spanclass="arithmatex">$\sum_{i=1}^{k} \frac{1}{2} {n_i \choose 2}$</span> pairs which yield a smaller final <spanclass="arithmatex">$d$</span>. This is, approximately, <spanclass="arithmatex">$\frac{1}{4} \sum_{i=1}^{k} n_i^2 = \frac{\lambda}{4} n$</span>. On the other hand, there are about <spanclass="arithmatex">$\frac{1}{2} n^2$</span> pairs that can be sampled. We have that the probability of sampling a pair with distance smaller than <spanclass="arithmatex">$d$</span> is at least (approximately) <spanclass="arithmatex">$\frac{\lambda n / 4}{n^2 / 2} = \frac{\lambda/2}{n}$</span>, so the probability of at least one such pair being chosen during the <spanclass="arithmatex">$n$</span> rounds (and therefore avoiding this situation) is <spanclass="arithmatex">$1 - (1 - \frac{\lambda/2}{n})^n \approx 1 - e^{-\lambda/2}$</span>. This goes to <spanclass="arithmatex">$1$</span> as <spanclass="arithmatex">$\lambda$</span> increases. <spanclass="arithmatex">$\quad \blacksquare$</span></p>
6887
+
<p><strong>Proof.</strong> Imagine the disposition of points in squares with a particular choice of <spanclass="arithmatex">$d$</span>, say <spanclass="arithmatex">$x$</span>. Consider <spanclass="arithmatex">$d$</span> a random variable, resulting from our sampling of distances. Let's define <spanclass="arithmatex">$C(x) = \sum_{i=1}^{k(x)} n_i(x)^2$</span> as the cost estimation for a particular disposition when we choose <spanclass="arithmatex">$d=x$</span>. Now, let's define <spanclass="arithmatex">$\lambda(x)$</span> such that <spanclass="arithmatex">$C(x) = \lambda(x) \, n$</span>. What is the probability that such choice <spanclass="arithmatex">$x$</span> survives the sampling of <spanclass="arithmatex">$n$</span> independent distances? If a single pair among the sampled ones has distance smaller than <spanclass="arithmatex">$x$</span>, this arrangement will be replaced by the smaller <spanclass="arithmatex">$d$</span>. Inside a square, at least a quarter of the pairs would raise a smaller distance (imagine four subsquares in every square, and use the pigeonhole principle), so we have <spanclass="arithmatex">$\sum_{i=1}^{k} \frac{1}{4} {n_i \choose 2}$</span> pairs which yield a smaller final <spanclass="arithmatex">$d$</span>. This is, approximately, <spanclass="arithmatex">$\frac{1}{8} \sum_{i=1}^{k} n_i^2 = \frac{1}{8} \lambda(x) n$</span>. On the other hand, there are about <spanclass="arithmatex">$\frac{1}{2} n^2$</span> pairs that can be sampled. We have that the probability of sampling a pair with distance smaller than <spanclass="arithmatex">$x$</span> is at least (approximately)
6888
+
<spanclass="arithmatex">$<spanclass="arithmatex">$\frac{\lambda(x) n / 8}{n^2 / 2} = \frac{\lambda(x)/4}{n}$</span>$</span>
6889
+
so the probability of at least one such pair being chosen during the <spanclass="arithmatex">$n$</span> rounds (and therefore finding a smaller <spanclass="arithmatex">$d$</span>) is
(we have used that <spanclass="arithmatex">$(1 + x)^n \le e^{xn}$</span> for any real number <spanclass="arithmatex">$x$</span>, check https://en.wikipedia.org/wiki/Bernoulli%27s_inequality#Related_inequalities). <br> Notice this goes to <spanclass="arithmatex">$1$</span> exponentially as <spanclass="arithmatex">$\lambda(x)$</span> increases. This hints that <spanclass="arithmatex">$\lambda$</span> will be small usually.</p>
6892
+
<p>We have shown that <spanclass="arithmatex">$\Pr(d \le x) \ge 1 - e^{-\lambda(x)/4}$</span>, or equivalently, <spanclass="arithmatex">$\Pr(d \ge x) \le e^{-\lambda(x)/4}$</span>. We need to know <spanclass="arithmatex">$\Pr(\lambda(d) \ge \text{something})$</span> to be able to estimate its expected value. We notice that <spanclass="arithmatex">$\lambda(d) \ge \lambda(x) \iff d \ge x$</span>. This is because making the squares smaller only reduces the number of points in each square (splits the points into other squares), and this keeps reducing the sum of squares. Therefore,
(we have used that <spanclass="arithmatex">$E[X] = \int_0^{+\infty} \Pr(X \ge x) \, \mathrm{d}x$</span>, check https://math.stackexchange.com/a/1690829).</p>
6895
+
<p>Finally, <spanclass="arithmatex">$\mathbb{E}[C(d)] = \mathbb{E}[\lambda(d) \, n] \le 4n$</span>, and the expected running time is <spanclass="arithmatex">$O(n)$</span>, with a reasonable constant factor. <spanclass="arithmatex">$\quad \blacksquare$</span></p>
6888
6896
<h4id="implementation-of-the-algorithm">Implementation of the algorithm<aclass="headerlink" href="#implementation-of-the-algorithm" title="Permanent link">¶</a></h4>
6889
6897
<p>The advantage of this algorithm is that it is straightforward to implement, but still has good performance in practise.</p>
<h3id="a-randomized-algorithm-with-expected-linear-time">A randomized algorithm with expected linear time<aclass="headerlink" href="#a-randomized-algorithm-with-expected-linear-time" title="Permanent link">¶</a></h3>
6983
+
<h3id="an-alternative-randomized-linear-expected-time-algorithm">An alternative randomized linear expected time algorithm<aclass="headerlink" href="#an-alternative-randomized-linear-expected-time-algorithm" title="Permanent link">¶</a></h3>
6976
6984
<p>Now we introduce a different randomized algorithm which is less practical but very easy to show that it runs in expected linear time.</p>
6977
6985
<ul>
6978
6986
<li>Permute the <spanclass="arithmatex">$n$</span> points randomly</li>
0 commit comments