πŸ“š μ½”λ”©ν…ŒμŠ€νŠΈ/JAVA

[JAVA] μž¬κ·€ν•¨μˆ˜ ( λ©”λͺ¨μ΄μ œμ΄μ…˜ )

deep_lee 2022. 11. 3. 19:22

μž¬κ·€ μ•Œκ³ λ¦¬μ¦˜ 

μž¬κ·€(Revursion) ν•¨μˆ˜λž€ νŠΉμ • ν•¨μˆ˜ λ‚΄μ—μ„œ μžμ‹ μ„ λ‹€μ‹œ ν˜ΈμΆœν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•΄λ‚˜κ°€λŠ” ν•¨μˆ˜μ΄λ‹€. 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ›λž˜ λ²”μœ„μ˜ λ¬Έμ œμ—μ„œ 더 μž‘μ€ λ²”μœ„μ˜ ν•˜μœ„ 문제λ₯Ό ν•΄κ²°ν•¨μœΌλ‘œμ¨ μ›λž˜ 문제λ₯Ό ν•΄κ²°ν•΄ λ‚˜κ°€λŠ” 방식이닀. 일반 λ°˜λ³΅λ¬Έμ„ 톡해 κ΅¬ν˜„ κ°€λŠ₯ν•œ κΈ°λŠ₯은 μž¬κ·€ ν•¨μˆ˜λ₯Ό 톡해 κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ©° λ°˜λŒ€λ‘œ μž¬κ·€ν•¨μˆ˜λ‘œ κ΅¬ν˜„ κ°€λŠ₯ν•œ κΈ°λŠ₯을 반볡문으둜 κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ‹€.

 

μž¬κ·€ ν•¨μˆ˜λŠ” ν•¨μˆ˜ λ‚΄μ—μ„œ 자기 μžμ‹ μ„ 계속 ν˜ΈμΆœν•˜λŠ” 방식이기 λ•Œλ¬Έμ— ν•¨μˆ˜ μ•ˆμ— λ°˜λ“œμ‹œ μ’…λ£Œ ꡬ간이 λ˜λŠ” BaseCaseλ₯Ό μƒκ°ν•˜μ—¬ μ½”λ“œλ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€. μ•„λž˜λŠ” μƒ˜ν”Œμ˜ˆμ œμ΄λ‹€.

public class Recursion
{
    public static void main(String[] args)
    {
        Function();
    }
    
    public static void Function()
    {
        System.out.println("λ°˜κ°‘μŠ΅λ‹ˆλ‹€");
 
        Function();
    }
}

 

Funtion ν•¨μˆ˜μ˜ κΈ°λŠ₯은 "λ°˜κ°‘μŠ΅λ‹ˆλ‹€"λ₯Ό ν˜ΈμΆœν•˜κ³  λ‹€μ‹œ 자기 μžμ‹ μ„ ν˜ΈμΆœν•˜κ³  μžˆλ‹€. ν•΄λ‹Ή μ†ŒμŠ€μ—μ„œ λ¬Έμ œλŠ” 자기 μžμ‹ μ„ ν˜ΈμΆœλ§Œν•˜κ³  μžˆμ§€ ν•¨μˆ˜ μ˜μ—­μ΄ μ’…λ£Œλ˜λŠ” ꡬ간이 μ—†κΈ° λ•Œλ¬Έμ— λ¬΄ν•œλ°˜λ³΅λ˜λŠ” 루프에 λΉ μ§€κ²Œ λœλ‹€.

 

public class Recursion_Test
{
    public static void main(String[] args)
    {
        Function(5);
    }
 
    public static void Function()
    {
        if(num==0)
        {
        return;
        }
        else
        {
        	System.out.println("λ°˜κ°‘μŠ΅λ‹ˆλ‹€.");
            Function(num-1);
    }
}

 

μ΄λ ‡κ²Œ μˆ˜μ •ν•œ κ²°κ³Ό, λ§€κ°œλ³€μˆ˜ 값이 0일 경우 return을 ν•˜κ³  μ•„λ‹κ²½μš° "λ°˜κ°‘μŠ΅λ‹ˆλ‹€"λ₯Ό 좜λ ₯ν•˜κ³  num-1에 ν•΄λ‹Ήν•˜λŠ” 값을 λ§€κ°œλ³€μˆ˜λ‘œ μ „λ‹¬ν•˜κ³  μžˆλ‹€. μ—¬κΈ°μ„œ num 이 0일 경우 return을 ν•˜κ²Œ λ˜λŠ” ꡬ문이 μž¬κ·€ ν•¨μˆ˜μ˜  BaseCaseꡬ간이 λœλ‹€.

ν•΄λ‹Ή μž¬μ‰¬ ν•¨μˆ˜μ˜ ν˜ΈμΆœμ„ 그림으둜 보면 μ•„λž˜μ™€ κ°™λ‹€.

 

 

 

 

 

예제 : 1λΆ€ν„° nκΉŒμ§€ ν•© κ΅¬ν•˜κΈ° ( recursive )

μœ„μ—μ„œ 배운 μž¬κ·€ μ•Œκ³ λ¦¬μ¦˜μ„ μ μš©ν•˜μ—¬ 1λΆ€ν„° nκΉŒμ§€ 합을 κ΅¬ν•˜λŠ” μž¬κ·€ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•΄λ³Έλ‹€.

 

public class Recursion
{
    public static void main(String[] args)
    {
        System.out.println("1λΆ€ν„° 5κΉŒμ§€μ˜ 합은 : " + Function(5));
    }
 
    public static int Function(int num)
    {
        if(num == 1)
        {
            return 1;
        }
        else
        {
            return num + Function(num -1);
        }
    }
}

 

 

 

예제 : μž…λ ₯받은 μ •μˆ˜ n을 2μ§„μˆ˜λ‘œ 좜λ ₯ν•˜κΈ° ( recursive )

class Recursive {
    public void recursive(int n) {
       if(n==0)
           return;
       else{
           recursive(n/2);
           System.out.print(n%2);
       }
    }
    public static void main(String[] args) {
        Solution T=new Solution();
        T.recursive(11);
    }
}
//////////
μž…λ ₯ : 11 
좜λ ₯ : 1011

 

 

 

예제 : νŒ©ν† λ¦¬μ–Ό ( recursive )

μž…λ ₯받은 μ •μˆ˜ n의 νŒ©ν† λ¦¬μ–Ό 값을 좜λ ₯ν•˜μ‹œμ˜€.

public class Recursive {
	// λ°˜ν™˜λ°›μ•„μ•Όν•˜λ‹ˆ int
    public int recursive(int n) {
       if(n==1) return 1;
       else return n*recursive(n-1);
    }
    public static void main(String[] args) {
        Solution T=new Solution();
        System.out.println(T.recursive(5));
    }
}
//////////
μž…λ ₯ : 5
좜λ ₯ : 5*4*3*2*1 = 120

 

 

 

 

 

예제 : ν”Όλ³΄λ‚˜μΉ˜ ( recursive )

1. κ°€μž₯ 기본적인 ν”Όλ³΄λ‚˜μΉ˜ recursive 

public class Solution {
    public int recursive(int n) {
       if(n==1) return 1;
       else if(n==2) return 1;
       else return recursive(n-1)+recursive(n-2);
    }
    public static void main(String[] args) {
        Solution T=new Solution();
        int n=10;
        for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
    }
}

 

 

2. 쑰금 더 μ΅œμ ν™”λœ ν”Όλ³΄λ‚˜μΉ˜ recursive

public class Solution {
    static int[] fibo;
    public int recursive(int n) {
       if(n==1) return fibo[n]=1;
       else if(n==2) return fibo[n]=1;
       else return fibo[n]=recursive(n-1)+recursive(n-2);
    }
    public static void main(String[] args) {
        Solution T=new Solution();
        int n=10;
        fibo=new int[n+1];
        T.recursive(n);
        for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
    }
}

 

 

λ©”λͺ¨μ΄μ œμ΄μ…˜μ„ ν™œμš©ν•œ ν”Όλ³΄λ‚˜μΉ˜ recursive ( κ°€μž₯ μ΅œμ ν™” )

λ©”λͺ¨μ΄μ œμ΄μ…˜(memoization)μ΄λž€ ?

컴퓨터 ν”„λ‘œκ·Έλž¨μ΄ λ™μΌν•œ 계산을 λ°˜λ³΅ν•΄μ•Ό ν•  λ•Œ, 이전에 κ³„μ‚°ν•œ 값을 λ©”λͺ¨λ¦¬μ— μ €μž₯ν•¨μœΌλ‘œμ¨ λ™μΌν•œ κ³„μ‚°μ˜ λ°˜λ³΅μˆ˜ν–‰μ„ μ œκ±°ν•˜μ—¬ ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 속도λ₯Ό λΉ λ₯΄κ²Œ ν•˜λŠ” 기술. 동적 κ³„νšλ²•μ˜ 핡심이 λ˜λŠ” κΈ°μˆ μ΄λ‹€.

 

λ©”λͺ¨μ΄μ œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 배열을 ν•˜λ‚˜ λ§Œλ“€κ³  값을 μž¬μ‚¬μš©ν•˜μ—¬ ν’€μ΄ν•œ κ²°κ³Ό, μž¬κ·€λ§Œμ„ μ‚¬μš©ν•œ μœ„μ˜ 풀이 방법과 λΉ„κ΅ν•˜μ—¬ 싀행속도에 μ—„μ²­λ‚œ 차이가 μžˆλ‹€.

 

 

public class Solution {
    static int[] fibo;
    public int recursive(int n) {
       if(fibo[n]>0) return fibo[n];
       if(n==1) return fibo[n]=1;
       else if(n==2) return fibo[n]=1;
       else return fibo[n]=recursive(n-2)+recursive(n-1);
    }
    public static void main(String[] args) {
        Solution T=new Solution();
        int n=45;
        fibo=new int[n+1];
        T.recursive(n);
        for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
    }
}