ํ์์ด๋ ๋ง์ ์์ ๋ฐ์ดํฐ ์ค์์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ฐพ๋ ๊ณผ์ ์ ๋๋ค. ๊ทธ์ค์์ DFS์ BFS๊ฐ ๋ํ์ ์ธ ํ์ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. ์ด ๋๊ฐ์ ์๊ณ ๋ฆฌ์ฆ์ ์๋ ค๋ฉด ์คํ, ํ, ์ฌ๊ท ํจ์๋ฅผ ์์์ผ ํฉ๋๋ค.
LIFO ๊ตฌ์กฐ์ ๋๋ค.
import java.util.*;
public class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
// ์ฝ์
(5) - ์ฝ์
(2) - ์ฝ์
(3) - ์ฝ์
(7) - ์ญ์ () - ์ฝ์
(1) - ์ฝ์
(4) - ์ญ์ ()
s.push(5);
s.push(2);
s.push(3);
s.push(7);
s.pop();
s.push(1);
s.push(4);
s.pop();
// ์คํ์ ์ต์๋จ ์์๋ถํฐ ์ถ๋ ฅ
while (!s.empty()) {
System.out.println(s.peek());
s.pop();
}
}
}
FIFO ๊ตฌ์กฐ์ ๋๋ค.
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
// ์ฝ์
(5) - ์ฝ์
(2) - ์ฝ์
(3) - ์ฝ์
(7) - ์ญ์ () - ์ฝ์
(1) - ์ฝ์
(4) - ์ญ์ ()
q.offer(5);
q.offer(2);
q.offer(3);
q.offer(7);
q.poll();
q.offer(1);
q.offer(4);
q.poll();
// ๋จผ์ ๋ค์ด์จ ์์๋ถํฐ ์ถ์ถ
while (!q.isEmpty()) {
System.out.println(q.poll());
}
}
}
์๊ธฐ ์์ ์ ๋ค์ ํธ์ถํ๋ ํจ์์ ๋๋ค.
import java.util.*;
public class Main { // ๋ฌดํํ ๋์ค๋ ์ฌ๊ทํจ์
public static void recursiveFunction() {
System.out.println("์ฌ๊ท ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.");
recursiveFunction();
}
public static void main(String[] args) {
recursiveFunction();
}
}
์ฌ๊ท ํจ์๋ฅผ ๋ฌธ์ ํ์ด์์ ์ฌ์ฉํ ๋๋ ๋ฐ๋์ ์ข ๋ฃ์กฐ๊ฑด์ ๋ช ์ํด์ผํฉ๋๋ค.
import java.util.*;
public class Main {
public static void recursiveFunction(int i) {
// 100๋ฒ์งธ ํธ์ถ์ ํ์ ๋ ์ข
๋ฃ๋๋๋ก ์ข
๋ฃ ์กฐ๊ฑด ๋ช
์
if (i == 100) return;
System.out.println(i + "๋ฒ์งธ ์ฌ๊ท ํจ์์์ " + (i + 1) + "๋ฒ์งธ ์ฌ๊ทํจ์๋ฅผ ํธ์ถํฉ๋๋ค.");
recursiveFunction(i + 1);
System.out.println(i + "๋ฒ์งธ ์ฌ๊ท ํจ์๋ฅผ ์ข
๋ฃํฉ๋๋ค.");
}
public static void main(String[] args) {
recursiveFunction(1);
}
}
์ฌ๊ท ํจ์์ ์ํ์ ์คํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํฉ๋๋ค.
import java.util.*;
public class Main {
// ๋ฐ๋ณต์ ์ผ๋ก ๊ตฌํํ n!
public static int factorialIterative(int n) {
int result = 1;
// 1๋ถํฐ n๊น์ง์ ์๋ฅผ ์ฐจ๋ก๋๋ก ๊ณฑํ๊ธฐ
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// ์ฌ๊ท์ ์ผ๋ก ๊ตฌํํ n!
public static int factorialRecursive(int n) {
// n์ด 1 ์ดํ์ธ ๊ฒฝ์ฐ 1์ ๋ฐํ
if (n <= 1) return 1;
// n! = n * (n - 1)!๋ฅผ ๊ทธ๋๋ก ์ฝ๋๋ก ์์ฑํ๊ธฐ
return n * factorialRecursive(n - 1);
}
public static void main(String[] args) {
// ๊ฐ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌํํ n! ์ถ๋ ฅ(n = 5)
System.out.println("๋ฐ๋ณต์ ์ผ๋ก ๊ตฌํ:" + factorialIterative(5));
System.out.println("์ฌ๊ท์ ์ผ๋ก ๊ตฌํ:" + factorialRecursive(5));
}
}
๊น์ด ์ฐ์ ํ์์ด๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, ๊ทธ๋ํ์์ ๊น์ ๋ถ๋ถ์ ์ฐ์ ์ ์ผ๋ก ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
๊ทธ๋ํ๋ Node์ Edge๋ก ํํ๋๋ฉฐ ์ด๋ Node๋ฅผ Vertex๋ผ๊ณ ๋งํฉ๋๋ค.
2์ฐจ์ ๋ฐฐ์ด๋ก ๊ทธ๋ํ์ ์ฐ๊ฒฐ๊ด๊ณ๋ฅผ ํํํ๋ ๋ฐฉ์
import java.util.*;
public class Main {
public static final int INF = 999999999; // ๋ฌดํ ๋น์ฉ ์ ์ธ
// 2์ฐจ์ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด ์ธ์ ํ๋ ฌ ํํ
public static int[][] graph = {
{0, 7, 5},
{7, 0, INF},
{5, INF, 0}
};
public static void main(String[] args) {
// ๊ทธ๋ํ ์ถ๋ ฅ
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
๋ฆฌ์คํธ๋ก ๊ทธ๋ํ์ ์ฐ๊ฒฐ๊ด๊ณ๋ฅผ ํํํ๋ ๋ฐฉ์
import java.util.*;
class Node {
private int index;
private int distance;
public Node(int index, int distance) {
this.index = index;
this.distance = distance;
}
public void show() {
System.out.print("(" + this.index + "," + this.distance + ") ");
}
}
public class Main {
// ํ(Row)์ด 3๊ฐ์ธ ์ธ์ ๋ฆฌ์คํธ ํํ
public static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
public static void main(String[] args) {
// ๊ทธ๋ํ ์ด๊ธฐํ
for (int i = 0; i < 3; i++) {
graph.add(new ArrayList<Node>());
}
// ๋
ธ๋ 0์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ (๋
ธ๋, ๊ฑฐ๋ฆฌ)
graph.get(0).add(new Node(1, 7));
graph.get(0).add(new Node(2, 5));
// ๋
ธ๋ 1์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ (๋
ธ๋, ๊ฑฐ๋ฆฌ)
graph.get(1).add(new Node(0, 7));
// ๋
ธ๋ 2์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ (๋
ธ๋, ๊ฑฐ๋ฆฌ)
graph.get(2).add(new Node(0, 5));
// ๊ทธ๋ํ ์ถ๋ ฅ
for (int i = 0; i < 3; i++) {
for (int j = 0; j < graph.get(i).size(); j++) {
graph.get(i).get(j).show();
}
System.out.println();
}
}
}
DFS๋ ์ฒซ ์์ ๋ ธ๋๋ฅผ ์ ํด์ผํ๊ณ ๋ฐฉ๋ฌธ ๊ธฐ์ค์ ์ ํด์ผํฉ๋๋ค. ๋ณดํต์ ๋ฐฉ๋ฌธ๊ธฐ์ค์ ๋ฒํธ๊ฐ ๋ฎ์ ์ธ์ ๋ ธ๋๊ฐ ๋ฉ๋๋ค.
import java.util.*;
public class Main {
public static boolean[] visited = new boolean[9];
public static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
// DFS ํจ์ ์ ์
public static void dfs(int x) {
// ํ์ฌ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
visited[x] = true;
System.out.print(x + " ");
// ํ์ฌ ๋
ธ๋์ ์ฐ๊ฒฐ๋ ๋ค๋ฅธ ๋
ธ๋๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐฉ๋ฌธ
for (int i = 0; i < graph.get(x).size(); i++) {
int y = graph.get(x).get(i);
if (!visited[y]) dfs(y);
}
}
public static void main(String[] args) {
// ๊ทธ๋ํ ์ด๊ธฐํ
for (int i = 0; i < 9; i++) {
graph.add(new ArrayList<Integer>());
}
// ๋
ธ๋ 1์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(1).add(2);
graph.get(1).add(3);
graph.get(1).add(8);
// ๋
ธ๋ 2์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(2).add(1);
graph.get(2).add(7);
// ๋
ธ๋ 3์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(3).add(1);
graph.get(3).add(4);
graph.get(3).add(5);
// ๋
ธ๋ 4์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(4).add(3);
graph.get(4).add(5);
// ๋
ธ๋ 5์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(5).add(3);
graph.get(5).add(4);
// ๋
ธ๋ 6์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(6).add(7);
// ๋
ธ๋ 7์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(7).add(2);
graph.get(7).add(6);
graph.get(7).add(8);
// ๋
ธ๋ 8์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(8).add(1);
graph.get(8).add(7);
dfs(1);
}
}
๋๋น ์ฐ์ ํ์์ด๋ผ๋ ์๋ฏธ์ ๋๋ค. ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. DFS๋ ๊ฐ์ฅ ๋จผ ๋ ธ๋๋ถํฐ ํ์ํ๋ ๋ฐ๋ฉด BFS๋ ๊ทธ ๋ฐ๋์ ๋๋ค.
import java.util.*;
public class Main {
public static boolean[] visited = new boolean[9];
public static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
// BFS ํจ์ ์ ์
public static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
q.offer(start);
// ํ์ฌ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
visited[start] = true;
// ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
while(!q.isEmpty()) {
// ํ์์ ํ๋์ ์์๋ฅผ ๋ฝ์ ์ถ๋ ฅ
int x = q.poll();
System.out.print(x + " ");
// ํด๋น ์์์ ์ฐ๊ฒฐ๋, ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์์๋ค์ ํ์ ์ฝ์
for(int i = 0; i < graph.get(x).size(); i++) {
int y = graph.get(x).get(i);
if(!visited[y]) {
q.offer(y);
visited[y] = true;
}
}
}
}
public static void main(String[] args) {
// ๊ทธ๋ํ ์ด๊ธฐํ
for (int i = 0; i < 9; i++) {
graph.add(new ArrayList<Integer>());
}
// ๋
ธ๋ 1์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(1).add(2);
graph.get(1).add(3);
graph.get(1).add(8);
// ๋
ธ๋ 2์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(2).add(1);
graph.get(2).add(7);
// ๋
ธ๋ 3์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(3).add(1);
graph.get(3).add(4);
graph.get(3).add(5);
// ๋
ธ๋ 4์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(4).add(3);
graph.get(4).add(5);
// ๋
ธ๋ 5์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(5).add(3);
graph.get(5).add(4);
// ๋
ธ๋ 6์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(6).add(7);
// ๋
ธ๋ 7์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(7).add(2);
graph.get(7).add(6);
graph.get(7).add(8);
// ๋
ธ๋ 8์ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ ๋ณด ์ ์ฅ
graph.get(8).add(1);
graph.get(8).add(7);
bfs(1);
}
}
์ฐธ์กฐ
https://velog.io/@sbinha/%EC%8A%A4%ED%83%9D-%ED%81%90
.
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=kciter&logNo=100199946363
.
https://www.youtube.com/watch?v=RPSVXjcFbvA
.
https://velog.io/@cocoacolar/Algorithm-%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-%EA%B7%B8%EB%9E%98%ED%94%84Graph
.
https://velog.io/@jg31109/5.-%EC%9D%B4%EC%BD%94%ED%85%8C-BFSDFS-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98
.
https://mgyo.tistory.com/194
.
https://niklas-dev.tistory.com/19