Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
74
Task/Closest-pair-problem/D/closest-pair-problem-1.d
Normal file
74
Task/Closest-pair-problem/D/closest-pair-problem-1.d
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import std.stdio, std.typecons, std.math, std.algorithm,
|
||||
std.random, std.traits, std.range, std.complex;
|
||||
|
||||
auto bruteForceClosestPair(T)(in T[] points) pure nothrow @nogc {
|
||||
// return pairwise(points.length.iota, points.length.iota)
|
||||
// .reduce!(min!((i, j) => abs(points[i] - points[j])));
|
||||
auto minD = Unqual!(typeof(T.re)).infinity;
|
||||
T minI, minJ;
|
||||
foreach (immutable i, const p1; points.dropBackOne)
|
||||
foreach (const p2; points[i + 1 .. $]) {
|
||||
immutable dist = abs(p1 - p2);
|
||||
if (dist < minD) {
|
||||
minD = dist;
|
||||
minI = p1;
|
||||
minJ = p2;
|
||||
}
|
||||
}
|
||||
return tuple(minD, minI, minJ);
|
||||
}
|
||||
|
||||
auto closestPair(T)(T[] points) pure nothrow {
|
||||
static Tuple!(typeof(T.re), T, T) inner(in T[] xP, /*in*/ T[] yP)
|
||||
pure nothrow {
|
||||
if (xP.length <= 3)
|
||||
return xP.bruteForceClosestPair;
|
||||
const Pl = xP[0 .. $ / 2];
|
||||
const Pr = xP[$ / 2 .. $];
|
||||
immutable xDiv = Pl.back.re;
|
||||
auto Yr = yP.partition!(p => p.re <= xDiv);
|
||||
immutable dl_pairl = inner(Pl, yP[0 .. yP.length - Yr.length]);
|
||||
immutable dr_pairr = inner(Pr, Yr);
|
||||
immutable dm_pairm = dl_pairl[0]<dr_pairr[0] ? dl_pairl : dr_pairr;
|
||||
immutable dm = dm_pairm[0];
|
||||
const nextY = yP.filter!(p => abs(p.re - xDiv) < dm).array;
|
||||
|
||||
if (nextY.length > 1) {
|
||||
auto minD = typeof(T.re).infinity;
|
||||
size_t minI, minJ;
|
||||
foreach (immutable i; 0 .. nextY.length - 1)
|
||||
foreach (immutable j; i + 1 .. min(i + 8, nextY.length)) {
|
||||
immutable double dist = abs(nextY[i] - nextY[j]);
|
||||
if (dist < minD) {
|
||||
minD = dist;
|
||||
minI = i;
|
||||
minJ = j;
|
||||
}
|
||||
}
|
||||
return dm <= minD ? dm_pairm :
|
||||
typeof(return)(minD, nextY[minI], nextY[minJ]);
|
||||
} else
|
||||
return dm_pairm;
|
||||
}
|
||||
|
||||
points.sort!q{ a.re < b.re };
|
||||
const xP = points.dup;
|
||||
points.sort!q{ a.im < b.im };
|
||||
return inner(xP, points);
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias C = complex;
|
||||
auto pts = [C(5,9), C(9,3), C(2), C(8,4), C(7,4), C(9,10), C(1,9),
|
||||
C(8,2), C(0,10), C(9,6)];
|
||||
pts.writeln;
|
||||
writeln("bruteForceClosestPair: ", pts.bruteForceClosestPair);
|
||||
writeln(" closestPair: ", pts.closestPair);
|
||||
|
||||
rndGen.seed = 1;
|
||||
Complex!double[10_000] points;
|
||||
foreach (ref p; points)
|
||||
p = C(uniform(0.0, 1000.0) + uniform(0.0, 1000.0));
|
||||
writeln("bruteForceClosestPair: ", points.bruteForceClosestPair);
|
||||
writeln(" closestPair: ", points.closestPair);
|
||||
}
|
||||
39
Task/Closest-pair-problem/D/closest-pair-problem-2.d
Normal file
39
Task/Closest-pair-problem/D/closest-pair-problem-2.d
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import std.stdio, std.random, std.math, std.typecons, std.complex,
|
||||
std.traits;
|
||||
|
||||
Nullable!(Tuple!(size_t, size_t))
|
||||
bfClosestPair2(T)(in Complex!T[] points) pure nothrow @nogc {
|
||||
auto minD = Unqual!(typeof(points[0].re)).infinity;
|
||||
if (points.length < 2)
|
||||
return typeof(return)();
|
||||
|
||||
size_t minI, minJ;
|
||||
foreach (immutable i; 0 .. points.length - 1)
|
||||
foreach (immutable j; i + 1 .. points.length) {
|
||||
auto dist = (points[i].re - points[j].re) ^^ 2;
|
||||
if (dist < minD) {
|
||||
dist += (points[i].im - points[j].im) ^^ 2;
|
||||
if (dist < minD) {
|
||||
minD = dist;
|
||||
minI = i;
|
||||
minJ = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return typeof(return)(tuple(minI, minJ));
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias C = Complex!double;
|
||||
auto rng = 31415.Xorshift;
|
||||
C[10_000] pts;
|
||||
foreach (ref p; pts)
|
||||
p = C(uniform(0.0, 1000.0, rng), uniform(0.0, 1000.0, rng));
|
||||
|
||||
immutable ij = pts.bfClosestPair2;
|
||||
if (ij.isNull)
|
||||
return;
|
||||
writefln("Closest pair: Distance: %f p1, p2: %f, %f",
|
||||
abs(pts[ij[0]] - pts[ij[1]]), pts[ij[0]], pts[ij[1]]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue