cmComputeComponentGraph.cxx 4.1 KB

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