Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion – some examples. Sum of Squares Write Vertical.

Similar presentations


Presentation on theme: "Recursion – some examples. Sum of Squares Write Vertical."— Presentation transcript:

1 Recursion – some examples

2 Sum of Squares

3 Write Vertical

4 LinkedList contains “A” “B” “C” “D” head tail

5 Searching a sorted array based list How long does it take us to find a number we are looking for? 012345678910 2781325293351899095

6 How long does it take us to find a number we are looking for? If you start at the front and proceed forward, each item you examine rules out 1 item 012345678910 2781325293351899095 Searching a sorted array based list

7 If instead we jump right to the middle, one of three things can happen: 1.The middle one happens to be the number we were looking for, yay! 2.We realize we went too far 3.We realize we didn’t go far enough 012345678910 2781325293351899095 Searching a sorted array based list

8 If instead we jump right to the middle, one of three things can happen: 1.The middle one happens to be the number we were looking for, yay! 2.We realize we went too far 3.We realize we didn’t go far enough Ruling out HALF the options in one step is so much faster than only ruling out one! 012345678910 2781325293351899095 Searching a sorted array based list

9 Binary search Let’s say the answer was 30, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward… 012345678910 2781325293351899095

10 Binary search Let’s say the answer was 30, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward…but why do that when we know we have a better way? Jump right to the middle of the region to search 012345678910 2781325293351899095

11 Binary search Let’s say the answer was 3, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward…but why do that when we know we have a better way? Jump right to the middle of the region to search 012345678910 2781325293351899095 RECURSION!!

12 To write a recursive function, we need base case(s) and recursive call(s) What would be a good base case for our Binary Search function? A.Only three items remain: save yourself an unnecessary function call that would trivially divide them into halves of size 1, and just check all three. B.Only two items remain: can’t divide into two halves with a middle, so just check the two. C.Only one item remains: just check it. D.No items remain: obviously we didn’t find it. E.More than one

13 Binary Search public static > boolean binarySearch(T[] data, int key){ return binarySearch(data, key, 0, data.size()-1); } public static binarySearch(T[] data, int key, int start, int end){ // Write code to handle the base case “No items remain: // obviously we didn’t find it” by returning false (A)if (data.size() <= 0) return false; (B)if (end-start <= 0) return false; (C)Other/none/more //to be continued… }

14 Binary Search public static > boolean binarySearch(T[] data, int key){ return binarySearch(data, key, 0, data.size()-1); } public static binarySearch(T[] data, int key, int start, int end){ if (end-start <= 0) return false; int middle = start + (end-start)/2; if (data[middle] == key) return true; else if (data[middle] > key) { (A)return binarySearch(data,key,middle+1,end); (B)return binarySearch(data,key,middle,end-1); (C)return binarySearch(data,key,start,middle-1); (D)Other/none/more } // to be continued… }

15 Binary Search

16 16 Towers of Hanoi Many, many years ago, in a distant part of the Orient -- in the Vietnamese city of Hanoi -- the Emperor devised a puzzle, declaring that its solver could have the job of wise person. The puzzle consisted of N disks and three poles: A (the original), B (the destination), and C (the temp)

17 17 Towers of Hanoi BCA

18 18 Towers of Hanoi BCA

19 19 Towers of Hanoi BCA

20 20 Towers of Hanoi BCA

21 21 Towers of Hanoi BCA

22 22 Towers of Hanoi BCA

23 23 Towers of Hanoi BCA

24 24 Towers of Hanoi BCA

25 25 Towers of Hanoi The strategy for moving n disks from Origin to Destination is: if n is 1, Move 1 disk from Origin to Destination Otherwise, Move n-1 disks (one at a time) from Origin to Temporary Move disk n from Origin to Destination Move n-1 disks (one at a time) from Temporary to Destination

26 26 Towers of Hanoi

27 27 Eight Queens Problem A chessboard contains 64 squares that form 8 rows and 8 columns. The Eight Queens problem asks you to place eight queens on the chess board so that no queen can attack any other queen. Uses a technique called “ backtracking ” which uses recursion to try all the possibilities

28 28 1 2 3 4 5 6 7 8

29 29 1 2 3 4 5 6 7 8

30 30 1 2 3 4 5 6 7 8

31 31 1 2 3 4 5 6 7 8

32 Eight Queens Backtrack Algorithm //first recursive call solution = eightQueens(board, 0) public boolean eightQueens(board, col) if (col == 8) return true for (row = 0; row<8; row ++) if ( canPlaceQueen(row, col) ) { board[row][col]=‘Q’ if ( eightQueens(board, col+1) ) return true board[row][col]=‘OPEN’ //backtrack } return false

33 Lab 7 Boggle -- Overview Load Boggle Board from a file Load a dictionary from a file Display board Prompt Player for a word While (word != empty) – Verify the word is in the dictionary Binary Search – Verify the word is in the Boggle board Backtrack search – Record player score – Display board – Prompt Player for a word AEHX AITN SECE AWTB

34 Lab 7 Boggle -- Before Lab Create an eclipse project and download – words.txt – board.txt Write down a design – using a UML class diagram with methods Design is up to you, but the lab includes suggestions – Dictionary Class – BoggleBoard Class Recursive formula for Boggle board search AEHX AITN SECE AWTB


Download ppt "Recursion – some examples. Sum of Squares Write Vertical."

Similar presentations


Ads by Google