CalculateStronglyConnectedComponents
(
ICollection<IVertex> vertices,
IGraph graph,
Boolean sortAscending
)
{
Debug.Assert(vertices != null);
Debug.Assert(graph != null);
Int32 iNextIndex = 0;
Stack<IVertex> oStack = new Stack<IVertex>();
// Note: A List is used instead of a LinkedList for the strongly
// connected components only because LinkedList does not have a Sort()
// method.
List<LinkedList<IVertex>> oStronglyConnectedComponents =
new List<LinkedList<IVertex>>();
foreach (IVertex oVertex in vertices)
{
if (!oVertex.ContainsKey(
ReservedMetadataKeys.ConnectedComponentCalculatorIndex))
{
RunTarjanAlgorithm(oVertex, oStack,
oStronglyConnectedComponents, ref iNextIndex);
}
}
SortStronglyConnectedComponents(oStronglyConnectedComponents, graph,
sortAscending);
// Remove the metadata that was added to each vertex.
foreach (IVertex oVertex in vertices)
{
oVertex.RemoveKey(
ReservedMetadataKeys.ConnectedComponentCalculatorIndex);
oVertex.RemoveKey(
ReservedMetadataKeys.ConnectedComponentCalculatorLowLink);
}
return (oStronglyConnectedComponents);
}