Recursive algorithm for maximum value
For an array with a[1...N]
Call Max (n) when the algorithm is called
Max (i) if 1 return a[i] Else if A[i]>max (i1return a[i] elsereturn max (i-1 ) ifif
Recursive algorithm of average value
For array a[1...N]
Sum initial value is 0
The index initial value is 1
Call the algorithm using Ave (A,SUM,INDEX)
Ave (int a[],int sum,int index) if
return sum/(index-1) Else return Ave (A,sum+=a[index], index+1)
Recursive algorithm of Hanoi
voidMoveintNCharLeftCharMiddle,CharRight ) { if(n==1)//Move the 1th number plate .cout<<n<<"Number plate"<<":"<<left<<"→"<<right<<Endl; Else{Move (n-1, Left,right,middle); cout<<n<<"Number plate"<<":"<<left<<"→"<<right<<Endl;//moving the N-Number plateMove (n1, Middle,left,right); }}
Dynamic planning Issues
Lcs eldest-son sequential recursive
ai = bi l[i,j] = l[i-1, J11; AI! = Bi L[i,j] = max{l[i-1, j],l[i,j-1 ]}
Floyd Shortest-Path recursive
0-1 the recursive type of backpack
Si > J v[i,j] = v[i-1, J] // current backpack size smaller than the volume of the item =< j V[i,j] = max{v[i-1< /c6>,j],v[i-1, J-si]+vi}// current backpack size greater than or equal to the volume of the item
When the 0-1 backpack turns into a full backpack,
Can modify the above recursive modification to the format k = si/j
Si > J v[i,j] = v[i-1, J] // current backpack size smaller than the volume of the item =< j V[i,j] = max{ v[i-1, j],v[i-1, J-k*si]+k*vi}// current backpack size greater than or equal to the volume of the item
3 recursive algorithm for coloring problems
input: g= (v,e) Output: Graph node 3 coloring vector c[1.. N],1≤c[k]≤3(1≤k≤n). 1. Graphcolorrec (1) process Graphcolorrec (k)1. forcolor←1To32. C[k]←color3.ifc[1.. K] for legal coloring Then//Partial solution or Solution4.ifK=n Then//Solution5. Output c[1.. N] and exit6.Else //Partial Solution7. Graphcolorrec (k +1)//go to the next node.8. Endif9. EndifTen. End for
4 Queen problem recursive algorithm
input: null output: Vector c[corresponding to 4 queens problem1..4] (global amount)1. Advanced1) process Advanced (k)1. forcol←1To4 //only 4 columns at most2. C[k]←col3.ifc[1.. K] is the solution then//Partial solution or Solution4.ifk=4Then//Complete Solution5. Output c[1..4] and exit6.Else //Partial Solution7. Advanced (k +1)//move to the next line8. Endif 9. Endif Ten. End for
Some common algorithms for dynamic programming of recursive algorithms