2

Given n points A[1..n] in the 2-D plane, where i-th point has two attributes A[i].x and A[i].y representing x-coordinate and y-coordinate. Find 3 points A[i], A[j], A[k] (i, j, k are distinct) such that distance(A[i], A[j]) + distance(A[j], A[k]) + distance(A[k], A[i]) is minimized. Here distance(A[i], A[j]) means the Euclid Distance, which is equals to sqrt( (A[i].x - A[j].x)^2 + (A[i].y - A[j].y)^2). You need to output the minimum possible distance.

Input: The first line contains an integer T indicating the number of test cases.

Each test case starts with a positive integer n indicating the number of points.

The following n lines contain two real numbers A[i].x and A[i].y with at-most 6 digits after decimal point describing the coordinates of the point.

Output: T lines in total. Each line starts with "Case #: " and followed by minimum length. Here "#" is the number of the test case starting from 1.

Answers within an absolute or relative error of 10^-6 will be accepted.

Constraints:

  • 1 ≤ T ≤ 10
  • 3 ≤ n ≤ 100000
  • 0 ≤ A[i].x, A[i].y ≤ 10000
  • A[i].x, A[i].y are real numbers with atmost 6 digits after decimal position.

Example Input:

1
4
0.0 0.0
2.0 2.0
2.0 0.0
1.0 1.0

Example Output:

Case 1: 4.8284271247

Explanation One possible solution is to select these 3 points (0, 0), (2, 0), (1, 1), the length is sqrt(2) + sqrt(2) + 2

from math import sqrt as s

class Point():
   def __init__(self,x,y):
      self.x = x
      self.y = y


def bruteforce(p):
    d = []
    q=len(p)
    for i in range(q):
        j=i+1
        while j<q:
            temp = s((p[j].x - p[i].x) * (p[j].x - p[i].x) + (p[j].y - p[i].y) * (p[j].y - p[i].y))
            d.append(temp)
    d.sort()
    return (d[0]+d[1]+d[2])

def solution():
  t = int(input())
  for i in range(t):
     temp = 0
     n = int(input())
     p = []
     for j in range(n):
        a, b = map(float, input().split())
        temp = Point(a, b)
        p.append(temp)
  return(bruteforce(p))

print(solution())

What shall I do to decrease the time complexity? Anyone please help me!

Woodford
  • 3,746
  • 1
  • 15
  • 29
  • Given that you have code that works, and you just want to improve it, this would be a better question for [codereview.se] – G. Anderson Feb 21 '22 at 17:50
  • Your code doesn't seem right... Your `return(bruteforce(p))` should be indented one more time. Also, inside the `while j – LeoE Feb 21 '22 at 18:09
  • Won't improve speed, but will improve readability: `from math import dist` and then `temp = dist(p[i], p[j]) + dist(p[j], p[k]) + dist(p[k], p[i])` – Stef Feb 21 '22 at 18:17
  • 1
    Does this answer your question? [Closest group of 3 points](https://stackoverflow.com/questions/7539623/closest-group-of-3-points) – Stef Feb 21 '22 at 18:19
  • @Stef in your code what is k, we didn't have k – Anushka Krishna Feb 22 '22 at 01:28
  • can you all tell what else can be done to decrease the time – Anushka Krishna Feb 22 '22 at 02:04
  • @LeoE I made the change and added ```j+=1``` this is just running public test cases and private cases get time exceeded – Anushka Krishna Feb 22 '22 at 02:06
  • 1
    You are currently iterating through all combinations of 3 points and finding distances across each of those combinations, which is `O(n^3)`. Given your `n` is 10^5, your solution would take 10^15 operations which is very high. You can solve it in `O(nlogn)` using divide and conquer technique as in https://stackoverflow.com/a/7608443/8677071 – Jay Feb 22 '22 at 06:20
  • @AnushkaKrishna Yes, sorry, there shouldn't be a k. I just meant you can replace the long `s((p[j].x - p[i].x) * (p[j].x - p[i].x) + (p[j].y - p[i].y) * (p[j].y - p[i].y))` with a much shorter `dist(p[i], p[j])`, which would be much easier to read. – Stef Feb 22 '22 at 12:32
  • In this question, I have been given a time limit of 4 seconds with Python version limit 3.6 so I can't use it as `math.dist(p,q)` is in python's 3.8th version. Can anyone please help me with this code ? – Anushka Krishna Feb 24 '22 at 06:14

0 Answers0