ctkDependencyGraphTest1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*=========================================================================
  2. Library: qCTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // CTK includes
  11. #include "ctkDependencyGraph.h"
  12. // STL includes
  13. #include <cstdlib>
  14. #include <iostream>
  15. namespace
  16. {
  17. void printIntegerList(const char* msg, const QList<int>& list, bool endl = true)
  18. {
  19. std::cerr << msg;
  20. foreach(int l, list)
  21. {
  22. std::cerr << l << " ";
  23. }
  24. if (endl)
  25. {
  26. std::cerr << std::endl;
  27. }
  28. }
  29. }
  30. //-----------------------------------------------------------------------------
  31. int ctkDependencyGraphTest1(int argc, char * argv [] )
  32. {
  33. Q_UNUSED(argc);
  34. Q_UNUSED(argv);
  35. const int numberOfVertices = 14;
  36. ctkDependencyGraph graph(numberOfVertices);
  37. graph.setVerbose(true);
  38. graph.setVerbose(false);
  39. //graph.setVerbose(true);
  40. //
  41. // 6 - 8
  42. // |
  43. // 7 - 5 - 1 - 2 -
  44. // | | | - 9
  45. // | 4 - 3 - |
  46. // | |
  47. // 10 - 11 - 12 - 13 - 14
  48. //
  49. graph.insertEdge(3,4);
  50. graph.insertEdge(4,1);
  51. graph.insertEdge(1,5);
  52. graph.insertEdge(5,7);
  53. graph.insertEdge(2,1);
  54. graph.insertEdge(8,6);
  55. graph.insertEdge(6,7);
  56. graph.insertEdge(9,2);
  57. graph.insertEdge(9,3);
  58. graph.insertEdge(14,9);
  59. graph.insertEdge(14,13);
  60. graph.insertEdge(13,12);
  61. graph.insertEdge(12,11);
  62. graph.insertEdge(11,10);
  63. graph.insertEdge(10,7);
  64. int expectedNumberOfEdge = 15;
  65. graph.printAdditionalInfo();
  66. graph.printGraph();
  67. int nov = graph.numberOfVertices();
  68. if( nov != numberOfVertices )
  69. {
  70. return EXIT_FAILURE;
  71. }
  72. int noe = graph.numberOfEdges();
  73. if( noe != expectedNumberOfEdge )
  74. {
  75. return EXIT_FAILURE;
  76. }
  77. bool cfc = graph.checkForCycle();
  78. if( cfc == true )
  79. {
  80. return EXIT_FAILURE;
  81. }
  82. bool cdtd = graph.cycleDetected();
  83. if( cdtd == true )
  84. {
  85. return EXIT_FAILURE;
  86. }
  87. int corigin = graph.cycleOrigin();
  88. int cend = graph.cycleEnd();
  89. QList<int> path;
  90. QList<int> expectedPath;
  91. graph.findPath( 8, 7, path );
  92. expectedPath << 8 << 6 << 7;
  93. if (path != expectedPath)
  94. {
  95. std::cerr << "Problem with findPath()" << std::endl;
  96. printIntegerList("current:", path);
  97. printIntegerList("expected:", expectedPath);
  98. return EXIT_FAILURE;
  99. }
  100. path.clear();
  101. expectedPath.clear();
  102. graph.findPath( 1, 7, path );
  103. expectedPath << 1 << 5 << 7;
  104. if (path != expectedPath)
  105. {
  106. std::cerr << "Problem with findPath()" << std::endl;
  107. printIntegerList("current:", path);
  108. printIntegerList("expected:", expectedPath);
  109. return EXIT_FAILURE;
  110. }
  111. path.clear();
  112. expectedPath.clear();
  113. graph.findPath( 3, 7, path );
  114. expectedPath << 3 << 4 << 1 << 5 << 7;
  115. if (path != expectedPath)
  116. {
  117. std::cerr << "Problem with findPath()" << std::endl;
  118. printIntegerList("current:", path);
  119. printIntegerList("expected:", expectedPath);
  120. return EXIT_FAILURE;
  121. }
  122. path.clear();
  123. expectedPath.clear();
  124. graph.findPath( 2, 5, path );
  125. expectedPath << 2 << 1 << 5;
  126. if (path != expectedPath)
  127. {
  128. std::cerr << "Problem with findPath()" << std::endl;
  129. printIntegerList("current:", path);
  130. printIntegerList("expected:", expectedPath);
  131. return EXIT_FAILURE;
  132. }
  133. path.clear();
  134. expectedPath.clear();
  135. QList<QList<int>* > paths;
  136. QList<int> expectedPath1;
  137. QList<int> expectedPath2;
  138. QList<int> expectedPath3;
  139. graph.findPaths(14, 5, paths);
  140. expectedPath1 << 14 << 9 << 3 << 4 << 1 << 5;
  141. expectedPath2 << 14 << 9 << 2 << 1 << 5;
  142. foreach(QList<int>* p, paths)
  143. {
  144. if (*p != expectedPath1 && *p != expectedPath2)
  145. {
  146. printIntegerList("current:", *p);
  147. printIntegerList("expected:", expectedPath1, false);
  148. printIntegerList(" or ", expectedPath2);
  149. return EXIT_FAILURE;
  150. }
  151. }
  152. expectedPath1.clear();
  153. expectedPath2.clear();
  154. graph.findPaths(14, 7, paths);
  155. expectedPath1 << 14 << 9 << 3 << 4 << 1 << 5 << 7;
  156. expectedPath2 << 14 << 9 << 2 << 1 << 5 << 7;
  157. expectedPath3 << 14 << 13 << 12 << 11 << 10 << 7;
  158. foreach(QList<int>* p, paths)
  159. {
  160. if (*p != expectedPath1 && *p != expectedPath2 && *p != expectedPath3)
  161. {
  162. printIntegerList("current:", *p);
  163. printIntegerList("expected:", expectedPath1, false);
  164. printIntegerList(" or ", expectedPath2, false);
  165. printIntegerList(" or ", expectedPath3);
  166. return EXIT_FAILURE;
  167. }
  168. }
  169. // QList<int> list;
  170. // graph.setEdgeListToExclude( list );
  171. //
  172. // graph.shouldExcludeEdge(2);
  173. //
  174. // QList<int> sortedlist;
  175. // graph.topologicalSort( sortedlist );
  176. return EXIT_SUCCESS;
  177. }