cmComputeComponentGraph.h 2.1 KB

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