Update 01_breadth-first_search.cpp (#124)
Improved code stylistic integrity.
This commit is contained in:
committed by
Aditya Bhargava
parent
ee7dcd3b3d
commit
4bc8132647
@@ -18,9 +18,8 @@ bool search(const T& name, const std::unordered_map<T, std::vector<T>>& graph) {
|
|||||||
std::unordered_set<T> searched;
|
std::unordered_set<T> searched;
|
||||||
|
|
||||||
// add all friends to search queue
|
// add all friends to search queue
|
||||||
for (auto friend_name : graph.find(name) -> second) {
|
for (auto&& friend_name : graph.find(name) -> second)
|
||||||
search_queue.push(friend_name);
|
search_queue.push(friend_name);
|
||||||
}
|
|
||||||
|
|
||||||
while (!search_queue.empty()) {
|
while (!search_queue.empty()) {
|
||||||
T& person = search_queue.front();
|
T& person = search_queue.front();
|
||||||
@@ -32,12 +31,10 @@ bool search(const T& name, const std::unordered_map<T, std::vector<T>>& graph) {
|
|||||||
cout << person << " is a mango seller!" << endl;
|
cout << person << " is a mango seller!" << endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
std::vector<T> friend_list = graph.find(person) -> second;
|
|
||||||
|
|
||||||
// add all friends of a person to search queue
|
// add all friends of a person to search queue
|
||||||
for (T friend_name : friend_list) {
|
for (auto&& friend_name : graph.find(person) -> second)
|
||||||
search_queue.push(friend_name);
|
search_queue.push(friend_name);
|
||||||
}
|
|
||||||
|
|
||||||
// mark this person as searched
|
// mark this person as searched
|
||||||
searched.insert(person);
|
searched.insert(person);
|
||||||
@@ -61,4 +58,4 @@ int main() {
|
|||||||
std::string name = "you";
|
std::string name = "you";
|
||||||
bool result = search(name, graph);
|
bool result = search(name, graph);
|
||||||
cout << "Found mango seller: " << result << endl;
|
cout << "Found mango seller: " << result << endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user