# How to partition the diagram into disconnected sets of nodes and links?

I am using horizontal, vertical and grid layout… I have requirement to find number of tree…
Ex:-

A–>B–>E–>K
H–>E
C–>E

M–>L–>N–>Y
R–>L

Z–>G

Now we can say 3 tree are available, how we can find tree which own chain not mixed with other tree…

Well, your problem description seems ambiguous to me. Can you have this situation?
A - B - C
Z - B
Y - C

A - D - E
Z - D - E

Anyway, it seems to me you at least need to find nodes that share the same identifiers, such as B and C in my first example.

Yes, your example also possible… but my need is total tree/map in canvas…
A - B - C
Z - B
Y - C
Is one Tree/map

A - D - E
Z - D - E
Is one tree/map

So both tree/map is in page/canvas…
I need count 2…

Let me take one more example:
Let’s say I have attached tree… in that I need count that is 3 as 3 tree are available which is not shared any node with other tree

Let’s see below to understand count of 3…

You are not using GridLayout, so my understanding of your problem was completely wrong.

The sketches help. But it makes clear that you do not have trees, either. It is not the case that every node has at most one parent. Otherwise there would be a trivial answer, the number of roots.

Are you using LayeredDigraphLayout?

Yes, Iam using LayeredDigraphLayout

OK, it is easy enough to partition a set of nodes into a collection of subsets of nodes, where there are no links (in either direction) between any two subsets. Here is some JavaScript code:

``````  function computeSubsets(nodes) {
var colls = new go.Set();

function findSubset(node) {
var it = colls.iterator;
while (it.next()) {
var coll = it.value;
if (coll.has(node)) return coll;
}
return null;
}

while (allnodes.count > 0) {
var node = allnodes.first();
var subset = findSubset(node);
if (!subset) {
var newset = new go.Set();

function followSubset(node) {
if (!newset.has(node)) {
allnodes.remove(node);
node.findNodesConnected().each(followSubset);
}
}

So now you can evaluate `computeSubsets(myDiagram.nodes)` to get a go.Set