1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
////BFS Traversal
funcprot(0)
function[q2]=push(ele,q1)
if(q1.rear==q1.front)
q1.a=ele;
q1.rear=q1.rear+1;
else
q1.a=[q1.a(:,:) ele];
q1.rear=q1.rear+1;
end
q2=q1;
endfunction
function[ele,q2]=pop(q1)
ele=-1;
q2=0;
if(q1.rear==q1.front)
return;
else
ele=q1.a(q1.rear-q1.front);
q1.front=q1.front+1;
i=1;
a=q1.a(1);
for i=2:(q1.rear-q1.front)
a=[a(:,:) q1.a(i)];
end
q1.a=a;
end
q2=q1;
endfunction
function[]=Bfs(adj,n);
i=1,j=1;
colour=[];
for i=1:n
for j=1:n
colour=[colour(:,:) 0];
end
end
disp("The BFS Traversal is");
bfs(adj,colour,1,n);
endfunction
function[]=bfs(adj,colour,s,n)
colour(s)=1;
q=struct('rear',0,'front',0,'a',0);
q=push(s,q);
while((q.rear)-(q.front)>0)
[u,q]=pop(q);
disp(u," ");
for i=1:n
if(adj((u-1)*n+i)&(colour(i)==0))
colour(i)=1;
q=push(i,q);
end
end
colour(u)=2;
end
endfunction
//Calling Routine:
n=4;
adj=[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0]
Bfs(adj,n)
|