시간복잡도(노드 수: N, 에지 수 E)
- O(N+E)
1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|
T | F | F | F | F | F |
boolean[] arr = new boolean[7];
으로 배열을 만들어 준다.( 0번 인덱스는 사용하지 않는다. )(편의를 위해 poll() 괄호 안에 값을 넣도록 하겠다.)
1. 위에서 1을 꺼낸 뒤, 노드2,3을 넣은 상태이다.
2. poll(2)
-> 2의 방문배열 True -> 2의 인접 리스트 5,6을 넣는다. offer(5)
, offer(6)
3. poll(3)
-> 3의 방문배열 True -> 3의 인접 리스트 4를 넣는다. offer(4)
4. poll(5)
-> 5의 방문배열 True -> 5의 인접리스트는 없다.
5. poll(6)
-> 6의 방문배열 True -> 6의 인접리스트는 없다.
6. poll(4)
-> 4의 방문배열 True -> 4의 인접리스트는 없다.
7. queue.isEmpty() == true
static boolean[] visited = new boolean[7]; //방문 배열 초기화
static Queue<Integer> queue = new LinkedList<>();
static ArrayList<Integer>[] A = new ArrayList[7]; //ArrayList 타입 배열 선언
static ArrayList<Integer> procedure = new ArrayList<>(); //탐색 순서 출력을 위한 리스트
public static void main(String[] args){
//ArrayList 형 배열 A 초기화
for(int i=1;i<=6;i++) {
A[i] = new ArrayList<>();//배열의 각 요소마다 ArrayList를 가진다.
}
//위의 예제 인접 리스트 초기화
A[1].add(2);
A[1].add(3);
A[2].add(5);
A[2].add(6);
A[3].add(4);
A[4].add(6);
BFS(1);
System.out.println(procedure.toString()); //[1, 2, 3, 5, 6, 4]
}
private static void BFS(int start){
queue.offer(start);
while(!queue.isEmpty()){
int now = queue.poll();
// poll() 할때, 탐색순서 리스트에 넣어주고,방문배열을 true로 바꿔준다.
procedure.add(now);
visited[now] = true;
// 꺼낸 노드의 인접노드를 전부 확인한다.
for(int i=0;i<A[now].size();i++){
int node = A[now].get(i);
//인접노드가 방문한적 없는 노드면 queue에 넣어준다.
if(!visited[node]){
queue.offer(node);
}
}
}//while문
}//BFS 메소드
}
(출처) Do it! 알고리즘 코딩 테스트 자바 편 e-book
http://www.yes24.com/Product/Goods/108571508