Title: Youve learned from last two classes:
1- Youve learned from last two classes
- What is recursion and how to use it
- When a big problem can be broken down into the
same problem of smaller size, it is natural to
use recursion. - Any problem that can be solved with recursion can
also be solved without it. - The overhead of using recursion extra method
calls.
2- This class - bigger examples where
- Solutions with recursion is very intuitive and
easy while solutions without recursion may be
much more complicated. - The ease to design and understand algorithms with
recursion is worth the overhead.
3Example Towers of Hanoi Three poles, some disks
of decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
4Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
5Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
6Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
7Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
8Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
9Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
10Tower of Hanoi Three poles, some disks of
decreasing sizes on the first pole, each one
smaller than the one below. Task move all disks
to the third pole Restrictions move one disk at
a time a disk cannot be put on top of a bigger
disk disks can only be moved from pole to pole.
11Solution with recursion
12Solution with recursion
Consider 4 disks can we solve it using the
solution for 3 disks? Yes ! Move top 3 disks to
the middle pole
13Solution with recursion
Consider 4 disks can we solve it using the
solution for 3 disks? Yes ! Move top 3 disks to
the middle pole move the bottom disk to the
target pole
14Solution with recursion
Consider 4 disks can we solve it using the
solution for 3 disks? Yes ! Move top 3 disks to
the middle pole move the bottom disk to the
target pole move the 3 disks to the target pole.
15- The java code with recursion
- void moveTower(int n, int pole1, int pole2, int
pole3) -
- if (n 1) // base case
- // move disk1 from pole1 to pole3
- moveDisk(1, pole1, pole3)
- else // recursion
- moveTower(n-1, pole1, pole3, pole2)
- moveDisk(n, pole1, pole3) // move
disk n - moveTower(n-1, pole2, pole1, pole3)
-
16- How to moveDisk depending on how you like to
represent the solution irrelavant to the
algorithm. - For example
- static void moveDisk(int n, int startPole, int
targetPole) -
- System.out.println(move disk n
- from pole
startPole - to pole
targetPole)
17Now think about a solution without recursion . . .
It is possible but very tricky. . . I found
research papers to develop non-recursive
algorithms for Towers of Hanoi.
18- public static void main(String args )
-
- System.out.println(" Solving the Towers
of Hanoi - Problem
") - System.out.print("Number of disks ")
- int n Stdin.readInt()
- if (nlt1) //validate the input
- System.out.println("input cannot be
negative!") - System.exit(1)
-
- moveTower(n, 1, 2, 3) //start of the
recursive call - System.out.println("\nDone!")
19- Solving the Towers of Hanoi Problem
- Number of disks 3
- move disk 1 from pole 1 to pole 3
- move disk 2 from pole 1 to pole 2
- move disk 1 from pole 3 to pole 2
- move disk 3 from pole 1 to pole 3
- move disk 1 from pole 2 to pole 1
- move disk 2 from pole 2 to pole 3
- move disk 1 from pole 1 to pole 3
- Done!
20- Example N-Queen problem (references provided
by Prof. Margaret Lamb) - Place n queens on an n x n chessboard, so that no
queen can capture another queen. - For non chess players a queen can capture any
piece on the same row, column or diagonal. - So problem becomes place queens so that no 2
queens are on same row, column or diagonal.
21As usual, to find a solution with recursion, we
must study simple cases first.
- The following animation, made by Prof. Alan
McLeod, shows the process to find a solution for
n 4.
22- Solution with recursion
- Observation from the animation
- look for a safe position in a column
- If found, place a queen and go to the next column
- If not found, backtrack to previous column
The general case for any n given c-1 queens safe
in the first c-1 columns, try to put a queen in
columns c, c1, . . . , n.
23- solveNQ(int col) //pseudocode
- for row 0 to n-1
- if row,col is a safe position
- put a queen in row, col
- call solveNQ(col1) //recursive call
- if successful
- return true
- else
- remove the queen in row, col
- //ready to try next row in col
- end-if
- end-if
- end-for
- //if we get here, no position worked
- return false
24- Method solveNQ should apply on a chess board
- static boolean solveNQ(Board B, int col)
- int size B.getSize()
- if (col gt size) return true
- for (int row 0 row lt size row)
- if (B.safePosition(row, col))
- B.putQueen(row, col)
- if (solveNQ(B, col1))
- return true
-
- else
- B.removeQueen(row, col)
- //end if-else
- //end if
- //end for
- return false
- //end solveNQ
25- Now here are what solveNQ needs from class Board
- public class Board
- private int size
- private boolean squares //true means queen
- //Constructor to create an empty n x n
- //chessboard, i.e., all squares are false
- public Board(int n) ...
- //method to put a queen on a square
- public void putQueen(int row, int column)...
- //method to remove a queen on a square
- public void removeQueen(int row, int column)
- //return the size of the chess board
- public int getSize() ...
- //print the whole chess board
- public void print() ...
- ... ...
26- One more method for the class Board
- public class Board
- ... ...
- //check if a square is safe to put a queen.
- //assume no queens to the right of this
column. - public boolean safePosition(int row, int col)
-
- //check straight to the left
- ... ...
- //check to the left and down, diagonally
- ... ...
- //check to the left and up, diagonally
- ... ...
- //if successful, return true
- ... ...
-
27- The main class to run the algorithm
- public class NQueen
- // place queens in column col to the right
- // return true if succeeded
- static boolean solveNQ(board B, int col) ...
- public static void main(String args)
- System.out.print("board size ")
- int n Stdin.readInt()
- if (n lt 1) n 1 //validate the input
- board myBoard new board(n)
- if ( solveNQ(myBoard, 0) )
- System.out.println("Found a
solution") - System.out.println()
- myBoard.print()
-
- else
- System.out.println("no solution
found") - //end main
28Again, think about a solution without recursion
for the N-Queen problem . . .
Again, non-recursive solution is possible but
complicated. You need to keep track of the
positions of queens while you proceed from column
1 to n, and programming backtracking is tricky.
29You are expected to understand Under many such
situations that the problems can be defined
recursively, using recursion makes the design of
the programs intuitive and easy, makes the
programs easy to understand, and the
non-recursive solutions may be hard to design.
Next class new topic complexity Its about how
to estimate the running time of a program.