cmComputeComponentGraph.cxx 3.9 KB

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