TheAlgorithms-C-Plus-Plus/graph/bfs.cpp

63 lines
1.4 KiB
C++
Raw Normal View History

2020-04-18 10:43:43 +08:00
#include <iostream>
2016-11-29 00:48:08 +08:00
using namespace std;
class graph {
int v;
list<int> *adj;
2019-08-21 10:10:08 +08:00
public:
graph(int v);
void addedge(int src, int dest);
void printgraph();
void bfs(int s);
2016-11-29 00:48:08 +08:00
};
graph::graph(int v) {
this->v = v;
this->adj = new list<int>[v];
2016-11-29 00:48:08 +08:00
}
void graph::addedge(int src, int dest) {
src--;
dest--;
adj[src].push_back(dest);
// adj[dest].push_back(src);
2016-11-29 00:48:08 +08:00
}
void graph::printgraph() {
for (int i = 0; i < this->v; i++) {
cout << "Adjacency list of vertex " << i + 1 << " is \n";
list<int>::iterator it;
for (it = adj[i].begin(); it != adj[i].end(); ++it) {
cout << *it + 1 << " ";
}
cout << endl;
}
2016-11-29 00:48:08 +08:00
}
void graph::bfs(int s) {
bool *visited = new bool[this->v + 1];
memset(visited, false, sizeof(bool) * (this->v + 1));
visited[s] = true;
list<int> q;
q.push_back(s);
list<int>::iterator it;
while (!q.empty()) {
int u = q.front();
cout << u << " ";
q.pop_front();
for (it = adj[u].begin(); it != adj[u].end(); ++it) {
if (visited[*it] == false) {
visited[*it] = true;
q.push_back(*it);
}
}
}
2016-11-29 00:48:08 +08:00
}
int main() {
graph g(4);
g.addedge(1, 2);
g.addedge(2, 3);
g.addedge(3, 4);
g.addedge(1, 4);
g.addedge(1, 3);
// g.printgraph();
g.bfs(2);
return 0;
2019-02-17 20:28:40 +08:00
}