[Java] ์ฝ๋ฉ ํ ์คํธ์์ ์ ์ฐ์ด๋ java ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ
์์ฃผ ์ฐ์ด๋ ์๋ฃ๊ตฌ์กฐ
๋ฐฐ์ด
import java.util.*;
public class ListExample {
public static void main(String[] args) {
// intํ ๋ฐฐ์ด
int[] intArray = new int[100]; // ํฌ๊ธฐ๊ฐ 100์ธ intํ ๋ฐฐ์ด ์์ฑ (์ด๊ธฐ๊ฐ 0)
int[] intArrayInit = {1, 2, 3, 4, 5}; // ์์ฑ๊ณผ ํจ๊ป ๊ฐ์ ํ ๋นํ ๋ฐฐ์ด
// ์์ ์ ๊ทผ (์ธ๋ฑ์ค ์ฌ์ฉ)
System.out.println(intArray[0)); // ์ถ๋ ฅ: 0
System.out.println(intArrayInit[0)); // ์ถ๋ ฅ: 1
// ๋ฌธ์์ด ๋ฐฐ์ด
String[] stringArray = new String[100]; // ํฌ๊ธฐ๊ฐ 100์ธ ๋ฌธ์์ด ๋ฐฐ์ด
// ๋
ผ๋ฆฌํ ๋ฐฐ์ด
boolean[] boolArray = new boolean[100]; // ํฌ๊ธฐ๊ฐ 100์ธ ๋
ผ๋ฆฌํ ๋ฐฐ์ด(์ด๊ธฐ ๊ฐ false)
// ArrayList ์์ฑ
List<Integer> list = new ArrayList<>();
// ์์ ์ถ๊ฐ
list.add(10);
list.add(20);
list.add(30);
// ์์ ์ ๊ทผ (์ธ๋ฑ์ค ์ฌ์ฉ)
System.out.println(list.get(0)); // ์ถ๋ ฅ: 10
// ํฌ๊ธฐ ํ์ธ
System.out.println(list.size()); // ์ถ๋ ฅ: 3
// ์์ ์ญ์
list.remove(1); // ์ธ๋ฑ์ค 1์ ์์ (20) ์ญ์
System.out.println(list); // ์ถ๋ ฅ: [10, 30]
}
}
๋ฐฐ์ด์ ๊ฒฝ์ฐ, ์ ์ํ(int or long ํ์ ), ๋ฌธ์ํ(String ํ์ ), ๋ ผ๋ฆฌํ(boolean ํ์ )์ ๋ฐฐ์ด์ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
๊ฐ์ ๊ณ์ ์ฝ์ , ์ ๊ฑฐํ๋ ๊ฒฝ์ฐ ArrayList๋ฅผ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
ํ์ง๋ง ์ต๋ ํฌ๊ธฐ๊ฐ ์ ํด์ง ๋ฐฐ์ด ์์์ ์กฐ์์ ํ๋ค๋ฉด, ์ฑ๋ฅ์ ์ผ๋ก ArrayList๋ณด๋ค int, String, boolean ํ์ ์ ๋ฐฐ์ด์ ์์ฑํด์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
๋ฐฐ์ด - ArrayList ๋ฐฐ์ด
import java.util.ArrayList;
import java.util.List;
public class ArrayListArrayExample {
public static void main(String[] args) {
// 1. ArrayList<Integer>๋ฅผ ๋ด์ ์ ์๋ ํฌ๊ธฐ 5์ง๋ฆฌ ๋ฐฐ์ด ์ ์ธ ๋ฐ ์์ฑ
ArrayList<Integer>[] arrayOfLists = new ArrayList[5];
// ๋๋ List<Integer>[] arrayOfLists = new List[5]; (List ์ธํฐํ์ด์ค๋ก ์ ์ธํ๋๊ฒ ๋ ์ ์ฐ)
// 2. ๋ฐฐ์ด์ ๊ฐ ์นธ์ ์ค์ ArrayList ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฃ์ด์ฃผ๊ธฐ
for (int i = 0; i < arrayOfLists.length; i++) {
arrayOfLists[i] = new ArrayList<>();
}
// 3. ๊ฐ ArrayList์ ๋ฐ์ดํฐ ์ถ๊ฐํ๊ธฐ
arrayOfLists[0].add(1);
arrayOfLists[0].add(2);
arrayOfLists[1].add(11);
arrayOfLists[2].add(100);
arrayOfLists[2].add(200);
arrayOfLists[2].add(300);
// 4. ๋ฐ์ดํฐ ํ์ธ
for (int i = 0; i < arrayOfLists.length; i++) {
System.out.println("arrayOfLists[" + i + "]์ ๋ด์ฉ: " + arrayOfLists[i]);
}
// 5. ํน์ ์์ ์ ๊ทผ
System.out.println("\narrayOfLists[2]์ ์ฒซ ๋ฒ์งธ ์์: " + arrayOfLists[2].get(0));
}
}
๋ฆฌ์คํธ ๋ฐฐ์ด
import java.util.ArrayList;
import java.util.List;
public class NestedArrayListExample {
public static void main(String[] args) {
// ArrayList<ArrayList<Integer>> ์ ์ธ ๋ฐ ์์ฑ
ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<>();
// ๋๋ List<List<Integer>> listOfLists = new ArrayList<>();
// ์์ชฝ ArrayList (ํ) ์ถ๊ฐ ๋ฐ ๋ฐ์ดํฐ ์ฝ์
ArrayList<Integer> row1 = new ArrayList<>();
row1.add(1);
row1.add(2);
listOfLists.add(row1); // ์ฒซ ๋ฒ์งธ ํ ์ถ๊ฐ
ArrayList<Integer> row2 = new ArrayList<>();
row2.add(10);
row2.add(20);
row2.add(30);
listOfLists.add(row2); // ๋ ๋ฒ์งธ ํ ์ถ๊ฐ
// ์ธ ๋ฒ์งธ ํ์ ๋ง๋ค๋ฉด์ ๋ฐ๋ก ๋ฐ์ดํฐ ์ถ๊ฐ
listOfLists.add(new ArrayList<>()); // ์ธ ๋ฒ์งธ ํ ์ถ๊ฐ
listOfLists.get(2).add(100);
listOfLists.get(2).add(200);
// ๋ด์ฉ ์ถ๋ ฅ
System.out.println("์ ์ฒด ๋ฆฌ์คํธ ๋ด์ฉ:");
for (int i = 0; i < listOfLists.size(); i++) {
System.out.println(" ํ " + i + ": " + listOfLists.get(i));
}
// ํน์ ์์ ์ ๊ทผ
System.out.println("\n๋ ๋ฒ์งธ ํ (์ธ๋ฑ์ค 1)์ ์ธ ๋ฒ์งธ ์์ (์ธ๋ฑ์ค 2): " + listOfLists.get(1).get(2)); // ์ถ๋ ฅ: 30
// ์๋ก์ด ํ ์ถ๊ฐ
listOfLists.add(new ArrayList<>()); // ๋ค ๋ฒ์งธ ํ ์ถ๊ฐ
listOfLists.get(3).add(999);
System.out.println("\n๋ค ๋ฒ์งธ ํ ์ถ๊ฐ ํ ์ ์ฒด ๋ฆฌ์คํธ ๋ด์ฉ:");
for (int i = 0; i < listOfLists.size(); i++) {
System.out.println(" ํ " + i + ": " + listOfLists.get(i));
}
}
}
๋ฆฌ์คํธ ํ์ ์ ๊ฐ์ง๋ ๋ฆฌ์คํธ
Set
Set ์๋ฃ๊ตฌ์กฐ๋ ์ค๋ณต ๊ฐ์ ์ ๊ฑฐํด์ ๊ฐ๋ค์ ์ ์ฅํ ๋ ์ ์ฉ
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// HashSet ์์ฑ
Set<String> set = new HashSet<>();
// ์์ ์ถ๊ฐ (์ค๋ณต์ ํ๋๋ง ์ ์ฅ๋จ)
set.add("Java");
set.add("Python");
set.add("Java"); // ์ค๋ณต
set.add("C++");
System.out.println(set); // ์ถ๋ ฅ: [Java, C++, Python] (์์ ๋ณด์ฅ ์๋จ)
// ์์ ์กด์ฌ ํ์ธ
System.out.println(set.contains("Python")); // ์ถ๋ ฅ: true
// ์์ ์ญ์
set.remove("C++");
System.out.println(set); // ์ถ๋ ฅ: [Java, Python] (์์ ๋ณด์ฅ ์๋จ)
}
}
Stack
import java.util.*;
class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
// stack์ด ๋น์ด์์ง ์๋ค๋ฉด, ๊ณ์ํด์ pop
while (!s.empty()) {
System.out.println("stack pop " + s.pop());
}
}
}
Stack์ First in Last out์ผ๋ก ๋ง์ง๋ง์ ๋ค์ด๊ฐ ๊ฐ(push)์ด ์ฒ์์ผ๋ก ์ถ๋ ฅ๋๋ค.(pop)
stack์ ๊ฒฝ์ฐ, ๊ฐ์ด ๋น์ด์์ ๋, pop์ ์๋ํ๋ฉด EmptyStackException์ด ๋ฐ์ํ๋ค.
๋๋ฌธ์ ์ฌ์ฉ ์, try-cath๋ก ๋ฌถ์ด์ ์ฌ์ฉํ๊ฑฐ๋ pop ์ด์ ์ empty() ํจ์๋ก ์ฌ์ ์ฒดํฌํด์ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํด์ค์ผ ํ๋ค.
Queue
import java.util.*;
class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.offer(1); // 1์ถ๊ฐ
q.offer(2); // 2์ถ๊ฐ
q.offer(3); // 3์ถ๊ฐ
// ํ๊ฐ ๋น์ด์์ง ์๋ค๋ฉด ๊ณ์ ์ถ๋ ฅ
while(q.peek() != null) {
System.out.println("๋ค์ ๊ฐ์ " + q.peek());
q.poll(); // ๊ฐ์ฅ ์์ ๊ฐ์ ๋ฆฌํด
}
System.out.println("๋ง์ง๋ง " + q.poll()); // ์ถ๋ ฅ: ๋ง์ง๋ง์ null
}
}
Queue ์๋ฃ๊ตฌ์กฐ๋ First in First out์ผ๋ก ์ฒ์ ๋ค์ด์จ ๊ฐ์ ์ฒ์ ์ถ๋ ฅํ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
Queue ์๋ฃ๊ตฌ์กฐ๋ ์ธํฐํ์ด์ค๋ก ์กด์ฌํ๋ฉฐ, ์ธ์คํด์ค ์์ฑ์ LinkedList๋ก ์์ฑํ๋ฉด ๋๋ค.
offer(value): value ๊ฐ์ ์ถ๊ฐํ๋ค.
peek(): ํ์ ๊ฐ์ฅ ์์ ๊ฐ์ ํ์ธํ๋ค(์ถ๋ ฅ X). ๋น์ด ์๋ค๋ฉด null์ ์ถ๋ ฅํ๋ค. ๋น์ทํ๊ฒ isEmpty() ํจ์๋ก ํ์ธ ๊ฐ๋ฅํ๋ค.
poll(): ํ์ ๊ฐ์ฅ ์์ ๊ฐ์ ์ถ๋ ฅํ๋ค(๊ฐ์ฅ ๋จผ์ ๋ค์ด์จ ๊ฐ). ๊ฐ์ด ๋น์ด ์๋ค๋ฉด, null์ ์ถ๋ ฅํ๋ค.
Deque
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
// Deque ์์ฑ (Queue์ฒ๋ผ ์ฌ์ฉ)
Deque<Integer> queue = new ArrayDeque<>();
queue.offerLast(1); // ๋ค์ ์ถ๊ฐ (enqueue)
queue.offerLast(2);
queue.offerLast(3);
System.out.println("Queue poll: " + queue.pollFirst()); // ์์์ ์ ๊ฑฐ (dequeue), ์ถ๋ ฅ: 1
System.out.println("Queue peek: " + queue.peekFirst()); // ์์์ ํ์ธ, ์ถ๋ ฅ: 2
// Deque ์์ฑ (Stack์ฒ๋ผ ์ฌ์ฉ)
Deque<String> stack = new ArrayDeque<>();
stack.offerFirst("A"); // ์์ ์ถ๊ฐ (push)
stack.offerFirst("B");
stack.offerFirst("C");
System.out.println("Stack poll: " + stack.pollFirst()); // ์์์ ์ ๊ฑฐ (pop), ์ถ๋ ฅ: C
System.out.println("Stack peek: " + stack.peekFirst()); // ์์์ ํ์ธ, ์ถ๋ ฅ: B
}
}
๋ฑ์ ํ์ ์คํ์ ๋์์ ํจ๊ป ์์ฉํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
๋ํ, ์ฑ๋ฅ์ ์ธ ์ธก๋ฉด์์๋ Stack๊ณผ Queue ๋ณด๋ค ์ ๋ฆฌํ ๋ฉด์ด ์์ด์ ์ค์ ํ ์คํธ์์ Stack๊ณผ Queue๋ฅผ ์ฌ์ฉํด์ผํ ๋, ๋ชจ๋ Deque๋ฅผ ์ฌ์ฉํด๋ ์ข๋ค.
๋ค๋ง, Heap ๊ตฌ์กฐ๋ฅผ ์ํ ์ฐ์ ์์ ํ(PriorityQueue)๋ก ์ฌ์ฉ์ ๋ถ๊ฐ๋ฅํ๋ค.
Heap
import java.util.PriorityQueue;
import java.util.Collections; // ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ํ์
public class Main {
public static void main(String[] args) {
// ๊ธฐ๋ณธ PriorityQueue (์ต์ ํ - ์ค๋ฆ์ฐจ์)
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
minHeap.offer(30);
minHeap.offer(10);
minHeap.offer(20);
System.out.println("Min Heap: " + minHeap); // ๋ด๋ถ ์ ์ฅ ์์๋ ๋ค๋ฅผ ์ ์์ง๋ง, poll/peek์ ์ฐ์ ์์๋๋ก!
System.out.println("Poll from Min Heap: " + minHeap.poll()); // ์ถ๋ ฅ: 10 (๊ฐ์ฅ ์์ ๊ฐ)
System.out.println("Poll from Min Heap: " + minHeap.poll()); // ์ถ๋ ฅ: 20
System.out.println("Poll from Min Heap: " + minHeap.poll()); // ์ถ๋ ฅ: 30
System.out.println("--------------------");
// PriorityQueue (์ต๋ ํ - ๋ด๋ฆผ์ฐจ์)
// Collections.reverseOrder() ์ฌ์ฉํด์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ ๊ธฐ์ค ๋ณ๊ฒฝ
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
maxHeap.offer(30);
maxHeap.offer(10);
maxHeap.offer(20);
System.out.println("Max Heap: " + maxHeap); // ๋ด๋ถ ์ ์ฅ ์์๋ ๋ค๋ฅผ ์ ์์ง๋ง, poll/peek์ ์ฐ์ ์์๋๋ก!
System.out.println("Poll from Max Heap: " + maxHeap.poll()); // ์ถ๋ ฅ: 30 (๊ฐ์ฅ ํฐ ๊ฐ)
System.out.println("Poll from Max Heap: " + maxHeap.poll()); // ์ถ๋ ฅ: 20
System.out.println("Poll from Max Heap: " + maxHeap.poll()); // ์ถ๋ ฅ: 10
}
}
์์ฃผ ์ฐ์ด๋ ์๊ณ ๋ฆฌ์ฆ
์ ๋ ฌ
import java.util.*;
public class SortingExample {
public static void main(String[] args) {
// ๋ฐฐ์ด ์ ๋ ฌ (๊ธฐ๋ณธ ์ค๋ฆ์ฐจ์)
int[] arr = {5, 2, 8, 1, 4};
Arrays.sort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr)); // ์ถ๋ ฅ: [1, 2, 4, 5, 8]
// ๋ฐฐ์ด ์ ๋ ฌ (๋๋ค์์ ์ด์ฉํ ๋ด๋ฆผ์ฐจ์)
Integer[] arrDesc = {5, 2, 8, 1, 4}; // ๊ธฐ๋ณธํ int๋ ๋๋ค์ ์ฐ๋ ค๋ฉด Wrapper ํด๋์ค(Integer) ์ฌ์ฉ ํ์
Arrays.sort(arrDesc, (a, b) -> b - a);
System.out.println("Sorted Array (Desc): " + Arrays.toString(arrDesc)); // ์ถ๋ ฅ: [8, 5, 4, 2, 1]
// List ์ ๋ ฌ
List<String> list = new ArrayList<>(Arrays.asList("Cherry", "Apple", "Banana"));
Collections.sort(list); // ๊ธฐ๋ณธ ์ค๋ฆ์ฐจ์
System.out.println("Sorted List: " + list); // ์ถ๋ ฅ: [Apple, Banana, Cherry]
// List ์ ๋ ฌ (๋๋ค์์ ์ด์ฉํ ๋ด๋ฆผ์ฐจ์)
Collections.sort(list, (a, b) -> b.compareTo(a));
System.out.println("Sorted List (Desc): " + list); // ์ถ๋ ฅ: [Cherry, Banana, Apple]
}
}
์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ์ํด์๋ Wrapper ํด๋์ค๋ก ๋๋ค์์ ์ฌ์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌํ๋ค.
๋ค๋ง int ๋ฐฐ์ด์ ๊ฒฝ์ฐ, Integer ๋ฐฐ์ด์ ์ฌ์ฉํด์ผํ๋๋ฐ Integer ๋ฐฐ์ด์ ์ฌ์ฉํ์ง ์์ผ๋ ค๋ฉด ์ฝ๊ฐ์ ๊ธฐ๊ต๋ก ์ด์ฉํด์ ์ฝ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
1. ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ ํ ํ์์ ๊ฒ์์ ๋ฐ๋๋ก ์ํ
2. ๋ชจ๋ ๊ฐ์ -1์ ๊ณฑํด์ ์ค๋ฆ์ฐจ์ ํ ์ค์ ํ์ํ ๊ฐ์ -1์ ๊ณฑํด์ ์ฌ์ฉ
import java.util.*;
public class SortingExample {
public static void main(String[] args) {
// ๋ฐฐ์ด ์ ๋ ฌ (๊ธฐ๋ณธ ์ค๋ฆ์ฐจ์)
int[] arr = {5, 2, 8, 1, 4};
Arrays.sort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr)); // ์ถ๋ ฅ: [1, 2, 4, 5, 8]
// ๋ด๋ฆผ์ฐจ์ ๊ธฐ๊ต
int[] arr2 = {-5, -2, -8, -1, -4}; // ์๋ ๊ฐ์ -1์ ๊ณฑํ ๊ฐ์ ์ฌ์ฉ
Arrays.sort(arr2); // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
System.out.println("Sorted Array: " + Arrays.toString(arr2)); // ์ถ๋ ฅ: [-8, -5, -4, -1] -> ๊ฐ์ ์ฌ์ฉํ ๋, -1์ ๊ณฑํด์ ์ฌ์ฉ
}
}
์ด์งํ์
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// ์ด์ง ํ์์ ๋ฐ๋์ ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ์ฌ์ฉ
int[] sortedArr = {1, 2, 4, 5, 8, 10, 12};
// ๊ฐ 5 ์ฐพ๊ธฐ
int index = Arrays.binarySearch(sortedArr, 5);
System.out.println("Index of 5: " + index); // ์ถ๋ ฅ: 3 (0๋ถํฐ ์์)
// ๊ฐ 7 ์ฐพ๊ธฐ (์๋ ๊ฐ)
int indexNotFound = Arrays.binarySearch(sortedArr, 7);
System.out.println("Index of 7: " + indexNotFound); // ์ถ๋ ฅ: ์์ ๊ฐ (์ฐพ์ง ๋ชปํ์์ ์๋ฏธ)
}
}
์ด์งํ์์ ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ํน์ ๊ฐ์ ์ฐพ๋๋ฐ ์ ์ฉํ๋ค. ์ ๋ ฌ์ด ์๋์ด์๋ ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ค๋ฉด sort๋ก ์ง์ ์ ๋ ฌ ์ํ.
Java์์๋ ์ด์งํ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ฉ์๋๋ก ์ ๊ณตํ์ฌ ์ฝ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
BFS
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BFSExample {
static List<List<Integer>> graph;
static boolean[] visited;
public static void bfs(int startNode) {
Queue<Integer> q = new LinkedList<>();
q.offer(startNode);
visited[startNode] = true;
while (!q.isEmpty()) {
int currentNode = q.poll();
System.out.print(currentNode + " "); // ๋ฐฉ๋ฌธ ๋
ธ๋ ์ถ๋ ฅ
// ํ์ฌ ๋
ธ๋์ ์ฐ๊ฒฐ๋ ๋
ธ๋๋ค ํ์ธ
for (int nextNode : graph.get(currentNode)) {
if (!visited[nextNode]) {
visited[nextNode] = true;
q.offer(nextNode);
}
}
}
}
public static void main(String[] args) {
// ๊ทธ๋ํ ์์ฑ (์์)
int numNodes = 5;
graph = new ArrayList<>();
for (int i = 0; i <= numNodes; i++) {
graph.add(new ArrayList<>());
}
graph.get(1).add(2);
graph.get(1).add(3);
graph.get(2).add(4);
graph.get(3).add(4);
graph.get(3).add(5);
graph.get(4).add(5);
visited = new boolean[numNodes + 1]; // ๋ฐฉ๋ฌธ ๊ธฐ๋ก ๋ฐฐ์ด
System.out.print("BFS ํ์ ๊ฒฐ๊ณผ (1๋ถํฐ): ");
bfs(1); // 1๋ฒ ๋
ธ๋๋ถํฐ BFS ์์
System.out.println();
}
}
BFS(๋๋น ์ฐ์ ํ์)์ ์์ ๋ ธ๋๋ฅผ ์ฃผ๋ณ์ผ๋ก ๊ฐ๊น์ด ๋ ธ๋๋ค์ ๋จผ์ ํ์ํ๋ฉด์ ๋ฒ์๋ฅผ ํ์ฅํ๋ ํ์ ๋ฐฉ๋ฒ์ด๋ค.
๊ทธ๋ํ๋ ํธ๋ฆฌ๊ตฌ์กฐ์ ํ์์ ์ฌ์ฉํ๋ฉฐ, ์ต๋จ๊ฑฐ๋ฆฌ, ๋คํธ์ํฌ ์ฐพ๊ธฐ ๋ฑ ์ด๋์ ๊ฐ์ค์น๊ฐ ๋์ผํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
DFS
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
static int[] numbers = {1, 2, 3};
static int n = numbers.length; // ์ด ์ซ์์ ๊ฐ์
static int r = 3; // ์ ํํ ์ซ์์ ๊ฐ์ (3์๋ฆฌ ์์ด)
static boolean[] visited = new boolean[n]; // ์ซ์๋ฅผ ์ฌ์ฉํ๋์ง ์ฒดํฌ
static int[] result = new int[r]; // ํ์ฌ ๋ง๋ค์ด์ง๊ณ ์๋ ์์ด
// DFS (Backtracking) ํจ์
// depth: ํ์ฌ ๋ช ๋ฒ์งธ ์ซ์๋ฅผ ์ ํํ๊ณ ์๋์ง (0๋ถํฐ ์์)
public static void dfs(int depth) {
// ๊ธฐ์ ์กฐ๊ฑด (Base Case): r๊ฐ์ ์ซ์๋ฅผ ๋ชจ๋ ์ ํํ์ ๋
if (depth == r) {
// ์์ฑ๋ ์์ด ์ถ๋ ฅ
System.out.println(Arrays.toString(result));
return; // ๋๋์๊ฐ๊ธฐ
}
// ์ฌ๊ท ํธ์ถ ๋ถ๋ถ: ํ์ฌ depth ์์น์ ์ด๋ค ์ซ์๋ฅผ ๋ฃ์์ง ์ ํ
for (int i = 0; i < n; i++) {
// i๋ฒ์งธ ์ซ์๋ฅผ ์์ง ์ฌ์ฉํ์ง ์์๋ค๋ฉด
if (!visited[i]) {
// 1. ์ ํ (์ํ ๋ณ๊ฒฝ)
visited[i] = true; // i๋ฒ์งธ ์ซ์๋ฅผ ์ฌ์ฉํ๋ค๊ณ ํ์
result[depth] = numbers[i]; // ํ์ฌ depth ์์น์ i๋ฒ์งธ ์ซ์ ์ ์ฅ
// 2. ํ์ (๋ ๊น์ด ๋ค์ด๊ฐ์ ๋ค์ ์ซ์ ์ ํ)
dfs(depth + 1);
// 3. ์๋ณต (๋ฐฑํธ๋ํน)
// depth+1 ์์ ๋์์ค๋ฉด i๋ฒ์งธ ์ซ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ทจ์
// ๊ทธ๋์ผ ๋ค์ ๋ฐ๋ณต(i+1)์์ ๋ค๋ฅธ ์ซ์๋ฅผ ์ ํํด์ ์๋ก์ด ์์ด
visited[i] = false;
// result[depth]๋ ๋ค์ ๋ฐ๋ณต์์ ๋ฎ์ด์์์ง๋ฏ๋ก ์ฌ๊ธฐ์ ๋ฐ๋ก ์๋ณต ์ ํด๋ ๋จ (์๋ก์ด ๊ฐ์ผ๋ก ์ฑ์์ง)
}
}
}
public static void main(String[] args) {
System.out.println("์์ด ์์ฑ ๊ฒฐ๊ณผ:");
dfs(0); // depth 0๋ถํฐ ์์ (์ฒซ ๋ฒ์งธ ์ซ์ ์ ํ)
/*
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์์ด ์์ฑ ๊ฒฐ๊ณผ:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
*/
}
}
DFS(๊น์ด ์ฐ์ ํ์)์ ๊ฒฝ์ฐ, ๊ทธ๋ํ๋ ํธ๋ฆฌ๊ตฌ์กฐ์ ํ์์์ ์ฌ์ฉํ๋ฉฐ ํ์ฌ ๋ ธ๋์ ์ฐ๊ฒฐ๋ ๋ ธ๋๋ฅผ ๋ชจ๋ ๋จผ์ ํ์ํ์ง ์๊ณ
๋ค์ ๋ ธ๋์ ์ฐ๊ฒฐ๋ ๋ค์ ๋ ธ๋๊ฐ ์๋ค๋ฉด ํด๋น ๋ ธ๋๋ฅผ ์ฐ์ ํ์ํ๋ค.
1๋ฒ ๋ ธ๋๊ฐ 2, 3, 4 ๋ ธ๋์ด๊ณ 2๋ฒ๊ณผ ์ฐ๊ฒฐ๋ ๋ ธ๋๊ฐ 5, 6(1์ ์ธ)๋ผ๋ฉด, 1->2->5 ์์ผ๋ก ํ์ํ๋ค.
์ ์ฝ๋๋ 1~3๊น์ง์ ์ซ์๋ก ๋ง๋ค ์ ์๋ ๋ฐฐ์ด์ ์กฐํฉ์ ์ถ๋ ฅํ๋ ์ฝ๋
DFS ํ์์ ์์ ํ์์ผ๋ก ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๋ค๋ณด๋ ์๊ฐ์ด๊ณผ์ ์ด์๊ฐ ์์ ์ ์๋ค.
3๋ฒ์ ๋ฐฑํธ๋ํน์ ํตํด์ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ, ๋ ์ด์ ํ์ํ์ง ์๊ณ ์ข ๋ฃํ๊ฑฐ๋ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๋ ์กฐ๊ฑด์ ๋ง๋ ๊ฒฝ์ฐ๋ง ์ฐพ์์ผํ ๋ ์ฌ์ฉํ ์ ์๋ค.