
Public Member Functions | |
| TopologicalSort (Collection< Vertex > vertices) | |
| List | sort () |
| boolean | hasCycles () |
| Map | getCyclicVertices () |
Private Member Functions | |
| void | dfsVisit (Vertex vertex) |
Private Attributes | |
| Collection< Vertex > | m_vertices |
| LinkedList< Vertex > | m_sortedVertices |
| Map< Vertex, List< Vertex > > | m_cyclicVertices |
Topological sort algorithm, following Cormen et al, "Introduction to Algorithms".
To be used to sort a list of interdependent nodes, or to find out that this is impossible because the dependencies are cyclic. Applications need to wrap each of their nodes with a Vertex object, set up the directed dependencies between any two such vertex objects (see addAdjacentVertex), run the sort algorithm, and then extract the original nodes from the sorted vertices.
Expected use is to detangle component dependencies etc.
| alma::acs::algorithms::TopologicalSort::TopologicalSort | ( | Collection< Vertex > | vertices | ) | [inline] |
Constructor that takes the nodes, which we hope form a directed acyclic graph
| vertices |
References m_sortedVertices, and m_vertices.
| void alma::acs::algorithms::TopologicalSort::dfsVisit | ( | Vertex | vertex | ) | [inline, private] |
descendant.setPredecessor(vertex);
References alma::acs::algorithms::Vertex::getAdjacencyList(), alma::acs::algorithms::Vertex::getColor(), m_cyclicVertices, m_sortedVertices, and alma::acs::algorithms::Vertex::setColor().
Referenced by sort().
| Map alma::acs::algorithms::TopologicalSort::getCyclicVertices | ( | ) | [inline] |
Returns the vertices (graph nodes) that form a back edge in the graph.
References m_cyclicVertices.
Referenced by alma::acs::algorithms::TopologicalSortTest::testFailOnCyclicGraph().
| boolean alma::acs::algorithms::TopologicalSort::hasCycles | ( | ) | [inline] |
States whether the graph contains cyclic dependencies, which implies that it could not be sorted. To be called after sort() was called.
References m_cyclicVertices.
Referenced by alma::acs::algorithms::TopologicalSortTest::testFailOnCyclicGraph(), and alma::acs::algorithms::TopologicalSortTest::testSortDirectedAcyclicGraph().
| List alma::acs::algorithms::TopologicalSort::sort | ( | ) | [inline] |
Tries to sort the vertices and to return them in a list. The list will be incomplete if the graph could not be sorted, see hasCycles().
References dfsVisit(), alma::acs::algorithms::Vertex::getColor(), m_sortedVertices, and m_vertices.
Referenced by alma::acs::algorithms::TopologicalSortTest::testFailOnCyclicGraph(), and alma::acs::algorithms::TopologicalSortTest::testSortDirectedAcyclicGraph().
Map<Vertex, List<Vertex> > alma::acs::algorithms::TopologicalSort::m_cyclicVertices [private] |
Referenced by dfsVisit(), getCyclicVertices(), and hasCycles().
LinkedList<Vertex> alma::acs::algorithms::TopologicalSort::m_sortedVertices [private] |
Referenced by dfsVisit(), sort(), and TopologicalSort().
Collection<Vertex> alma::acs::algorithms::TopologicalSort::m_vertices [private] |
Referenced by sort(), and TopologicalSort().
1.6.2