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…




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

Any suggestions please…

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

Or what about this?
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…
Let me take your ex:
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 allnodes = new go.Set().addAll(nodes);
    var colls = new go.Set();

    function findSubset(node) {
      var it = colls.iterator;
      while ( {
        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)) {


    return colls;

So now you can evaluate computeSubsets(myDiagram.nodes) to get a go.Set
of go.Sets, where each subset holds all of the Nodes that are indirectly connected with each other and not with any other subset.

Thank you Walter for quick support…
I thought some predefined methods will be there to get number of partitions…but this satisfies my need, Iam happy with this…