用广度优先搜索建立图的邻接矩阵。
state:array[0..100] of longint;
v:array[0..100] of boolean;
g:array[0..100,0..100] of longint;
n,e:longint;
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],' ');
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);
init;
bfs;
程序:
var
procedure bfs;
end;
procedure init;
end;
begin
end.
本文介绍了一个使用广度优先搜索(BFS)算法来更新图的邻接矩阵的程序示例。通过遍历图中的节点并清除已访问节点之间的连接,该程序展示了如何在图上应用BFS。此外,还提供了初始化图结构并读取边的方法。

168万+

被折叠的 条评论
为什么被折叠?



