Title: Recursive Algorithms
1Chapter 2.10
- Recursive Algorithms
- and
- Backtracking
2Recursive AlgorithmsFactorial
- Step-up function Fac(n) (n)Fac(n-1)
- Trivial solution Fac(1) 1
- Recursive procedure
PROCEDURE Fac(nCARDINAL) CARDINAL BEGIN IF n gt
1 THEN RETURN nFac(n-1) ELSE RETURN 1 END ( IF
) END Fac
3Recursive AlgorithmsFactorial
- Alternative Iterative Procedure
PROCEDURE Fac(nCARDINAL) CARDINAL VAR f
CARDINAL BEGIN f 1 WHILE n gt 1 DO f f
n n n - 1 END ( WHILE ) RETURN f END Fac
4Recursive AlgorithmsFibonaci
- Step-up function Fib(n) Fib(n-1)Fib(n-2)
- Trivial solution Fib(1) 1 Fib(0) 0
- Recursive procedure
PROCEDURE Fib(nCARDINAL) CARDINAL BEGIN IF n gt
1 THEN RETURN Fib(n-1)Fib(n-2) ELSIF n1 THEN
RETURN 1 ELSE RETURN 0 END ( IF ) END
Fib
5Recursive AlgorithmsFibonaci
- Alternative Iterative Procedure
PROCEDURE Fib(nCARDINAL) CARDINAL VAR
i,fn,fnm1,fnm2 CARDINAL BEGIN IF n 0 THEN
RETURN 0 ELSIF n 1 THEN RETURN 1 ELSE
fnm2 0 fnm1 1 FOR i 2 TO n
DO fn fnm1 fnm2
fnm2 fnm1 fnm1 fn END ( FOR )
RETURN fn END ( IF ) END Fib
6Recursive AlgorithmsTowers of Hanoi
- The problem
- Moving one tower with n rings from A to B using C
- Step-up function
- Move the n-1 upper rings from A to C
- Move one ring from A to B
- Move the n-1 rings from C to B
- Trivial solution
- Moving a tower with no rings
7Recursive AlgorithmsTowers of Hanoi
PROCEDURE MoveTower (Height CARDINAL
From,Towards,UsingCHAR) PROCEDURE
MoveDisk..... BEGIN IF Height gt 0
THEN MoveTower(Height-1,From,Using,Towards) MoveDi
sk(From,Towards) MoveTower(Height-1,Using,Towards
,From) END ( IF ) END MoveTower
8Recursive Algorithms Conclusion
9Backtracking AlgorithmsA treelike maze
10Generic Search with Backtracking
Select a successor node
Allowed node ?
No
Yes
Record selected node
Final node ?
Not yet
Yes
Call recursively the backtracking procedure
for the next node
Display the solution
Erase previous node selection
UNTIL all successor nodes have been explored
11The Eight Queens Problem
12Eight Queens Procedure
13Eight Queens - Try
- PROCEDURE Try(Row INTEGER)
- VAR Col INTEGER
- PROCEDURE Safe
- BEGIN
- FOR Col 1 TO 8 DO
- IF Safe(Col,Row) THEN
- BoardCol,Row FALSE
- IF Row lt 8 THEN Try(Row 1)
- ELSE PrintSolution
- END
- BoardCol,Row TRUE
- END ( IF )
- END ( FOR )
- END Try
14Eight Queens - Board
Extended board for determination of safe positions
15Eight Queens - Safe
PROCEDURE Safe(Col,Row INTEGER)BOOLEAN VAR
r INTEGER f BOOLEAN BEGIN f TRUE
FOR r 1 TO Row-1 DO f f AND
BoardCol,r AND BoardColRow-r,r
AND BoardCol-Rowr,r END ( FOR
) RETURN f END Safe
16Eight Queens - Main program
BEGIN FOR Col -6 TO 15 DO FOR Row 1
TO 8 DO BoardCol,Row TRUE END
( FOR Rows) END ( FOR Cols)
Try(1) END Queens.
17Eight Queens - Non recursive
FOR Col1 1 TO 8 DO IF Safe(Col1,1) THEN
BoardCol1,1 FALSE FOR Col2 1 TO 8
DO IF Safe(Col2,2) THEN BoardCol2,2
FALSE ... FOR Col8 1 TO
8 DO IF Safe(Col8,8) THEN
BoardCol8,8 FALSE
PrintSolution BoardCol8,8
TRUE END END ( FOR 8
) ... BoardCol2,2 TRUE
END ( IF 2 ) END ( FOR 2 )
BoardCol1,1 TRUE END ( IF 1 ) END (
FOR 1 )
18Frequency Synthetizer
SCM 175 855 554 875
19Frequency synthetizer Procedure
20Traveling Salesman with Bactracking
Select next town
Total distance lt Min ?
No
Yes
Record next town
Final town ?
Not yet
Yes
Call recursively the backtracking procedure
for the next town
Min Total distance
Erase previous town
UNTIL all possible next towns have been selected
21Recursive Fractals (1)
Graphical libraries
FROM Graph IMPORT Init, Plot, Rectangle,
_WHITE, _BLUE, _clrLIGHTRED, _clrWHITE
Basic building block a rectangle
PROCEDURE Rectangle(xl,yl,xr,yh,color)
22Recursive Fractals(2)
To draw a square of size 2d centered in x,y
PROCEDURE Box(x,y,dCARDINAL,color) BEGIN
Rectangle(x-d,y-d,xd,yd,color) END Box
23Recursive Fractals(3)
To draw an elementary fractal box
Rectangle(x-d,y-d,xd,yd) Box(x-d,y-d,d DIV
2) Box(x-d,yd,d DIV 2) Box(xd,y-d,d DIV
2) Box(xd,yd,d DIV 2)
24Recursive Fractals(4)
To draw a series of n fractal boxes
PROCEDURE FractalBox(x,y,d,nCARDINAL) BEGIN
Rectangle(x-d,y-d,xd,yd) n n-1 IF n gt 0
THEN FractalBox(x-d,y-d,d DIV 2,n)
FractalBox(x-d,yd,d DIV 2,n)
FractalBox(xd,y-d,d DIV 2,n)
FractalBox(xd,yd,d DIV 2,n) END END
FractalBox
25(No Transcript)