========== Clustering ========== Fathom provides a flexible clustering algorithm, useful for finding nodes that are bunched together spatially or according to some other metric. By default, it groups nodes based on their proximity and ancestry. It is documented here as top-level functions but is also available directly within rulesets as :func:`bestCluster`, which has the advantage of letting you direct its results to further rules. The clustering routines hang off a ``clusters`` object in the top-level Fathom module. To import them, do something like this: .. code-block:: js const { clusters: { distance }, } = require('fathom-web'); This will result in a top-level ``distance`` symbol. .. note:: Clustering is computationally expensive (at least O(n^2)). It is powerful, but it should be used only when more efficient alternatives are exhausted. .. autofunction:: clusters Example: .. code-block:: js const {clusters} = require('fathom-web/clusters'); theClusters = clusters(anArrayOfNodes, 4); In the above, 4 is the distance beyond which Fathom will decide nodes belong in separate clusters. Turn it up to more aggressively invite nearby nodes into a cluster. Turn it down to keep clusters smaller. The output looks like a list of lists, with each list representing a cluster: .. code-block:: js [[nodeA, nodeB, nodeC], [nodeD]] Various factors influence the measured distance between nodes. The first is the obvious one: topological distance, the number of steps along the DOM tree from one node to another. The second is structural similarity. In the following, the divs ``a`` and ``b`` are farther apart… .. code-block:: html