How to resolve the algorithm Closest-pair problem step by step in the Visual FoxPro programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Closest-pair problem step by step in the Visual FoxPro programming language
Table of Contents
Problem Statement
Provide a function to find the closest two points among a set of given points in two dimensions, i.e. to solve the Closest pair of points problem in the planar case. The straightforward solution is a O(n2) algorithm (which we can call brute-force algorithm); the pseudo-code (using indexes) could be simply: A better algorithm is based on the recursive divide&conquer approach, as explained also at Wikipedia's Closest pair of points problem, which is O(n log n); a pseudo-code could be:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Closest-pair problem step by step in the Visual FoxPro programming language
Source code in the visual programming language
CLOSE DATABASES ALL
CREATE CURSOR pairs(id I, xcoord B(6), ycoord B(6))
INSERT INTO pairs VALUES (1, 0.654682, 0.925557)
INSERT INTO pairs VALUES (2, 0.409382, 0.619391)
INSERT INTO pairs VALUES (3, 0.891663, 0.888594)
INSERT INTO pairs VALUES (4, 0.716629, 0.996200)
INSERT INTO pairs VALUES (5, 0.477721, 0.946355)
INSERT INTO pairs VALUES (6, 0.925092, 0.818220)
INSERT INTO pairs VALUES (7, 0.624291, 0.142924)
INSERT INTO pairs VALUES (8, 0.211332, 0.221507)
INSERT INTO pairs VALUES (9, 0.293786, 0.691701)
INSERT INTO pairs VALUES (10, 0.839186, 0.728260)
SELECT p1.id As id1, p2.id As id2, ;
(p1.xcoord-p2.xcoord)^2 + (p1.ycoord-p2.ycoord)^2 As dist2 ;
FROM pairs p1 JOIN pairs p2 ON p1.id < p2.id ORDER BY 3 INTO CURSOR tmp
GO TOP
? "Closest pair is " + TRANSFORM(id1) + " and " + TRANSFORM(id2) + "."
? "Distance is " + TRANSFORM(SQRT(dist2))
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Idoneal numbers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Factor programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Factorial step by step in the Aime programming language