Is there an established algorithm for finding redundant edges in a graph?
For example, I\'d like to find that a->d and a->e are redundant, and then get rid of them,
Since the Wikipedia article mentioned by @Craig gives only a hit for an implementation, I post my implementation with Java 8 streams:
Map> reduction = usages.entrySet().stream()
.collect(toMap(
Entry::getKey,
(Entry> entry) -> {
String start = entry.getKey();
Set neighbours = entry.getValue();
Set visited = new HashSet<>();
Queue queue = new LinkedList<>(neighbours);
while (!queue.isEmpty()) {
String node = queue.remove();
usages.getOrDefault(node, emptySet()).forEach(next -> {
if (next.equals(start)) {
throw new RuntimeException("Cycle detected!");
}
if (visited.add(next)) {
queue.add(next);
}
});
}
return neighbours.stream()
.filter(s -> !visited.contains(s))
.collect(toSet());
}
));