Deep_Dev
article thumbnail

์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 

์žฌ๊ท€(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)+" ");
    }
}