Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /*
  2. * File: DirectedCycle.h
  3. * Author: Olivier Cuisenaire
  4. * Created on 08. octobre 2014, 10:46
  5. *
  6. * A implementer
  7. * Classe permettant la detection de cycle sur un graphe oriente
  8. */
  9.  
  10. #ifndef ASD2_DirectedCycle_h
  11. #define ASD2_DirectedCycle_h
  12.  
  13. template<typename GraphType>
  14. class DirectedCycle {
  15. private:
  16. /* A DEFINIR */
  17. DiGraph * graph;
  18. std::vector<int> elements ; // all
  19. bool foundedCycle; //boolean denoting if a cycle is contained in this graph
  20. public:
  21. //constructeur
  22. DirectedCycle(const GraphType& g) {
  23. /* A IMPLEMENTER */
  24. graph = new DiGraph(g.V());
  25. foundedCycle=HasCycle();
  26. elements=Cycle();
  27. }
  28.  
  29.  
  30. //indique la presence d'un cycle
  31. bool HasCycle() {
  32. /* A IMPLEMENTER */
  33. return foundedCycle;
  34. }
  35.  
  36. bool CyclicRecursive(int v, bool visited[], bool *recStack)
  37. {
  38. if(visited[v] == false)
  39. {
  40. // Mark the current node as visited and part of recursion stack
  41. visited[v] = true;
  42. recStack[v] = true;
  43.  
  44. // Recur for all the vertices adjacent to this vertex
  45. std::list<int>::iterator i;
  46. for(i = this->g.adjacencyLists[v].begin(); i != this->g.adjacencyLists[v].end(); ++i)
  47. {
  48. if ( !visited[*i] && CyclicRecursive(*i, visited, recStack) )
  49. return true;
  50. else if (recStack[*i])
  51. return true;
  52. }
  53.  
  54. }
  55. recStack[v] = false; // remove the vertex from recursion stack
  56. return false;
  57. }
  58.  
  59. //liste les indexes des sommets formant une boucle
  60.  
  61. std::list<int> Cycle() {
  62. // A IMPLEMENTER
  63. std::list<int> cycle;
  64. bool *visited = new bool[graph.V()];
  65. bool *recStack = new bool[graph.V()];
  66. for(int i = 0; i < graph.V(); i++)
  67. {
  68. visited[i] = false;
  69. recStack[i] = false;
  70. }
  71. for(int i = 0; i < V; i++)
  72. if (CyclicRecursive(i, visited, recStack))
  73. foundedCycle = true;
  74.  
  75. return false;
  76. }
  77.  
  78.  
  79.  
  80. };
  81. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement