Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dfs page #47

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public String stack() {
public String setsAndMaps() {
return "algorithm/setsAndMaps";
}

@GetMapping("/codingtest/dfsandbfs")
public String dfsAndBfs() {

return "algorithm/dfsAndBfs";
}
}
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.
117 changes: 117 additions & 0 deletions src/main/resources/templates/algorithm/dfsAndBfs.html
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&lt;&gt;();
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>
8 changes: 8 additions & 0 deletions src/main/resources/templates/algorithm/doumiAlgorithm.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
<div class="author">도우미</div>
<div class="create_time">2024-03-16</div>
</li>
<li class="cote_container_header">
<div class="sequence">4</div>
<div class="title">
<a href="/codingtest/dfsandbfs">DFS와 BFS</a>
</div>
<div class="author">도우미</div>
<div class="create_time">2024-03-20</div>
</li>
</div>
</main>

Expand Down