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

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)

% Copyright 2015 The MathWorks, Inc.