1
0

Graph.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Created by 李洋 on 2023/10/21.
  3. // 以邻接矩阵实现的INT图及其具体操作
  4. //
  5. #ifndef LEECODE_C_GRAPH_H
  6. #define LEECODE_C_GRAPH_H
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <random>
  12. #include <iostream>
  13. #include <stack>
  14. using namespace std;
  15. class Graph {
  16. public:
  17. int size;
  18. Graph(int n, bool direction) {
  19. this->size = n;
  20. G = vector<vector<int>>(size, vector<int>(size, 0));
  21. visited = vector<int>(size, 0);
  22. random_device rd;
  23. mt19937 gen(rd());
  24. uniform_int_distribution<int> dis(0, 1);
  25. if (!direction) {
  26. for (int i = 0; i < size; ++i) {
  27. for (int j = i + 1; j < size; ++j) {
  28. if (dis(gen)) {
  29. G[i][j] = 1;
  30. G[j][i] = 1;
  31. }
  32. }
  33. }
  34. } else {
  35. for (int i = 0; i < size; ++i) {
  36. for (int j = 0; j < size; ++j) {
  37. if (i == j) {
  38. continue;
  39. }
  40. if (dis(gen)) {
  41. G[i][j] = 1;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. // 0 1 0 0 0 0
  48. // 1 0 1 1 0 0
  49. // 0 1 0 0 0 1
  50. // 0 1 0 0 0 0
  51. // 0 0 0 0 0 1
  52. // 0 0 1 0 1 0
  53. Graph(int x) {
  54. size = 6;
  55. G = vector<vector<int>>(size, vector<int>(size, 0));
  56. visited = vector<int>(size, 0);
  57. vector<vector<int>> Edges;
  58. if (x == 1) {
  59. Edges.push_back({0, 1});
  60. Edges.push_back({1, 2});
  61. Edges.push_back({1, 3});
  62. Edges.push_back({2, 5});
  63. Edges.push_back({4, 5});
  64. }
  65. if (x == 2) {
  66. }
  67. for (auto Edge: Edges) {
  68. G[Edge[0]][Edge[1]] = 1;
  69. G[Edge[1]][Edge[0]] = 1;
  70. }
  71. }
  72. Graph *GraphByEdge(vector<vector<int>> &Edges) {
  73. for (auto Edge: Edges) {
  74. vertex.insert(Edge[1]);
  75. vertex.insert(Edge[2]);
  76. }
  77. size = vertex.size();
  78. G = vector<vector<int>>(size, vector<int>(size, 0));
  79. for (auto Edge: Edges) {
  80. G[Edge[1]][Edge[2]] = 1;
  81. }
  82. return this;
  83. }
  84. void printG() {
  85. for (int i = 0; i < size; ++i) {
  86. for (int j = 0; j < size; ++j) {
  87. cout << G[i][j] << " ";
  88. }
  89. cout << endl;
  90. }
  91. }
  92. queue<int> DFS(int start) {
  93. queue<int> Q;
  94. stack<pair<int, int>> S;
  95. Q.push(start);
  96. S.emplace(start, -1);
  97. visited[0] = 1;
  98. while (!S.empty()) {
  99. auto temp = S.top();
  100. auto next = NextPoint(temp.first, temp.second);
  101. S.top().second = next;
  102. if (next == -1) {
  103. S.pop();
  104. continue;
  105. }
  106. if (visited[next]) {
  107. continue;
  108. }
  109. Q.push(next);
  110. visited[next] = 1;
  111. S.emplace(next, -1);
  112. }
  113. if (Q.size() != size) {
  114. // 将其他的点再进行DSF
  115. }
  116. return Q;
  117. }
  118. queue<int> BFS(int start) {
  119. queue<int> Q;
  120. queue<pair<int, int>> S;
  121. Q.push(start);
  122. S.emplace(start, -1);
  123. visited[0] = 1;
  124. while (!S.empty()) {
  125. auto temp = S.front();
  126. auto next = NextPoint(temp.first, temp.second);
  127. S.front().second = next;
  128. if (next == -1) {
  129. S.pop();
  130. continue;
  131. }
  132. if (visited[next]) {
  133. continue;
  134. }
  135. Q.push(next);
  136. visited[next] = 1;
  137. S.emplace(next, -1);
  138. }
  139. if (Q.size() != size) {
  140. // 将其他的点再进行BSF
  141. }
  142. return Q;
  143. }
  144. int NextPoint(int vex, int now) {
  145. auto row = G[vex];
  146. for (int i = now + 1; i < size; ++i) {
  147. if (row[i] != 0) {
  148. return i;
  149. }
  150. }
  151. return -1;
  152. }
  153. private:
  154. vector<vector<int>> G; // adjacent matrix
  155. vector<int> visited;
  156. set<int> vertex; // The collection of vertex
  157. set<set<int>> ccp; // The graph has multiple connected components.
  158. };
  159. #endif //LEECODE_C_GRAPH_H
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。