2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2008-02-08 00:14:05 +03:00
|
|
|
#include "cmComputeComponentGraph.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input)
|
|
|
|
: InputGraph(input)
|
2008-02-08 00:14:05 +03:00
|
|
|
{
|
|
|
|
// Identify components.
|
|
|
|
this->Tarjan();
|
|
|
|
|
|
|
|
// Compute the component graph.
|
|
|
|
this->ComponentGraph.resize(0);
|
|
|
|
this->ComponentGraph.resize(this->Components.size());
|
|
|
|
this->TransferEdges();
|
|
|
|
}
|
|
|
|
|
|
|
|
cmComputeComponentGraph::~cmComputeComponentGraph()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmComputeComponentGraph::Tarjan()
|
|
|
|
{
|
|
|
|
int n = static_cast<int>(this->InputGraph.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
TarjanEntry entry = { 0, 0 };
|
2008-02-08 00:14:05 +03:00
|
|
|
this->TarjanEntries.resize(0);
|
|
|
|
this->TarjanEntries.resize(n, entry);
|
|
|
|
this->TarjanComponents.resize(0);
|
|
|
|
this->TarjanComponents.resize(n, -1);
|
|
|
|
this->TarjanWalkId = 0;
|
|
|
|
this->TarjanVisited.resize(0);
|
|
|
|
this->TarjanVisited.resize(n, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2008-02-08 00:14:05 +03:00
|
|
|
// Start a new DFS from this node if it has never been visited.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->TarjanVisited[i]) {
|
2008-02-08 00:14:05 +03:00
|
|
|
assert(this->TarjanStack.empty());
|
|
|
|
++this->TarjanWalkId;
|
|
|
|
this->TarjanIndex = 0;
|
|
|
|
this->TarjanVisit(i);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmComputeComponentGraph::TarjanVisit(int i)
|
|
|
|
{
|
|
|
|
// We are now visiting this node.
|
|
|
|
this->TarjanVisited[i] = this->TarjanWalkId;
|
|
|
|
|
|
|
|
// Initialize the entry.
|
|
|
|
this->TarjanEntries[i].Root = i;
|
|
|
|
this->TarjanComponents[i] = -1;
|
|
|
|
this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
|
|
|
|
this->TarjanStack.push(i);
|
|
|
|
|
|
|
|
// Follow outgoing edges.
|
2010-08-20 22:11:18 +04:00
|
|
|
EdgeList const& nl = this->InputGraph[i];
|
2016-05-16 17:34:04 +03:00
|
|
|
for (EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni) {
|
2008-02-08 00:14:05 +03:00
|
|
|
int j = *ni;
|
|
|
|
|
|
|
|
// Ignore edges to nodes that have been reached by a previous DFS
|
|
|
|
// walk. Since we did not reach the current node from that walk
|
|
|
|
// it must not belong to the same component and it has already
|
|
|
|
// been assigned to a component.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->TarjanVisited[j] > 0 &&
|
|
|
|
this->TarjanVisited[j] < this->TarjanWalkId) {
|
2008-02-08 00:14:05 +03:00
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
// Visit the destination if it has not yet been visited.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->TarjanVisited[j]) {
|
2008-02-08 00:14:05 +03:00
|
|
|
this->TarjanVisit(j);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
// If the destination has not yet been assigned to a component,
|
|
|
|
// check if it has a better root for the current object.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->TarjanComponents[j] < 0) {
|
|
|
|
if (this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
|
|
|
|
this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex) {
|
2008-02-08 00:14:05 +03:00
|
|
|
this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
// Check if we have found a component.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->TarjanEntries[i].Root == i) {
|
2008-02-08 00:14:05 +03:00
|
|
|
// Yes. Create it.
|
|
|
|
int c = static_cast<int>(this->Components.size());
|
|
|
|
this->Components.push_back(NodeList());
|
|
|
|
NodeList& component = this->Components[c];
|
|
|
|
|
|
|
|
// Populate the component list.
|
|
|
|
int j;
|
2016-05-16 17:34:04 +03:00
|
|
|
do {
|
2008-02-08 00:14:05 +03:00
|
|
|
// Get the next member of the component.
|
|
|
|
j = this->TarjanStack.top();
|
|
|
|
this->TarjanStack.pop();
|
|
|
|
|
|
|
|
// Assign the member to the component.
|
|
|
|
this->TarjanComponents[j] = c;
|
|
|
|
this->TarjanEntries[j].Root = i;
|
|
|
|
|
|
|
|
// Store the node in its component.
|
|
|
|
component.push_back(j);
|
2016-05-16 17:34:04 +03:00
|
|
|
} while (j != i);
|
2008-02-08 00:14:05 +03:00
|
|
|
|
|
|
|
// Sort the component members for clarity.
|
|
|
|
std::sort(component.begin(), component.end());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmComputeComponentGraph::TransferEdges()
|
|
|
|
{
|
|
|
|
// Map inter-component edges in the original graph to edges in the
|
|
|
|
// component graph.
|
|
|
|
int n = static_cast<int>(this->InputGraph.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2008-02-08 00:14:05 +03:00
|
|
|
int i_component = this->TarjanComponents[i];
|
2010-08-20 22:11:18 +04:00
|
|
|
EdgeList const& nl = this->InputGraph[i];
|
2016-05-16 17:34:04 +03:00
|
|
|
for (EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni) {
|
2008-02-08 00:14:05 +03:00
|
|
|
int j = *ni;
|
|
|
|
int j_component = this->TarjanComponents[j];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (i_component != j_component) {
|
2010-08-25 18:07:25 +04:00
|
|
|
// We do not attempt to combine duplicate edges, but instead
|
|
|
|
// store the inter-component edges with suitable multiplicity.
|
|
|
|
this->ComponentGraph[i_component].push_back(
|
|
|
|
cmGraphEdge(j_component, ni->IsStrong()));
|
2008-02-08 00:14:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-08 00:14:05 +03:00
|
|
|
}
|