cmComputeComponentGraph.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmComputeComponentGraph.h"
  11. #include <algorithm>
  12. #include <assert.h>
  13. //----------------------------------------------------------------------------
  14. cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input):
  15. InputGraph(input)
  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. //----------------------------------------------------------------------------
  25. cmComputeComponentGraph::~cmComputeComponentGraph()
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. void cmComputeComponentGraph::Tarjan()
  30. {
  31. int n = static_cast<int>(this->InputGraph.size());
  32. TarjanEntry entry = {0,0};
  33. this->TarjanEntries.resize(0);
  34. this->TarjanEntries.resize(n, entry);
  35. this->TarjanComponents.resize(0);
  36. this->TarjanComponents.resize(n, -1);
  37. this->TarjanWalkId = 0;
  38. this->TarjanVisited.resize(0);
  39. this->TarjanVisited.resize(n, 0);
  40. for(int i = 0; i < n; ++i)
  41. {
  42. // Start a new DFS from this node if it has never been visited.
  43. if(!this->TarjanVisited[i])
  44. {
  45. assert(this->TarjanStack.empty());
  46. ++this->TarjanWalkId;
  47. this->TarjanIndex = 0;
  48. this->TarjanVisit(i);
  49. }
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. void cmComputeComponentGraph::TarjanVisit(int i)
  54. {
  55. // We are now visiting this node.
  56. this->TarjanVisited[i] = this->TarjanWalkId;
  57. // Initialize the entry.
  58. this->TarjanEntries[i].Root = i;
  59. this->TarjanComponents[i] = -1;
  60. this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
  61. this->TarjanStack.push(i);
  62. // Follow outgoing edges.
  63. NodeList const& nl = this->InputGraph[i];
  64. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  65. {
  66. int j = *ni;
  67. // Ignore edges to nodes that have been reached by a previous DFS
  68. // walk. Since we did not reach the current node from that walk
  69. // it must not belong to the same component and it has already
  70. // been assigned to a component.
  71. if(this->TarjanVisited[j] > 0 &&
  72. this->TarjanVisited[j] < this->TarjanWalkId)
  73. {
  74. continue;
  75. }
  76. // Visit the destination if it has not yet been visited.
  77. if(!this->TarjanVisited[j])
  78. {
  79. this->TarjanVisit(j);
  80. }
  81. // If the destination has not yet been assigned to a component,
  82. // check if it has a better root for the current object.
  83. if(this->TarjanComponents[j] < 0)
  84. {
  85. if(this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
  86. this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex)
  87. {
  88. this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
  89. }
  90. }
  91. }
  92. // Check if we have found a component.
  93. if(this->TarjanEntries[i].Root == i)
  94. {
  95. // Yes. Create it.
  96. int c = static_cast<int>(this->Components.size());
  97. this->Components.push_back(NodeList());
  98. NodeList& component = this->Components[c];
  99. // Populate the component list.
  100. int j;
  101. do
  102. {
  103. // Get the next member of the component.
  104. j = this->TarjanStack.top();
  105. this->TarjanStack.pop();
  106. // Assign the member to the component.
  107. this->TarjanComponents[j] = c;
  108. this->TarjanEntries[j].Root = i;
  109. // Store the node in its component.
  110. component.push_back(j);
  111. } while(j != i);
  112. // Sort the component members for clarity.
  113. std::sort(component.begin(), component.end());
  114. }
  115. }
  116. //----------------------------------------------------------------------------
  117. void cmComputeComponentGraph::TransferEdges()
  118. {
  119. // Map inter-component edges in the original graph to edges in the
  120. // component graph.
  121. int n = static_cast<int>(this->InputGraph.size());
  122. for(int i=0; i < n; ++i)
  123. {
  124. int i_component = this->TarjanComponents[i];
  125. NodeList const& nl = this->InputGraph[i];
  126. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  127. {
  128. int j = *ni;
  129. int j_component = this->TarjanComponents[j];
  130. if(i_component != j_component)
  131. {
  132. this->ComponentGraph[i_component].push_back(j_component);
  133. }
  134. }
  135. }
  136. }