2012-11-07 16:00:00 UTC

# The art of mergers

by Fel

Status: Passed
Results: 1424 (cyc: 6, node: 1910)
CPU Time: 27.183
Score: 1497.34
Submitted at: 2012-11-03 00:39:46 UTC
Scored at: 2012-11-03 01:03:04 UTC

Current Rank: 1338th (Highest: 56th )
Based on: The art of mergers (diff)
Basis for: The art of mergers (diff)
Basis for: The art of mergers (diff)
Basis for: The art of mergers (diff)
...and 1 other.

Code
```function xyOut = solver(a, xyIn, wts)

% Place nodes in radial pattern
xy{1}       = solver_alfonso  (a, xyIn, wts);
xy{2}       = solver_hannes   (a, xyIn);
xy{3}       = solver_jonathan (a);
[~,best]    = min(N);
xyOut       = xy{best};

end

function xyOut = solver_alfonso(a, xyIn, wts)

N          = size(xyIn,1);
D          = min(N,mindist(a,N));
[xyOut0,~] = svd(D-repmat(mean(D,1),[size(D,1),1])-repmat(mean(D,1)',[1,size(D,2)])+mean(D(:)));
xyOut      = xyOut0(:,1:2);
for ncut=1:2
M      = a + eye(size(a));
Mbeta  = (((0.9)*eye(N) + 0.1*bsxfun(@rdivide,M,sum(M,2)))^10);
for n1=1:120
xyOut      = Mbeta*xyOut;
mxyOut     = mean(xyOut,1);
xc         = bsxfun(@minus,xyOut,mxyOut);
cxyOut     = (xc' * xc) / N;
[c1,d1,c2] = svd(cxyOut);
xyOut      = (xyOut - ones(N,1)*mxyOut)*c1*diag(1./sqrt(0.1+diag(d1)))*c2';
end
[i,j] = find(a>0.01);
dd    = sum((xyOut(i,:)-xyOut(j,:)).^2,2);
k     = find(dd > 4*mean(dd));

if isempty(k), break; end

a(i(k)+N*(j(k)-1)) = 0.009;
end
xyOut0 = sqrt(N)*detrend(xyOut,'constant')*diag(1./max(eps,std(xyOut,1,1)));
k = 5;
[sxyOut0,idxequal]=sortrows(round(12*xyOut0)/12);
idxequal=idxequal(all(~diff(sxyOut0,1,1),2));
xyOut=round(xyOut0);
while size(unique(xyOut,'rows'),1)~=N
k=k*1.1;
xyOut=xyOut0*k;
xyOut(idxequal,:)=(xyOut0(idxequal,:)+randn(numel(idxequal),2)/10)*k;
xyOut=round(xyOut);
end
if N<30 % small map knot lines
xyOut = 2*xyOut+(randi(3,size(xyOut))-2);
end
dxy = round(wts*(xyOut-xyIn)./sum(wts));
xyOut = bsxfun(@minus,xyOut,dxy);

end

function D = mindist(C,N)

X=logical(speye(N));
D=inf(N);
D(X)=0;
for n=1:N,
X  = ((C*X)>0)&(D>n);
if ~any(X(:)),break;end
D(X)=n;
end

end

function xyOut = solver_hannes(a, xyIn)
% Entry 67561: 2503 (cyc: 2, node: 166) CPU Time: 1.536 Score: 2505.98

sz      = size(a,1);
deg     = diag(sum(a));
L       = deg-a;
[V,~]   = eig(L,deg);
xyOut   = 0;
outdiam = norm(max(V(:,2:3)) - min(V(:,2:3)));
indiam  = norm(max(xyIn)     - min(xyIn));
mult    = indiam/outdiam;

while size(unique(xyOut,'rows'),1) < sz

xyOut      = round(V(:,2:3)*mult);
[~,i]      = unique(xyOut,'rows');
m          = setdiff(1:sz,i);
xyOut(m,:) = xyOut(m,:) + round(rand(length(m),2))*2 - 1;
mult       = mult*1.5;

end

end

function X = solver_jonathan(a)
% Entry 67763: 2523 (cyc: 4, node: 180) CPU Time: 1.371 Score: 2526.46

% JLS solver; Based on spectral graph theory.

L = diag(sum(a)) - a; % graph laplacian

% get eigenvectors of interest
[U, ~, ~] = svd(L);
U         = U(:, end-2:end-1);

% scale to integers, with truncation
X = U;
n = size(X, 1);
k = max(fix(log10(abs(U(:))))) + ceil(log10(n));
X = round(U .* 10^k);

% resolve repeated rows arbitrarily
[~, m, ~] = unique(X, 'rows');
m         = numel(m);
while m ~= n
for row1 = 1:size(X, 1)
idx = find(X(:, 1) == X(row1, 1) & X(:, 2) == X(row1, 2));
for row2 = 2:numel(idx)
X(idx(row2), 2) = X(idx(row2), 2) + row2;
end
end
[~, m, ~] = unique(X, 'rows');
m         = numel(m);
end

end

function S = gradeIt(a,XYold,XYnew,wts)

N = numKnots(XYnew,a);
d = sqrt(sum((XYnew-XYold).^2,2));
A = bsxfun(@minus,XYold',mean(XYold',2));
S = full(dot(A,A,1));
D = bsxfun(@plus,S,S')-full(2*(A'*A));
D = sqrt(max(max(D)));
S = N + sum(d.*wts')/D/sum(wts);

end

function N = numKnots(XY,A)

A         = triu(A,1);
nLines    = nnz(A);
[p1i,p2i] = find(A);
pick      = tril(true(nLines),-1);
x1        = pickCoordinates(XY(p1i,1) ,1,nLines,pick);
y1        = pickCoordinates(XY(p1i,2) ,1,nLines,pick);
x2        = pickCoordinates(XY(p2i,1) ,1,nLines,pick);
y2        = pickCoordinates(XY(p2i,2) ,1,nLines,pick);
x3        = pickCoordinates(XY(p1i,1)',nLines,1,pick);
y3        = pickCoordinates(XY(p1i,2)',nLines,1,pick);
x4        = pickCoordinates(XY(p2i,1)',nLines,1,pick);
y4        = pickCoordinates(XY(p2i,2)',nLines,1,pick);
N         = sum(areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4));

end

function x = pickCoordinates(xy,n,m,pick)
x         = repmat(xy,n,m);
x         = x(pick);
end

function bool = areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4)
% Determine if two line segments intersect.
%
% line1 = [x1, y1, x2, y2]
% line2 = [x3, y3, x4, y4]
%
% This function is robust to floating point precision issues assuming all
% input coordinates are integer-valued. If all coordinates have absolute
% values that do not exceed x, then the arithmetic operations used in this
% function could, as an intermediate step, produce a value with magnitude
% as large as 8*x^4. For double-precision floating point, this implies
% that the following must be satisfied:
%
% 8*x^4 < 2^53
%
% or equivalently, x < 5792.
%
% http://en.wikipedia.org/wiki/Line-line_intersection

bool = false(size(x1));
gen  = true(size(x1));

% Test to see if the lines share exactly one endpoint. If so, the lines
% intersect if either of the free points lies on the line segment formed by
% the other two points.

s13         = find((x1==x3)&(y1==y3));
bool(s13)   = (isPointOnSegment(x2, y2, x1, y1, x4, y4, s13)|isPointOnSegment(x4, y4, x1, y1, x2, y2, s13));
gen(s13)    = false;
s14         = find((x1==x4)&(y1==y4));
bool(s14)   = (isPointOnSegment(x2, y2, x1, y1, x3, y3, s14)|isPointOnSegment(x3, y3, x1, y1, x2, y2, s14));
gen(s14)    = false;
s23         = find((x2==x3)&(y2==y3));
bool(s23)   = (isPointOnSegment(x1, y1, x2, y2, x4, y4, s23)|isPointOnSegment(x4, y4, x1, y1, x2, y2, s23));
gen(s23)    = false;
s24         = find((x2==x4)&(y2==y4));
bool(s24)   = (isPointOnSegment(x1, y1, x2, y2, x3, y3, s24)|isPointOnSegment(x3, y3, x1, y1, x2, y2, s24));
gen(s24)    = false;

% Next we check for parallel and coincident lines. Parallel lines
% obviously don't intersect. Coincident lines intersect if an endpoint from
% one of the lines lies on the other segment.

ss          = find(gen&haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4, 1:numel(x1)));
gen(ss)     = false;
si          = ss(haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4,ss));
UNO         = ones(size(x1));
bool(si)    = ((isPointBetween(x3, x4, x1, UNO, si) & isPointBetween(y3, y4, y1, UNO, si)) | ...
(isPointBetween(x3, x4, x2, UNO, si) & isPointBetween(y3, y4, y2, UNO, si)) | ...
(isPointBetween(x1, x2, x3, UNO, si) & isPointBetween(y1, y2, y3, UNO, si)) | ...
(isPointBetween(x1, x2, x4, UNO, si) & isPointBetween(y1, y2, y4, UNO, si)));

% If we get this far, we're in the general case of two well-defined
% non-parallel lines. The lines formed by the two segments must intersect
% somewhere. Find this intersection point and determine if it lies on the
% segments.

gen  = find(gen);

Px_n = (x1.*y2 - y1.*x2).*(x3 - x4) - (x1 - x2).*(x3.*y4 - y3.*x4);
Px_d = (x1 - x2).*(y3 - y4) - (y1 - y2).*(x3 - x4);

Py_n = (x1.*y2 - y1.*x2).*(y3 - y4) - (y1 - y2).*(x3.*y4 - y3.*x4);
Py_d = (x1 - x2).*(y3 - y4) - (y1 - y2).*(x3 - x4);

bool(gen) = (isPointBetween(x1, x2, Px_n, Px_d, gen) & ...
isPointBetween(y1, y2, Py_n, Py_d, gen) & ...
isPointBetween(x3, x4, Px_n, Px_d, gen) & ...
isPointBetween(y3, y4, Py_n, Py_d, gen));

end

function bool = haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4, ii)
bool = (x4(ii) - x3(ii)).*(y1(ii).*(x2(ii) - x1(ii)) - x1(ii).*(y2(ii) - y1(ii))) == ...
(x2(ii) - x1(ii)).*(y3(ii).*(x4(ii) - x3(ii)) - x3(ii).*(y4(ii) - y3(ii)));
end

function bool = haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4, ii)
bool = (y2(ii) - y1(ii)).*(x4(ii) - x3(ii)) == (x2(ii) - x1(ii)).*(y4(ii) - y3(ii));
end

function bool = isPointOnSegment(x1, y1, x2, y2, x3, y3, ii)
% Determine if point (x1, y1) is on the segment formed by (x2, y2) and (x3, y3).
UNO = ones(size(x1));
bool = haveSameSlope(x2, y2, x1, y1, x1, y1, x3, y3, ii) & ...
isPointBetween(x2, x3, x1, UNO, ii) & ...
isPointBetween(y2, y3, y1, UNO, ii);
end

function bool = isPointBetween(pt1, pt2, num, den, ii)
% Determine if either of the following is satisfied:
% pt1 <= num/den <= pt2
% pt1 >= num/den >= pt2

bool = ((pt1(ii).*den(ii) <= num(ii)) & (num(ii) <= pt2(ii).*den(ii))) | ...
((pt1(ii).*den(ii) >= num(ii)) & (num(ii) >= pt2(ii).*den(ii)));
end
```