getDependencyDepth method

int getDependencyDepth(
  1. String nodeId
)

Returns the longest dependency chain reachable from nodeId.

Implementation

int getDependencyDepth(String nodeId) {
  final memo = <String, int>{};

  int dfs(String currentId, Set<String> path) {
    if (path.contains(currentId)) {
      return 0;
    }
    final cached = memo[currentId];
    if (cached != null) {
      return cached;
    }

    final nextPath = {...path, currentId};
    final dependencies = getDependencies(currentId);
    if (dependencies.isEmpty) {
      memo[currentId] = 0;
      return 0;
    }

    final depth = dependencies
        .map((dependency) => 1 + dfs(dependency, nextPath))
        .reduce((a, b) => a > b ? a : b);
    memo[currentId] = depth;
    return depth;
  }

  return dfs(nodeId, const {});
}