// // Created by 李洋 on 2023/10/21. // 以邻接矩阵实现的INT图及其具体操作 // #ifndef LEECODE_C_GRAPH_H #define LEECODE_C_GRAPH_H #include #include #include #include #include #include #include using namespace std; class Graph { public: int size; Graph(int n, bool direction) { this->size = n; G = vector>(size, vector(size, 0)); visited = vector(size, 0); random_device rd; mt19937 gen(rd()); uniform_int_distribution dis(0, 1); if (!direction) { for (int i = 0; i < size; ++i) { for (int j = i + 1; j < size; ++j) { if (dis(gen)) { G[i][j] = 1; G[j][i] = 1; } } } } else { for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { if (i == j) { continue; } if (dis(gen)) { G[i][j] = 1; } } } } } // 0 1 0 0 0 0 // 1 0 1 1 0 0 // 0 1 0 0 0 1 // 0 1 0 0 0 0 // 0 0 0 0 0 1 // 0 0 1 0 1 0 Graph(int x) { size = 6; G = vector>(size, vector(size, 0)); visited = vector(size, 0); vector> Edges; if (x == 1) { Edges.push_back({0, 1}); Edges.push_back({1, 2}); Edges.push_back({1, 3}); Edges.push_back({2, 5}); Edges.push_back({4, 5}); } if (x == 2) { } for (auto Edge: Edges) { G[Edge[0]][Edge[1]] = 1; G[Edge[1]][Edge[0]] = 1; } } Graph *GraphByEdge(vector> &Edges) { for (auto Edge: Edges) { vertex.insert(Edge[1]); vertex.insert(Edge[2]); } size = vertex.size(); G = vector>(size, vector(size, 0)); for (auto Edge: Edges) { G[Edge[1]][Edge[2]] = 1; } return this; } void printG() { for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { cout << G[i][j] << " "; } cout << endl; } } queue DFS(int start) { queue Q; stack> S; Q.push(start); S.emplace(start, -1); visited[0] = 1; while (!S.empty()) { auto temp = S.top(); auto next = NextPoint(temp.first, temp.second); S.top().second = next; if (next == -1) { S.pop(); continue; } if (visited[next]) { continue; } Q.push(next); visited[next] = 1; S.emplace(next, -1); } if (Q.size() != size) { // 将其他的点再进行DSF } return Q; } queue BFS(int start) { queue Q; queue> S; Q.push(start); S.emplace(start, -1); visited[0] = 1; while (!S.empty()) { auto temp = S.front(); auto next = NextPoint(temp.first, temp.second); S.front().second = next; if (next == -1) { S.pop(); continue; } if (visited[next]) { continue; } Q.push(next); visited[next] = 1; S.emplace(next, -1); } if (Q.size() != size) { // 将其他的点再进行BSF } return Q; } int NextPoint(int vex, int now) { auto row = G[vex]; for (int i = now + 1; i < size; ++i) { if (row[i] != 0) { return i; } } return -1; } private: vector> G; // adjacent matrix vector visited; set vertex; // The collection of vertex set> ccp; // The graph has multiple connected components. }; #endif //LEECODE_C_GRAPH_H