us.fatehi.utility.graph.SimpleTopologicalSort Maven / Gradle / Ivy
The newest version!
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2024, Sualeh Fatehi .
All rights reserved.
------------------------------------------------------------------------
SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.
You may elect to redistribute this code under any of these licenses.
The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html
The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/
========================================================================
*/
package us.fatehi.utility.graph;
import static java.util.Comparator.naturalOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
public class SimpleTopologicalSort> {
private final DirectedGraph graph;
public SimpleTopologicalSort(final DirectedGraph graph) {
this.graph = Objects.requireNonNull(graph, "No diagram provided");
}
public List topologicalSort() throws GraphException {
if (containsCycle()) {
throw new GraphException("Graph contains a cycle, so cannot be topologically sorted");
}
final Collection> vertices = graph.vertexSet();
final int collectionSize = vertices.size();
final Collection> edges = new ArrayList<>(graph.edgeSet());
final List sortedValues = new ArrayList<>(collectionSize);
while (!vertices.isEmpty()) {
final List nodesAtLevel = new ArrayList<>(collectionSize);
// Remove unattached nodes
for (final Iterator> iterator = vertices.iterator(); iterator.hasNext(); ) {
final Vertex vertex = iterator.next();
if (isUnattachedNode(vertex, edges)) {
nodesAtLevel.add(vertex.getValue());
iterator.remove();
}
}
// Find all nodes at the current level
final List> startNodes = new ArrayList<>(collectionSize);
for (final Vertex vertex : vertices) {
if (isStartNode(vertex, edges)) {
startNodes.add(vertex);
}
}
for (final Vertex vertex : startNodes) {
// Save the vertex value
nodesAtLevel.add(vertex.getValue());
// Remove all out edges
dropOutEdges(vertex, edges);
// Remove the vertex itself
vertices.remove(vertex);
}
nodesAtLevel.sort(naturalOrder());
sortedValues.addAll(nodesAtLevel);
}
return sortedValues;
}
private boolean containsCycle() {
final SimpleCycleDetector cycleDetector = new SimpleCycleDetector<>(graph);
return cycleDetector.containsCycle();
}
private void dropOutEdges(final Vertex vertex, final Collection> edges) {
for (final Iterator> iterator = edges.iterator(); iterator.hasNext(); ) {
final DirectedEdge edge = iterator.next();
if (edge.isFrom(vertex)) {
iterator.remove();
}
}
}
private boolean isStartNode(final Vertex vertex, final Collection> edges) {
for (final DirectedEdge edge : edges) {
if (edge.isTo(vertex)) {
return false;
}
}
return true;
}
private boolean isUnattachedNode(
final Vertex vertex, final Collection> edges) {
for (final DirectedEdge edge : edges) {
if (edge.isTo(vertex) || edge.isFrom(vertex)) {
return false;
}
}
return true;
}
}