320 likes | 428 Views
Military Technical Academy Data Structures and Algorithms. Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 0912 312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 13. Recursion Removal. Lecture 13. Recursion Removal (1/2). References:
E N D
Military Technical Academy Data Structures and Algorithms Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 0912 312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 13. Recursion Removal Dr. Nguyen Nam Hong, Le Quy Don Technical University
Lecture 13. Recursion Removal (1/2) References: • Data structures and Algorithms Stacks.htm • Kyle Loudon Mastering Algorithms Chapter 6 Stacks and Queues • Elliz Horowitz – Fundamentals of Data Structures Chapter 3 Stacks and Queues • Deshpande Kakle – C and Data Structures Chapter 19. Stacks and Queues Dr. Nguyen Nam Hong, Le Quy Don Technical University
Lecture 13. Recursion Removal (2/2) Contents: 13.1. Common Principles 13.2. NonRecursive Factorial 13.3. NonRecursive Fibonacci 13.4. NonRecursive Hanoi Tower 13.5. NonRecursive QuickSort Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.1. Common Principles (1/5) Normally when you make a subroutine call, the computer does three things: • First, it save any information it needs to resume execution when the subroutine return. • Second, it prepares for the call and transfers control to the subroutine. • Third, when the called routine ends, the computer restores the information it saved in the first step and transfers control back to the appropriate point in the program. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.1. Common Principles (2/5) Stack Frames or Activation Records • Programs compiled from modern high level languages make use of a stack frame for the working memory of each method invocation. • When any method is called, a number of words - the stack frame - is pushed onto a program stack. • When the method returns, this frame of data is popped off the stack. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.1. Common Principles (3/5) Activation Records • As a method calls another method, first its arguments, then the return address and finally space for local variables is pushed onto the stack. • Actual implementation is slightly different. • The top of stack can store the current method environment. • When method is called, the new environment is pushed onto stack. • On return, the old environment is restored by popping the method environment.
13.1. Common Principles (4/5) Each activation record contains: 1. A variable that contains the return address in the calling method 2. For each value formal parameter, a variable that contains a copy of the argument 3. For each reference formal parameter, a variable that contains the address of the argument 4. For each local variable(defined in the method’s block), a variable that contains a copy of that defined variable. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.1. Common Principles (5/5) Implementation notes: • If all the items of the activation record have the same type, we can • use pushes to store items on a stack • use pops to restore items from the stack • If items of the activation record have different types, we can • use a stack of records or • Use multiple stack, each stack for each type Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.2. Non-Recursive Factorial (1/5) The RecurFib Function Func RecurFact(n: Long): Long If n <= 1 Then RecurFact = 1 Else RecurFact = n * RecurFact(n - 1) End If End Func Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.2. Non-Recursive Factorial (2/5) • The Recursive part has only one call to itself, so removing recursion is simple. • Stack frame contains only one value nn • nn is dynamic value at the current level • RetAdd is set by the multiple operation. • Initiate: • S is empty. • a loop push all values of nn on the Stack • a second loop pop all the value from the stack and execute the multiplication. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.2. Non-Recursive Factorial (3/5) Func nonRecurFact(n As Long) As Long Dim m As Long, nn As Long If n <= 1 Then nonRecurFact = 1 Else ‘Loop part to replace recursive part End If End Function Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.2. Non-Recursive Factorial (4/5) Loop part to replace the recursive part nn = n Do While nn > 1 mStack.Push nn: nn = nn - 1 Loop m = 1 Do While Not mStack.isSEmpty() mStack.Pop nn: m = m * nn Loop nonRecurFact = m Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.2. Non-Recursive Factorial (5/5) Loop part execution for nonRecursiveFact(4) nn = 4 Push nn: nn = nn-1=4-1=3 Push nn: nn = nn-1=3-1=2 Push nn: nn = nn-1=2-1=1 m=1 Pop nn: nn=1: m = m*nn=1*1=1 Pop nn: nn=2: m = m*nn=1*2=2 Pop nn: nn=3: m = m*nn=2*3=6 Pop nn: nn=4: m = m*nn=6*4=24 Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.3. Non-Recursive Fibonacci (1/5) The RecFib Function Func RecFib(n:Int, vl1:Int, vl2:Int):Int If n <= 1 Then RecFib = vl2 Else RecFib = RecFib(n - 1, vl2, vl1 + vl2) End If End Function Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.3. Non-Recursive Factorial (2/5) • The Recursive part has only one call to itself, so removing recursion is simple. • Stack frame contains only one value nn • nn is dynamic value at the current level • RetAdd is set by the addition operation. • Initiate: • S is empty. • a loop push all values of nn on the Stack • a second loop pop all the value from the stack and execute the addition. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.3. Non-Recursive Factorial (3/5) Func nonRecFib(n:Int):Int Var m: Long, nn: Long, vl1:Int, vl2:Int If n <= 2 Then nonRecFib = 1 Else ‘Loop part to replace recursive part End If End Func Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.3. Non-Recursive Factorial (4/5) Loop part to replace the recursive part nn = n Do While nn > 2 mStack.Push nn: nn = nn - 1 Loop m = 0: vl1=1: vl2=1 Do While Not mStack.isSEmpty() mStack.Pop nn: m = vl1+vl2 vl1=vl2: vl2=m Loop nonRecFib = m Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.3. Non-Recursive Factorial (5/5) Loop part execution for nonRecFib(5) nn = 5 Push nn: nn = nn-1=5-1=4 Push nn: nn = nn-1=4-1=3 m=0: vl1=1: vl2=1 Pop nn: nn=3: m=vl1+vl2=1+1=2: vl1=1, vl2=2 Pop nn: nn=4: m=vl1+vl2=1+2=3: vl1=2, vl2=3 Pop nn: nn=5: m=vl1+vl2=2+3=5: vl1=3, vl2=5 Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (1/7) Sub mQuickSort(Lindex: Long, Uindex: Long) var Mindex, UIndex1, LIndex1: Long If (LIndex < UIndex) Then Part LIndex, UIndex, MIndex UIndex1 = MIndex - 1 LIndex1 = MIndex + 1 mQuickSort LIndex, UIndex1 mQuickSort LIndex1, UIndex End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (2/7) • The Recursive part has two calls to itself, so removing recursion is more complex. • Stack frame contains two values LB and UB • LB is lower bound of new subsequence • UB is upper bound of new subsequence. • Initiate: • Stack is empty. • a loop pushes all values on the Stack • a second loop pops all values from the Stack Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (3/7) Declaration Pivot: the value of pivot • Rightest item of working region NewCall: to determine if a pop is needed. LIndex: Lower bound of working region UIndex: Upper bound of working region LIndex1: Index to scan from left UIndex1: Index to scan from right mStack: A stack to use, • init with LIndex and Uindex of the whole sequence. Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (4/7) Sub NonRecQS() If NewCall And (Not mStack.Empty()) Then mStack.Pop Uindex: mStack.Pop Lindex ElseIf mStack.Empty() And (NewCall) Then Exit Sub End If If LIndex < UIndex Then mQuickSort LIndex, UIndex, NewCal End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (5/7) Sub mQuickSort(LI:Int, UI:Int, nC: Bool) If (LI < UI) Then Part LI, UI, LI1, UI1 If LI1 < UI1 Then PairSwap LI1, UI1 nC = False ElseIf (LI1 >= UI1) Then ‘Save Activate Record End If End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (6/7) Save Activate Record nC = True If LI <> UI1 Then PivotSwap LI, UI1 End If If LI < UI1 - 1 Then mStack.Push LI: mStack.Push UI1 - 1 End If If UI > UI1 + 1 Then mStack.Push UI1+1: mStack.Push UI End If Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.4. Non-Recursive QuickSort (7/7) Sub Part(LI: Int, UI: Int, L: Int, R: Int) Pivot = b(LI): Left = LI + 1 R = UI Do While (R > LI) And (b(R) > Pivot) R = R - 1 Loop Do While (L < UI) And (b(L) < Pivot) L = L + 1 Loop End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (1/7) Sub HANOI(n:Int, o1:Char, o2:Char, o3:Char) If (n = 1) Then myMove 1, o1, o3 Else HANOI n - 1, o1, o3, o2 myMove n, o1, o3 HANOI n - 1, o2, o3, o1 End If End Sub (o1: Source, o3: Target, o2: Intermediate) Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (2/7) • The Recursive part has two calls to itself, so removing recursion is more complex. • Stack frame contains five values nn, tt, aa, bb, cc (see next slide) • Initiate: • Stack is empty. • a loop pushes all values on the Stack • a second loop pops all values from the Stack Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (3/7) Declaration nn: number of disks at the pilar tt: original disk index aa: name of the source pilar. bb: name of the target pilar cc: name of the intermediate pilar pushValue: group of values to push on the stack popValue: group of values to pop from the stack mStack: A stack to use Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (4/7) Sub InitTower() Set pushValue = New Hanoi If n=1 Then pushValue.Create n, 1, "A", "B", "C“ Else pushValue.Create n, 0, "A", "B", "C“ End If mStack.Push pushValue End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (5/7) Sub NonRecTower() nn = 0 Do While (nn <> 1) PopValues If (nn = 1) Then If (tt > 0) Then myMove tt, aa, cc Else PushValues End If Loop End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (6/7) Sub PopValues() Set popValue = New Hanoi mStack.Pop popValue nn = popValue.n tt = popValue.t aa = popValue.a bb = popValue.b cc = popValue.c End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
13.5. Non-Recursive Hanoi Tower (7/7) Sub PushValues() Set pushVal = New Hanoi pushVal.Create nn - 1, IIf(nn = 2, 1, 0), bb, aa, cc mStack.Push pushVal Set pushVal = New Hanoi pushVal.Create 1, nn, aa, bb, cc mStack.Push pushVal Set pushVal = New Hanoi pushVal.Create nn - 1, IIf(nn = 2, 1, 0), aa, cc, bb mStack.Push pushVal End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University