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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | //그래프의 인접 리스트 표현 vector<vector <int> > adj; //start에서 시작해 그래프를 너비 우선 탐색하고 각 정점의 방문 순서를 반환한다. vector<int> bfs(int start) { //각 정점의 방문 여부 vector<bool> discovered(adj.size(), false); //방문할 정점 목록을 유지하는 큐 queue<int> q; //정점의 방문 순서 vector<int> order; discovered[start] = true; q.push(start); while (!q.empty()) { int here = q.front(); q.pop(); //here를 방문한다. order.push_back(here); //모든 인접한 정점을 검사한다. for (int i = 0; i < adj[here].size(); i++) { int there = adj[here][i]; //처음 보는 정점이면 방문목록에 집어넣는다. if (!discovered[there]) { q.push(there); discovered[there] = true; } } } return order; } //최단 경로를 계산하는 너비 우선 탐색 //start에서 시작해 그래프를 너비 우선 탐색하고 시작점부터 각 정점까지의 //최단 거리와 너비 우선 탐색 스패닝 트리를 계산한다. //distance[i]=start부터 i까지의 최단 거리 //parent[i]=너비 우선 탐색 스패닝 트리에서 i의 부모의 번호.루트인 경우 자신의 번호 void bfs2(int start, vector<int>& distance, vector<int>& parent) { distance = vector<int>(adj.size(), -1); parent = vector<int>(adj.size(), -1); //방문할 정점 목록을 유지하는 큐 queue<int> q; distance[start] = 0; parent[start] = start; q.push(start); while (!q.empty()) { int here = q.front(); q.pop(); //here의 모든 인접한 정점을 검사한다. for (int i = here; i < adj[here].size(); i++) { int there = adj[here][i]; //처음 보는 정점이면 방문 목록에 집어넣는다. if (distance[there] == -1) { q.push(there); distance[there] = distance[here] + 1; parent[there] = here; } } } } //v로부터 시작점까지의 최단 경로를 계산한다. //시작점에서 v까지의 경로도 구할 수 있음. path에 경로 저장 vector<int> shortestPath(int v, const vector<int>& parent) { vector<int> path(1, v); while (parent[v] != v) { v = parent[v]; path.push_back(v); } reverse(path.begin(), path.end()); return path; } | cs |
'알고리즘 > 구현' 카테고리의 다른 글
간단한 기본 다익스트라 구현 (0) | 2019.05.12 |
---|---|
플로이드 최단경로 알고리즘 (0) | 2019.03.25 |
부분 합 (0) | 2019.02.06 |