Skip to content

11.3 Highlight nodes, edges and groups in Graphviz

You set colors, shapes and line styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs.

You want a picture of a graph that shows what an analysis found. A plain picture shows only the graph. You choose the colors, line styles and frames, and the nodes to leave out, based on your data and the results of your analyses. You need this for:

  • a cycle, or the edges that close one, drawn in a color of their own;
  • groups of nodes in frames, such as the strongly connected components or the classes of a package;
  • nodes that only crowd the picture and are left out, such as a package of helpers that many packages use.

The example graph dependencies models the dependencies between the eight packages of a small program. It has an edge from each package to each package it uses. The array names holds the names of the packages:

0: app1: cli2: ui3: service4: model5: storage6: json7: util

model and storage use each other, so they lie on a cycle. They form an SCC (strongly connected component) of their own: a group of nodes that reach each other, and that no further node can join. See Find the groups of nodes that reach each other. The drawing should frame that SCC and draw the edge that closes the cycle dashed and red. It should also leave out the package of helpers util, which four other packages use.

// Frame the SCCs with a cycle: a partition in which every other package is in no
// group (-1).
Sccs sccs = Components.sccsOf(dependencies);
int[] frameOf = new int[dependencies.nodeCount()];
for (int node = 0; node < frameOf.length; node++) {
frameOf[node] = sccs.containsCycle(sccs.sccOf(node)) ? sccs.sccOf(node) : -1;
}
NodePartition frames = NodePartition.of(dependencies, frameOf, sccs.sccCount());
EdgeTypes edgeTypes = EdgeTypes.of(dependencies);
String dot = DotWriter.of(dependencies)
.nodeLabel(node -> names[node])
.clusters(frames, scc -> "SCC " + scc)
// Draw the edges that close a cycle dashed and red.
.edgeAttributes((edge, attributes) -> {
if (edgeTypes.typeOf(edge) == EdgeType.BACK) {
attributes.put("style", "dashed");
attributes.put("color", "red");
}
})
// Leave out util, the package of helpers: four packages use it, and its edges
// only crowd the picture.
.nodeFilter(node -> !names[node].equals("util"))
.writeToString();
Full example
// Frame the SCCs with a cycle: a partition in which every other package is in no
// group (NONE).
let sccs = components::sccs_of(&dependencies);
let mut frame_of = vec![NONE; dependencies.node_count() as usize];
for node in dependencies.nodes() {
if sccs.contains_cycle(sccs.scc_of(node)) {
frame_of[node as usize] = sccs.scc_of(node);
}
}
let frames = NodePartition::of(&dependencies, &frame_of, sccs.scc_count())?;
let edge_types = EdgeTypes::of(&dependencies);
let dot = DotWriter::of(&dependencies)
.node_label(|node| Some(names[node as usize].into()))
.clusters(&frames, |scc| Some(format!("SCC {scc}").into()))
// Draw the edges that close a cycle dashed and red.
.edge_attributes(|edge, attributes| {
if edge_types.type_of(edge) == EdgeType::Back {
attributes.insert("style", "dashed");
attributes.insert("color", "red");
}
})
// Leave out util, the package of helpers: four packages use it, and its edges
// only crowd the picture.
.node_filter(|node| names[node as usize] != "util")
.write_to_string();
Full example
// Frame the SCCs with a cycle: a partition in which every other package is in no
// group (-1).
const sccs = Components.sccsOf(dependencies);
const frameOf = new Int32Array(dependencies.nodeCount);
for (let node = 0; node < frameOf.length; node++) {
frameOf[node] = sccs.containsCycle(sccs.sccOf(node)) ? sccs.sccOf(node) : -1;
}
const frames = NodePartition.of(dependencies, frameOf, sccs.sccCount);
const edgeTypes = EdgeTypes.of(dependencies);
const dot = DotWriter.of(dependencies)
.nodeLabel((node) => names[node])
.clusters(frames, (scc) => `SCC ${scc}`)
// Draw the edges that close a cycle dashed and red.
.edgeAttributes((edge, attributes) => {
if (edgeTypes.typeOf(edge) === EdgeType.BACK) {
attributes.set("style", "dashed");
attributes.set("color", "red");
}
})
// Leave out util, the package of helpers: four packages use it, and its edges only
// crowd the picture.
.nodeFilter((node) => names[node] !== "util")
.writeToString();
Full example
// Frame the SCCs with a cycle: a partition in which every other package is in no
// group (-1).
Sccs sccs = Components.SccsOf(dependencies);
int[] frameOf = new int[dependencies.NodeCount];
for (int node = 0; node < frameOf.Length; node++)
{
frameOf[node] = sccs.ContainsCycle(sccs.SccOf(node)) ? sccs.SccOf(node) : -1;
}
NodePartition frames = NodePartition.Of(dependencies, frameOf, sccs.SccCount);
EdgeTypes edgeTypes = EdgeTypes.Of(dependencies);
string dot = DotWriter.Of(dependencies)
.NodeLabel(node => names[node])
.Clusters(frames, scc => $"SCC {scc}")
// Draw the edges that close a cycle dashed and red.
.EdgeAttributes((edge, attributes) =>
{
if (edgeTypes.TypeOf(edge) == EdgeType.Back)
{
attributes["style"] = "dashed";
attributes["color"] = "red";
}
})
// Leave out util, the package of helpers: four packages use it, and its edges
// only crowd the picture.
.NodeFilter(node => names[node] != "util")
.WriteToString();
Full example

The partition passed to clustersclustersclustersClusters has a group for every SCC, but only the group of the SCC with a cycle has nodes. Every other package is in no group and stands outside any frame, and a group without nodes gets no frame. EdgeTypes.ofEdgeTypes::ofEdgeTypes.ofEdgeTypes.Of classifies the edges in a depth-first search. Its one back edge is storage -> model, which closes the cycle. The writer returns this text:

digraph {
subgraph cluster_3 {
label="SCC 3";
4 [label="model"];
5 [label="storage"];
}
0 [label="app"];
1 [label="cli"];
2 [label="ui"];
3 [label="service"];
6 [label="json"];
0 -> 1;
0 -> 2;
0 -> 3;
2 -> 3;
2 -> 4;
3 -> 4;
3 -> 5;
4 -> 5;
5 -> 4 [style="dashed", color="red"];
5 -> 6;
}

Graphviz draws it like this, with the SCC in its frame and without util and its edges:

The dependencies between the packages without util, the SCC of model and storage framed, drawn by Graphviz