-
Notifications
You must be signed in to change notification settings - Fork 2
/
bfs.m
300 lines (189 loc) · 7.15 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function [searchNodes,iterations] = bfs(source,target,names,startNode)
% BFS performs breadth first search on graph with source and target
% vectors.
%
% Syntax:
%
% [searchNodes,iterations] = bfs(source,target,names,startNode)
% [searchNodes,iterations] = bfs(source,target,startNode)
%
% Inputs:
%
% source = Vector or cell array containing starting node of each of the edge.
% target = Vector or cell array containing ending node of each of the edge.
% names = Cell array containing string names of each of the node.
% startNode = Initial node in the graph.
%
% Outputs:
%
% path = Cell array containing search path.
% iterations = Table containing bfs iteration summary.
%
% Example 01:
%
% s = {'A','A','A','B','B','C'};
% t = {'B','C','D','E','F','G'};
% [searchNodes,iterations] = bfs(s,t,'A')
%
% Example 02:
%
% s = [1 1 1 2 2 3];
% t = [2 3 4 5 6 7];
% names = {'A','B','C','D','E','F','G'};
% [searchNodes,iterations] = bfs(s,t,names,'A')
%
% Example 03:
%
% s = [1 1 1 2 2 3];
% t = [2 3 4 5 6 7];
% [searchNodes,iterations] = bfs(s,t,1)
%
% Coded by Ali Asghar Manjotho
% Lecturer, CSE-MUET
% Email: ali.manjotho.ali@gmail.com
iterations = table;
% Refactor source, target, names & startNode vectors to numbers
% If names argument missing
if (nargin<4)
% Third argument (i.e. names) is starting Node
startingNode = names;
[s,t,n,sNode] = refactor(source,target,startingNode);
else
% Fourth argument (i.e. startNode) is starting Node
startingNode = startNode;
[s,t,n,sNode] = refactor(source,target,names,startingNode);
end
% Get all unique nodes from source and target vectors
uniqueNodes = getNodes(s,t);
% Initialize visited list and queue
visited = [];
queue = [];
% Set starting node as current node and add it in to visited list
currentNode = sNode;
visited = [visited sNode];
% Local variables to track iteration number
iteration = 1;
% Update Iterations table
iterations = [iterations; updateTable(s,t,n,currentNode,queue,visited,iteration,'Starting Node')];
% Repeat until queue is empty
while(~isempty(currentNode))
% Get all childs of current node
childs = getChilds(s,t,currentNode);
for i=1:length(childs)
% If new unvisited child found add it in queue and visited list
if(length(find(visited==childs(i)))==0)
queue = [queue childs(i)];
visited = [visited childs(i)];
% Increase iteration number
iteration = iteration + 1;
iterations = [iterations; updateTable(s,t,n,currentNode,queue,visited,iteration,strcat('Unvisited node found ',n(childs(i))))];
end
end
% If no new child found for current node then remove first item
% from queue and make it as current node
if (length(queue)>0)
currentNode = queue(1);
queue(1) = [];
comments = strcat('Dequeue ',n(currentNode),' from queue');
iteration = iteration + 1;
iterations = [iterations; updateTable(s,t,n,currentNode,queue,visited,iteration,comments)];
else
currentNode = [];
end
end
% Update iteration table for last state of queue, visited list, current
% node and search path when after queue is empty
if(iteration > 0)
iteration = iteration +1;
iterations = [iterations; updateTable(s,t,n,currentNode,queue,visited,iteration,'BFS Converged')];
end
searchNodes = n(visited);
iterations.Properties.VariableNames = {'Iteration' 'CurrentNode' 'Queue' 'Visited' 'Comments'};
end
function childs = getChilds(source,target,node)
childs = sort(target(find(source==node)));
end
function nodes = getNodes(s,t)
nodes = unique(horzcat(s,t));
end
function [s,t,n,sn] = refactor(source,target,names,startNode)
% If names argument missing
if (nargin<4)
% Third argument (i.e. names) is starting Node
sn = names;
else
% Fourth argument (i.e. startNode) is starting Node
sn = startNode;
end
% Get all unique nodes
uNodes = unique(horzcat(source,target));
% If source and target are cell arrays
if(iscell(source) && iscell(target))
% If names argument missing
if(nargin<4)
n = uNodes;
else
n = names;
end
% Get unique nodes cell array
uNodes = unique(horzcat(source,target));
s = [];
t = [];
% Populate source and target with equivalent numeric values
for i=1:length(source)
[sFound,sIndex] = ismember(source(i),uNodes);
[tFound,tIndex] = ismember(target(i),uNodes);
s = [s sIndex];
t = [t tIndex];
end
else
s = source;
t = target;
% If names argument missing
if(nargin<4)
uNodes = unique(horzcat(source,target));
n = cell(1,length(uNodes));
for i=1:length(uNodes)
n{i} = num2str(uNodes(i));
end
else
n = names;
end
end
% If starting node is not a number
if(~isnumeric(sn))
sn = find(ismember(n,sn));
end
end
function tableIteration = updateTable(s,t,n,currentNode,queue,visited,iteration,comments)
uniqueNodes = getNodes(s,t);
% Display current queue
queueStr = '[';
for i=length(queue):-1:1
if(i==1)
queueStr = strcat(queueStr,sprintf('%s',char(n(find(uniqueNodes==queue(i))))));
else
queueStr = strcat(queueStr,sprintf('%s ',char(n(find(uniqueNodes==queue(i))))));
end
end
queueStr = strcat(queueStr,sprintf(']'));
% Display current visited list
visitedStr = '[';
for i=1:length(visited)
if(i==length(visited))
visitedStr = strcat(visitedStr,sprintf('%s',char(n(find(uniqueNodes==visited(i))))));
else
visitedStr = strcat(visitedStr,sprintf('%s ',char(n(find(uniqueNodes==visited(i))))));
end
end
visitedStr = strcat(visitedStr,sprintf(']'));
% Display current node
if(~isempty(currentNode))
node = n(currentNode);
currentNodeStr = sprintf('[%s]',node{1,1}(1,1));
else
currentNodeStr = sprintf('[]');
end
array = {iteration currentNodeStr queueStr visitedStr comments};
tableIteration = cell2table(array);
end