일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스
- 방통대
- LV1
- 개발자
- machine learning
- omscs
- leetcode
- mpnp
- 주정부이민
- LAA
- cpp
- 코딩테스트
- 딥러닝
- zeros
- 기본
- 알고리즘
- 선형대수
- 방송통신대학교
- Plotting
- 마니토바
- 온라인석사
- 캐나다 영주권
- 조지아텍
- 컴퓨터과학과
- 매트랩
- Deep learning
- MATLAB
- EOI
- 머신러닝
- C++
Archives
- Today
- Total
Krononberg
bfs graph (C++) 본문
#include <iostream>
#include <list>
#include <queue>
using namespace std;
class Graph{
int V;
list<int>*adj;
public:
Graph(int V);
~Graph();
void add_line(int from, int to);
void bfs(int start);
};
Graph::Graph(int V){
this->V = V;
adj = new list<int>[V];
}
Graph::~Graph(){
delete[] adj;
}
void Graph::add_line(int from, int to){
adj[from].push_back(to);
}
void Graph::bfs(int start){
bool *visited = new bool[V]{false};
visited[start] = true;
queue<int> queue;
queue.push(start);
while(!queue.empty()){
int current = queue.front();
queue.pop();
cout << current << ' ';
for(auto i = adj[current].begin(); i != adj[current].end(); ++i){
if(!visited[*i]){
visited[*i] = true;
queue.push(*i);
}
}
}
delete[] visited;
}
int main(){
Graph g(5);
g.add_line(0, 1);
g.add_line(1, 2);
g.add_line(2, 3);
g.add_line(2, 4);
g.add_line(4, 1);
g.bfs(0);
return 0;
}
'개발 로그 > 자료구조' 카테고리의 다른 글
binary search (C++) (0) | 2024.02.09 |
---|---|
quick sort (C++) (0) | 2024.02.09 |