cmComputeComponentGraph.cxx 4.5 KB

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