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. Shell编程规范:Don't use ls | grep

    目录 一.背景 二.ShellCheck的规范 三.修改写法 1.ls | grep file 2.ls | grep regexp 3.ls | grep -v multi 四.最后 五.参考 一. ...

  2. Java IDEA 根据mybatis-generator-core自动生成代码支持sqlserver获取备注(二)

    mybatis generator代码生成虽然好用,但是好像不支持sqlserver获取备注信息,这里我主要分享mybatis generator改写后支持sqlserver获取备注信息,mysql以 ...

  3. laravel数据填充

    post表有这2个字段 填充20条数据, 执行 php artisan tinker 执行预览 factory(App\Post::class,20)->make(); 插入数据库 factor ...

  4. ECS服务器快速迁移

    ECS服务器快速迁移 前提 一.停机 二.创建镜像 三.复制镜像 前提 服务器都在同一个区域,举例:华南1(深圳) 同一个账号 具体迁移步骤如下: 一.停机 先从阿里云ECS控制台,将要迁移的两台EC ...

  5. C语言讲义——变量的输出

    变量输出·常规 头文件:stdio.h 函数: printf("%", ); 需要占位符%_,要输出几个变量就需要几个占位符. 类型 占位符 助记 int %d d表示十进制dec ...

  6. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  7. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  8. 冰河教你一次性成功安装K8S集群(基于一主两从模式)

    写在前面 研究K8S有一段时间了,最开始学习K8S时,根据网上的教程安装K8S环境总是报错.所以,我就改变了学习策略,先不搞环境搭建了.先通过官网学习了K8S的整体架构,底层原理,又硬啃了一遍K8S源 ...

  9. 老猿学5G专栏文章目录

    ☞ ░ 前往老猿Python博文目录 ░ 一.3GPP规范文档及其他推荐阅读博文 老猿学5G扫盲贴:3GPP规范文档命名规则及同系列文档阅读指南 老猿学5G扫盲贴:3GPP规范中部分与计费相关的规范序 ...

  10. Python模块学习遇到的问题

    Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案 Python使用import导入模块 ...