sf.util.graph.SimpleTopologicalSort Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schemacrawler Show documentation
Show all versions of schemacrawler Show documentation
SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2016, 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 sf.util.graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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);
}
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);
}
Collections.sort(nodesAtLevel);
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;
}
}