CSS 342 Data Structures, Algorithms, and Discrete Mathematics I

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Backtracking.
CSS342: Recursion1 Professor: Munehiro Fukuda. CSS342: Recursion2 Topics Day 1: Lecture –Basic recursions 1.Sum 2.Fact 3.Print a number in any base 4.Binary.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
1 Recursion. 2 Basic Recursions Break a problem into smaller identical problems –Each recursive call solves an identical but smaller problem. a base caseStop.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
CSC 211 Data Structures Lecture 13
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
CSC 205 Programming II Lecture 9 More on Recursion.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Recursion – some examples. Sum of Squares Write Vertical.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Programming with Recursion
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 14 Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 5 Recursion as a Problem-Solving Technique
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion: The Mirrors
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Analysis of Algorithms
Towers of Hanoi Move n (4) disks from pole A to pole C
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSCI 104 Backtracking Search
Chapter 19 Recursion.
Data Structures and 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"?
CSE 143 Lecture 19 More Recursive Backtracking
More Recursion.
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Algorithm Design and Analysis (ADA)
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CSCE 411 Design and Analysis of Algorithms
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion: The Mirrors
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Unit-2 Divide and Conquer
CSE 1342 Programming Concepts
Searching and Sorting Arrays
Recursion.
Searching: linear & binary
adapted from Recursive Backtracking by Mike Scott, UT Austin
RECURSION Annie Calpe
Basics of Recursion Programming with Recursion
Recursion: The Mirrors
Recursion: The Mirrors
CSE 143 Lecture 18 More Recursive Backtracking
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
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"?
Chapter 5 Recursion.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 7. 161020 CARRANO Carrano Ch 5.3-5.4

Agenda HW2 Questions Time of Invocation Recursion Quiz

Time of Invocation (constructors) Automatic Local Each time block is executed Static Local Once –first time it is hit Global In order of declaration in translation unit Typically before main() is entered Destroyed in reverse order of construction Dynamic (tbd) malloc/free new/delete

Recursion Recursion

Binary Search An entire sorted list First half Second half First half Find a value in an array.

Binary Search Base Case: Recursive: low or high index finds number Determine midpoint (high+low)/2 Search either lower or upper array

Binary Search int BinarySearch(int value, int arr[], int index1, int index2) { if (value == arr[index1]) return index1; else if (value == arr[index2]) return index2; else if (index2 <= index1 + 1) return -1; else int midpoint = (index1 + index2) / 2; if (arr[midpoint] > value) return BinarySearch(value, arr, index1, midpoint); return BinarySearch(value, arr, midpoint, index2); }

Recursion w/Alice https://www.youtube.com/watch?v=-3Mcu7o1YSg

Choosing k out of n. A child can have one sweet a day while on vacation. The vacation lasts k days and there are n total different types of sweets. How many different combination of sweets can the child enjoy assuming order is not important and the child does not repeat sweets. (k) n Let’s use recursion to determine the answer.

Choosing k out of n. Hint: think of an array of n items; item i is either in chosen (k) or not. Recursion: f(n, k) = f(n-1, k-1) + f(n-1, k) Possible Base cases: f(n,0) = 1 f(n, 1) = n f(n,n) = 1

int KOutOfN(int n, int k) { if (k == 0) return 1; } else if (k == n) else return KOutOfN(n - 1 , k) + KOutOfN(n - 1, k - 1);

Quiz Best Wishes

CLASS BELL

Typical types of recursion Returns Value (n!, …) Performs an action (adds to a string, … Divides in half recursively (binary search, … Tail Recursion Backtracking (will introduce in future slides)

Costs of Recursion Main cost of recursion is memory usage on stack Stack v. Heap Compiler chooses where to allocate space from. Not always clear and compiler can make optimizations. Stack is per thread and limited. When space runs out: Stack Overflow. Function calls place locals, call parameters on stack Heap gets calls to malloc/free; new/delete generally on heap

Review: The Box Trace. Memory usage on thread stack! FIGURE 2-5 Box trace of fact(3) … continued Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Backtracking as a problem solving technique While (solution not complete) { if (in good state) “move forward guessing at solution” else “backtrack to last good state” } Often used in combination with Recursion

Backtrack: 8 queens problem Place 8 queens on a 8 * 8 chessboard so that no queen can attack any other queen.

Place one queen in one column at a time. Backtrack when problems occur. *from Carranno

8 Queens: Pseudo-code bool addQueen( bool t[SIZE][SIZE], int col ) { if ( col >= SIZE ) return true; // all cols have been examined for ( int row = 0; row < SIZE; row++ ) { if ( safeLocation( t, row, col ) ) { // this row may be a candidate t[row][col] = true; // place a new queen; if ( addQueen( t, col + 1 ) ) return true; // all the following cols were filled else t[row][col] = false; // A wrong position. Try the next row } return false; // all rows examined, but no candidates

Recursive Applications Discrete Mathematics (Combinatorics, Puzzles, Coding Theory) Tower of Hanoi and Gray Code Divide and Conquer Mergesort, Convex Hall, and Fast Fourier Transform Backtrack 8 Queens, Maze and Classic Chess Program Fractal Figures Koch, Sierpinski Allowhead, Gosper, Hilbert, and Dragon curves