ctkDependencyGraph.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. // CTK includes
  15. #include "ctkDependencyGraph.h"
  16. // STD includes
  17. #include <iostream>
  18. #include <sstream>
  19. #include <algorithm>
  20. #include <vector>
  21. #include <set>
  22. #include <map>
  23. #include <list>
  24. #include <queue>
  25. #include <cassert>
  26. #define MAXV 100
  27. #define MAXDEGREE 50
  28. //----------------------------------------------------------------------------
  29. class ctkDependencyGraphPrivate
  30. {
  31. public:
  32. ctkDependencyGraph* const q_ptr;
  33. ctkDependencyGraphPrivate(ctkDependencyGraph& p);
  34. /// Compute outdegree
  35. void computeOutdegrees(std::vector<int>& computedOutdegrees);
  36. /// Traverse tree using Depth-first_search
  37. void traverseUsingDFS(int v);
  38. /// Called each time an edge is visited
  39. void processEdge(int from, int to);
  40. /// Called each time a vertex is processed
  41. void processVertex(int v);
  42. /// Retrieve the path between two vertices
  43. void findPathDFS(int from, int to, std::list<int>& path);
  44. /// Recursive function used by findPaths to retrieve the path between two vertices
  45. void findPathsRec(int from, int to, std::list<int>* path, std::list<std::list<int>* >& paths);
  46. void setEdge(int vertice, int degree, int value);
  47. int edge(int vertice, int degree)const;
  48. void verticesWithIndegree(int indegree, std::list<int>& list);
  49. int subgraphSize(int rootId);
  50. void subgraphSizeRec(int rootId, std::set<int>& uniqueVertices);
  51. void subgraphInsert(ctkDependencyGraph& subgraph, int rootId,
  52. std::map<int,int>& subgraphIdToGlobal, std::map<int,int>& globalIdToSubgraph);
  53. int getOrGenerateSubgraphId(std::map<int, int>& subgraphIdToGlobal,
  54. std::map<int, int>& globalIdToSubgraph,
  55. int globalId);
  56. /// See http://en.wikipedia.org/wiki/Adjacency_list
  57. std::vector<std::vector<int>* > Edges;
  58. std::vector<int> OutDegree;
  59. std::vector<int> InDegree;
  60. int NVertices;
  61. int NEdges;
  62. /// Structure used by DFS
  63. /// See http://en.wikipedia.org/wiki/Depth-first_search
  64. std::vector<bool> Processed; // processed vertices
  65. std::vector<bool> Discovered; // discovered vertices
  66. std::vector<int> Parent; // relation discovered
  67. bool Abort; // Flag indicating if traverse should be aborted
  68. bool Verbose;
  69. bool CycleDetected;
  70. int CycleOrigin;
  71. int CycleEnd;
  72. std::list<int> ListOfEdgeToExclude;
  73. };
  74. //----------------------------------------------------------------------------
  75. // Returns a space separated string of T.
  76. template<class T>
  77. std::string listToString(const std::list<T>& list)
  78. {
  79. std::stringstream outputString;
  80. typename std::list<T>::const_iterator iter;
  81. for (iter = list.begin(); iter != list.end(); iter++)
  82. {
  83. outputString << *iter << " ";
  84. }
  85. return outputString.str();
  86. }
  87. //----------------------------------------------------------------------------
  88. // Returns true if the map contains the key and false otherwise.
  89. template<class T1, class T2>
  90. bool mapContainsKey(const std::map<T1, T2>& map, const T1& key)
  91. {
  92. bool result = false;
  93. if (map.find(key) != map.end())
  94. {
  95. result = true;
  96. }
  97. return result;
  98. }
  99. //----------------------------------------------------------------------------
  100. // Returns true if the list contains the value and false otherwise.
  101. template<class T>
  102. bool listContainsValue(const std::list<T>& list, const T& value)
  103. {
  104. bool result = false;
  105. typename std::list<T>::const_iterator iter;
  106. for (iter = list.begin(); iter != list.end(); iter++)
  107. {
  108. if ((*iter) == value)
  109. {
  110. result = true;
  111. break;
  112. }
  113. }
  114. return result;
  115. }
  116. //----------------------------------------------------------------------------
  117. // ctkInternal methods
  118. //----------------------------------------------------------------------------
  119. ctkDependencyGraphPrivate::ctkDependencyGraphPrivate(ctkDependencyGraph& object)
  120. :q_ptr(&object)
  121. {
  122. this->NVertices = 0;
  123. this->NEdges = 0;
  124. this->Abort = false;
  125. this->Verbose = false;
  126. this->CycleDetected = false;
  127. this->CycleOrigin = 0;
  128. this->CycleEnd = 0;
  129. }
  130. //----------------------------------------------------------------------------
  131. void ctkDependencyGraphPrivate::computeOutdegrees(std::vector<int>& computedOutdegrees)
  132. {
  133. for (int i=1; i <= this->NVertices; i++)
  134. {
  135. computedOutdegrees[i] = 0;
  136. }
  137. for (int i=1; i <= this->NVertices; i++)
  138. {
  139. for (int j=0; j < this->OutDegree[i]; j++)
  140. {
  141. computedOutdegrees[ this->edge(i,j) ] ++;
  142. }
  143. }
  144. }
  145. //----------------------------------------------------------------------------
  146. void ctkDependencyGraphPrivate::traverseUsingDFS(int v)
  147. {
  148. // allow for search termination
  149. if (this->Abort)
  150. {
  151. return;
  152. }
  153. this->Discovered[v] = true;
  154. this->processVertex(v);
  155. int y; // successor vertex
  156. for (int i=0; i<this->OutDegree[v]; i++)
  157. {
  158. y = this->edge(v, i);
  159. if (q_ptr->shouldExcludeEdge(this->edge(v, i)) == false)
  160. {
  161. this->Parent[y] = v;
  162. if (this->Discovered[y] == false)
  163. {
  164. this->traverseUsingDFS(y);
  165. }
  166. else
  167. {
  168. if (this->Processed[y] == false)
  169. {
  170. this->processEdge(v,y);
  171. }
  172. }
  173. }
  174. if (this->Abort)
  175. {
  176. return;
  177. }
  178. }
  179. this->Processed[v] = true;
  180. }
  181. //----------------------------------------------------------------------------
  182. void ctkDependencyGraphPrivate::processEdge(int from, int to)
  183. {
  184. if (this->Discovered[to] == true)
  185. {
  186. this->CycleDetected = true;
  187. this->CycleOrigin = to;
  188. this->CycleEnd = from;
  189. if (this->Verbose)
  190. {
  191. std::list<int> path;
  192. this->findPathDFS(from, to, path);
  193. std::cerr << "ERROR: Cycle detected from " << to << " to " << from << std::endl;
  194. std::cerr << " " << listToString<int>(path) << std::endl;
  195. path.clear();
  196. this->findPathDFS(to, from, path);
  197. std::cerr << " " << listToString<int>(path) << std::endl;
  198. }
  199. this->Abort = true;
  200. }
  201. }
  202. //----------------------------------------------------------------------------
  203. void ctkDependencyGraphPrivate::processVertex(int v)
  204. {
  205. if (this->Verbose)
  206. {
  207. std::cout << "processed vertex " << v << std::endl;
  208. }
  209. }
  210. //----------------------------------------------------------------------------
  211. void ctkDependencyGraphPrivate::setEdge(int vertice, int degree, int value)
  212. {
  213. assert(vertice <= this->NVertices);
  214. assert(degree < MAXDEGREE);
  215. (*this->Edges[vertice])[degree] = value;
  216. }
  217. //----------------------------------------------------------------------------
  218. int ctkDependencyGraphPrivate::edge(int vertice, int degree)const
  219. {
  220. assert(vertice <= this->NVertices);
  221. assert(degree < MAXDEGREE);
  222. return (*this->Edges[vertice])[degree];
  223. }
  224. //----------------------------------------------------------------------------
  225. void ctkDependencyGraphPrivate::findPathDFS(int from, int to, std::list<int>& path)
  226. {
  227. if ((from == to) || (to == -1))
  228. {
  229. path.push_back(from);
  230. }
  231. else
  232. {
  233. this->findPathDFS(from, this->Parent[to], path);
  234. path.push_back(to);
  235. }
  236. }
  237. //----------------------------------------------------------------------------
  238. void ctkDependencyGraphPrivate::findPathsRec(
  239. int from, int to, std::list<int>* path, std::list<std::list<int>* >& paths)
  240. {
  241. if (from == to)
  242. {
  243. return;
  244. }
  245. std::list<int> branch;
  246. branch = *path;
  247. int child = from;
  248. for (int j=0; j < this->OutDegree[child]; j++)
  249. {
  250. if (j == 0)
  251. {
  252. int parent = this->edge(child, j);
  253. (*path).push_back(parent);
  254. this->findPathsRec(parent, to, path, paths);
  255. }
  256. else
  257. {
  258. int parent = this->edge(child, j);
  259. // Copy path and add it to the list
  260. std::list<int>* pathCopy = new std::list<int>();
  261. *pathCopy = branch;
  262. paths.push_back(pathCopy);
  263. (*pathCopy).push_back(parent);
  264. this->findPathsRec(parent, to, pathCopy, paths);
  265. }
  266. }
  267. }
  268. //----------------------------------------------------------------------------
  269. void ctkDependencyGraphPrivate::verticesWithIndegree(int indegree, std::list<int>& list)
  270. {
  271. assert(indegree >= 0);
  272. for (int i=1; i <= this->NVertices; i++)
  273. {
  274. if (this->InDegree[i] == indegree)
  275. {
  276. list.push_back(i);
  277. }
  278. }
  279. }
  280. //----------------------------------------------------------------------------
  281. void ctkDependencyGraphPrivate::subgraphSizeRec(int rootId, std::set<int>& uniqueVertices)
  282. {
  283. assert(rootId > 0);
  284. for (int i = 0; i < this->OutDegree[rootId]; ++i)
  285. {
  286. int child = this->edge(rootId, i);
  287. uniqueVertices.insert(child);
  288. subgraphSizeRec(child, uniqueVertices);
  289. }
  290. }
  291. //----------------------------------------------------------------------------
  292. int ctkDependencyGraphPrivate::subgraphSize(int rootId)
  293. {
  294. assert(rootId > 0);
  295. std::set<int> vertices;
  296. vertices.insert(rootId);
  297. this->subgraphSizeRec(rootId, vertices);
  298. return vertices.size();
  299. }
  300. void ctkDependencyGraphPrivate::subgraphInsert(
  301. ctkDependencyGraph& subgraph, int rootId,
  302. std::map<int,int>& subgraphIdToGlobal, std::map<int,int>& globalIdToSubgraph)
  303. {
  304. int from = this->getOrGenerateSubgraphId(subgraphIdToGlobal, globalIdToSubgraph, rootId);
  305. for (int i = 0; i < this->OutDegree[rootId]; ++i)
  306. {
  307. int childId = this->edge(rootId, i);
  308. int to = this->getOrGenerateSubgraphId(subgraphIdToGlobal, globalIdToSubgraph,
  309. childId);
  310. subgraph.insertEdge(from, to);
  311. this->subgraphInsert(subgraph, childId, subgraphIdToGlobal, globalIdToSubgraph);
  312. }
  313. }
  314. //----------------------------------------------------------------------------
  315. int ctkDependencyGraphPrivate::getOrGenerateSubgraphId(
  316. std::map<int, int>& subgraphIdToGlobal,
  317. std::map<int, int>& globalIdToSubgraph,
  318. int globalId)
  319. {
  320. // If needed, generate vertex id
  321. int subgraphId = -1;
  322. if (!mapContainsKey<int, int>(globalIdToSubgraph,globalId))
  323. {
  324. subgraphId = globalIdToSubgraph.size() + 1;
  325. globalIdToSubgraph[globalId] = subgraphId;
  326. subgraphIdToGlobal[subgraphId] = globalId;
  327. }
  328. else
  329. {
  330. subgraphId = globalIdToSubgraph[globalId];
  331. }
  332. return subgraphId;
  333. }
  334. //----------------------------------------------------------------------------
  335. // ctkDependencyGraph methods
  336. //----------------------------------------------------------------------------
  337. ctkDependencyGraph::ctkDependencyGraph(int nvertices)
  338. :d_ptr(new ctkDependencyGraphPrivate(*this))
  339. {
  340. d_ptr->NVertices = nvertices;
  341. // Resize internal array
  342. d_ptr->Processed.resize(nvertices + 1);
  343. d_ptr->Discovered.resize(nvertices + 1);
  344. d_ptr->Parent.resize(nvertices + 1);
  345. d_ptr->Edges.resize(nvertices + 1);
  346. d_ptr->OutDegree.resize(nvertices + 1);
  347. d_ptr->InDegree.resize(nvertices + 1);
  348. for (int i=1; i <= nvertices; i++)
  349. {
  350. d_ptr->OutDegree[i] = 0;
  351. d_ptr->InDegree[i] = 0;
  352. }
  353. // initialize Edge adjacency list
  354. for (int i=0; i <= nvertices; i++)
  355. {
  356. d_ptr->Edges[i] = new std::vector<int>();
  357. d_ptr->Edges[i]->resize(MAXDEGREE);
  358. }
  359. // initialize search
  360. for (int i=1; i <= nvertices; i++)
  361. {
  362. d_ptr->Processed[i] = false;
  363. d_ptr->Discovered[i] = false;
  364. d_ptr->Parent[i] = -1;
  365. }
  366. }
  367. //----------------------------------------------------------------------------
  368. ctkDependencyGraph::~ctkDependencyGraph()
  369. {
  370. // Clean memory
  371. for (int i=0; i <= d_ptr->NVertices; i++)
  372. {
  373. delete d_ptr->Edges[i];
  374. }
  375. }
  376. //----------------------------------------------------------------------------
  377. void ctkDependencyGraph::printAdditionalInfo()const
  378. {
  379. std::cout << "ctkDependencyGraph (" << this << ")" << std::endl
  380. << " NVertices:" << this->numberOfVertices() << std::endl
  381. << " NEdges:" << this->numberOfEdges() << std::endl
  382. << " Abort:" << d_ptr->Abort << std::endl;
  383. std::cout << " [Processed]" << std::endl;
  384. for(unsigned int i=1; i < d_ptr->Processed.size(); i++)
  385. {
  386. std::cout << i << "->" << d_ptr->Processed[i] << std::endl;
  387. }
  388. std::cout << " [Discovered]" << std::endl;
  389. for(unsigned int i=1; i < d_ptr->Discovered.size(); i++)
  390. {
  391. std::cout << i << "->" << d_ptr->Discovered[i] << std::endl;
  392. }
  393. std::cout << " [Parent]" << std::endl;
  394. for(unsigned int i=1; i < d_ptr->Parent.size(); i++)
  395. {
  396. std::cout << i << "->" << d_ptr->Parent[i] << std::endl;
  397. }
  398. std::cout << " [Graph]" << std::endl;
  399. this->printGraph();
  400. }
  401. //----------------------------------------------------------------------------
  402. void ctkDependencyGraph::printGraph()const
  403. {
  404. for(int i=1; i <= d_ptr->NVertices; i++)
  405. {
  406. std::cout << i << ":";
  407. for (int j=0; j < d_ptr->OutDegree[i]; j++)
  408. {
  409. std::cout << " " << d_ptr->edge(i, j);
  410. }
  411. std::cout << std::endl;
  412. }
  413. }
  414. //----------------------------------------------------------------------------
  415. int ctkDependencyGraph::numberOfVertices()const
  416. {
  417. return d_ptr->NVertices;
  418. }
  419. //----------------------------------------------------------------------------
  420. int ctkDependencyGraph::numberOfEdges()const
  421. {
  422. return d_ptr->NEdges;
  423. }
  424. //----------------------------------------------------------------------------
  425. void ctkDependencyGraph::setVerbose(bool verbose)
  426. {
  427. d_ptr->Verbose = verbose;
  428. }
  429. //----------------------------------------------------------------------------
  430. void ctkDependencyGraph::setEdgeListToExclude(const std::list<int>& list)
  431. {
  432. d_ptr->ListOfEdgeToExclude = list;
  433. }
  434. //----------------------------------------------------------------------------
  435. bool ctkDependencyGraph::shouldExcludeEdge(int edge)const
  436. {
  437. return listContainsValue(d_ptr->ListOfEdgeToExclude, edge);
  438. }
  439. //----------------------------------------------------------------------------
  440. bool ctkDependencyGraph::checkForCycle()
  441. {
  442. if (d_ptr->NEdges > 0)
  443. {
  444. // Store unprocessed vertex ids
  445. std::list<int> uncheckedVertices;
  446. for (int i = 1; i <= d_ptr->NVertices; ++i)
  447. {
  448. uncheckedVertices.push_back(i);
  449. }
  450. // Start the cycle detection on the source vertices
  451. std::list<int> sources;
  452. this->sourceVertices(sources);
  453. std::list<int>::const_iterator sourcesIterator;
  454. for (sourcesIterator = sources.begin(); sourcesIterator != sources.end(); sourcesIterator++)
  455. {
  456. d_ptr->traverseUsingDFS(*sourcesIterator);
  457. if (this->cycleDetected()) return true;
  458. for (unsigned int i=0; i < d_ptr->Processed.size(); i++)
  459. {
  460. if (d_ptr->Processed[i] == true)
  461. {
  462. uncheckedVertices.remove(i);
  463. }
  464. d_ptr->Discovered[i] = false;
  465. d_ptr->Processed[i] = false;
  466. }
  467. }
  468. // If a component does not have a source vertex,
  469. // i.e. it is a cycle a -> b -> a, check all non
  470. // processed vertices.
  471. while (!uncheckedVertices.empty())
  472. {
  473. d_ptr->traverseUsingDFS((*uncheckedVertices.rbegin()));
  474. if (this->cycleDetected()) return true;
  475. for (unsigned int i=0; i < d_ptr->Processed.size(); i++)
  476. {
  477. if (d_ptr->Processed[i] == true)
  478. {
  479. uncheckedVertices.remove(i);
  480. }
  481. d_ptr->Discovered[i] = false;
  482. d_ptr->Processed[i] = false;
  483. }
  484. }
  485. }
  486. return this->cycleDetected();
  487. }
  488. //----------------------------------------------------------------------------
  489. bool ctkDependencyGraph::cycleDetected()const
  490. {
  491. return d_ptr->CycleDetected;
  492. }
  493. //----------------------------------------------------------------------------
  494. int ctkDependencyGraph::cycleOrigin()const
  495. {
  496. return d_ptr->CycleOrigin;
  497. }
  498. //----------------------------------------------------------------------------
  499. int ctkDependencyGraph::cycleEnd()const
  500. {
  501. return d_ptr->CycleEnd;
  502. }
  503. //----------------------------------------------------------------------------
  504. void ctkDependencyGraph::insertEdge(int from, int to)
  505. {
  506. assert(from > 0 && from <= d_ptr->NVertices);
  507. assert(to > 0 && to <= d_ptr->NVertices);
  508. // resize if needed
  509. int capacity = d_ptr->Edges[from]->capacity();
  510. if (d_ptr->OutDegree[from] > capacity)
  511. {
  512. d_ptr->Edges[from]->resize(capacity + capacity * 0.3);
  513. }
  514. d_ptr->setEdge(from, d_ptr->OutDegree[from], to);
  515. d_ptr->OutDegree[from]++;
  516. d_ptr->InDegree[to]++;
  517. d_ptr->NEdges++;
  518. }
  519. //----------------------------------------------------------------------------
  520. void ctkDependencyGraph::findPaths(int from, int to, std::list<std::list<int>* >& paths)
  521. {
  522. std::list<int>* path = new std::list<int>;
  523. (*path).push_back(from);
  524. (paths).push_back(path);
  525. d_ptr->findPathsRec(from, to, path, paths);
  526. // Remove lists not ending with the requested element
  527. std::list<std::list<int>* >::iterator pathsIterator;
  528. pathsIterator = paths.begin();
  529. while (paths.size() > 0 && pathsIterator != paths.end())
  530. {
  531. std::list<int>* pathToCheck = (*pathsIterator);
  532. assert(pathToCheck);
  533. if (*(pathToCheck->rbegin()) != to)
  534. {
  535. pathsIterator = paths.erase(pathsIterator);
  536. }
  537. else
  538. {
  539. pathsIterator++;
  540. }
  541. }
  542. }
  543. //----------------------------------------------------------------------------
  544. void ctkDependencyGraph::findPath(int from, int to, std::list<int>& path)
  545. {
  546. std::list<std::list<int>* > paths;
  547. this->findPaths(from, to, paths);
  548. if (!paths.empty())
  549. {
  550. std::list<int>::iterator pathIterator;
  551. for (pathIterator = (*(paths.begin()))->begin(); pathIterator != (*(paths.begin()))->end(); pathIterator++)
  552. {
  553. path.push_back(*pathIterator);
  554. }
  555. }
  556. std::list<std::list<int>* >::iterator pathsIterator;
  557. for(pathsIterator = paths.begin(); pathsIterator != paths.end(); pathsIterator++)
  558. {
  559. if (*pathsIterator != NULL)
  560. {
  561. delete *pathsIterator;
  562. }
  563. }
  564. }
  565. //----------------------------------------------------------------------------
  566. bool ctkDependencyGraph::topologicalSort(std::list<int>& sorted, int rootId)
  567. {
  568. if (rootId > 0)
  569. {
  570. ctkDependencyGraph subgraph(d_ptr->subgraphSize(rootId));
  571. std::map<int,int> subgraphIdToGlobal;
  572. std::map<int,int> globalIdToSubgraph;
  573. d_ptr->subgraphInsert(subgraph, rootId, subgraphIdToGlobal, globalIdToSubgraph);
  574. std::list<int> subgraphSorted;
  575. bool result = subgraph.topologicalSort(subgraphSorted);
  576. std::list<int>::const_iterator subgraphSortedIterator;
  577. for (subgraphSortedIterator = subgraphSorted.begin(); subgraphSortedIterator != subgraphSorted.end(); subgraphSortedIterator++)
  578. {
  579. sorted.push_back(subgraphIdToGlobal[*subgraphSortedIterator]);
  580. }
  581. return result;
  582. }
  583. std::vector<int> outdegree; // outdegree of each vertex
  584. outdegree.resize(MAXV);
  585. std::queue<int> zeroout; // vertices of outdegree 0
  586. int x, y; // current and next vertex
  587. outdegree.resize(d_ptr->NVertices + 1);
  588. // resize if needed
  589. if (d_ptr->NVertices > MAXV)
  590. {
  591. outdegree.resize(d_ptr->NVertices);
  592. }
  593. d_ptr->computeOutdegrees(outdegree);
  594. for (int i=1; i <= d_ptr->NVertices; i++)
  595. {
  596. if (outdegree[i] == 0)
  597. {
  598. zeroout.push(i);
  599. }
  600. }
  601. int j=0;
  602. while (zeroout.empty() == false)
  603. {
  604. j = j+1;
  605. x = zeroout.front();
  606. zeroout.pop();
  607. sorted.push_back(x);
  608. for (int i=0; i < d_ptr->OutDegree[x]; i++)
  609. {
  610. y = d_ptr->edge(x, i);
  611. outdegree[y] --;
  612. if (outdegree[y] == 0)
  613. {
  614. zeroout.push(y);
  615. }
  616. }
  617. }
  618. if (j != d_ptr->NVertices)
  619. {
  620. return false;
  621. }
  622. return true;
  623. }
  624. //----------------------------------------------------------------------------
  625. void ctkDependencyGraph::sourceVertices(std::list<int>& sources)
  626. {
  627. d_ptr->verticesWithIndegree(0, sources);
  628. }