-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from fernandokkang/develop
feat: add dfs page
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<!DOCTYPE html> | ||
<html lang="ko" xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Doumi</title> | ||
<link href="../../static/css/codingtest/timeComplexity.css" | ||
th:href="@{/css/codingtest/timeComplexity.css}" | ||
rel="stylesheet" type="text/css"/> | ||
<link rel="stylesheet" | ||
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css"> | ||
</head> | ||
<body> | ||
<header></header> | ||
<main class="cont-main"> | ||
<section class="content-header"> | ||
<button class="back-button"><a href="/doumiAlgorithm">< 목록으로</a></button> | ||
<h1 class="title">DFS와 BFS(DFS & BFS)</h1> | ||
<div class="content-info"> | ||
<a href="" class="board-writer-name">도우미</a> | ||
<p class="content-createdAt">24.03.20 21:30 작성</p> | ||
</div> | ||
</section> | ||
|
||
<section class="content-main"> | ||
<h2>DFS (Dept-First Search)</h2> | ||
<br> | ||
<p>깊이 우선 탐색이라고 부르며, 그래프에서 깊은 부분을 우선적으로 탐색하는 알고리즘이다.</p> | ||
<p>이것은 스택이나 재귀 호출을 이용하여 구현할 수 있다.</p> | ||
<p> | ||
<img src="/images/algorithm/dfs.gif" alt="dfs.gif"></p> | ||
<p>재귀함수를 사용하여 구현할 때는 종료 조건을 명확히 해줘야 한다. 그렇지 않으면 무한 루프에 빠지게 된다.</p> | ||
<h3>재귀를 활용한 DFS</h3> | ||
<pre> | ||
<code class="lang-java"> | ||
class Solution { | ||
|
||
public int solution(int[] numbers, int target) { | ||
|
||
return dfs(numbers, 0, 0, target); | ||
} | ||
|
||
public int dfs(int [] numbers, int idx, int sum, int target) { | ||
|
||
if(idx == numbers.length) { | ||
if(sum == target) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
return dfs(numbers, idx + 1, sum + numbers[idx], target) | ||
+ dfs(numbers, idx + 1, sum - numbers[idx], target); | ||
} | ||
} | ||
</code> | ||
</pre> | ||
<h3>DFS를 활용하여 풀 수 있는 문제</h3> | ||
<ul> | ||
<li>모든 경우의 수를 구해야 하는 문제</li> | ||
<li>검색 대상의 규모가 큰 문제</li> | ||
<li>경로의 특징을 저장해야 하는 문제</li> | ||
</ul> | ||
<br><br> | ||
<h2>BFS (Breadth-Firsh Search)</h2> | ||
<br> | ||
<p>너비 우선 탐색이라고 부르며, 그래프에서 인접한 노드를 모두 탐색하고 다음 노드를 탐색하는 알고리즘이다. 이것은 | ||
큐와 반복문을 이용하여 구현할 수 있다.</p> | ||
<p> | ||
<img src="/images/algorithm/bfs.gif" alt="bfs.gif"></p> | ||
<p>큐를 활용한 BFS</p> | ||
<pre> | ||
<code class="lang-java"> | ||
|
||
public static void bfs(int start) { | ||
|
||
q = new LinkedList<>(); | ||
q.add(start); | ||
|
||
visit[start] = true; | ||
|
||
System.out.print(start+" "); | ||
|
||
while(!q.isEmpty()) { | ||
|
||
int temp = q.poll(); | ||
|
||
for(int i=1; i<=n; i++) { | ||
if(adjust[temp][i] == 1 && !visit[i]) | ||
|
||
q.add(i); | ||
visit[i] = true; | ||
System.out.print(i+" "); | ||
} | ||
|
||
</code> | ||
</pre> | ||
|
||
<h3> 활용하여 풀 수 있는 문제</h3> | ||
최단 경로 찾는 문제 | ||
|
||
<h3>연습 문제</h3> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/1844#">프로그래머스 - 게임 맵 찾기</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/49994#">프로그래머스 - 방문 길이</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/84512">프로그래머스 - 모음 사전</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/87946#">프로그래머스 - 피로도</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/43165#">프로그래머스 - 타겟 넘버</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/72411">프로그래머스 - 메뉴 리뉴얼</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/42839">프로그래머스 - 소수 찾기</a></p> | ||
<p><a href="https://programmers.co.kr/learn/courses/30/lessons/12977">프로그래머스 - 소수 만들기</a></p> | ||
|
||
</section> | ||
</main> | ||
<script src="../../static/Js/header.js" th:src="@{/Js/header.js}"></script> | ||
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script> | ||
<script>hljs.highlightAll();</script> | ||
</body> | ||
</html> |
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