250 likes | 273 Views
Chapter 2.10. Recursive Algorithms and Backtracking. Step-up function : Fac(n) = (n)*Fac(n-1) Trivial solution : Fac(1) = 1 Recursive procedure :. Recursive Algorithms Factorial. PROCEDURE Fac(n:CARDINAL): CARDINAL; BEGIN IF n > 1 THEN RETURN n*Fac(n-1) ELSE RETURN 1
E N D
Chapter 2.10 Recursive Algorithms and Backtracking
Step-up function :Fac(n) = (n)*Fac(n-1) Trivial solution :Fac(1) = 1 Recursive procedure : Recursive AlgorithmsFactorial • PROCEDURE Fac(n:CARDINAL): CARDINAL; • BEGIN • IF n > 1 • THENRETURN n*Fac(n-1) • ELSERETURN 1 • END (* IF *) • END Fac;
Alternative Iterative Procedure : Recursive AlgorithmsFactorial • PROCEDURE Fac(n:CARDINAL): CARDINAL; • VAR f : CARDINAL; • BEGIN • f := 1; • WHILE n > 1DO • f := f * n ; n := n - 1 • END; (* WHILE *) • RETURN f • END Fac;
Step-up function : Fib(n) = Fib(n-1)+Fib(n-2) Trivial solution : Fib(1) = 1; Fib(0) = 0 Recursive procedure : Recursive AlgorithmsFibonaci • PROCEDURE Fib(n:CARDINAL): CARDINAL; • BEGIN • IF n > 1 • THEN RETURN Fib(n-1)+Fib(n-2) • ELSIF n=1THEN RETURN1 • ELSERETURN0 • END (* IF *) • END Fib;
Alternative Iterative Procedure : Recursive AlgorithmsFibonaci • PROCEDURE Fib(n:CARDINAL): CARDINAL; • VAR i,fn,fnm1,fnm2 : CARDINAL; • BEGINIF n = 0THEN RETURN0 • ELSIF n = 1THEN RETURN1 • ELSE fnm2 := 0; fnm1 := 1; • FOR i := 2TO n DO • fn := fnm1 + fnm2; • fnm2 := fnm1; fnm1 := fn • END; (* FOR *) • RETURN fn • END (* IF *) • END Fib;
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 Recursive AlgorithmsTowers of Hanoi
Recursive procedure : Recursive AlgorithmsTowers of Hanoi • PROCEDURE MoveTower (Height: CARDINAL • From,Towards,Using:CHAR); • PROCEDURE MoveDisk..... • BEGIN • IF Height > 0 THEN • MoveTower(Height-1,From,Using,Towards) • MoveDisk(From,Towards); • MoveTower(Height-1,Using,Towards,From) • END (* IF *) • END MoveTower;
Simultaneous activations Simple Algorithm Complex Algorithm Few ... YES Many Never ??? Recursive AlgorithmsConclusion
Generic 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
PROCEDURE Try (Row : CARDINAL); FOR Col := 1 TO 8 DO Board[Row,Col] safe ? No Yes Put queen on Board[Row,Col] (Row = 8) ? Not yet Yes Try(Row+1) Display the Board Remove queen from Board[Row,Col] Eight Queens Procedure
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; Eight Queens - Try
Eight Queens - Board Extended board for determination of safe positions
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 f END Safe;
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.
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 *)
./. n6 2075 Hz ./. n5 1925 Hz F = ? ./. n4 1625 Hz ./. n3 1475 Hz ./. n2 1025 Hz ./. n1 875 Hz Frequency Synthetizer SCM = 175 855 554 875
PROCEDURE Try(Fr : CARDINAL); FOR Fact[Fr] := MinFact[Fr] TO MaxFact[Fr] DO Acceptable factor ? No Yes Update frequency range for oscillator (Fr = 6) ? Not yet Yes Try(Fr+1) Print Factors Restore previous frequency range Frequency synthetizer Procedure
Traveling Salesman with Bactracking Select next town Total distance < 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
yh xl xr yl Recursive 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)
d x,y d Recursive Fractals(2) To draw a square of size 2d centered in x,y: PROCEDURE Box(x,y,d:CARDINAL,color); BEGIN Rectangle(x-d,y-d,x+d,y+d,color) END Box;
Recursive Fractals(3) To draw an elementary fractal box : 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);
Recursive Fractals(4) To draw a series of n fractal boxes : 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); END END FractalBox