ctkDependencyGraph.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkDependencyGraph_h
  15. #define __ctkDependencyGraph_h
  16. // CTK includes
  17. #if !defined(NO_SYMBOL_EXPORT)
  18. #include "ctkCoreExport.h"
  19. #else
  20. #define CTK_CORE_EXPORT
  21. #endif
  22. #include <list>
  23. class ctkDependencyGraphPrivate;
  24. /// \ingroup Core
  25. /// \class ctkDependencyGraph
  26. /// \brief Class to implement a dependency graph, converted to STL instead of Qt.
  27. class CTK_CORE_EXPORT ctkDependencyGraph
  28. {
  29. public:
  30. ctkDependencyGraph(int nvertices);
  31. virtual ~ctkDependencyGraph();
  32. void printAdditionalInfo()const;
  33. void printGraph()const;
  34. /// Get the number of vertices associated with current graph
  35. int numberOfVertices()const;
  36. /// Get the number of edges associated with current graph
  37. int numberOfEdges()const;
  38. /// Traverse graph and check for cycle
  39. bool checkForCycle();
  40. /// Return true if there is at least one cycle
  41. bool cycleDetected()const;
  42. /// If a cycle has been detected, return the origin of the cycle otherwise 0.
  43. int cycleOrigin()const;
  44. /// If a cycle has been detected, return the end of the cycle otherwise 0.
  45. int cycleEnd()const;
  46. // The traverse of the tree will print information on standard output
  47. void setVerbose(bool verbose);
  48. /// Insert edge
  49. /// (from, to) indicate a relation between two vertices
  50. /// Note also that vertex id should be >= 1
  51. void insertEdge(int from, int to);
  52. /// Retrieve the paths between two vertices
  53. /// Caller is responsible to clear paths list
  54. void findPaths(int from, int to, std::list<std::list<int>* >& paths);
  55. /// Retrieve the path between two vertices
  56. void findPath(int from, int to, std::list<int>& path);
  57. /// List of edge to exclude
  58. /// An edge is specified using its extremity
  59. void setEdgeListToExclude(const std::list<int>& list);
  60. /// The default implementation check if 'edge' is in the list of edge to exclude
  61. /// See setEdgeListToExclude
  62. virtual bool shouldExcludeEdge(int edge)const;
  63. /// Called each time an edge is visited
  64. virtual void processEdge(int /*from*/, int /*to*/){}
  65. /// Perform a topological sort
  66. /// Return false if the graph contains cycles
  67. /// If a rootId is given, the subgraph starting at the root id is sorted
  68. /// See cycleDetected, cycleOrigin, cycleEnd
  69. bool topologicalSort(std::list<int>& sorted, int rootId = -1);
  70. /// Retrieve all vertices with indegree 0
  71. void sourceVertices(std::list<int>& sources);
  72. protected:
  73. ctkDependencyGraphPrivate* d_ptr;
  74. private:
  75. // Intentionally disable copy semantics
  76. ctkDependencyGraph(const ctkDependencyGraph &);
  77. ctkDependencyGraph &operator=(const ctkDependencyGraph &);
  78. };
  79. #endif