-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
38 lines (38 loc) · 1.08 KB
/
bfs.cpp
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
#include<iostream>
#include<vector>
#include<queue>
void bfs(std::vector<std::vector<int> >&adjacencyList, std::vector<bool> &visited){
int currentVertex = 1;
int minLength = 0;
std::queue<int> q;
q.push(currentVertex);
while(!q.empty()){
currentVertex = q.front();
q.pop();
visited[currentVertex] = true;
for(auto adjacentVertex: adjacencyList[currentVertex]){
if(adjacentVertex==6){
std::cout<<minLength;
return;
}
q.push(adjacentVertex);
}
++minLength;
}
}
int main(){
std::vector<std::vector<int> >adjacencyList(7);
adjacencyList[1].push_back(2);
adjacencyList[2].push_back(3);
adjacencyList[2].push_back(1);
adjacencyList[2].push_back(5);
adjacencyList[3].push_back(1);
adjacencyList[3].push_back(4);
adjacencyList[3].push_back(6);
adjacencyList[4].push_back(3);
adjacencyList[5].push_back(2);
adjacencyList[6].push_back(3);
std::vector<bool> visited(7, false);
bfs(adjacencyList, visited);
return 0;
}