-
Notifications
You must be signed in to change notification settings - Fork 8
/
bfs.m
85 lines (81 loc) · 2.67 KB
/
bfs.m
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function [shortest_paths,distance] = bfs(A,s,t)
% breathe-first search algorithm as described in "Network: an Introduction
% (Newman 2010)"
% Input:
% A: adjacency matrix of the network
% s: starting node
% t: destination node
% Outputs:
% shortest_paths: all the shortest paths from s to t
% distance: vector containing distances from s to all other nodes
% Author: Anh-Dung Nguyen
% Email: [email protected]
distance = ones(1,size(A,1))*-1;
distance(s) = 0;
queue = zeros(1,size(A,1));
queue(1) = s;
read_pointer = 1;
write_pointer = 2;
sp_tree = zeros(size(A,1));
found = 0;
while read_pointer~=write_pointer % algorithm is terminated when the read and write pointers are pointing to the same element
d = distance(queue(read_pointer)); % find the distance d for the vertex in queue
neighbor = setdiff(find(A(queue(read_pointer),:)==1),queue(read_pointer)); % find neighboring vertex
for i=1:length(neighbor)
if distance(neighbor(i))==-1 % if the distance of the neighboring vertex is unknown
distance(neighbor(i)) = d+1; % assigne it distance d+1
queue(write_pointer) = neighbor(i); % store its label in the queue
write_pointer = write_pointer+1; % increase the write pointer by one
sp_tree(neighbor(i),queue(read_pointer)) = 1; %
elseif distance(neighbor(i))==d+1
sp_tree(neighbor(i),queue(read_pointer)) = 1;
end
if neighbor(i)==t
found = 1;
break;
end
end
if found
break;
end
read_pointer = read_pointer+1; % increase the read pointer to read the following element
end
% reconstruction of all the shortest paths
if found
if s==t
shortest_paths = [];
else
shortest_paths = find_paths(s,t);
end
else
shortest_paths = [];
end
function p = find_paths(u,v)
if sp_tree(v,u)==1
p = v;
else
neighbors = find(sp_tree(v,:)==1);
if length(neighbors)==1
pp = find_paths(u,neighbors);
if ~iscell(pp)
p = cat(2,pp,v);
else
for m=1:length(pp)
p{m} = cat(2,pp{m},v);
end
end
else
for n=1:length(neighbors)
pp = find_paths(u,neighbors(n));
if ~iscell(pp)
p{n} = cat(2,pp,v);
else
for m=1:length(pp)
p{m+n-1} = cat(2,pp{m},v);
end
end
end
end
end
end
end