๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Code-States

[D+29] ์žฌ๊ท€ํ•จ์ˆ˜

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 10. 6.

 

 

D+29

-  ์žฌ๊ท€ํ•จ์ˆ˜ -

(์žฌ๊ท€)

 


 

 

์žฌ๊ท€ (Recursion)

•์–ด๋–ค ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ๋•Œ, ๊ตฌ์กฐ๋Š” ๋™์ผํ•˜์ง€๋งŒ ๋” ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ๋จผ์ € ํ•ด๊ฒฐํ•จ์œผ๋กœ์จ ๊ทธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

 

•ํ•จ์ˆ˜ ์Šค์Šค๋กœ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ

 

 

์žฌ๊ท€ ์ดํ•ดํ•˜๊ธฐ

Q. ์ž์—ฐ์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์˜ ํ•ฉ์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ 'arrSum'์„ ์ž‘์„ฑํ•ด๋ผ.

๋งŒ์•ฝ ํ•ด๋‹น๋ฌธ์ œ๋ฅผ ์ง๋ฉดํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ์šฐ๋ฆฌ๋Š” ์–ด๋ ต์ง€ ์•Š๊ฒŒ 'for๋ฌธ'์„ ์‚ฌ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

function arrSum(arr) {
  let sum = 0;
  for(let i = 0; i < arr.length; i++) {
    sum = sum + arr[i];
  }
  return sum;
}

์˜ˆ๋กœ๋“ค๋ฉด, arr์— [10, 3, 6, 2]๊ฐ€ ์ž…๋ ฅ๋˜์—ˆ์„ ๊ฒฝ์šฐ '10 + 3 + 6 + 2'๋กœ '21'์ด ๋ฆฌํ„ด๋  ๊ฒƒ์ด๋‹ค.

 

์ด์ฒ˜๋Ÿผ ์šฐ๋ฆฌ๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ

'for๋ฌธ' ๋ง๊ณ ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ '์žฌ๊ท€ํ•จ์ˆ˜'๋ฅผ ์ด์šฉํ•ด ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ’€๊ธฐ ์ „์— ์žฌ๊ท€์˜ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ์ชผ๊ฐœ์„œ ์ƒ๊ฐํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

 

 

1. ๋ฌธ์ œ๋ฅผ ์ชผ๊ฐœ์„œ ๊ณ„์‚ฐ์„ ์‹œ๋„ํ•œ๋‹ค.

// ๋ฌธ์ œ๋ฅผ ํ•œ๋ฒˆ์— ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ชผ๊ฐœ์„œ ๋‚˜๋ˆ„์–ด์„œ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค.

arr([10, 3, 6, 2]) = 10 + arrSum([3, 6, 2]);

 

2. ๋ฌธ์ œ๊ฐ€ ์ž‘์•„์ง€์ง€ ์•Š์„ ๋•Œ ๊นŒ์ง€ ๋” ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•œ๋‹ค.

// ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฐ์—ด์ด ๋์ด ๋ณด์ผ๋•Œ ๊นŒ์ง€ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

arrSum([3, 6, 2]) = 3 + arrSum([6, 2]);
arrSum([6, 2]) = 6 + arrSum([2]);
arrSum([2]) = 2 + arrSum([]);

 

3. ๋ฌธ์ œ๊ฐ€ ๊ฐ„๋‹จ๋ช…๋ฃŒํ•ด์ ธ์„œ ํ’€ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์„ ๊ฒฝ์šฐ์— ๋ฌธ์ œ๋ฅผ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๊ฒฐํ•ด ์ค€๋‹ค.

 

์ด๋•Œ์— ์ฃผ์˜ํ•  ์ ์€ ์žฌ๊ท€๋Š” ์ข…๋ฃŒ์ง€์ ์„ ๋งŒ๋“ค์–ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š” ์ด์ƒ ๊ณ„์† ์‹คํ–‰๋˜๊ธฐ๋•Œ๋ฌธ์—

์ข…๋ฃŒ์ง€์ ์„ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค.

arrSum([]) = 0; // ๋ฌธ์ œ๊ฐ€ ๋”์ด์ƒ ์ž‘์•„์ง€์ง€ ์•Š๋Š” ์ˆœ๊ฐ„

// ๊ฐ€์žฅ ์ž‘์€ ๊ฒฝ์šฐ์˜ ํ•ด๊ฒฐ์ฑ…์„ ์ ์šฉํ•œ๋‹ค.
arrSum([2]) = 2 + arrSum([]) = 2;
arrSum([6, 2]) = 6 + arrSum([2]) = 6 + 2 = 8;
arrSum([3, 6, 2]) = 3 + arrSum([6, 2]) = 3 + 8 = 11;
arrSum([10, 3, 6, 2]) = 10 + arrSum([3, 6, 2]) = 10 + 11 = 2

 

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•ด๋‹น ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋˜๋ฉด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

function arrSum(arr) {
  if(arr.length === 0) { // ์žฌ๊ท€ํ•จ์ˆ˜๊ฐ€ ๋๋‚  ์ง€์ ์„ ์ œ์‹œ
    return 0;
  }
  let head = arr[0];
  let tail = arr.slice(1);
  return head + arrSum(tail);
}

 

 

์žฌ๊ท€์˜ ์žฅ๋‹จ์ 

•์žฅ์ 

์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žฌ๊ท€๋กœ ํ‘œํ˜„ํ•˜๊ธฐ์— ์ž์—ฐ์Šค๋Ÿฌ์šธ ๊ฒฝ์šฐ, ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค.

 

•๋‹จ์ 

๊ฐ’์ด ๋ฆฌํ„ด๋˜๊ธฐ ์ „๊นŒ์ง€ ํ˜ธ์ถœ๋งˆ๋‹ค call stack์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—

๋ฉ”๋ชจ๋ฏธ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

•์ฃผ์–ด์ง„ ๋ฌธ์ œ๊ฐ€ ๋ฐ˜๋ณต์ ์ด๊ณ  ๋” ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋‰˜์–ด ์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ

 

•์ค‘์ฒฉ๋œ ๋ฃจํ”„๊ฐ€ ๋งŽ๊ฑฐ๋‚˜ ์ค‘์ฒฉ์˜ ์ •๋„๋ฅผ ๋ฏธ๋ฆฌ ์•Œ๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ

 

for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
        for (let k = 0; k < n; k++) {
            for (let l = 0; l < n; l++) {
                for (let m = 0; m < n; m++) {
                    for (let n = 0; n < n; n++) {
                        for (let o = 0; o < n; o++) {
                            for (let p = 0; p < n; p++) {
                                // do something
                                someFunc(i, j, k, l, m, n, o, p);
                           }
                        }
                    }
                }
            }
        }
    }
 }

๋ชจ๋“  ์žฌ๊ท€ํ•จ์ˆ˜๋Š” while์ด๋‚˜ for๋ฌธ์„ ํ†ตํ•ด ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์œ„์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ

๋ฌดํ•œํ•œ ์‹์„ ์ผ์ผํžˆ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒ๋ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์ด ์ข‹์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ

๊ฐ„๋‹จํ•˜๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€