cmComputeComponentGraph.cxx 4.0 KB

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