File: bfs_builtin.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (55 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (2)
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
function v = bfs_builtin (A, s)
%BFS_BUILTIN breadth-first-search using purely built-in methods
%
% v = bfs_builtin (A, s)
%
% A is a square binary matrix, corresponding to the adjacency matrix of a
% graph, with A(i,j)=1 denoting the edge (i,j).  Self loops are permitted, and
% A may be unsymmetric.  s is a scalar input with the source node.  The output
% v is the level each node in the graph, where v(s)=1 (the first level), v(j)=2
% if there is an edge (s,j) (the 2nd level), etc.  v(j)=k if node j is in the
% kth level, where the shortest path (in terms of # of edges) from  s to j has
% length k+1.  The source node s defaults to 1.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

[m, n] = size (A) ;
if (m ~= n)
    error ('A must be square') ;
end
n = size (A,1) ;
v = zeros (n,1) ;

if (nargin < 2)
    s = 1 ;
end

% ensure A is binary, and transpose it
AT = spones (A') ;

q = zeros (n,1) ;
q (s) = 1 ;         % q is the current level

for level = 1:n

    % assign the level to all nodes in q
    v (q ~= 0) = level ;

    % find all neighbors of q, as a binary vector
    qnew = spones (AT * q) ;

    % discard nodes in qnew that are already seen
    qnew (v ~= 0) = 0 ;         %#ok

    % move to the new level
    q = qnew ;

    % stop if the new level is empty
    if (~any (q))
        break ;
    end

end