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

cdc.graphs.core.GraphTransitiveReduction Maven / Gradle / Ivy

There is a newer version: 0.71.2
Show newest version
package cdc.graphs.core;

import java.util.HashSet;
import java.util.Set;

import cdc.graphs.EdgeDirection;
import cdc.graphs.EdgeTip;
import cdc.graphs.GraphAdapter;
import cdc.graphs.impl.ExplicitSubGraph;
import cdc.graphs.impl.RestrictionSubGraph;

/**
 *
 * @author Damien Carbonne
 *
 * @param  Node class
 * @param  Edge class
 */
public class GraphTransitiveReduction extends GraphBase {
    public GraphTransitiveReduction(GraphAdapter adapter) {
        super(adapter);
    }

    public ExplicitSubGraph computeTransitiveReduction() {
        final ExplicitSubGraph result = new RestrictionSubGraph<>(adapter);

        final Set done = new HashSet<>();

        for (final N node : result.getNodes()) {
            for (final E edge : result.getEdges(node, EdgeDirection.OUTGOING)) {
                final N child = result.getTip(edge, EdgeTip.TARGET);
                done.clear();
                df(result, node, child, done);
            }
        }

        return result;
    }

    private void df(ExplicitSubGraph result,
                    N node,
                    N child,
                    Set done) {
        if (!done.contains(child)) {
            for (final E edge : result.getEdges(child, EdgeDirection.OUTGOING)) {
                final N subchild = result.getTip(edge, EdgeTip.TARGET);
                result.removeEdges(node, subchild);
                df(result, node, subchild, done);
                done.add(child);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy