cdc.graphs.core.GraphTransitiveReduction Maven / Gradle / Ivy
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);
}
}
}
}