function re=biaoji(j,biao) %判断j点是否已被标记
l=length(biao);
for i=1:l
if j==biao(i)
re=1;
return;
end
end
re=0;
return;
end

clc;
clear all;
close all;

G=[0 4 0 0 0 0 0 8 0;
4 0 8 0 0 0 0 11 0;
0 8 0 7 0 4 0 0 2;
0 0 7 0 9 14 0 0 0;
0 0 0 9 0 10 0 0 0;
0 0 4 14 10 0 2 0 0;
0 0 0 0 0 2 0 1 6;
8 11 0 0 0 0 1 0 7;
0 0 2 0 0 0 6 7 0];
tmp = find(G~=0);
g = G(tmp);
k =length(G(:,1));
[tmpa,tmpb] = find(G~= 0);
e = [tmpa,tmpb];
E =[g,tmpa,tmpb];
[x,y] = cylinder(1,k);
plot(x(1,:),y(1,:),'r*','markersize',10,'linewidth',1);
axis([-1.5,1.5,-1.5,1.5]);
hold on
for i = 1 :k
tem = ['V',int2str(i)];
text(x(1,i)+0.05,y(1,i),tem);
end
for i = 1:length(e(:,1))
plot(x(1,e(i,:)),y(1,e(i,:)),'b','linewidth',1);
end
%% prim algorithm 
[m,n] = size(G);
q = [1];
k = 1;
A = [];
while length(q) ~= m
e = [];
for i = 1:k
for j = 1:n
if G(q(i),j)~= 0 && ~biaoji(j,q)
e = [e;G(q(i),j) q(i),j];
end
end
end
[junk index]=min(e(:,1)); %求与当前标记的所有元素相邻的权重最小的边的索引
A=[A;e(index,:)]; %最小生成树的三元组表示
q=[q e(index,3)];
k=k+1; 
end

a = A(:,2:3);
for i = 1:length(a(:,1))
plot(x(1,a(i,:)),y(1,a(i,:)),'r-.','linewidth',2);
end

prim algorithm的更多相关文章

  1. algorithm@ dijkstra algorithm & prim algorithm

    #include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...

  2. prim算法

    最小生成树 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边.最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里姆)算法 ...

  3. HackerRank "Prim's (MST) : Special Subtree"

    An intuitive Prim algorithm impl. #include <vector> #include <iostream> #include <que ...

  4. 最小生成树算法prim and kruskal

    一.最小生成树定义:  从不同顶点出发或搜索次序不同,可得到不同的生成树  生成树的权:对连通网络来说,边附上权,生成树也带权,我们把生成树各边的权值总和称为生成树的权  最小代价生成树:在一个连通网 ...

  5. Prim Algoritm(最小生成树)

    Prim Algorithm.这个算法可以分为下面几个步骤: 将顶点集V分成两个集合A和B,其中集合A表示目前已经在MST中的顶点,而集合B则表示目前不在MST中的顶点. 在B寻找与集合A连通的最短的 ...

  6. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  7. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. Greedy is Good

    作者:supernova 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=greedyAlg Joh ...

  9. UESTC_秋实大哥与连锁快餐店 2015 UESTC Training for Graph Theory<Problem A>

    A - 秋实大哥与连锁快餐店 Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

随机推荐

  1. 【基于PUPPETEER前端自动化框架】【一】TypeScript+Puppeteer+Jest 整合

    前提:掌握Jest + Puppeteer 1.Jest环境配置 2.Jest-MATCHERS匹配器 3.Jest-全局变量设置 4.Puppeteer安装 5.Puppeteer元素获取 6.Pu ...

  2. Robot Framework安装和入门

    1:安装 python 安装python并且配置好环境变量 2:安装 Robot Framework pip install robotframework 3:安装GUI界面 pip install ...

  3. Java类的组合

    1 package 类的组合; 2 3 public class Clock { 4 private int hour,minute,second;//字段: 保存分秒数据 5 public void ...

  4. 网络篇:朋友面试之TCP/IP,回去等通知吧

    前言 最近和一同学聊天,他想换工作,然后去面了一家大厂.当时,他在简历上写着精通TCP/IP,本着对TCP协议稍有了解,面试官也不会深问的想法,就写了精通二字.没想到,大意了 关注公众号,一起交流,微 ...

  5. 【微信开发】缓存的asscess_token过期

    开发中有遇到这样一个问题,我们一般会将从微信拿到的寿命2个小时的access_token缓存起来,业务里这个缓存的时间是90分钟, 90分钟之后缓存过期,会重新请求新的access_token使旧的a ...

  6. redis数据量大时bgsave线程阻塞redis原因

    rt 转载 Latency generated by fork In order to generate the RDB file in background, or to rewrite the A ...

  7. 使用@RequestBody注解获取Ajax提交的json数据

    最近在学习有关springMVC的知识,今天学习如何使用@RequestBody注解来获取Ajax提交的json数据内容. Ajax部分代码如下: 1 $(function(){ 2 $(" ...

  8. 「刷题笔记」LCA问题相关

    板子 ll lg[40]; ll dep[N],fa[N][40]; ll dis[N]; void dfs(ll u,ll f) { dep[u]=dep[f]+1; fa[u][0]=f; for ...

  9. moviepy用VideoFileClip加载视频时报UnicodeDecodeError: utf-8 codec cant decode byte invalid start byte错误

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 使用moviepy用: clip1 = Video ...

  10. 转:浅谈HTTP中Get、Post、Put与Delete的区别

    1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行多少次操 ...