How to resolve the algorithm Closest-pair problem step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Closest-pair problem step by step in the Swift 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 Swift programming language

Source code in the swift programming language

import Foundation

struct Point {
  var x: Double
  var y: Double

  func distance(to p: Point) -> Double {
    let x = pow(p.x - self.x, 2)
    let y = pow(p.y - self.y, 2)
    
    return (x + y).squareRoot()
  }
}

extension Collection where Element == Point {
  func closestPair() -> (Point, Point)? {
    let (xP, xY) = (sorted(by: { $0.x < $1.x }), sorted(by: { $0.y < $1.y }))
    
    return Self.closestPair(xP, xY)?.1
  }
  
  static func closestPair(_ xP: [Element], _ yP: [Element]) -> (Double, (Point, Point))? {
    guard xP.count > 3 else { return xP.closestPairBruteForce() }
    
    let half = xP.count / 2
    let xl = Array(xP[..<half])
    let xr = Array(xP[half...])
    let xm = xl.last!.x
    let (yl, yr) = yP.reduce(into: ([Element](), [Element]()), {cur, el in
      if el.x > xm {
        cur.1.append(el)
      } else {
        cur.0.append(el)
      }
    })
    
    guard let (distanceL, pairL) = closestPair(xl, yl) else { return nil }
    guard let (distanceR, pairR) = closestPair(xr, yr) else { return nil }
    
    let (dMin, pairMin) = distanceL > distanceR ? (distanceR, pairR) : (distanceL, pairL)
    
    let ys = yP.filter({ abs(xm - $0.x) < dMin })
    
    var (closest, pairClosest) = (dMin, pairMin)
    
    for i in 0..<ys.count {
      let p1 = ys[i]
      
      for k in i+1..<ys.count {
        let p2 = ys[k]
        
        guard abs(p2.y - p1.y) < dMin else { break }
        
        let distance = abs(p1.distance(to: p2))
        
        if distance < closest {
          (closest, pairClosest) = (distance, (p1, p2))
        }
      }
    }
    
    return (closest, pairClosest)
  }
  
  func closestPairBruteForce() -> (Double, (Point, Point))? {
    guard count >= 2 else { return nil }
    
    var closestPoints = (self.first!, self[index(after: startIndex)])
    var minDistance = abs(closestPoints.0.distance(to: closestPoints.1))
    
    guard count != 2 else { return (minDistance, closestPoints) }
    
    for i in 0..<count {
      for j in i+1..<count {
        let (iIndex, jIndex) = (index(startIndex, offsetBy: i), index(startIndex, offsetBy: j))
        let (p1, p2) = (self[iIndex], self[jIndex])
        
        let distance = abs(p1.distance(to: p2))
        
        if distance < minDistance {
          minDistance = distance
          closestPoints = (p1, p2)
        }
      }
    }
    
    return (minDistance, closestPoints)
  }
}

var points = [Point]()

for _ in 0..<10_000 {
  points.append(Point(
    x: .random(in: -10.0...10.0),
    y: .random(in: -10.0...10.0)
  ))
}

print(points.closestPair()!)


  

You may also check:How to resolve the algorithm Truncatable primes step by step in the VBScript programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Environment variables step by step in the OCaml programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the Sidef programming language