j.tiberghien chapter 2.10 recursive algorithms and backtracking
TRANSCRIPT
J.Tiberghien
Recursive Algorithms
Factorial• Step-up function : Fac(n) = (n)*Fac(n-1)• Trivial solution : Fac(1) = 1• Recursive procedure :
PROCEDURE Fac(n:CARDINAL): CARDINAL;BEGIN
IF n > 1THEN RETURN n*Fac(n-1)ELSE RETURN 1
END (* IF *)END Fac;
J.Tiberghien
Recursive Algorithms
Factorial• Alternative Iterative Procedure :
PROCEDURE Fac(n:CARDINAL): CARDINAL; VAR f : CARDINAL;BEGIN f := 1;
WHILE n > 1 DOf := f * n ; n := n - 1
END; (* WHILE *)RETURN f
END Fac;
J.Tiberghien
Recursive Algorithms
Fibonaci• Step-up function : Fib(n) = Fib(n-1)+Fib(n-2)• Trivial solution : Fib(1) = 1; Fib(0) = 0• Recursive procedure :
PROCEDURE Fib(n:CARDINAL): CARDINAL;BEGIN
IF n > 1THEN RETURN Fib(n-1)+Fib(n-2)ELSIF n=1 THEN RETURN 1ELSE RETURN 0
END (* IF *)END Fib;
J.Tiberghien
Recursive Algorithms
Fibonaci• Alternative Iterative Procedure :
PROCEDURE Fib(n:CARDINAL): 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;
J.Tiberghien
Recursive 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
J.Tiberghien
Recursive AlgorithmsTowers of Hanoi• Recursive procedure :
PROCEDURE MoveTower (Height: CARDINAL From,Towards,Using:CHAR); PROCEDURE MoveDisk.....BEGIN
IF Height > 0 THENMoveTower(Height-1,From,Using,Towards)MoveDisk(From,Towards);MoveTower(Height-1,Using,Towards,From)
END (* IF *)END MoveTower;
J.Tiberghien
Recursive Algorithms Conclusion
Simultaneous activations
Few
Many
ComplexAlgorithm
YES
???
SimpleAlgorithm
...
Never
J.Tiberghien
Generic Search with Backtracking
UNTIL all successor nodes have been explored
Select a successor node
Record selected node
Erase previous node selection
Allowed node ?No Yes
Call recursivelythe backtracking
procedure forthe next node
Final node ?Not yet Yes
Display thesolution
J.Tiberghien
Eight Queens Procedure
FOR Col := 1 TO 8 DO
Put queen on Board[Row,Col]
Remove queen from Board[Row,Col]
Board[Row,Col] safe ?No Yes
Try(Row+1)
(Row = 8) ?Not yet Yes
Display the Board
PROCEDURE Try (Row : CARDINAL);
J.Tiberghien
Eight Queens - Try
PROCEDURE Try(Row : INTEGER); VAR Col : INTEGER; PROCEDURE Safe … … … ;BEGIN FOR Col := 1 TO 8 DO IF Safe(Col,Row) THEN Board[Col,Row] := FALSE; IF Row < 8 THEN Try(Row + 1) ELSE PrintSolution END; Board[Col,Row] := TRUE END (* IF *) END (* FOR *)END Try;
J.Tiberghien
Eight 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 Board[Col,r] AND Board[Col+Row-r,r] AND Board[Col-Row+r,r] END; (* FOR *) RETURN fEND Safe;
J.Tiberghien
Eight Queens - Main program
BEGIN FOR Col := -6 TO 15 DO FOR Row := 1 TO 8 DO Board[Col,Row] := TRUE END; (* FOR Rows*) END; (* FOR Cols*) Try(1)END Queens.
J.Tiberghien
Eight Queens - Non recursive FOR Col1 := 1 TO 8 DO
IF Safe(Col1,1) THEN Board[Col1,1] := FALSE; FOR Col2 := 1 TO 8 DO IF Safe(Col2,2) THEN Board[Col2,2] := FALSE; ... FOR Col8 := 1 TO 8 DO IF Safe(Col8,8) THEN Board[Col8,8] := FALSE; PrintSolution Board[Col8,8] := TRUE; END; END; (* FOR 8 *) ... Board[Col2,2] := TRUE END; (* IF 2 *) END; (* FOR 2 *) Board[Col1,1] := TRUE
END; (* IF 1 *) END; (* FOR 1 *)
J.Tiberghien
Frequency Synthetizer
2075 Hz
1925 Hz
1625 Hz
1475 Hz
1025 Hz
875 Hz./. n1
./. n2
./. n3
./. n4
./. n5
./. n6
F = ?
SCM =175 855 554 875
J.Tiberghien
Frequency synthetizer Procedure
FOR Fact[Fr] := MinFact[Fr] TO MaxFact[Fr] DO
Update frequency range for oscillator
Restore previous frequency range
Acceptable factor ?No Yes
Try(Fr+1)
(Fr = 6) ?Not yet Yes
Print Factors
PROCEDURE Try(Fr : CARDINAL);
J.Tiberghien
Traveling Salesman with Bactracking
UNTIL all possible next towns have been selected
Select next town
Record next town
Erase previous town
Total distance < Min ?No Yes
Call recursivelythe backtracking
procedure forthe next town
Final town ?Not yet Yes
Min := Total distance
J.Tiberghien
Recursive Fractals (1)
Basic building block: a rectangle
PROCEDURE Rectangle(xl,yl,xr,yh,color)
Graphical libraries:
FROM Graph IMPORT Init, Plot, Rectangle, _WHITE, _BLUE, _clrLIGHTRED, _clrWHITE;
yl
yh
xrxl
J.Tiberghien
Recursive Fractals(2)
PROCEDURE Box(x,y,d:CARDINAL,color);BEGIN Rectangle(x-d,y-d,x+d,y+d,color)END Box;
To draw a square of size 2d centered in x,y:
d
d
x,y
J.Tiberghien
Recursive Fractals(3)
Rectangle(x-d,y-d,x+d,y+d);Box(x-d,y-d,d DIV 2);Box(x-d,y+d,d DIV 2);Box(x+d,y-d,d DIV 2);Box(x+d,y+d,d DIV 2);
To draw an elementary fractal box :
J.Tiberghien
Recursive Fractals(4)
PROCEDURE FractalBox(x,y,d,n:CARDINAL);BEGIN Rectangle(x-d,y-d,x+d,y+d); n := n-1 IF n > 0 THEN FractalBox(x-d,y-d,d DIV 2,n); FractalBox(x-d,y+d,d DIV 2,n); FractalBox(x+d,y-d,d DIV 2,n); FractalBox(x+d,y+d,d DIV 2,n); ENDEND FractalBox
To draw a series of n fractal boxes :