-
Notifications
You must be signed in to change notification settings - Fork 76
/
BFS.m
executable file
·114 lines (90 loc) · 2.44 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
% Simple implementation of breadth-first-search of a graph.
% Returns a breadth-first-search tree.
%
% INPUTs: adjacency list (nxn), "adjL"
% start node index, "s"
% end node index, "t"
% OUTPUTs: BFS tree, in adjacency list {} format (directed)
% (This is the tree that the algorithm walks in
% search of the target. If the target is not found,
% this tree is effectively a spanning tree of the
% entire graph.
%
% Last updated: Nov 8 2014
function T = BFS(adjL, s, t)
discovered = [s]; % mark visited
q = [s]; % add to queue
T = cell(1, length(adjL)); % initialize search path/tree
if s == t
printf('BFS(): The start and the end node are the same.\n')
return
end
while not(isempty(q))
j = q(1); q = q(2:length(q)); % pop the front
neigh = adjL{j};
for nn = 1:length(neigh)
if isempty(find(discovered == neigh(nn)))
T{j} = [T{j}, neigh(nn)];
if neigh(nn) == t
return
end
discovered = [discovered, neigh(nn)];
q = [q, neigh(nn)];
end
end
end
printf('BFS(): Target node not found.\n')
%!test
%!shared adjL, T, tt
%! T = load_test_graphs();
%! adjL = {1:2, 2:[]};
%! tt = BFS(adjL, 1, 1);
%!assert(tt, {[], []})
%! tt = BFS(adjL, 1, 2);
%!assert(tt{1}, 2)
%!assert(length(tt),2)
%!assert(class(tt),'cell')
%! tt = BFS(adjL, 2, 2);
%!assert(tt, {[],[]})
%! tt = BFS(adjL, 2, 3);
%!assert(tt, {[],[]})
%!assert(length(tt),2)
%!assert(class(tt),'cell')
%! tt = BFS(adjL, 1, 3);
%!assert(tt{1}, 2)
%!assert(tt{2}, [])
%!assert(length(tt),2)
%!assert(class(tt),'cell')
%! tt = BFS(T{9}{2}, 1, 4);
%!assert(tt{1},[2 3])
%!assert(tt{2},[])
%!assert(tt{3},[4])
%!assert(tt{4},[])
%!assert(tt{5},[])
%!assert(tt{6},[])
%! tt = BFS(T{9}{2}, 2, 6);
%!assert(tt{2},[1 3])
%!assert(tt{1},[])
%!assert(tt{3},[4])
%!assert(tt{4},[5 6])
%!assert(tt{5},[])
%!assert(tt{6},[])
%! tt = BFS(T{9}{2}, 5, 2);
%!assert(tt{5},[4 6])
%!assert(tt{6},[])
%!assert(tt{4},[3])
%!assert(tt{3},[1 2])
%!assert(tt{2},[])
%!assert(tt{1},[])
%! tt = BFS(T{9}{2}, 5, 10);
%!assert(tt{5},[4 6])
%!assert(tt{6},[])
%!assert(tt{4},[3])
%!assert(tt{3},[1 2])
%!assert(tt{2},[])
%!assert(tt{1},[])
%!demo
%! % adjacency list representation of the bowtie graph (I>−<I)
%! L = {[2, 3], [1, 3], [1, 2, 4], [3, 5, 6], [4, 6], [4, 5]};
%! BFS(L, 1, 6)
%! BFS(L, 5, 3)