2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2008-02-07 21:14:05 +00:00
|
|
|
#ifndef cmComputeComponentGraph_h
|
|
|
|
#define cmComputeComponentGraph_h
|
|
|
|
|
2017-04-11 20:00:21 +00:00
|
|
|
#include "cmConfigure.h" // IWYU pragma: keep
|
2008-02-07 21:14:05 +00:00
|
|
|
|
|
|
|
#include "cmGraphAdjacencyList.h"
|
|
|
|
|
|
|
|
#include <stack>
|
2016-08-17 22:24:24 +00:00
|
|
|
#include <vector>
|
2008-02-07 21:14:05 +00:00
|
|
|
|
|
|
|
/** \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 18:11:18 +00:00
|
|
|
typedef cmGraphEdgeList EdgeList;
|
2008-02-07 21:14:05 +00:00
|
|
|
typedef cmGraphAdjacencyList Graph;
|
|
|
|
|
|
|
|
cmComputeComponentGraph(Graph const& input);
|
|
|
|
~cmComputeComponentGraph();
|
|
|
|
|
|
|
|
/** Get the adjacency list of the component graph. */
|
2016-05-16 14:34:04 +00:00
|
|
|
Graph const& GetComponentGraph() const { return this->ComponentGraph; }
|
2010-08-20 18:11:18 +00:00
|
|
|
EdgeList const& GetComponentGraphEdges(int c) const
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
return this->ComponentGraph[c];
|
|
|
|
}
|
2008-02-07 21:14:05 +00:00
|
|
|
|
|
|
|
/** Get map from component index to original node indices. */
|
|
|
|
std::vector<NodeList> const& GetComponents() const
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
return this->Components;
|
|
|
|
}
|
|
|
|
NodeList const& GetComponent(int c) const { return this->Components[c]; }
|
2008-02-07 21:14:05 +00:00
|
|
|
|
|
|
|
/** Get map from original node index to component index. */
|
|
|
|
std::vector<int> const& GetComponentMap() const
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
return this->TarjanComponents;
|
|
|
|
}
|
2008-02-07 21:14:05 +00: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 07:43:11 +00:00
|
|
|
std::vector<NodeList> Components;
|
2008-02-07 21:14:05 +00:00
|
|
|
std::stack<int> TarjanStack;
|
2015-06-07 07:43:11 +00:00
|
|
|
int TarjanWalkId;
|
2008-02-07 21:14:05 +00:00
|
|
|
int TarjanIndex;
|
|
|
|
void Tarjan();
|
|
|
|
void TarjanVisit(int i);
|
|
|
|
|
|
|
|
// Connected components.
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|