Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.6 Connected Components

### 8.1.6 Connected Components

The connectedcomponents procedure is used to count the connected components in a graph. The procedure uses element sets to obtain information about nodes and edges of the graph.

The procedure has four attributes:

• set (required): the element set that constitutes the nodes of the graph.
• nodes (required): a set expression that returns, for a node, the set of connected nodes to which there is an (outgoing) edge.
• undirected (optional): takes values "true" or "false". The default value is "false" and the procedure calculates the strongly connected components of the directed graph. When set to "true", the connected components of the underlying undirected graph will be computed (i.e., the direction of edges is ignored, an edge can always be followed in both ways).
• minnodes (optional): only counts connected components exceeding a certain size. For example, if you specify minnodes="3", only connected components that have at least three nodes will be reported.
For example, to calculate the number of inheritance hierarchies among the classes in a package, we define three element sets (see Section 8.2 "Definition of Sets"):
• set Classes: the set of classes in a package
• set Parents: the set of parent classes of a class
• set Children: the set of children classes of a class
We can then calculate the number of connected components in the inheritance hierarchy as follows:
```<metric name="InhHierarchies" domain="package">
<description>The number of inheritance hierarchies.</description>
<connectedcomponents set="Classes" nodes="Parents+Children" />
</metric>
```
The following example uses an undirected graph and only counts connected components with at least two nodes (i.e., ignoring isolated classes that do not participate in any inheritance relationship):
```<metric name="InhHierarchies" domain="package">
<description>The number of non-trivial inheritance hierarchies.</description>
<connectedcomponents set="Classes" nodes="Parents" undirected="true"
minnodes="2" />
</metric>
```