computeNodeCDs function

void computeNodeCDs(
  1. DirectedGraph<String> graph,
  2. @modified Model model
)

Compute the component dependency of each node. The component dependency (CD) is the number of nodes a particular node depends on directly or transitively, including itself.

Implementation

void computeNodeCDs(DirectedGraph<String> graph, @modified Model model) {
  for (var v in graph.vertices) {
    model.nodes[v]!.cd = 0;
  }
  for (var v in graph.vertices) {
    var nodes = [v];
    var visited = <String>{};
    while (nodes.isNotEmpty) {
      var next = nodes.removeAt(0);
      // Only visit each node once
      if (!visited.contains(next)) {
        // Solution to += error: https://stackoverflow.com/a/66472892
        model.nodes[v]!.cd = model.nodes[v]!.cd! + 1;
        visited.add(next);
        nodes.addAll(graph.edges(next));
      }
    }
  }
}