Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Tirgul no. 8 Topics covered: H Recursion: Fibonacci Factorial, GCD Backtracking – N-Queens and Knight moves.

Similar presentations


Presentation on theme: "1 Tirgul no. 8 Topics covered: H Recursion: Fibonacci Factorial, GCD Backtracking – N-Queens and Knight moves."— Presentation transcript:

1 1 Tirgul no. 8 Topics covered: H Recursion: Fibonacci Factorial, GCD Backtracking – N-Queens and Knight moves

2 2 Fibonacci (1202) Adapted from : http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances. Problem: Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was... Question: How many pairs will there be in one year?

3 3 Fibonacci Answer: At the end of the first month, they mate, but there is still one only 1 pair. At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field. At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field. At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.

4 4 Fibonacci The Rabbits problem is not very realistic! 1) It seems to imply that brother and sisters mate, which, genetically, leads to problems. We can get round this by saying that the female of each pair mates with any male and produces another pair. 2) Another problem which again is not true to life, is that each birth is of exactly two rabbits, one male and one female. Let’s look at HoneyBees – for a more realistic example!

5 5 Fibonacci – HoneyBees! In a colony of honeybees there is one special female called the queen. There are many worker bees who are female too but unlike the queen bee, they produce no eggs. There are some drone bees who are male and do no work. Males are produced by the queen's unfertilized eggs, so male bees only have a mother but no father! All the females are produced when the queen has mated with a male and so have two parents. Females usually end up as worker bees but some are fed with a special substance called royal jelly which makes them grow into queens ready to go off to start a new colony when the bees form a swarm and leave their home (a hive) in search of a place to build a new nest.

6 6 Fibonacci – HoneyBees! (cont.) So female bees have 2 parents, a male and a female whereas male bees have just one parent, a female. Let’s look at a family tree of a male drone!

7 7 Fibonacci – HoneyBees! (cont.) 1.He had 1 parent, a female. 2.He has 2 grand-parents, since his mother had two parents, a male and a female. 3.He has 3 great-grand-parents: his grand-mother had two parents but his grand-father had only one. 4.How many great-great-grand parents did he have?

8 8 Fibonacci – HoneyBees! (cont.)Great- Great- Grand- Parents Great- Great- Grand- Parents Great- Grand- Parents Grand- Parents # of 85321Male bee 138532Female bee

9 9 H We can make another picture showing the Fibonacci numbers 1,1,2,3,5,8,13,21,.. if we start with two small squares of size 1 next to each other. On top of both of these draw a square of size 2 (=1+1) and so on. Fibonacy Rectangles and Shell Spirals

10 10 H If we now draw quarter circles in each of the rectangles: H This is a spiral (the Fibonacci Spiral). H A similar curve to this occurs in nature as the shape of a snail shell or some sea shells Fibonacy Rectangles and Shell Spirals

11 11 H Fibonacci numbers can also be seen in the arrangement of seeds on flower heads H The picture here is a beautiful photograph of a Coneflower: Fibonacy Spirals: Seed Heads

12 12 H Let’s look from the top: H The same happens in many seed and flower heads in nature. The reason seems to be that this arrangement forms an optimal packing of the seeds so that, no matter how large the seed head, they are uniformly packed at any stage, all the seeds being the same size, no crowding in the centre and not too sparse at the edges. Fibonacy Spirals: Seed Heads

13 13 Fibonacci – Java Code! The Fibonacci series is defined as : Fibonacci(1) = 1 Fibonacci(2) =1 Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) Java implementation: /** * This method makes Fibonacci(n)-1 additions, not the best * way to go. */ public int recursiveFibonacci(int n) { if ((n == 1) || (n == 2)) return 1; else return(recursiveFibonacci(n-1) + recursiveFibonacci(n-2)); }

14 14 Fibonacci Improvement no. 1: Iterative solution: /** * This method makes n-1 additions. */ public int iterativeFibonacci(int n) { int prev,cur,tmp; prev = 0; cur = 1; for(int i = 1; i< n ; i++) { tmp = prev + cur; prev = cur; cur = tmp; } return cur; }

15 15 Fibonacci Improvement no. 2: closed form solution Compute the fibonacci number explicitly according to Binet’s formula: public int explicitFibonacci(int n) { double sqrtFive = Math.sqrt(5); return(int) ((1/sqrtFive)*Math.pow(((1+sqrtFive)/2),n) - (1/sqrtFive)*Math.pow(((1-sqrtFive)/2),n)); }

16 16 Factorial /** * Computes the factorial of a number. * @param n The given number. * @return n! - The factorial of n. */ public static long factorial(long n) { long multiplication = 1; for (int i=1; i<n; i++) { multiplication *= i; } return multiplication; } /** * Computes the factorial of a number. * @param n A positive integer. * @return n! - The factorial of n. */ public static long factorial(long n) { if (n==1) { return 1; } return n*factorial(n-1); }

17 17 Gcd (Greatest Common Divisor) /** * Method for computing the greatest common divisor of two numbers. * gcd(y,x) if(y>x) * gcd(x,y) = x if(y==0) * gcd(y, x % y) if x>0 */ public int gcd(int x, int y) { int xAbs = Math.abs(x); int yAbs = Math.abs(y); if(xAbs<yAbs) return gcd(yAbs,xAbs); else if(yAbs == 0) return xAbs; else return gcd(yAbs,xAbs % yAbs); }

18 18 Binary Search (recursive) /** * recursive binary search. */ public boolean find(int data[],int num,int low,int high) { int mid = (low+high)/2 ; if(data[mid] == num) return true; else if((data[mid] > num) && (low<mid)) return find(data,num,low,mid-1); else if((data[mid] mid)) return find(data,num,mid+1,high); //number isn't in the array return false; }

19 19 Polynom class /** * This class represents polynoms. * The polynoms are repersented by an array of their coefficients. * Pn(X) = A 0 *X n +A 1 *X n-1 +......+A n-1 *X+A n */ public class Polynom { private double coeff[]; /** * The constructor for a polynom. * @param an array of coefficients. */ public Polynom(double coeff[]) { this.coeff = new double[coeff.length]; System.arraycopy(coeff,0,this.coeff,0,coeff.length); } public double evaluateAt(double x) { return computeVal(x,coeff.length-1); }

20 20 Polynom cont. /** * Compute Pn(X) at x recursivly. * Pn(X) = A n +x(A n-1 + x(A n-2....+x(A 1 +xA 0 ))).....)) */ private double computeVal(double x,int deg) { if(deg == 0) return coeff[0]; else return x*computeVal(x,deg-1) + coeff[deg]; }

21 21 Recursion With Backtracking General Scheme recursiveMethod(DataType data,....) { if(end-condition) do-something; else { loop over all options { do something to the data; recursiveMethod(updated-data,....); undo what was done to the data; }

22 22 The N Queens problem Problem: Place N queens on an NxN board so that no queen threatens another. Solution: Use recursion and backtracking in order to exhaustively search for solutions.

23 23 QueensBoard class public class QueensBoard { private boolean board[][]; public QueensBoard(int size) { board = new boolean[size][size]; //board automatically initialized to false } public QueensBoard(QueensBoard original) { int size = original.getSize(); board = new boolean[size][size]; for(int i=0 ; i<size ; i++) System.arraycopy(original.board[i],0,board[i],0,size); } public int getSize() { return board.length; }

24 24 public boolean isOccupied(int row,int col) { return board[row][col] == true; } public void setQueen(int row,int col) { board[row][col] = true; } public void removeQueen(int row,int col){ board[row][col] = false; } public String toString() { StringBuffer res = new StringBuffer("*****************\n"); int size = getSize(); for(int row=0 ; row<size ; row++) { for(int col=0 ; col<size ; col++) { if(board[row][col]) res.append('X'); else res.append('O'); res.append('\t'); } res.append('\n'); } res.append("*****************\n"); return res.toString(); } QueensBoard class (cont)

25 25 PlaceQueens class public class PlaceQueens { private QueensBoard board; private boolean hasSolution; public PlaceQueens(int boardSize) { board = new QueensBoard(boardSize); hasSolution = false; }

26 26 public void solveBoard() { solve(board,0); if(!_hasSolution) System.out.println("There is no solution for board of size " + board.getSize()+"x"+board.getSize()); } private void solve(QueensBoard board, int col) { if(col == board.getSize()) { System.out.print(board); hasSolution = true; return; } for(int i=0 ; i<board.getSize() ; i++) { if(validPlacing(board,i,col)) { board.setQueen(i,col); solve(board,col+1); board.removeQueen(i,col); } PlaceQueens class (cont)

27 27 private boolean validPlacing(QueensBoard board,int row, int col) { int i,j; //check there isn't a queen in this row for(i=col-1 ; i>=0 ; i--) { if(board.isOccupied(row,i)) return false; } //check the diagonals for(i=col-1, j=row-1 ; i>=0 && j>=0 ; i--,j--) { if(board.isOccupied(j,i)) return false; } for(i=col-1, j=row+1 ; i>=0 && j<_board.getSize() ; i--,j++) { if(board.isOccupied(j,i)) return false; } //if we got here then there is no queen on the diagonals or the row return true; } PlaceQueens class (cont)

28 28 Knight Moves Problem: Starting at the lower-left corner, travel with a knight over all the chess board, without returning twice to the same place

29 29 Knight Moves - The Solution public class KnightMoves { private int[][] board; private int size; private static final int[][] MOVES= {{1,2},{1,-2},{-1,2},{-1,-2}, {2,1},{-2,1},{2,-1},{-2,-1}}; methods on next slides... }

30 30 KnightMoves - constructor public KnightMoves(int size) { int i,j; size = size; board = new int[size+4][size+4]; for(i=0; i<2; i++) { for(j=0; j < board.length; j++) board[i][j] = -1; } for(i= board.length-2; i < board.length; i++) { for(j=0; j < board.length; j++) board[i][j] = -1; } for(i=2; i < board.length-2; i++) { for(j=0; j<2; j++) board[i][j] = -1; } for(i=2; i < board.length-2; i++) { for(j=board.length-2; j< board.length; j++) board[i][j] = -1; }

31 31 KnightMoves - The Actual Work private boolean solveBoard(int row, int col, int steps) { int newRow, newCol,movesDone; if( board[row][col] != 0 ) //already been here return(false); board[row][col] = steps; if(steps == size*size) //traversed all the board return(true); for(movesDone=0; movesDone < MOVES.length; movesDone++) { newRow = row + MOVES[movesDone][0]; newCol = col + MOVES[movesDone][1]; if(solve(newRow,newCol,steps+1)) return(true); } board[row][col] = 0; return(false); } public boolean solve() { //start the recursive solving - skip the border return(solve(2,2,1)); }


Download ppt "1 Tirgul no. 8 Topics covered: H Recursion: Fibonacci Factorial, GCD Backtracking – N-Queens and Knight moves."

Similar presentations


Ads by Google