cmComputeComponentGraph.cxx 4.2 KB

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