<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
# Recursion - 遞歸
--------
#### 問題
序列$$ s $$有$$ n $$個成員$$ [x_1,x_2, \dots ,x_n] $$,每個成員可以選取$$ [1,2, \dots ,m] $$這$$ m $$種值。例如當$$ n = 5 $$,$$ m = 3 $$時,序列$$ s $$有如下排列組合:
$$
\begin{matrix}
[1,1,1,1,1] \\
[1,1,1,1,2] \\
[1,1,1,1,3] \\
[1,1,1,2,1] \\
\cdots
\end{matrix}
$$
求$$ s $$的所有排列組合。(與BruteForce問題一樣)
#### 解法
BruteForce存在一個問題,外圍for循環的數量是固定的,無法改變。因此我們用遞歸來解決這個問題。假設序列$$ s $$的長度從$$ 0 $$增長到$$ n $$。初始化時令序列為空$$ s = [] $$。
$$ (1) $$ 將序列$$ s $$的長度增加到$$ 1 $$,第$$ 1 $$個元素(唯一的元素)$$ x_1 $$有$$ m $$種選擇,即長度為$$ 1 $$的序列$$ s $$有$$ m $$個排列組合:
$$
\begin{matrix}
[ 1_1 ] \\
[ 2_1 ] \\
\cdots \\
[ m_1 ]
\end{matrix}
$$
$$ (2) $$ 將序列$$ s $$的長度增加到$$ 2 $$,得到$$ s = [x_1,x_2] $$,元素$$ x_2 $$的選擇可以看作在第$$ (1) $$輪的每個選擇的基礎上繼續選擇。對于$$ [1_1] $$可以得到$$ m $$個排列組合:
$$
\begin{matrix}
[ 1_1,1_2 ] \\
[ 1_1,2_1 ] \\
\cdots \\
[ 1_1,m_1 ]
\end{matrix}
$$
第$$ (2) $$輪操作后共有$$ m^2 $$個排列組合。重復$$ n $$次這樣的操作,可以得到$$ m^n $$個排列組合。
實際編寫代碼中,在遞歸方程中傳入一個參數$$ 0 \le prev \lt n $$,序列$$ s $$中的成員$$ x_{prev} $$可以取值$$ i \in [1,m] $$,然后$$ prev = prev+1 $$,繼續考慮序列$$ s $$中的下一個成員$$ x_{prev+1} $$。這樣直到當$$ n $$個成員都選擇了一個值時,即產生序列$$ s $$的一種排列組合。通過遞歸可以退回上一個函數棧,從而讓每個成員$$ x_{prev} $$都可以重新選擇。
對于成員數量為$$ n $$,每個成員有$$ m $$種值的序列$$ s $$,遍歷所有排列組合的時間復雜度$$ O(m^n) $$。
--------
#### 源碼
[Recursion.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/Recursion.h)
[Recursion.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/Recursion.cpp)
#### 測試
[RecursionTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/RecursionTest.cpp)
- Content 目錄
- Preface 前言
- Chapter-1 Sort 第1章 排序
- InsertSort 插入排序
- BubbleSort 冒泡排序
- QuickSort 快速排序
- MergeSort 歸并排序
- Chapter-2 Search 第2章 搜索
- BinarySearch 二分查找法(折半查找法)
- BruteForce 暴力枚舉
- Recursion 遞歸
- BreadthFirstSearch 廣度優先搜索
- BidirectionalBreadthSearch 雙向廣度搜索
- AStarSearch A*搜索
- DancingLink 舞蹈鏈
- Chapter-3 DataStructure 第3章 數據結構
- DisjointSet 并查集
- PrefixTree(TrieTree) 前綴樹
- LeftistTree(LeftistHeap) 左偏樹(左偏堆)
- SegmentTree 線段樹
- FenwickTree(BinaryIndexedTree) 樹狀數組
- BinarySearchTree 二叉查找樹
- AVLTree AVL平衡樹
- RedBlackTree 紅黑樹
- Chapter-4 DynamicProgramming 第4章 動態規劃
- Chapter-5 GraphTheory 第5章 圖論
- Chapter-6 Calculation 第6章 計算
- LargeNumber 大數字
- Exponentiation 求冪運算
- Chapter-7 CombinatorialMathematics 第7章 組合數學
- FullPermutation 全排列
- UniqueFullPermutation 唯一的全排列
- Combination 組合
- DuplicableCombination (元素)可重復的組合
- Subset 子集
- UniqueSubset 唯一的子集
- Permutation 排列
- PermutationGroup 置換群
- Catalan 卡特蘭數
- Chapter-8 NumberTheory 第8章 數論
- Sieve 篩選算法
- Euclid 歐幾里得
- EuclidExtension 歐幾里得擴展
- ModularLinearEquation 模線性方程
- ChineseRemainerTheorem 中國剩余定理
- ModularExponentiation 模冪運算
- Chapter-9 LinearAlgebra 第9章 線性代數
- Chapter-10 AnalyticGeometry 第10章 解析幾何
- Chapter-11 TextMatch 第11章 文本匹配
- SimpleMatch 簡單匹配
- AhoCorasickAutomata AC自動機
- KnuthMorrisPratt KMP匹配算法
- RabinKarp RabinKarp算法
- BoyerMoore BoyerMoore算法
- Chapter-12 GameTheory 第12章 博弈論
- BashGame 巴什博弈
- WythoffGame 威佐夫博弈
- NimGame 尼姆博弈