LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
weaklyconncomp.m
1%{ @file weaklyconncomp.m
2 % @brief Finds weakly connected components in a directed graph
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Finds weakly connected components in a directed graph
9 %
10 % @details
11 % Returns the weakly connected components in a graph G.
12 %
13 % @par Syntax:
14 % @code
15 % [S, C] = weaklyconncomp(G)
16 % @endcode
17 %
18 % @par Parameters:
19 % <table>
20 % <tr><th>Name<th>Description
21 % <tr><td>G<td>Adjacency matrix of the graph
22 % </table>
23 %
24 % @par Returns:
25 % <table>
26 % <tr><th>Name<th>Description
27 % <tr><td>S<td>Number of connected components
28 % <tr><td>C<td>Component assignment vector (C(i) is the component index of node i)
29 % </table>
30%}
31function [S,C] = weaklyconncomp(G)
32 G(isnan(G)) = 1;
33 [p,~,r] = dmperm(G'+speye(size(G)));
34 S = numel(r)-1;
35 C = cumsum(full(sparse(1,r(1:end-1),1,1,size(G,1))));
36 C(p) = C;
37end