Recursion – some examples. Sum of Squares Write Vertical.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
COSC 2006 Data Structures I Recursion III
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Factorial Recursion stack Binary Search Towers of Hanoi
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Chapter 5 Recursion as a Problem-Solving Technique.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 5.
Recursion.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Two Dimensional Arrays
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
CS1101: Programming Methodology Aaron Tan.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
CSC 211 Data Structures Lecture 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Problem-solving approaches  divide & conquer  greedy  backtracking examples: N-queens, Boggle,
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
CPS 100, Spring Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Programming Abstractions Cynthia Lee CS106X. Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Programming Abstractions
Backtracking, Search, Heuristics
Intro to Computer Science II
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Java Software Structures: John Lewis & Joseph Chase
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Programming Abstractions
searching Concept: Linear search Binary search
Programming Abstractions
Ch. 6 Recursion as a Problem Solving Technique
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Backtracking, Search, Heuristics
Presentation transcript:

Recursion – some examples

Sum of Squares

Write Vertical

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

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

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 Searching a sorted array based list

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 Searching a sorted array based list

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! Searching a sorted array based list

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…

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

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 RECURSION!!

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

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… }

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… }

Binary Search

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 Towers of Hanoi BCA

18 Towers of Hanoi BCA

19 Towers of Hanoi BCA

20 Towers of Hanoi BCA

21 Towers of Hanoi BCA

22 Towers of Hanoi BCA

23 Towers of Hanoi BCA

24 Towers of Hanoi BCA

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 Towers of Hanoi

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

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

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

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