2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2008-02-08 00:14:05 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2008-02-08 00:14:05 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2008-02-08 00:14:05 +03:00
|
|
|
#ifndef cmComputeComponentGraph_h
|
|
|
|
#define cmComputeComponentGraph_h
|
|
|
|
|
|
|
|
#include "cmStandardIncludes.h"
|
|
|
|
|
|
|
|
#include "cmGraphAdjacencyList.h"
|
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
/** \class cmComputeComponentGraph
|
|
|
|
* \brief Analyze a graph to determine strongly connected components.
|
|
|
|
*
|
|
|
|
* Convert a directed graph into a directed acyclic graph whose nodes
|
|
|
|
* correspond to strongly connected components of the original graph.
|
|
|
|
*
|
|
|
|
* We use Tarjan's algorithm to enumerate the components efficiently.
|
|
|
|
* An advantage of this approach is that the components are identified
|
|
|
|
* in a topologically sorted order.
|
|
|
|
*/
|
|
|
|
class cmComputeComponentGraph
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Represent the graph with an adjacency list.
|
|
|
|
typedef cmGraphNodeList NodeList;
|
2010-08-20 22:11:18 +04:00
|
|
|
typedef cmGraphEdgeList EdgeList;
|
2008-02-08 00:14:05 +03:00
|
|
|
typedef cmGraphAdjacencyList Graph;
|
|
|
|
|
|
|
|
cmComputeComponentGraph(Graph const& input);
|
|
|
|
~cmComputeComponentGraph();
|
|
|
|
|
|
|
|
/** Get the adjacency list of the component graph. */
|
2016-05-16 17:34:04 +03:00
|
|
|
Graph const& GetComponentGraph() const { return this->ComponentGraph; }
|
2010-08-20 22:11:18 +04:00
|
|
|
EdgeList const& GetComponentGraphEdges(int c) const
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->ComponentGraph[c];
|
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
/** Get map from component index to original node indices. */
|
|
|
|
std::vector<NodeList> const& GetComponents() const
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->Components;
|
|
|
|
}
|
|
|
|
NodeList const& GetComponent(int c) const { return this->Components[c]; }
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
/** Get map from original node index to component index. */
|
|
|
|
std::vector<int> const& GetComponentMap() const
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->TarjanComponents;
|
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void TransferEdges();
|
|
|
|
|
|
|
|
Graph const& InputGraph;
|
|
|
|
Graph ComponentGraph;
|
|
|
|
|
|
|
|
// Tarjan's algorithm.
|
|
|
|
struct TarjanEntry
|
|
|
|
{
|
|
|
|
int Root;
|
|
|
|
int VisitIndex;
|
|
|
|
};
|
|
|
|
std::vector<int> TarjanVisited;
|
|
|
|
std::vector<int> TarjanComponents;
|
|
|
|
std::vector<TarjanEntry> TarjanEntries;
|
2015-06-07 10:43:11 +03:00
|
|
|
std::vector<NodeList> Components;
|
2008-02-08 00:14:05 +03:00
|
|
|
std::stack<int> TarjanStack;
|
2015-06-07 10:43:11 +03:00
|
|
|
int TarjanWalkId;
|
2008-02-08 00:14:05 +03:00
|
|
|
int TarjanIndex;
|
|
|
|
void Tarjan();
|
|
|
|
void TarjanVisit(int i);
|
|
|
|
|
|
|
|
// Connected components.
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|