首页 > 其他分享 >[Algorithm] Prim's Algorithm

[Algorithm] Prim's Algorithm

时间:2024-05-14 15:09:10浏览次数:33  
标签:Prim Algorithm tree vertex complexity edges time

Prim's algorithm is a popular method used in computer science for finding a minimum spanning tree for a connected, undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. Prim's algorithm is particularly useful in network design, such as designing the least expensive network of roads, pipes, or cables to connect multiple points.

Explanation of Prim's Algorithm

Here’s how Prim's algorithm works, step-by-step:

  1. Initialize:

    • Start with a graph that has vertices (nodes) connected by edges with weights.
    • Select an arbitrary vertex to start the tree from.
    • Initialize a priority queue to keep track of edges, where the edges are prioritized by their weights.
  2. Grow the Spanning Tree:

    • While there are still vertices not included in the tree:
      • Add the least weight edge from the queue that connects a vertex in the tree to a vertex not yet in the tree.
      • Add this new vertex to the tree.
      • For each connected vertex to this newly added vertex, if it is not in the tree, add the corresponding edge to the priority queue.
  3. Repeat until all vertices are included in the tree or all edges are considered.

Time Complexity Analysis

The time complexity of Prim's algorithm depends on the data structures used for the priority queue:

  1. Simple Array:

相关文章

  • 题解:SP10232 AMR11E - Distinct Primes
    前话这咋人名都和HP一模一样了,SPOJ出题人里是不是全是哈迷啊。思路非常直观的一个思路:从前往后枚举每一个数,看是否满足条件,输出满足条件的第一个。CODE#include<bits/stdc++.h>usingnamespacestd;boolis(intn){//判断质数if(n<2)return0;for(inti=2;i<......
  • [LeetCode] 1584.连接所有点的最小费用 Kruskal And Prim 算法 Java 并查集
    Problem:1584.连接所有点的最小费用目录Kruskal算法复杂度CodePrim算法复杂度CodeKruskal算法复杂度时间复杂度:添加时间复杂度,示例:$O(mlog(m))$空间复杂度:添加空间复杂度,示例:$O(n)$CodeclassSolution{publicintminCostConnectPoints(int[][]po......
  • TheAlgorithms/C - 各种基础算法、数据结构的 C 语言实现+armink/SFUD - 一款基于 JED
    1、OpenMV-RT-基于恩智浦i.MXRT系列的开源机器视觉AI模块OpenMV-RT是一款基于恩智浦最近主打的i.MXRT超高性能系列MCU的视觉模块,模块设计者是恩智浦大牛工程师宋岩(对,就是ARMCortex-M3权威指南中文版作者)。模块源代码: https://github.com/RockySong/micropython......
  • Python-PostgreSQL主键自动填充报错:SAWarning: Column x is marked as a member of th
    importdatetimefromsqlalchemyimportColumn,String,inspect,Integerfromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemy.ormimportsessionmakerfromsqlalchemyimportcreate_engineengine=create_engine(DATABASE_URL)Base=decla......
  • 2022 Benelux Algorithm Programming Contest (BAPC 22) A 、I、J、L
    A.AdjustedAverage(暴力枚举+二分查找)分析读完题目可以发现k很小,那么考虑暴力做法的时间复杂度为\(O(C_n^k)\),对于\(k\leq3\)的其实可以直接暴力创过去,但对于\(k=4\)的情况显然不适用。那么对应\(k=4\)的情况考虑优化,可以选择将数分为两个集合,先用一个set存下其中一个集合的所......
  • A Revisiting Study of Appropriate Offline Evaluation for Top-N Recommendation Al
    目录概实验设置EvaluationMetricsMetric的一致性不同的metrics导致的算法排名差异SampledmetricsSampledmetrics是否会导致和fullranking的metrics不同的评价数据集构建数据集的选择和预处理\(k\)-corefiltering的影响数据集的切分数据集的切分方式对结果的影响数据......
  • 查询指定用户的unique,primary索引名/键值
    --1.SQL用postgres账户查询PostgreSQL中指定DB以及schema下唯一索引的信息,按照表名:索引名:索引键值并按表名排序输出SELECTt.tablenameAStable_name,i.indexnameASindex_name,string_agg(a.attname,','ORDERBYa.attnum)ASindex_keysFROMpg_i......
  • prime1
    prime1主机发现发现服务、得到80http、22ssh有登录框就SQL注入、密码爆破无登录框就目录扫描目录扫描:dribdirsearch御剑dirbuster(kali终端输入,启动图形化界面)burpsuite普通扫描dirbhttp://192.168.218.146/得到http://192.168.218.146/devhttp://192.16......
  • Bluestein's Algorithm
    Bluestein'sAlgorithm用于当不是\(2\)的整数次幂时对多项式的(I)DFT。考虑现在要求:\[f_m=\sum\limits_{k=0}^{n-1}a_kw^{mk}\]Bluestein的核心思想在于拆\(mk\)。不难证明\(mk=\frac{m(m-1)}{2}+\frac{k(k+1)}{2}-\frac{(m-k)(m-k-1)}{2}\)。......
  • Fast Training Algorithms for Deep Convolutional Fuzzy Systems With Application t
    类似深度卷积神经网络DCNN,模糊系统领域有个深度卷积模糊系统deepconvolutionalfuzzysystem(DCFS),每一层都是一个模糊系统,上一层的输出是下一层的输入。这篇论文目的是加速DCFS的计算速度,解决可解释性1990年提出,也用反向传播训练DCFS受困于低维度小数据集,大数据量时计算负担太......