cmComputeComponentGraph.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmComputeComponentGraph.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input)
  7. : InputGraph(input)
  8. {
  9. // Identify components.
  10. this->Tarjan();
  11. // Compute the component graph.
  12. this->ComponentGraph.resize(0);
  13. this->ComponentGraph.resize(this->Components.size());
  14. this->TransferEdges();
  15. }
  16. cmComputeComponentGraph::~cmComputeComponentGraph()
  17. {
  18. }
  19. void cmComputeComponentGraph::Tarjan()
  20. {
  21. int n = static_cast<int>(this->InputGraph.size());
  22. TarjanEntry entry = { 0, 0 };
  23. this->TarjanEntries.resize(0);
  24. this->TarjanEntries.resize(n, entry);
  25. this->TarjanComponents.resize(0);
  26. this->TarjanComponents.resize(n, -1);
  27. this->TarjanWalkId = 0;
  28. this->TarjanVisited.resize(0);
  29. this->TarjanVisited.resize(n, 0);
  30. for (int i = 0; i < n; ++i) {
  31. // Start a new DFS from this node if it has never been visited.
  32. if (!this->TarjanVisited[i]) {
  33. assert(this->TarjanStack.empty());
  34. ++this->TarjanWalkId;
  35. this->TarjanIndex = 0;
  36. this->TarjanVisit(i);
  37. }
  38. }
  39. }
  40. void cmComputeComponentGraph::TarjanVisit(int i)
  41. {
  42. // We are now visiting this node.
  43. this->TarjanVisited[i] = this->TarjanWalkId;
  44. // Initialize the entry.
  45. this->TarjanEntries[i].Root = i;
  46. this->TarjanComponents[i] = -1;
  47. this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
  48. this->TarjanStack.push(i);
  49. // Follow outgoing edges.
  50. EdgeList const& nl = this->InputGraph[i];
  51. for (cmGraphEdge const& ni : nl) {
  52. int j = ni;
  53. // Ignore edges to nodes that have been reached by a previous DFS
  54. // walk. Since we did not reach the current node from that walk
  55. // it must not belong to the same component and it has already
  56. // been assigned to a component.
  57. if (this->TarjanVisited[j] > 0 &&
  58. this->TarjanVisited[j] < this->TarjanWalkId) {
  59. continue;
  60. }
  61. // Visit the destination if it has not yet been visited.
  62. if (!this->TarjanVisited[j]) {
  63. this->TarjanVisit(j);
  64. }
  65. // If the destination has not yet been assigned to a component,
  66. // check if it has a better root for the current object.
  67. if (this->TarjanComponents[j] < 0) {
  68. if (this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
  69. this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex) {
  70. this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
  71. }
  72. }
  73. }
  74. // Check if we have found a component.
  75. if (this->TarjanEntries[i].Root == i) {
  76. // Yes. Create it.
  77. int c = static_cast<int>(this->Components.size());
  78. this->Components.emplace_back();
  79. NodeList& component = this->Components[c];
  80. // Populate the component list.
  81. int j;
  82. do {
  83. // Get the next member of the component.
  84. j = this->TarjanStack.top();
  85. this->TarjanStack.pop();
  86. // Assign the member to the component.
  87. this->TarjanComponents[j] = c;
  88. this->TarjanEntries[j].Root = i;
  89. // Store the node in its component.
  90. component.push_back(j);
  91. } while (j != i);
  92. // Sort the component members for clarity.
  93. std::sort(component.begin(), component.end());
  94. }
  95. }
  96. void cmComputeComponentGraph::TransferEdges()
  97. {
  98. // Map inter-component edges in the original graph to edges in the
  99. // component graph.
  100. int n = static_cast<int>(this->InputGraph.size());
  101. for (int i = 0; i < n; ++i) {
  102. int i_component = this->TarjanComponents[i];
  103. EdgeList const& nl = this->InputGraph[i];
  104. for (cmGraphEdge const& ni : nl) {
  105. int j = ni;
  106. int j_component = this->TarjanComponents[j];
  107. if (i_component != j_component) {
  108. // We do not attempt to combine duplicate edges, but instead
  109. // store the inter-component edges with suitable multiplicity.
  110. this->ComponentGraph[i_component].emplace_back(j_component,
  111. ni.IsStrong());
  112. }
  113. }
  114. }
  115. }