Python分治法解决最近对问题

本文阅读 0 分钟
首页 Python笔记 正文
  1. class nearestPoint:
  2. def __init__(self, aList):
  3. self.aList = aList
  4. def calculate(self):
  5. if self.aList == None or len(self.aList) == 0:
  6. return
  7. if len(self.aList) == 1:
  8. return 0
  9. global xRangeList, yRangeList
  10. xRangeList, yRangeList = self.Mergesort(self.aList, 0), self.Mergesort(self.aList, 1)
  11. return self.ClosestPair(xRangeList, yRangeList)
  12. def Mergesort(self, aList, axis):
  13. if len(aList) <= 1:
  14. return aList
  15. pivot = len(aList)//2
  16. leftArray = aList[:pivot]
  17. rightArray = aList[pivot:]
  18. return self.Merge(self.Mergesort(leftArray, axis), self.Mergesort(rightArray, axis), axis)
  19. def Merge(self, leftList, rightList, axis):
  20. output = []
  21. leftIndex, rightIndex = 0, 0
  22. while leftIndex < len(leftList) and rightIndex < len(rightList):
  23. if leftList[leftIndex][axis] < rightList[rightIndex][axis]:
  24. output.append(leftList[leftIndex])
  25. leftIndex += 1
  26. else:
  27. output.append(rightList[rightIndex])
  28. rightIndex += 1
  29. while leftIndex < len(leftList):
  30. output.append(leftList[leftIndex])
  31. leftIndex += 1
  32. while rightIndex < len(rightList):
  33. output.append(rightList[rightIndex])
  34. rightIndex += 1
  35. return output
  36. def ClosestPair(self, xList, yList):
  37. if len(xList) == 2:
  38. temp1 = ((xList[0][0]-xList[1][0])**2+(xList[0][1]-xList[1][1])**2)**0.5
  39. temp2 = ((yList[0][0]-yList[1][0])**2+(yList[0][1]-yList[1][1])**2)**0.5
  40. return min(temp1, temp2)
  41. elif len(xList) == 3:
  42. temp1 = min((xList[1][0] - xList[0][0])**2 + (xList[1][1] - xList[0][1])**2, (yList[1][0] - yList[0][0])**2 + (yList[1][1] - yList[0][1])**2)
  43. temp2 = min((xList[2][0] - xList[0][0])**2 + (xList[2][1] - xList[0][1])**2, (yList[2][0] - yList[0][0])**2 + (yList[2][1] - yList[0][1])**2)
  44. temp3 = min((xList[2][0] - xList[1][0])**2 + (xList[2][1] - xList[1][1])**2, (yList[2][0] - yList[1][0])**2 + (yList[2][1] - yList[1][1])**2)
  45. temp = min(temp1, temp2)
  46. return (min(temp, temp3))**0.5
  47. else:
  48. length = len(xList)
  49. pivot = length//2
  50. xLeft, xRight, yLeft, yRight = xList[:pivot], xList[pivot:], yList[:pivot], yList[pivot:]
  51. d1, d2 = self.ClosestPair(xLeft, yLeft), self.ClosestPair(xRight, yRight)
  52. d = min(d1, d2)
  53. m = xList[pivot][0]
  54. S = []
  55. for i in range(len(yList)):
  56. if abs(yList[i][0] - m) < d:
  57. S.append(yList[i])
  58. dminsq = d**2
  59. if len(S) > 2:
  60. for i in range(len(S)-1):
  61. k = i + 1
  62. while k <= len(S) - 1 and (S[k][1] - S[i][1])**2 < dminsq:
  63. dminsq = min((S[k][0]-S[i][0])**2+(S[k][1]-S[i][1])**2, dminsq)
  64. k += 1
  65. elif len(S) == 2:
  66. dminsq = min((S[1][0]-S[0][0])**2+(S[1][1]-S[0][1])**2, dminsq)
  67. return dminsq**0.5
  68. A = [[3.7,2.7],[6.9,10.2],[10.5,6.3],[2.0,5.2],[7.6,9.6],[6.4,6.8],[8.9,6.3],[4.8,9.9],[3.1,6.1],[5.3,5.8],[3.6,8.1],[2.9,3.7],[3.2,4.1],[4.7,3.9]]
  69. s = nearestPoint(A)
  70. print(s.calculate())
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

Python八皇后问题
« 上一篇 01-21
Python判断平衡二叉树
下一篇 » 01-21

发表评论