Min Spanning Tree-Prim‘s Algorithm-解法

这是一个关于求解最小生成树的问题,可以使用Prim或Kruskal算法。以下是Prim算法的实现,详细介绍了算法的时间复杂度和如何将二维平面上的点通过曼哈顿距离进行连接。给定一组坐标点,返回使所有点相连的最小总距离。问题转化为寻找最小联通路径,采用局部最优思想,逐步将距离当前集合最近的点加入,以构建全局最优解。此过程将图转换为树形结构。
摘要由CSDN通过智能技术生成

原题:

"""
This is a typical minimum spanning tree question, it can be solved using either Kruskal or Prim's algorithm
Below is a Prim's algorithm implementation
Here is a wiki for Pirm's algorithm https://en.wikipedia.org/wiki/Prim's_algorithm
Time Complexity: Prim's Algorithm takes O(NlgN) but the whole solution is dominated by O(N*N) due to graph creation (nested loop)

You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].
The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.
Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.

Input: points = [[3,12],[-2,5],[-4,1]]
Output: 18
"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值