Professional Documents
Culture Documents
CS-102
Fall 2018
Instructor: Dr. Jamal Abdul Nasir
Department of Computer Science and Software Engineering
International Islamic University, Islamabad
M o st o f th e m ate rial in th is co u rse is take n fro m D r. Ju stin H sia’s co u rse at W ash in gto n U n ive rsity, afte r w ritte n ap p ro val fro m h im .
v Iteration v Recursion
§ Repeat part of algorithm a § Algorithm calls itself to help
specified number of times solve the problem on smaller
parts
for(int i=20; i<400; i=i+60) {
line(i,40,i+60,80);
}
v Recursive case(s)
§ Function calls itself one or more times on “smaller”
problems
• Divide the problem into one or more simpler/smaller parts
• Invoke the function (recursively) on each part
• Combine the solutions of the parts into a solution for the problem
§ Divide: y is reduced by 1
§ Invoke: call to add(x+1,y-1)
§ Combine: none
§ Divide: y is reduced by 1
§ Invoke: call to add(x,y-1)
§ Combine: add 1 to result
1 2 3
CS-102, Fall 2018 Lecture#21&22- Recursion 18
Solving the Tower of Hanoi
v To reconstruct the tower on peg 3, we first need to
get the largest disk (red) onto peg 3
§ Can’t do this while the other disks are on top
§ Solution: First move the 4 disks on top to peg 2
1 2 3
CS-102, Fall 2018 Lecture#21&22- Recursion 19
Solving the Tower of Hanoi
v To reconstruct the tower on peg 3, we first need to
get the largest disk (red) onto peg 3
§ Can’t do this while the other disks are on top
§ Solution: First move the 4 disks on top to peg 2
• moveTower(4,peg1,peg2); ← just assume it works!
1 2 3
CS-102, Fall 2018 Lecture#21&22- Recursion 20
Solving the Tower of Hanoi
v Now we can move the red disk to peg 3
1 2 3
CS-102, Fall 2018 Lecture#21&22- Recursion 21
Solving the Tower of Hanoi
v Now we can move the red disk to peg 3
§ moveTower(1,peg1,peg3);
1 2 3
CS-102, Fall 2018 Lecture#21&22- Recursion 22
Solving the Tower of Hanoi
v Generalized recursive solution to move tower of
height H from source peg to destination peg:
§ Move tower of height H-1 from source peg to extra peg
§ Move the remaining bottom disk from source peg to
destination peg
§ Move tower of height H-1 from extra peg to destination peg
1 2 3
CS-102, Fall 2018 “Source” “Extra”
Lecture#21&22- Recursion “Destination” 23
Solving the Tower of Hanoi
v Generalized recursive solution to move tower of
height H from source peg to destination peg:
§ moveTower(H-1,peg1,peg2);
§ moveTower(1,peg1,peg3);
§ moveTower(H-1,peg2,peg3);
1 2 3
CS-102, Fall 2018 “Source” “Extra”
Lecture#21&22- Recursion “Destination” 24
Solving the Tower of Hanoi
v What’s the base case?
§ Don’t recurse (but still move disk) when H == 1
1 2 3
CS-102, Fall 2018 “Source” “Extra”
Lecture#21&22- Recursion “Destination” 25
‘Inception’ Analogy (2010 film)
v Each dream is a function call, each “kick” is a function return
§ e.g. the ‘reality’ function calls the ‘Robert Fischer dream’ function
§ Characters are the parameters – they may have the same names, but
are different (clothes?) in every layer
http://images.fastcompany.com/upload/INCEPTION%20infographic%20v3.5.2_dwang.jpg
CS-102, Fall 2018 Lecture#21&22- Recursion 26