123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include "cmComputeComponentGraph.h"
- #include <algorithm>
- #include <assert.h>
- cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input)
- : InputGraph(input)
- {
-
- this->Tarjan();
-
- 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());
- TarjanEntry entry = { 0, 0 };
- 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);
- for (int i = 0; i < n; ++i) {
-
- if (!this->TarjanVisited[i]) {
- assert(this->TarjanStack.empty());
- ++this->TarjanWalkId;
- this->TarjanIndex = 0;
- this->TarjanVisit(i);
- }
- }
- }
- void cmComputeComponentGraph::TarjanVisit(int i)
- {
-
- this->TarjanVisited[i] = this->TarjanWalkId;
-
- this->TarjanEntries[i].Root = i;
- this->TarjanComponents[i] = -1;
- this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
- this->TarjanStack.push(i);
-
- EdgeList const& nl = this->InputGraph[i];
- for (cmGraphEdge const& ni : nl) {
- int j = ni;
-
-
-
-
- if (this->TarjanVisited[j] > 0 &&
- this->TarjanVisited[j] < this->TarjanWalkId) {
- continue;
- }
-
- if (!this->TarjanVisited[j]) {
- this->TarjanVisit(j);
- }
-
-
- if (this->TarjanComponents[j] < 0) {
- if (this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
- this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex) {
- this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
- }
- }
- }
-
- if (this->TarjanEntries[i].Root == i) {
-
- int c = static_cast<int>(this->Components.size());
- this->Components.emplace_back();
- NodeList& component = this->Components[c];
-
- int j;
- do {
-
- j = this->TarjanStack.top();
- this->TarjanStack.pop();
-
- this->TarjanComponents[j] = c;
- this->TarjanEntries[j].Root = i;
-
- component.push_back(j);
- } while (j != i);
-
- std::sort(component.begin(), component.end());
- }
- }
- void cmComputeComponentGraph::TransferEdges()
- {
-
-
- int n = static_cast<int>(this->InputGraph.size());
- for (int i = 0; i < n; ++i) {
- int i_component = this->TarjanComponents[i];
- EdgeList const& nl = this->InputGraph[i];
- for (cmGraphEdge const& ni : nl) {
- int j = ni;
- int j_component = this->TarjanComponents[j];
- if (i_component != j_component) {
-
-
- this->ComponentGraph[i_component].emplace_back(j_component,
- ni.IsStrong());
- }
- }
- }
- }
|