Advertisement
Guest User

Untitled

a guest
May 28th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include "graph.h"
  2. #include "node.h"
  3.  
  4. //[1p] Creaza un graph gol, fara noduri, fara muchii
  5. Graph::Graph()
  6. {
  7.  
  8. }
  9.  
  10. //[1p] adauga un nod in graf
  11. void Graph::AddNode(int x, int y, const int& value){
  12. Node n1;
  13. n1.x = x;
  14. n1.y = y;
  15. n1.Value = value;
  16. this->n.push_back(n1);
  17. }
  18. //[5p] adaugat o muchie in graf. Returneaza true daca indecsii sunt valizi, diferiti intre ei si muchia nu exista deja, false altfel
  19. bool Graph::AddEdge(int firstNodeIndex, int secondNodeIndex){
  20. if (firstNodeIndex != secondNodeIndex)
  21. return false;
  22. else
  23. {
  24. for (vector<Edge>::iterator it = this->e.begin(); it != e.end()-1; ++it)
  25. for (vector<Edge>::iterator it2 = this->e.begin()+1; it != e.end() ; ++it)
  26. if (it.operator==(it2))
  27. return false;
  28. else
  29. {
  30. Edge e1;
  31. e1.IndexNode1 = firstNodeIndex;
  32. e1.IndexNode2 = secondNodeIndex;
  33. this->e.push_back(e1);
  34. return true;
  35. }
  36.  
  37.  
  38. }
  39. }
  40.  
  41. int Graph::GetNodesCount(){
  42. return n.size();
  43. }
  44.  
  45.  
  46. //[1p] returneaza numarul de muchii
  47. int Graph::GetEdgesCount(){
  48. return e.size();
  49. }
  50.  
  51. //[1p] returneaza nodul corespunzator pentru un index, sau NULL daca indexul nu e valid
  52. Node* Graph::GetNode(int index){
  53. if (index < n.size)
  54. return &n.at(index);
  55. return NULL;
  56. }
  57.  
  58. //[3p] sterge o muchie
  59. bool Graph::DeleteEdge(int index){
  60. if (index < e.size())
  61. {
  62. e.erase(e.begin()+index);
  63. return true;
  64. }
  65.  
  66. return false;
  67. }
  68.  
  69. //[9p] returneaza true daca graful este conex, false altfel
  70. bool Graph::IsConex()
  71. {
  72. for (int i = 0; i < n.size(); i++)
  73. n.at(i).colour = -1;
  74. Node aux = n.at(0);
  75. aux.colour = 0;
  76. for (int j = 0; j<e.size();j++)
  77. for (int i = 0; i < n.size(); i++)
  78. {
  79. if (e.at(j).IndexNode1 == n.at(i).x || e.at(j).IndexNode2 == n.at(i).x)
  80. if (n.at(i).colour == -1;)
  81. n.at(i).colour = 0;
  82. }
  83. for (int i = 0; i < n.size(); i++)
  84. {
  85. if (n.at(i).colour == -1)
  86. return false;
  87. }
  88. return true;
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement