您的位置:首页 > 其它

有关图的存储结构的程序3(图论算…

2016-04-08 21:45 316 查看
用广度优先搜索建立图的邻接矩阵。

程序:

var
state:array[0..100] of longint;
v:array[0..100] of boolean;
g:array[0..100,0..100] of longint;
n,e:longint;

procedure bfs;
var

head,tail,i,ans:longint;
begin
head:=0;
tail:=1;
state[1]:=1;
v[1]:=false;
ans:=1;
repeat

inc(head);
for
i:=1 to n do

if (g[state[head],i]=1)and(v[i]) then

begin

g[state[head],i]:=0;

g[i,state[head]]:=0;

inc(tail);

state[tail]:=i;

v[i]:=false;

end;
until
head>=tail;
for i:=1 to head
do

write(state[i],' ');
end;

procedure init;
var
x,y,i:longint;
begin
readln(n,e);
for i:=1 to e do

begin

readln(x,y);

g[x,y]:=1;

g[y,x]:=1;

end;

fillchar(v,sizeof(v),true);
end;

begin
init;
bfs;
end.

版权属于: Chris

原文地址: http://blog.sina.com.cn/s/blog_83ac6af80102v0tv.html

转载时必须以链接形式注明原始出处及本声明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: