幅優先検索 (BFS) アルゴリズム
幅優先検索 (BFS) の時間計算量: O ( V + E ) O (V+E)O ( V+E )の場合、アルゴリズムには補助キュー構造が含まれています。
コード上で直接
パイソン
from collections import deque
def person_is_seller(name):
return name[-1] == 'm'
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"] = []
def search(name):
search_queue = deque()
search_queue += [name]
# This is how you keep track of which people you've searched before.
searched = set()
while search_queue:
person = search_queue.popleft()
# Only search this person if you haven't already searched them.
if person in searched:
continue
if person_is_seller(person):
print(person + " is a mango seller!")
return True
search_queue += graph[person]
# Marks this person as searched
searched.add(person)
return False
search("you")
ジャバ:
import java.util.*;
public class BreadthFirstSearch {
private static Map<String, List<String>> graph = new HashMap<>();
private static boolean search(String name) {
Queue<String> searchQueue = new ArrayDeque<>(graph.get(name));
// This list is how you keep track of which people you've searched before.
List<String> searched = new ArrayList<>();
while (!searchQueue.isEmpty()) {
String person = searchQueue.poll();
// Only search this person if you haven't already searched them
if (!searched.contains(person)) {
if (person_is_seller(person)) {
System.out.println(person + " is a mango seller!");
return true;
} else {
searchQueue.addAll(graph.get(person));
// Marks this person as searched
searched.add(person);
}
}
}
return false;
}
private static boolean person_is_seller(String name) {
return name.endsWith("m");
}
public static void main(String[] args) {
graph.put("you", Arrays.asList("alice", "bob", "claire"));
graph.put("bob", Arrays.asList("anuj", "peggy"));
graph.put("alice", Arrays.asList("peggy"));
graph.put("claire", Arrays.asList("thom", "jonny"));
graph.put("anuj", Collections.emptyList());
graph.put("peggy", Collections.emptyList());
graph.put("thom", Collections.emptyList());
graph.put("jonny", Collections.emptyList());
search("you");
}
}
C++
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <queue>
#include <unordered_set>
using std::cout;
using std::endl;
bool is_seller(const std::string& name) {
return name.back() == 'm';
}
template <typename T>
bool search(const T& name, const std::unordered_map<T, std::vector<T>>& graph) {
std::queue<T> search_queue;
std::unordered_set<T> searched;
// add all friends to search queue
for (auto&& friend_name : graph.find(name) -> second)
search_queue.push(friend_name);
while (!search_queue.empty()) {
T& person = search_queue.front();
search_queue.pop();
// only search this person if you haven't already searched them.
if (searched.find(person) == searched.end()) {
if (is_seller(person)) {
cout << person << " is a mango seller!" << endl;
return true;
}
// add all friends of a person to search queue
for (auto&& friend_name : graph.find(person) -> second)
search_queue.push(friend_name);
// mark this person as searched
searched.insert(person);
}
}
return false;
}
int main() {
std::unordered_map<std::string, std::vector<std::string>> graph;
graph.insert({
"you", {
"alice", "bob", "claire"}});
graph.insert({
"bob", {
"anuj", "peggy"}});
graph.insert({
"alice", {
"peggy"}});
graph.insert({
"claire", {
"thom", "jonny"}});
graph.insert({
"anuj", {
}});
graph.insert({
"peggy", {
}});
graph.insert({
"thom", {
}});
graph.insert({
"jonny", {
}});
std::string name = "you";
bool result = search(name, graph);
cout << "Found mango seller: " << result << endl;
}