[μκ³ λ¦¬μ¦] μμ΄κ³Ό μ‘°ν©
1. μμ΄ Permutation
- κΈΈμ΄κ° nκ°μΈ λ°°μ΄μμ rκ°μ μμλ₯Ό μ°¨λ‘λ‘ λ½μ μλ‘μ΄ λ°°μ΄μ λ§λ€μμ λ κ°λ₯ν λͺ¨λ λ°°μ΄(μ€λ³΅μ μΈ)
→ μμ΄, μ€λ³΅ μμ΄, μ‘°ν©, μ€λ³΅μ‘°ν© λͺ¨λ nκ°μμ rκ°λ₯Ό λ½λλ€λ μ μμ λμΌ - μμ΄μ μ μμμλ μ λ ¬. μμκ° μλ€λ κ²μ΄ νΉμ§
public class AlgorithmStudy {
public static void permutation(int[] arr, int[] out, boolean[] visited, int depth, int r){
if(depth == r){
for(int num: out) System.out.print(num);
System.out.println();
return;
}
for(int i=0; i<arr.length; i++){
if(!visited[i]){
visited[i] = true;
out[depth] = arr[i];
permutation(arr, out, visited, depth+1, r);
visited[i] = false;
}
}
}
public static void main(String[] args){
int[] arr = {1, 2, 3};
int r = 2;
permutation(arr, new int[r], new boolean[arr.length], 0, r);
}
}
→ κ²°κ³Ό : 1 2 / 1 3 / 2 1 / 2 3 / 3 1 / 3 2
2. μ€λ³΅ μμ΄
- μλ‘ λ€λ₯Έ nκ°μμ μ€λ³΅μ΄ κ°λ₯νκ² rκ°λ₯Ό λ½μμ μ λ ¬νλ κ²½μ°μ μ
- μμκ° μλ€λ μ μμ μμ΄κ³Ό λμΌ. κ°μ μμλ₯Ό μ€λ³΅ν΄μ λ½μ μ μλ€λ μ μμ μ°¨μ΄
→ μμ΄ μ½λμμ μ€λ³΅λ°©μ§λ₯Ό μν΄ μ¬μ©νλ visited λΆλΆμ μ κ±°νλ©΄ λλ€.
public class AlgorithmStudy {
public static void permutation(int[] arr, int[] out, int depth, int r){
if(depth == r){
for(int num: out) System.out.print(num);
System.out.println();
return;
}
for(int i=0; i<arr.length; i++){
out[depth] = arr[i];
permutation(arr, out, depth+1, r);
}
}
public static void main(String[] args){
int[] arr = {1, 2, 3};
int r = 2;
permutation(arr, new int[r], 0, r);
}
}
→ κ²°κ³Ό : 1 1 / 1 2 / 1 3 / 2 1 / 2 2 / 2 3 / 3 1 / 3 2 / 3 3
3. μ‘°ν© Combination
- μλ‘ λ€λ₯Έ nκ°μμ μμ μμ΄ rκ°λ₯Ό λ½λ κ²½μ°μ μ(μ€λ³΅μ μΈ)
→[1, 2] μ [2, 1]μ κ°μ κ²μΌλ‘ μ·¨κΈ β· μ΄ κ²½μ° [1, 2]λ§ μΉ΄μ΄ν - μμ μκ΄μ΄ μκ³ , μ€λ³΅λ μκΈ° λλ¬Έμ μ νλ μμλ₯Ό λ°λ‘ μ μ₯νμ§ μκ³ ,
μμλ€κ³Ό visitedλ§ μ΄μ©νμ¬ λ°©λ¬Έν μμλ€μ μμλλ‘ νμΈνλ©΄ κ³§ μ νλ μ‘°ν©
public class AlgorithmStudy {
public static void combination(int[] arr, boolean[] visited, int start, int depth, int r){
if(depth == r){
for(int i=0; i<arr.length; i++){
if(visited[i]) System.out.print(arr[i]);
}
System.out.println();
return;
}
for(int i=start; i<arr.length; i++){
if(!visited[i]){
visited[i] = true;
combination(arr, visited, i+1, depth+1, r);
visited[i] = false;
}
}
}
public static void main(String[] args){
int[] arr = {1, 2, 3};
int r = 2;
combination(arr, new boolean[arr.length], 0, 0, r);
}
}
→ κ²°κ³Ό : 1 2 / 1 3 / 2 3
4. μ€λ³΅ μ‘°ν©
- μλ‘ λ€λ₯Έ nκ°μμ μμ μμ΄, μ€λ³΅μ΄ κ°λ₯νκ² rκ°λ₯Ό λ½λ κ²½μ°μ μ
- μμ μμ΄ λ½λ μ‘°ν©κ³Ό λμΌνμ§λ§, μ€λ³΅μ΄ κ°λ₯νλ€λ μ μμ μ°¨μ΄(μ¦, μ΄λ―Έ λ½μ κ²μ λ λ½μ μ μμ)
→ μ‘°ν© μ½λμμ μ€λ³΅λ°©μ§λ₯Ό μν΄μ¬μ©νλ visited λΆλΆμ μ κ±°νλ©΄ λλ€.
→ μ€λ³΅μ΄ κ°λ₯νλ―λ‘ μ νν μμλ₯Ό μ μ₯νλ λ°°μ΄ νμ
public class AlgorithmStudy {
public static void combination(int[] arr, int[] out, int start, int depth, int r){
if(depth == r){
for(int num : out) System.out.print(num);
System.out.println();
return;
}
for(int i=start; i<arr.length; i++){
out[depth] = arr[i];
combination(arr, out, i, depth+1, r);
}
}
public static void main(String[] args){
int[] arr = {1, 2, 3};
int r = 2;
combination(arr, new int[r], 0, 0, r);
}
}
κ²°κ³Ό → 1 1 / 1 2 / 1 3 / 2 2 / 2 3 / 3 3
μ°Έκ³ ) https://minhamina.tistory.com/37
[Java] μμ΄ Permutation
μμ΄ μμ΄μ΄λ n κ°μ κ° μ€μμ r κ°μ μ«μλ₯Ό λͺ¨λ μμλλ‘ λ½λ κ²½μ°λ₯Ό λ§νλ€. μνμμ μμ΄μ μλ‘ λ€λ₯Έ nκ°μ μμμμ rκ°λ₯Ό λ½μν μ€λ‘ μΈμ°λ κ²½μ°μ μμ΄λ€. μμ΄μ κ°μλ nμ κ³μΉ
minhamina.tistory.com
μ°Έκ³ ) https://bcp0109.tistory.com/14
μμ΄ Permutation (Java)
μμ΄μ°μ΅ λ¬Έμ μμ΄μ΄λ n κ°μ κ° μ€μμ r κ°μ μ«μλ₯Ό λͺ¨λ μμλλ‘ λ½λ κ²½μ°λ₯Ό λ§ν©λλ€. μλ₯Ό λ€μ΄ [1, 2, 3] μ΄λΌλ 3 κ°μ λ°°μ΄μμ 2 κ°μ μ«μλ₯Ό λ½λ κ²½μ°λ [1, 2] [1, 3] [2, 1] [2, 3] [3, 1] [3,
bcp0109.tistory.com