60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
|
/**
|
|||
|
* File: graph_bfs.cpp
|
|||
|
* Created Time: 2023-03-02
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
#include "./graph_adjacency_list.cpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD> */
|
|||
|
// ʹ<><CAB9><EFBFBD>ڽӱ<DABD><D3B1><EFBFBD><EFBFBD><EFBFBD>ʾͼ<CABE><CDBC><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽӶ<DABD><D3B6><EFBFBD>
|
|||
|
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
vector<Vertex *> res;
|
|||
|
// <20><>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>¼<EFBFBD>ѱ<EFBFBD><D1B1><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>Ķ<EFBFBD><C4B6><EFBFBD>
|
|||
|
unordered_set<Vertex *> visited = {startVet};
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5> BFS
|
|||
|
queue<Vertex *> que;
|
|||
|
que.push(startVet);
|
|||
|
// <20>Զ<EFBFBD><D4B6><EFBFBD> vet Ϊ<><CEAA><EFBFBD>㣬ѭ<E3A3AC><D1AD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
|
|||
|
while (!que.empty()) {
|
|||
|
Vertex *vet = que.front();
|
|||
|
que.pop(); // <20><><EFBFBD><EFBFBD><D7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
res.push_back(vet); // <20><>¼<EFBFBD><C2BC><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽӶ<DABD><D3B6><EFBFBD>
|
|||
|
for (auto adjVet : graph.adjList[vet]) {
|
|||
|
if (visited.count(adjVet))
|
|||
|
continue; // <20><><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><D1B1><EFBFBD><EFBFBD>ʵĶ<CAB5><C4B6><EFBFBD>
|
|||
|
que.push(adjVet); // ֻ<><D6BB><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ʵĶ<CAB5><C4B6><EFBFBD>
|
|||
|
visited.emplace(adjVet); // <20><><EFBFBD>Ǹö<C7B8><C3B6><EFBFBD><EFBFBD>ѱ<EFBFBD><D1B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
// <20><><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return res;
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ */
|
|||
|
vector<Vertex *> v = valsToVets({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
|||
|
vector<vector<Vertex *>> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[1], v[4]},
|
|||
|
{v[2], v[5]}, {v[3], v[4]}, {v[3], v[6]}, {v[4], v[5]},
|
|||
|
{v[4], v[7]}, {v[5], v[8]}, {v[6], v[7]}, {v[7], v[8]}};
|
|||
|
GraphAdjList graph(edges);
|
|||
|
cout << "\n<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼΪ\\n";
|
|||
|
graph.print();
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD> */
|
|||
|
vector<Vertex *> res = graphBFS(graph, v[0]);
|
|||
|
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BFS<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ" << endl;
|
|||
|
printVector(vetsToVals(res));
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
for (Vertex *vet : v) {
|
|||
|
delete vet;
|
|||
|
}
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|