ctkDependencyGraph.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.commontk.org/LICENSE
  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. // Qt includes
  17. #include <QScopedPointer>
  18. #include <QString>
  19. #include <QList>
  20. #include <QtGlobal>
  21. // CTK includes
  22. #if !defined(NO_SYMBOL_EXPORT)
  23. #include "CTKCoreExport.h"
  24. #else
  25. #define CTK_CORE_EXPORT
  26. #endif
  27. class ctkDependencyGraphPrivate;
  28. class CTK_CORE_EXPORT ctkDependencyGraph
  29. {
  30. public:
  31. ctkDependencyGraph(int nvertices);
  32. virtual ~ctkDependencyGraph();
  33. void printAdditionalInfo()const;
  34. void printGraph()const;
  35. /// Get the number of vertices associated with current graph
  36. int numberOfVertices()const;
  37. /// Get the number of edges associated with current graph
  38. int numberOfEdges()const;
  39. /// Traverse graph and check for cycle
  40. bool checkForCycle();
  41. /// Return true if there is at least one cycle
  42. bool cycleDetected()const;
  43. /// If a cycle has been detected, return the origin of the cycle otherwise 0.
  44. int cycleOrigin()const;
  45. /// If a cycle has been detected, return the end of the cycle otherwise 0.
  46. int cycleEnd()const;
  47. // The traverse of the tree will print information on standard output
  48. void setVerbose(bool verbose);
  49. /// Insert edge
  50. /// (from, to) indicate a relation between two vertices
  51. /// Note also that vertex id should be >= 1
  52. void insertEdge(int from, int to);
  53. /// Retrieve the paths between two vertices
  54. /// Caller is responsible to clear paths list
  55. void findPaths(int from, int to, QList<QList<int>* >& paths);
  56. /// Retrieve the path between two vertices
  57. void findPath(int from, int to, QList<int>& path);
  58. /// List of edge to exclude
  59. /// An edge is specified using its extremity
  60. void setEdgeListToExclude(const QList<int>& list);
  61. /// The default implementation check if 'edge' is in the list of edge to exclude
  62. /// See setEdgeListToExclude
  63. virtual bool shouldExcludeEdge(int edge)const;
  64. /// Called each time an edge is visited
  65. virtual void processEdge(int /*from*/, int /*to*/){}
  66. /// Perform a topological sort
  67. /// Return false if the graph contains cycles
  68. /// If a rootId is given, the subgraph starting at the root id is sorted
  69. /// See cycleDetected, cycleOrigin, cycleEnd
  70. bool topologicalSort(QList<int>& sorted, int rootId = -1);
  71. /// Retrieve all vertices with indegree 0
  72. void sourceVertices(QList<int>& sources);
  73. protected:
  74. QScopedPointer<ctkDependencyGraphPrivate> d_ptr;
  75. private:
  76. Q_DECLARE_PRIVATE(ctkDependencyGraph);
  77. Q_DISABLE_COPY(ctkDependencyGraph);
  78. };
  79. #endif