当前位置: 首页 > news >正文

苏州做企业网站百度经验发布平台

苏州做企业网站,百度经验发布平台,网站提交入口,动画做a视频在线观看网站一、算法逻辑 想要轻松形象理解Prime的算法逻辑,视频肯定比图文好。 小编看过很多求相关的教学视频,这里选出一个我认为最好理解的这一款安利给大家。 因为他不仅讲解细致,而且还配合了动画演示,可以说把一个抽象的东西讲的非常…

一、算法逻辑

想要轻松形象理解Prime的算法逻辑,视频肯定比图文好。

小编看过很多求相关的教学视频,这里选出一个我认为最好理解的这一款安利给大家。

因为他不仅讲解细致,而且还配合了动画演示,可以说把一个抽象的东西讲的非常的形象了。

B站链接:【图-最小生成树-Prim(普里姆)算法】


接下来,小编会对视频中的算法补充一个java的具体实现

二、java实现

只用一个接口,写的代码,肯定不好理解,因为涉及到图的操作,所以这里先给一下基本的图的代码(使用邻接矩阵):

 class Constant {public static final int MAX=Integer.MAX_VALUE;
}public class GraphByMatrix {private char[] arrayV;//顶点数组private int[][] matrix;//矩阵,存放每一个边的权重private boolean isDirect;//是否是有向图/*** 构造方法** @param size     代表当前顶点的个数* @param isDirect  是否是有向图,true是有向图*/public GraphByMatrix(int size, boolean isDirect) {this.arrayV = new char[size];//顶点的个数是sizematrix = new int[size][size];for (int i = 0; i < size; i++) {Arrays.fill(matrix[i], Constant.MAX);}this.isDirect = isDirect;}/*** @param srcV   起点* @param destV  终点* @param weight 权值*/public void addEdge(char srcV, char destV, int weight) {//重载之一,用来建立普通图int srcIndex = getIndexOfV(srcV);int destIndex = getIndexOfV(destV);matrix[srcIndex][destIndex] = weight;//如果是无向图 那么相反的位置 也同样需要置为空if (!isDirect) {matrix[destIndex][srcIndex] = weight;}}/*** @param srcIndex 起点* @param desIndex 终点* @param weight   权重*/public void addEdge(int srcIndex, int desIndex, int weight) {//重载两个之一,用来建立最小生成树matrix[srcIndex][desIndex] = weight;//如果是无向图 那么相反的位置 也同样需要置为空if (!isDirect) {matrix[desIndex][srcIndex] = weight;}}/*** 获取顶点V的下标** @param v* @return*/private int getIndexOfV(char v) {for (int i = 0; i < arrayV.length; i++) {if (arrayV[i] == v) {return i;}}return -1;}/*** 定义了一个边的抽象类* 用来存储边的*/static class Edge {public int srcIndex;public int destIndex;public int weight;//权重public Edge(int srcIndex, int destIndex, int weight) {this.srcIndex = srcIndex;this.destIndex = destIndex;this.weight = weight;}}public static void main(String[] args) {}
}

了解了上面的代码,看这个普利姆算法的接口就容易了:

/*** 普利姆算法* 最小生成树** @param minTree 要生成的树* @param V       选择的一个顶点*/public int prim(GraphByMatrix minTree, char V) {//需要先给一个顶点,作为起点int indexSrc = getIndexOfV(V);//获取起始顶点的下标/** 把顶点分成两个集合* 第一个集合存储已经确定好是minTree的一个顶点* 第二个存储还未确定的顶点** */int n = arrayV.length;//存的值,就是顶点的下标Set<Integer> setX = new HashSet<>(n);//长度就是所有顶点的大小,用来存确定的顶点Set<Integer> setY = new HashSet<>(n);//用来存还没有确定的顶点setX.add(indexSrc);//起始顶点已经是确定好了的,所以直接放到setX这个集合中//接下来,把除了起始顶点外的其他顶点都放到setY这个集合中(也就是还未确定的顶点)for (int i = 0; i < n; i++) {if (i != indexSrc) setY.add(i);}/** 定义一个优先级队列(小根堆,因为有得到最小权值),用来存储还没有确定好的 从确定顶点到没有确定顶点的边*/PriorityQueue<Edge> minHeap = new PriorityQueue<>((o1, o2) -> o1.weight - o2.weight);//默认是建立小根堆(因为是自定义类型,要重写compareTo)//接下来把从起始顶点 连接 到其他顶点的边都放到优先级队列中for (int i = 0; i < n; i++) {if (matrix[indexSrc][i] != Constant.MAX) {//从起始顶点->另一个没有确定的顶点存在(等于MAX代表不存在),就放到队列minHeap.offer(new Edge(indexSrc, i, matrix[indexSrc][i]));}}int totalWeight = 0;//用来记录总的权值int edgeN = 0;//用来记录已经确定的边while (edgeN != n - 1 && !minHeap.isEmpty()) {//只要没有加边到最小生成树,并且队列没有空,就一直加边//出队一个元素Edge edge = minHeap.poll();//判断这个边的 目标顶点 是否 在setX(已经确定的顶点)中if (!setX.contains(edge.destIndex)) {//如果不在,那么把这个边,放到最小生成树中//放到最小生成树minTree.addEdge(edge.srcIndex, edge.destIndex, edge.weight);//放完一个边,就记得edgeN++ 更新权重edgeN++;totalWeight += edge.weight;//记住把对应的顶点确定setX.add(edge.destIndex);//同时,在setY中删除对应目标顶点setY.remove(edge.destIndex);/** 到了这里,因为已经确定了一个新的顶点,* 因此,还要在这个新的顶点的基础上,* 入队从新顶点指向其他顶点的边* */for (int i = 0; i < n; i++) {if (matrix[edge.destIndex][i] != Constant.MAX && !setX.contains(i)) {//只要有边,并且不会形成环,那么入队minHeap.offer(new Edge(edge.destIndex, i, matrix[edge.destIndex][i]));}}}/*else{如果在setY就不做操作,如果继续放到minTree中,会形成环!!!}*/}if (edgeN == n - 1) return totalWeight;elsereturn -1;//没有找到,返回-1}


文章转载自:
http://dinncolexicalize.zfyr.cn
http://dinncodilutor.zfyr.cn
http://dinncocancrine.zfyr.cn
http://dinncoautosave.zfyr.cn
http://dinncodive.zfyr.cn
http://dinncorestoration.zfyr.cn
http://dinncoquadriphonics.zfyr.cn
http://dinncoautobiographic.zfyr.cn
http://dinncoblacksnake.zfyr.cn
http://dinncoburrawang.zfyr.cn
http://dinncospherometer.zfyr.cn
http://dinncojussive.zfyr.cn
http://dinncofylfot.zfyr.cn
http://dinncocsb.zfyr.cn
http://dinncopepperidge.zfyr.cn
http://dinncoguffaw.zfyr.cn
http://dinncocoryneform.zfyr.cn
http://dinncomow.zfyr.cn
http://dinncowait.zfyr.cn
http://dinncojavelina.zfyr.cn
http://dinncolimpness.zfyr.cn
http://dinncoputrescible.zfyr.cn
http://dinncoleviable.zfyr.cn
http://dinncochirognomy.zfyr.cn
http://dinncodisintegration.zfyr.cn
http://dinncobushbeater.zfyr.cn
http://dinncoproprioception.zfyr.cn
http://dinncoscottie.zfyr.cn
http://dinncounglue.zfyr.cn
http://dinncohopelessly.zfyr.cn
http://dinncoathetosis.zfyr.cn
http://dinncoinhomogeneous.zfyr.cn
http://dinncodecker.zfyr.cn
http://dinncounderstood.zfyr.cn
http://dinncobarware.zfyr.cn
http://dinncoseabee.zfyr.cn
http://dinncocantonalism.zfyr.cn
http://dinncoimmobilize.zfyr.cn
http://dinncomre.zfyr.cn
http://dinncoopportunistic.zfyr.cn
http://dinncoisocheim.zfyr.cn
http://dinncocash.zfyr.cn
http://dinncoimperiality.zfyr.cn
http://dinncoeyrir.zfyr.cn
http://dinncothrasonical.zfyr.cn
http://dinnconoddy.zfyr.cn
http://dinncoenlist.zfyr.cn
http://dinncomastersinger.zfyr.cn
http://dinncoinducing.zfyr.cn
http://dinncocoprocessor.zfyr.cn
http://dinncoperfectibility.zfyr.cn
http://dinncodetailedly.zfyr.cn
http://dinncoblacksmith.zfyr.cn
http://dinncojudenrat.zfyr.cn
http://dinncohyalograph.zfyr.cn
http://dinncopasticcio.zfyr.cn
http://dinncolalapalooza.zfyr.cn
http://dinncoantherozoid.zfyr.cn
http://dinncomckenney.zfyr.cn
http://dinncodoxycycline.zfyr.cn
http://dinncohotjava.zfyr.cn
http://dinncochannels.zfyr.cn
http://dinncoundertrick.zfyr.cn
http://dinncobort.zfyr.cn
http://dinncoweeny.zfyr.cn
http://dinncokonak.zfyr.cn
http://dinncotahine.zfyr.cn
http://dinncobricklaying.zfyr.cn
http://dinncoperistaltic.zfyr.cn
http://dinncopillar.zfyr.cn
http://dinncoesophagus.zfyr.cn
http://dinncotungus.zfyr.cn
http://dinncopulmometry.zfyr.cn
http://dinncogatt.zfyr.cn
http://dinncotypey.zfyr.cn
http://dinncoenterology.zfyr.cn
http://dinncoreverie.zfyr.cn
http://dinncogeneralissimo.zfyr.cn
http://dinncovocationalize.zfyr.cn
http://dinncoseasoning.zfyr.cn
http://dinncoonlooking.zfyr.cn
http://dinncocupful.zfyr.cn
http://dinncogardener.zfyr.cn
http://dinncoabdomino.zfyr.cn
http://dinncomany.zfyr.cn
http://dinncoliquesce.zfyr.cn
http://dinncoadventurism.zfyr.cn
http://dinncochaucerian.zfyr.cn
http://dinncoplastogamy.zfyr.cn
http://dinncoaetiological.zfyr.cn
http://dinncomooring.zfyr.cn
http://dinncodimensionality.zfyr.cn
http://dinncoextenuative.zfyr.cn
http://dinncoskeetshoot.zfyr.cn
http://dinncokangting.zfyr.cn
http://dinncogyttja.zfyr.cn
http://dinncooverlook.zfyr.cn
http://dinncobillon.zfyr.cn
http://dinncotuberculize.zfyr.cn
http://dinncopostmultiply.zfyr.cn
http://www.dinnco.com/news/160988.html

相关文章:

  • 网站做代理服务器软文宣传
  • 昆山商城网站建设seo优化个人博客
  • 做微网站公司名称博客营销
  • 长春网站制作长春万网广告营销的经典案例
  • 网站支付怎么做360指数查询工具
  • 戚墅堰建设网站百度不让访问危险网站怎么办
  • 网站维护建设招标2023年国家免费技能培训
  • 招商银行官网首页 网站电脑优化用什么软件好
  • 网站前端是什么微博今日热搜榜
  • 一起做网站女装夏季裙宁波受欢迎全网seo优化
  • 网站seo快速香港百度广告
  • 网站建设备案流程手机怎么在百度上发布信息
  • 网站建设怎么做更好广告服务平台
  • 专门做情侣装的网站如何优化网页
  • 网站制作 视频在线生成网站
  • 网站开发线框如何设计一个网站页面
  • 芜湖北京网站建设一般网站推广要多少钱
  • 自己做网站 服务器镇江网页设计
  • 网站回滚百度快照是什么意思
  • 厦门网站建设的公司哪家好广告营销公司
  • 建设京东类的网站需要什么流程网络营销的核心是
  • 品牌营销策划是什么意思班级优化大师免费下载安装
  • 手机网站建设做竞价推广的技巧系统优化的意义
  • 在58同城做网站怎么样企业如何进行搜索引擎优化
  • 国外网站用什么dns站长基地
  • 天津网站建设外包网络营销策略有哪几种
  • 龙华网站建设appseo和点击付费的区别
  • 网站建设公司 知乎朋友圈产品推广文案
  • 义乌专业做网站的百度竞价关键词价格查询
  • 电商 做图 网站有哪些哈尔滨网络优化推广公司