finallyprim.m ( File view )

  • By mulangic 2016-05-09
  • View(s):0
  • Download(s):0
  • Point(s): 1
			clear all;
close all;
Graph1;%调用Graph1M文件,产生图1的邻接矩阵
%Graph2;%调用Graph2M文件,产生图2的邻接矩阵

len=length(graph_adjacent);%求图中有多少个顶点
k=sprintf('please input the point where you want to start ,do remember it must be between 1 and %d ',len);
start_point=input(k);%输入最小生成树产生起点
while((start_point<=0)|(start_point>len))%如果输入的结点位置不合法即:小于等于零,或大于结点数,则重新输入
    disp('bad positon,please input again!');
    start_point=input(k);
end;
%************************************下面完成prim算法****************************
%相关变量初始设置
tree=zeros(len-1,2);%用于保存选入最小生成树的边
lowcost=zeros(1,len);%用来保存集合V-U与集合U中顶点的最短边权值,lowcost[v]=0表示顶点v已经
                   %加入最小生成树中
adjvex=zeros(1,len);%用来保存依附于该边在集合U中的节点,U集合为生成最小生成树的辅助集合,
                   %首先U={
start_point
},之后依次确定为把最小生成树的一边的另一节点加入U
                   %依次下去,直到图的全部顶点都在U中能找到
               
lowcost=graph_adjacent(start_point,:);%lowcost(i)的值为节点i与start_point的权值;
adjvex=start_point.*ones(1,len);%adjvex中所有元素的值都为初始节点
%以下循n-1次,用于找出最小生成树的len-1条边

for i=1:len-1
    k=lowcost>0;%k为一逻辑数组,它和lowcost同维,对于每一个位置i1lowcost(i)>0则k(i)=1
                %否则k(i)=0;稍候将用这个数组进行辅助寻址
    cost_min=min(lowcost(k));%找出lowcost中除0外的最小值
    index=find(lowcost==cost_min);%找出此最小值在lowcost中的下标,即找到相应的节点 
    index=index(1);%因为最
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
finallykruskal.m1.98 kB14-06-08|10:51
finallyprim.m2.35 kB14-06-08|10:51
Graph1.m321.00 B09-06-08|15:26
Graph11.m333.00 B09-06-08|21:11
Graph2.m392.00 B09-06-08|16:17
Graph22.m408.00 B09-06-08|21:12
kruskalenhance.m2.12 kB14-06-08|13:08
Readme.txt83.00 B14-06-08|13:29
test.m153.00 B14-06-08|10:21
testsparse.m187.00 B14-06-08|10:44
计算机仿真final.doc586.50 kB16-09-08|18:31
code0.00 B17-11-08|22:23
prim0.00 B0|0%
...
Sponsored links

finallyprim.m (330.27 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D