-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
60 lines (43 loc) · 1.22 KB
/
Copy pathbfs.cpp
File metadata and controls
60 lines (43 loc) · 1.22 KB
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
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
bool bfs(map<string, vector<string>> graph, string name, string seller) {
queue<string> search_queue {};
for (string s: graph[name]) {
search_queue.push(s);
}
set<string> searched {};
while(!search_queue.empty()) {
string person = search_queue.front();
search_queue.pop();
if (!searched.count(person)) {
if (person == seller) {
cout << person + " is a mango seller" << endl;
return true;
} else {
for(string s: graph[person]) {
search_queue.push(s);
}
searched.insert(person);
}
}
}
return false;
}
int main() {
// Given graph
map<string, vector<string>> graph {};
graph["you"] = {"alice", "bob", "claire"};
graph["bob"] = {"anuj", "peggy"};
graph["alice"] = {"peggy"};
graph["claire"] = {"thom", "jonny"};
graph["anuj"] = {};
graph["peggy"] = {};
graph["thom"] = {};
graph["jonny"] = {};
cout << "Result of bfs " << bfs(graph, "you", "anuj") << endl;
return 0;
}