Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include "function.h"
  2. #include <queue>
  3. #include <map>
  4. using namespace std;
  5.  
  6.  
  7. void Implement::addEdge(const int label_1, const int label_2 , const int weight)
  8. {
  9. bool f1 = 0, f2 = 0;
  10. for (auto i = VertexArr.begin(); i != VertexArr.end(); i++) {
  11. if (i->label == label_1) {
  12. for (auto j : i->neighbors) {
  13. if (j.label == label_2)return;
  14. }
  15. i->neighbors.push_back(Neighbor(label_2, weight));
  16. f1 = 1;
  17. } else if (i->label == label_2) {
  18. for (auto j : i->neighbors) {
  19. if (j.label == label_1)return;
  20. }
  21. i->neighbors.push_back(Neighbor(label_1, weight));
  22. f2 = 1;
  23. }
  24. }
  25. if (!f1) {
  26. VertexArr.push_back(Vertex(label_1));
  27. if(label_1==label_2)return;
  28. VertexArr.back().neighbors.push_back(Neighbor(label_2, weight));
  29. }
  30. if (!f2) {
  31. VertexArr.push_back(Vertex(label_2));
  32. VertexArr.back().neighbors.push_back(Neighbor(label_1, weight));
  33. }
  34. }
  35. void Implement::deleteEdge(const int label_1, const int label_2)
  36. {
  37. if(label_2==label_1)return;
  38. for (auto i = VertexArr.begin(); i != VertexArr.end(); i++) {
  39. if (i->label == label_1) {
  40. for (auto it = i->neighbors.begin(); it != i->neighbors.end(); it++) {
  41. if (it->label == label_2) {
  42. i->neighbors.erase(it);
  43. break;
  44. }
  45. }
  46. } else if (i->label == label_2) {
  47. for (auto it = i->neighbors.begin(); it != i->neighbors.end(); it++) {
  48. if (it->label == label_1) {
  49. i->neighbors.erase(it);
  50. break;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. void Implement::deleteVertex(const int label)
  57. {
  58. int length;
  59. map<int, bool> m;
  60. for (auto it = VertexArr.begin(); it != VertexArr.end(); it++) {
  61. if (it->label == label) {
  62. length = it->neighbors.size();
  63. for (auto i : it->neighbors)m[i.label] = 1;
  64. VertexArr.erase(it);
  65. break;
  66. }
  67. }
  68. for (auto i = VertexArr.begin(); i != VertexArr.end(); i++) {
  69. if (m[i->label]) {
  70. for (auto it = i->neighbors.begin(); it != i->neighbors.end(); it++) {
  71. if (it->label == label) {
  72. i->neighbors.erase(it);
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. int Implement::degree(const int label)
  80. {
  81. for (auto v : VertexArr) {
  82. if (v.label == label) {
  83. return v.neighbors.size();
  84. }
  85. }
  86. return 0;
  87. }
  88. bool Implement::isExistPath(const int label_1, const int label_2)
  89. {
  90. queue<int> s;
  91. map<int, bool> m;
  92. s.push(label_1);
  93. while (!s.empty()) {
  94. int a = s.front();
  95. s.pop();
  96. for (auto i : VertexArr) {
  97. if (i.label == a) {
  98. if (label_1 == label_2)return 1;
  99. for (auto j : i.neighbors) {
  100. if (j.label == label_2)return 1;
  101. else if (!m[j.label]) s.push(j.label), m[j.label] = 1;
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. return 0;
  108. }
  109. void Implement::deleteGraph()
  110. {
  111. VertexArr.clear();
  112. }
  113. int Implement::number_of_component()
  114. {
  115. if (VertexArr.empty())return 0;
  116. map<int, bool> m;
  117. int total = 0;
  118. for (auto i : VertexArr) {
  119. if (!m[i.label]) {
  120. total++;
  121. m[i.label] = 1;
  122. queue<int> s;
  123. s.push(i.label);
  124. while (!s.empty()) {
  125. int a = s.front();
  126. s.pop();
  127. for (auto j : VertexArr) {
  128. if (j.label == a) {
  129. for (auto k : j.neighbors) {
  130. if (!m[k.label]) {
  131. m[k.label] = 1;
  132. s.push(k.label);
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return total;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement