cmComputeComponentGraph.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef cmComputeComponentGraph_h
  11. #define cmComputeComponentGraph_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmGraphAdjacencyList.h"
  14. #include <stack>
  15. /** \class cmComputeComponentGraph
  16. * \brief Analyze a graph to determine strongly connected components.
  17. *
  18. * Convert a directed graph into a directed acyclic graph whose nodes
  19. * correspond to strongly connected components of the original graph.
  20. *
  21. * We use Tarjan's algorithm to enumerate the components efficiently.
  22. * An advantage of this approach is that the components are identified
  23. * in a topologically sorted order.
  24. */
  25. class cmComputeComponentGraph
  26. {
  27. public:
  28. // Represent the graph with an adjacency list.
  29. typedef cmGraphNodeList NodeList;
  30. typedef cmGraphAdjacencyList Graph;
  31. cmComputeComponentGraph(Graph const& input);
  32. ~cmComputeComponentGraph();
  33. /** Get the adjacency list of the component graph. */
  34. Graph const& GetComponentGraph() const
  35. { return this->ComponentGraph; }
  36. NodeList const& GetComponentGraphEdges(int c) const
  37. { return this->ComponentGraph[c]; }
  38. /** Get map from component index to original node indices. */
  39. std::vector<NodeList> const& GetComponents() const
  40. { return this->Components; }
  41. NodeList const& GetComponent(int c) const
  42. { return this->Components[c]; }
  43. /** Get map from original node index to component index. */
  44. std::vector<int> const& GetComponentMap() const
  45. { return this->TarjanComponents; }
  46. private:
  47. void TransferEdges();
  48. Graph const& InputGraph;
  49. Graph ComponentGraph;
  50. // Tarjan's algorithm.
  51. struct TarjanEntry
  52. {
  53. int Root;
  54. int VisitIndex;
  55. };
  56. int TarjanWalkId;
  57. std::vector<int> TarjanVisited;
  58. std::vector<int> TarjanComponents;
  59. std::vector<TarjanEntry> TarjanEntries;
  60. std::stack<int> TarjanStack;
  61. int TarjanIndex;
  62. void Tarjan();
  63. void TarjanVisit(int i);
  64. // Connected components.
  65. std::vector<NodeList> Components;
  66. };
  67. #endif