components.m

来自「数据降维工具箱」· M 代码 · 共 47 行

M
47
字号
function blocks = components(A)%COMPONENTS Finds connected components in a graph defined by a adjacency matrix%%   blocks = components(A)%% Finds connected components in a graph defined by the adjacency matrix A.% The function outputs an n-vector of integers 1:k in blocks, meaning that% A has k components. The vector blocks labels the vertices of A according % to component.% If the adjacency matrix A is undirected (i.e. symmetric), the blocks are % its connected components. If the adjacency matrix A is directed (i.e. % unsymmetric), the blocks are its strongly connected components.%%% This file is part of the Matlab Toolbox for Dimensionality Reduction v0.3b.% The toolbox can be obtained from http://www.cs.unimaas.nl/l.vandermaaten% You are free to use, change, or redistribute this code in any way you% want for non-commercial purposes. However, it is appreciated if you % maintain the name of the original author.%% (C) Laurens van der Maaten% Maastricht University, 2007    % Check size of adjacency matrix    [n, m] = size(A);    if n ~= m, error ('Adjacency matrix must be square'), end;    % Compute Dulmage-Mendelsohn permutation on A    if ~all(diag(A))         [foo, p, bar, r] = dmperm(A | speye(size(A)));    else        [foo, p, bar, r] = dmperm(A);      end    % Compute sizes and number of clusters    sizes = diff(r);    k = length(sizes);    % Now compute the array blocks    blocks = zeros(1, n);    blocks(r(1:k)) = ones(1,k);    blocks = cumsum(blocks);    % Permute blocks so it maps vertices of A to components    blocks(p) = blocks;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?