티스토리 뷰
트리구조에 대한 깊이 우선 탐색과 너비 우선 탐색
BFS는 큐를 이용하여 접속할 노드의 정보를 얻고 먼저 접속한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sdk.example; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
/** | |
* 1 > 2 | |
* 1 > 3 | |
* 2 > 4 | |
* 3 > 4 | |
* 의 간선을 가지는 그래프를 BFS를 이용하여 처리할 | |
* | |
* @author whitebeard | |
* | |
*/ | |
public class BredthFirstSearch { | |
public static void main(String[] args) { | |
int[][] array = { { 0, 1, 1, 0 }, | |
{ 1, 0, 0, 1 }, | |
{ 1, 0, 0, 1 }, | |
{ 0, 1, 1, 0 } }; | |
// 방문여부 확인 | |
int[] visited = new int[4]; | |
visited[0] = 1; | |
// 우선 접근을 위한 큐 | |
Queue<Integer> queue = new LinkedList<>(); | |
queue.add(0); | |
bfs(array, visited, queue, String.valueOf(0)); | |
} | |
public static void bfs(int[][] array, int[] visited, Queue<Integer> queue, String path) { | |
int size = queue.size(); | |
// 큐에 데이터가 존재하는 만큼 확인 | |
while(size-- > 0) { | |
// 큐의 첫번째 데이터로 접근 가능한 노드를 확인하여 큐에 입력 | |
int location = queue.poll(); | |
for(int i = 0; i < array.length; i++) { | |
if(array[location][i] == 1 && visited[i] == 0) { | |
queue.add(i); | |
visited[i] = 1; | |
path += String.valueOf(i); | |
} | |
} | |
} | |
if(queue.size() != 0) | |
bfs(array, visited, queue, path); | |
else | |
System.out.println(path); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sdk.example; | |
/** | |
* 1 > 2 | |
* 1 > 3 | |
* 2 > 4 | |
* 3 > 4 | |
* 의 간선을 가지는 그래프를 DFS를 이용하여 처리할 | |
* | |
* @author whitebeard | |
* | |
*/ | |
public class DepthFirstSearch { | |
public static void main(String[] args) { | |
int[][] array = { { 0, 1, 1, 0 }, | |
{ 1, 0, 0, 1 }, | |
{ 1, 0, 0, 1 }, | |
{ 0, 1, 1, 0 } }; | |
// 방문 여부 확인 | |
int[] visited = new int[4]; | |
visited[0] = 1; | |
dfs(array, visited, 0, String.valueOf(0)); | |
} | |
public static void dfs(int[][] array, int[] visited, int location, String path) { | |
System.out.println(path); | |
for(int i = 0; i < array.length; i++) { | |
// 방문 가능하면 우선적으로 접근 | |
if(array[location][i] == 1 && visited[i] == 0) { | |
visited[i] = 1; | |
dfs(array, visited, i, path + String.valueOf(i)); | |
} | |
} | |
} | |
} |
반응형
'알고리즘' 카테고리의 다른 글
[알고리즘] 알고리즘 강좌 자료와 온라인 문제 사이트 (0) | 2017.01.17 |
---|---|
[스크랩] 알고리즘 문제를 풀이하느데 도움이 되는 페이지 (0) | 2016.08.24 |
[알고리즘] 백트래킹 (0) | 2016.07.15 |
[스크랩] 나는 어떻게 알고리즘을 공부했을까? (1) | 2016.06.30 |
[스크랩] 최대 연속 부분 구간 합 문제를 푸는 알고리즘 (0) | 2016.06.27 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 오류
- 백준
- Linux
- error
- SPARK
- 파이썬
- ubuntu
- airflow
- 정올
- emr
- Python
- S3
- bash
- java
- 하이브
- oozie
- mysql
- 다이나믹
- Hadoop
- hbase
- nodejs
- AWS
- 하둡
- HIVE
- SQL
- build
- HDFS
- yarn
- Tez
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함