CS/μ•Œκ³ λ¦¬μ¦˜

[μ•Œκ³ λ¦¬μ¦˜] μˆœμ—΄κ³Ό μ‘°ν•©

코더999 2023. 1. 16. 11:34

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

 

 

더보기