Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
26
Task/Closest-pair-problem/AWK/closest-pair-problem.awk
Normal file
26
Task/Closest-pair-problem/AWK/closest-pair-problem.awk
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# syntax: GAWK -f CLOSEST-PAIR_PROBLEM.AWK
|
||||
BEGIN {
|
||||
x[++n] = 0.654682 ; y[n] = 0.925557
|
||||
x[++n] = 0.409382 ; y[n] = 0.619391
|
||||
x[++n] = 0.891663 ; y[n] = 0.888594
|
||||
x[++n] = 0.716629 ; y[n] = 0.996200
|
||||
x[++n] = 0.477721 ; y[n] = 0.946355
|
||||
x[++n] = 0.925092 ; y[n] = 0.818220
|
||||
x[++n] = 0.624291 ; y[n] = 0.142924
|
||||
x[++n] = 0.211332 ; y[n] = 0.221507
|
||||
x[++n] = 0.293786 ; y[n] = 0.691701
|
||||
x[++n] = 0.839186 ; y[n] = 0.728260
|
||||
min = 1E20
|
||||
for (i=1; i<=n-1; i++) {
|
||||
for (j=i+1; j<=n; j++) {
|
||||
dsq = (x[i]-x[j])^2 + (y[i]-y[j])^2
|
||||
if (dsq < min) {
|
||||
min = dsq
|
||||
mini = i
|
||||
minj = j
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("distance between (%.6f,%.6f) and (%.6f,%.6f) is %g\n",x[mini],y[mini],x[minj],y[minj],sqrt(min))
|
||||
exit(0)
|
||||
}
|
||||
|
|
@ -1,11 +1,16 @@
|
|||
import std.stdio, std.typecons, std.math, std.algorithm,
|
||||
std.array, std.random, std.traits;
|
||||
std.random, std.traits, std.range, std.complex;
|
||||
|
||||
//auto bfClosestPair(T)(in T[] points) pure nothrow {
|
||||
// return pairwise(points.length.iota, points.length.iota)
|
||||
// .reduce!(min!((i, j) => abs(points[i] - points[j])));
|
||||
//}
|
||||
|
||||
auto bruteForceClosestPair(T)(in T[] points) pure nothrow {
|
||||
auto minD = Unqual!(typeof(T.re)).infinity;
|
||||
Unqual!T minI, minJ;
|
||||
foreach (i, p1; points[0 .. $-1])
|
||||
foreach (j, p2; points[i+1 .. $]) {
|
||||
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;
|
||||
|
|
@ -17,23 +22,25 @@ auto bruteForceClosestPair(T)(in T[] points) pure nothrow {
|
|||
}
|
||||
|
||||
auto closestPair(T)(T[] points) /*pure nothrow*/ {
|
||||
static Tuple!(typeof(T.re),T,T) inner(in T[] xP, /*in*/ T[] yP) {
|
||||
static Tuple!(typeof(T.re), T, T) inner(in T[] xP, /*in*/ T[] yP)
|
||||
pure nothrow {
|
||||
if (xP.length <= 3)
|
||||
return bruteForceClosestPair(xP);
|
||||
const Pl = xP[0 .. xP.length/2];
|
||||
const Pr = xP[xP.length/2 .. $];
|
||||
immutable xDiv = Pl[$ - 1].re;
|
||||
auto Yr = partition!(p => p.re <= xDiv)(yP);
|
||||
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_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();
|
||||
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 (i; 0 .. nextY.length-1)
|
||||
foreach (j; i+1 .. min(i+8, nextY.length)) {
|
||||
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;
|
||||
|
|
@ -42,27 +49,29 @@ auto closestPair(T)(T[] points) /*pure nothrow*/ {
|
|||
}
|
||||
}
|
||||
return dm <= minD ? dm_pairm :
|
||||
Tuple!(typeof(T.re),T,T)(minD,nextY[minI],nextY[minJ]);
|
||||
typeof(return)(minD, nextY[minI], nextY[minJ]);
|
||||
} else
|
||||
return dm_pairm;
|
||||
}
|
||||
|
||||
sort!q{ a.re < b.re }(points);
|
||||
auto xP = points.dup;
|
||||
sort!q{ a.im < b.im }(points);
|
||||
points.sort!q{ a.re < b.re };
|
||||
const xP = points.dup;
|
||||
points.sort!q{ a.im < b.im };
|
||||
return inner(xP, points);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto pts= [5+9i, 9+3i, 2, 8+4i, 7+4i, 9+10i, 1+9i, 8+2i, 10i, 9+6i];
|
||||
writeln(pts);
|
||||
writeln("bruteForceClosestPair: ", bruteForceClosestPair(pts));
|
||||
writeln(" closestPair: ", closestPair(pts));
|
||||
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);
|
||||
|
||||
auto rnd = Random(1); // set seed
|
||||
cdouble[10_000] points;
|
||||
rndGen.seed = 1;
|
||||
Complex!double[10_000] points;
|
||||
foreach (ref p; points)
|
||||
p = uniform(0.0, 1000.0, rnd) + uniform(0.0, 1000.0, rnd) * 1i;
|
||||
writeln("bruteForceClosestPair: ", bruteForceClosestPair(points));
|
||||
writeln(" closestPair: ", closestPair(points));
|
||||
p = C(uniform(0.0, 1000.0) + uniform(0.0, 1000.0));
|
||||
writeln("bruteForceClosestPair: ", points.bruteForceClosestPair);
|
||||
writeln(" closestPair: ", points.closestPair);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,49 +1,39 @@
|
|||
import core.stdc.stdio, core.stdc.stdlib, std.math; // for Phobos
|
||||
//import tango.stdc.stdio, tango.stdc.stdlib, tango.math.Math;
|
||||
import std.stdio, std.random, std.math, std.typecons, std.complex,
|
||||
std.traits;
|
||||
|
||||
int bfClosestPair2(cdouble[] points, out size_t i1, out size_t i2) {
|
||||
auto minD = typeof(points[0].re).infinity;
|
||||
if (points.length < 2) {
|
||||
i1 = i2 = size_t.max;
|
||||
return -1;
|
||||
}
|
||||
size_t minI, minJ;
|
||||
for (int i = 0; i < points.length-1; i++) {
|
||||
auto points_i_re = points[i].re;
|
||||
auto points_i_im = points[i].im;
|
||||
for (int j = i+1; j < points.length; j++) {
|
||||
auto dre = points_i_re - points[j].re;
|
||||
auto dist = dre * dre;
|
||||
if (dist < minD) {
|
||||
auto dim = points_i_im - points[j].im;
|
||||
dist += dim * dim;
|
||||
if (dist < minD) {
|
||||
minD = dist;
|
||||
minI = i;
|
||||
minJ = j;
|
||||
Nullable!(Tuple!(size_t, size_t))
|
||||
bfClosestPair2(T)(in Complex!T[] points) pure nothrow {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i1 = minI;
|
||||
i2 = minJ;
|
||||
return 0;
|
||||
return typeof(return)(tuple(minI, minJ));
|
||||
}
|
||||
|
||||
void main() {
|
||||
srand(31415);
|
||||
auto pts = new cdouble[10_000];
|
||||
alias C = Complex!double;
|
||||
auto rng = 31415.Xorshift;
|
||||
C[10_000] pts;
|
||||
foreach (ref p; pts)
|
||||
p = 1000.0 * (cast(double)rand() / (RAND_MAX + 1.0)) +
|
||||
1000.0i * (cast(double)rand() / (RAND_MAX + 1.0));
|
||||
p = C(uniform(0.0, 1000.0, rng), uniform(0.0, 1000.0, rng));
|
||||
|
||||
size_t i, j;
|
||||
int err = bfClosestPair2(pts, i, j);
|
||||
if (err < 0)
|
||||
immutable ij = pts.bfClosestPair2;
|
||||
if (ij.isNull)
|
||||
return;
|
||||
double d = sqrt((pts[i].re - pts[j].re) * (pts[i].re - pts[j].re) +
|
||||
(pts[i].im - pts[j].im) * (pts[i].im - pts[j].im));
|
||||
printf("Closest pair: dist: %lf p1, p2: (%lf, %lf), (%lf, %lf)\n",
|
||||
d, pts[i].re, pts[i].im, pts[j].re, pts[j].im);
|
||||
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