그래프 혹은 트리를 너비 우선으로 탐색하는 알고리즘이다.
쉽게 말해 가로로 탐색한다고 생각
package algorithms.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class BFS {
public ArrayList<String> search(HashMap<String, ArrayList<String>> graph, String startNode) {
ArrayList<String> visited = new ArrayList<>();
ArrayList<String> needVisited = new ArrayList<>();
needVisited.add(startNode);
while (needVisited.size() > 0) {
String node = needVisited.remove(0);
if (!visited.contains(node)) {
visited.add(node);
needVisited.addAll(graph.get(node));
}
}
return visited;
}
public static void main(String[] args) {
HashMap<String, ArrayList<String>> graph = new HashMap<>();
graph.put("A", new ArrayList<String>(Arrays.asList("B", "C")));
graph.put("B", new ArrayList<String>(Arrays.asList("A", "D")));
graph.put("C", new ArrayList<String>(Arrays.asList("A", "G", "H", "I")));
graph.put("D", new ArrayList<String>(Arrays.asList("B", "E", "F")));
graph.put("E", new ArrayList<String>(Arrays.asList("D")));
graph.put("F", new ArrayList<String>(Arrays.asList("D")));
graph.put("G", new ArrayList<String>(Arrays.asList("C")));
graph.put("H", new ArrayList<String>(Arrays.asList("C")));
graph.put("I", new ArrayList<String>(Arrays.asList("C", "J")));
graph.put("J", new ArrayList<String>(Arrays.asList("I")));
BFS bfs = new BFS();
System.out.println(bfs.search(graph, "A"));
}
}