www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/MinimumSpanningForestFromSpecifiedRootNodeExample.m

    %% Minimum Spanning Forest from Specified Root Node
% Create and plot a graph that has multiple components. 

% Copyright 2015 The MathWorks, Inc.

s = {'a' 'a' 'a' 'b' 'b' 'c' 'e' 'e' 'f' 'f' 'f' 'f' 'g' 'g'};
t = {'b' 'c' 'd' 'c' 'd' 'd' 'f' 'g' 'g' 'h' 'i' 'j' 'i' 'j'};
G = graph(s,t);
p = plot(G,'Layout','layered');


%%
% Find the minimum spanning forest for the graph, starting at node |i|.
% Highlight the resulting forest in the plot. The graph node names are
% carried over into the minimum spanning tree graph.
[T,pred] = minspantree(G,'Type','forest','Root',findnode(G,'i'));
highlight(p,T)

%%
% Use the vector of predecessor nodes, |pred|, to create a directed version
% of the minimum spanning forest. All of the edges in this tree are
% directed away from the root nodes in each component (nodes |i| and |a|).
rootedTree = digraph(pred(pred~=0),find(pred~=0),[],G.Nodes.Name);
plot(rootedTree)