- toc {:toc}
What is Recursion?
-
์ฌ๊ท(Recursion) : ํจ์ ์์์ ์๊ธฐ ํจ์๋ฅผ ๋ค์ ํธ์ถํ๋ ๋ฐฉ์์ ํ๋ค.
- ์ง์ ์ : ์๊ธฐ ์์ ์ ์ง์ ๋ถ๋ฅด๋ ์ฌ๊ท์ ๊ตฌ์กฐ
- ๊ฐ์ ์ : ๋ ํจ์ ์ด์์ด ์ฐ๊ฒฐ๋์ด ์๋ก๋ฅผ ํธ์ถํ๋ ์ฌ๊ท์ ์ธ ๊ตฌ์กฐ
-
ํจ์๋ ํธ์ถ ์ ๋ฉ๋ชจ๋ฆฌ์ ์คํ ๋ถ๋ถ์ ์์นํ๋ค.
-
์ฌ๊ท๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ํจ์๊ฐ ๋ฐ๋ณต๋์ด ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ Stack overflow๋ฅผ ์ฃผ์ํด์ผ ํ๋ค.
-
์ฌ๊ท๋ ๋ฐ๋ณต๋ณด๋ค ํจ์จ์ ์ด์ง ์์ ๊ฒฝ์ฐ๊ฐ ๋ง์ง๋ง, ๊ฐ๋จํ๊ณ ์ฝ๋๊ฐ ์ง๊ด์ ์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ๋ค.
Structure of Recursion
- Base case : ๋น์ฌ๊ท์ ์ธ ์ํ๊ฐ ๋ ์ ์๋๋ก ํ๋ ๊ฒฝ์ฐ์ด๋ค. ์ฌ๊ท์ ์ข ๋ฃ, ๋ฐํ ์กฐ๊ฑด์ด ๋๋ค.
- General (recursive) case : ์๊ธฐ ์์ ์ ๋ ์์ ๋ฒ์ ์ผ๋ก ํธ์ถํ ์ ์๋ ์กฐ๊ฑด์ด๋ค.
- ์ฌ๊ท ์๊ณ ๋ฆฌ์ฆ์ ๋ฐ๋ณต์ ์ผ๋ก ํจ์๋ฅผ ํธ์ถํ ์๋ก ๋ฌธ์ ๊ฐ ๋ ์์์ง๋ ๊ตฌ์กฐ๋ก ํํ๋์ด์ผ ํ๋ค.
int Factorial(int number){
if (number == 0)
return 1;
else
return number * Factorial(number-1);
}
// ์๊ฐ ๋ณต์ก๋ : O(N)- ํ์ค์นผ์ ๊ณต์์ ์ฌ์ฉํด์ ์กฐํฉ๋ ์ฌ๊ท์ ์ผ๋ก ํํํ ์ ์๋ค.
int Combinations(int n, int k){
if (k==1)
return n;
else if (n==k)
return 1;
else
return Combination(n-1, k) + Combination(n-1, k-1);
}template<class ItemType>
bool BinarySearch(ItemType)(ItemType info[], ItemType item,
int fromLoc, int toLoc){
int mid;
if (fromLoc > toLoc)
return false;
else{
mid = (fromLoc + toLoc) / 2;
if (info[mid] == item)
return true;
else if (item < info[mid])
return BinarySearch(info, item, fromLoc, mid-1);
else
return BinarySearch(info, item, mid+1, toLoc);
}
}- Sorted List์ ์ฌ๊ท์ InsertItem
template <class ItemType>
void Insert(NodeType<ItemType>* &location, ItemType item){
if ((location==NULL) || (item < location->info)){
NodeType<ItemType>* tempPtr = location;
location = new NodeType<ItemType>;
location->info = item;
location->next = tempPtr;
}
else
Insert(location->next, newItem);
}
template <class ItemType>
void SortedType<ItemType>::InsertItem(ItemType newItem){
Insert(listData, newItem);
}- InsertItem์ ํ๋ฉด ์ด์ ํฌ์ธํฐ์ ๋ค์ ๊ฐ์ด insert item, insert item์ ๋ค์ ๊ฐ์ ํ์ฌ ๊ฐ์ผ๋ก ์ง์ ํด์ค์ผ ํ๋ค. ์ฆ, 2๋ฒ ๋ณ๊ฒฝํด์ผ ํ๋ค.
- ํ์ง๋ง ์์ ๊ฒฝ์ฐ, locationโnext = tempPtr ๋ฅผ ํตํด ํฌ์ธํฐ ํ๋๋ง ๋ณ๊ฒฝํด์ค๋ค. ์ ๊ทธ๋ด๊น?
-
- &location โ ํฌ์ธํฐ์ ์ฃผ์๋ฅผ ๋งํ๋ค.
- locationโnext๊ฐ ํ๋ผ๋ฏธํฐ๋ก ๋ค์ด๊ฐ๊ณ , locationโnext์ new๋ก ๋์ ํ ๋น์ ๋ฐ๊ธฐ ๋๋ฌธ์ ์๋ก ์๊ธด ๋ ธ๋๊ฐ new, new์ ์ด์ ๋ ธ๋๊ฐ prev๋ผ๋ฉด prevโnext๊ฐ ์ด๋ฏธ new๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๊ฒ ๋๋ค.
template <class ItemType>
void Delete(NodeType<ItemType>* &location, ItemType item){
if(item == location->info)) {
NodeType<ItemType>* tempPtr = location;
location = location->next;
delete tempPtr;
}
else
Delete(location->next, item);
}
template <class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item){
Delete(listData, item);
}- Tail Recursion : ํจ์์ ๋ง์ง๋ง ์ค์ ์ฌ๊ท ํธ์ถ์ด ํฌํจ๋ ๊ตฌ์กฐ
- Tail Recursion์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ฝ๊ฒ ๋์ฒดํ ์ ์๋ค.
- ์ฌ๊ท ํธ์ถ์ ํจ์์ ํ๋ผ๋ฏธํฐ์ ์ง์ญ ๋ณ์๋ฅผ ์ ์ฅํด์ผ ํ๊ธฐ ๋๋ฌธ์ activation record๋ก ์ง๋๊ณ ์์ด์ผ ํ๋ค. ํ์ง๋ง Tail Recursion์ ๋งจ ๋ง์ง๋ง์ ์ฌ๊ท ํธ์ถ์ ํ๊ณ ๊ทธ ์ดํ ๋์์ด ์๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ์ ์ฅํ ํ์๊ฐ ์์ด ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ์ฝ๋ค.
- Tail Recursion์ ํ๋์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ณํํ ์ ์๋ค.
Recursion vs. Iteration
-
๋ฐ๋ณต๋ฌธ : looping ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ค.
-
์ฌ๊ท๋ฌธ : branching ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ค.
-
์ฌ๊ท๋ ์๊ฐ, ๊ณต๊ฐ ์ธก๋ฉด์์ ๋ฐ๋ณต๋ฌธ๋ณด๋ค ๋นํจ์จ์ ์ด๋ค.
-
ํ์ง๋ง ๋ฌธ์ ๋ฅผ ๊ฐ๋จํ๊ฒ, ์ง๊ด์ ์ผ๋ก ํ ์ ์๊ณ ์ฝ๋๋ฅผ ์ฝ๊ฒ ์ดํดํ ์ ์๋ค.
-
Combination๊ณผ ๊ฐ์ ๊ฒฝ์ฐ ์ค๋ณต๋๋ ๊ณ์ฐ์ด ๋ง์ด ๋ฐ์ํ๋๋ฐ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ ์ค๋ณต๊ฐ์ ์ ์ฅํด ์ฌ์ฉํ๋ memoization ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ์ค๋ณต์ ์ค์ธ๋ค.
-
์ฌ๊ท์ ์ฌ์ฉ ์์
-
์ฌ๊ท ํธ์ถ์ ๊น์ด๊ฐ ๋ฌธ์ ์ ํฌ๊ธฐ์ ๋นํด ์๋์ ์ผ๋ก ์์ ๊ฒฝ์ฐ
-
์ฌ๊ท๊ฐ ๋น์ฌ๊ท์ ๋ฐฉ์๊ณผ ๋น์ทํ ๋ณต์ก๋๋ฅผ ๊ฐ์ง ๊ฒฝ
-
์ฌ๊ท๊ฐ ๋น์ฌ๊ท์ ๋ฐฉ์๋ณด๋ค ๋ ์งง๊ณ ๊ฐ๋จํ ๊ฒฝ์ฐ