CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 6. 150126. CARRANO CARRANO CH 5.3-5.4; CUSACK CH 8: OPTIONAL.

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.
Introduction to Recursion and Recursive Algorithms
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.
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
Induction and recursion
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Recursion. 2 Basic Recursions Break a problem into smaller identical problems –Each recursive call solves an identical but smaller problem. a base caseStop.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
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.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT 12.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
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 CUSACK CHAPT 4.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 6 Questions Quick Quiz
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Recursion – some examples. Sum of Squares Write Vertical.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion CENG 707.
Chapter 5 Recursion as a Problem-Solving Technique
Recursion: The Mirrors
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
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
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion as a Problem-Solving Technique
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL

Agenda Recursion HW2 Peer Design Review C++: Dynamic allocations, Vectors Intro to Induction

Recursive Solutions Recursion breaks a problem into smaller identical problems Base Case Call into current function Some recursive solutions are inefficient, impractical Complex problems can have simple recursive solutions DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

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

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

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); else return BinarySearch(value, arr, midpoint, index2); } Binary Search

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) { return 1; } else { return KOutOfN(n - 1, k) + KOutOfN(n - 1, k - 1); }

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)

Peer Design Reviews for HW2

Costs of Recursion/when to use 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

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

Dynamic Allocation 1. Pointer declarationint *p, *q; 2. Dynamic allocationp = new int; q = new int; 3. De-allocationdelete p; p = NULL; 4. Memory leakq = new int; ? ? ? p q p q p q p q ??? ? NULL Leak!

Dynamic Allocation Works with all Object types Bird *pBigBird pBigBird = Bird(“chicken”); string s = pBigBird->species;

Computer Scientist of the week Donald Knuth Born Milwaukee, WI Professor Emeritus at Stanford Father of Analysis of Algorithms Formalization of mathematical study of complexity of programs Wrote “Art of Computer Programming”, the definitive text on computer algorithms. Creator of TeX Won Grace Hopper Award, Turning Award, John von Neumann Medal. Opposed to granting computer patents

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

For practice… A Catalan number is defined by the following recursive formula: Write a recursive function which computes the nth Catalan number.

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

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

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

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 }

Induction Axiom: The principle of mathematical induction A property P(n) that involves an integer n is true for all n ≥ 0 if the following are true: 1.P(0) is true. 2.If P(k) is true for any k ≥ 0, then P(k+1) is true.

Intuitive Reasoning about proofs by induction Imagine a monkey climbing a ladder Teach how to go from one rung to another rung: K to K+1 Teach how to get onto Rung 1 Imagine a set of dominoes The k numbered domino knocks over the k+1 domino Knock over the first domino Syllabus updates: Cusack book optional for induction, but helpful