All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jtrim2.taskgraph.basic.GraphUtils Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package org.jtrim2.taskgraph.basic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

final class GraphUtils {
    public static  List sortRecursively(
            DirectedGraph graph,
            Collection rootNodes,
            Set src) {
        Objects.requireNonNull(graph, "graph");
        Objects.requireNonNull(src, "src");

        List result = new ArrayList<>(src.size());
        Set visited = new HashSet<>();
        rootNodes.forEach((root) -> {
            Objects.requireNonNull(root, "rootNodess[?]");
            addNodesRecursively(graph, root, src, visited, result);
        });
        return result;
    }

    private static  void addNodesRecursively(
            DirectedGraph graph,
            N root,
            Set src,
            Set visited,
            Collection result) {

        if (!visited.add(root)) {
            return;
        }

        graph.getChildren(root).forEach((child) -> {
            addNodesRecursively(graph, child, src, visited, result);
        });

        if (src.contains(root)) {
            result.add(root);
        }
    }

    private GraphUtils() {
        throw new AssertionError();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy