Recursion COMP 112 2017T1 #6.

Slides:



Advertisements
Similar presentations
CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Advertisements

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Recursion: Overview What is Recursion? Reasons to think recursively? An Example How does recursion work?
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
New Mexico Computer Science For All Sorting Algorithms Maureen Psaila-Dombrowski.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
Chapter 16: Searching, Sorting, and the vector Type.
Recursion To understand recursion, you first have to understand recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Lecture 10 CS Sorting Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. First, sorting.
Algorithm Analysis 1.
Merge Sort.
Advanced Sorting.
Using recursion for Searching and Sorting
Chapter 16: Searching, Sorting, and the vector Type
Big-O notation.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Chapter 15 Recursion.
Decrease-and-Conquer Approach
Chapter 15 Recursion.
COMP108 Algorithmic Foundations Divide and Conquer
Introduction to complexity
Divide-and-Conquer 6/30/2018 9:16 AM
Section 10.3a Merge Sort.
Sorting by Tammy Bailey
Applications of Recursion
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Teach A level Computing: Algorithms and Data Structures
Divide and Conquer.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithm design and Analysis
CSC212 Data Structure - Section RS
“Human Sorting” It’s a “Problem Solving” game:
Sorting Algorithms Ellysa N. Kosinaya.
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
Searching CLRS, Sections 9.1 – 9.3.
Sub-Quadratic Sorting Algorithms
Recursion.
Basics of Recursion Programming with Recursion
Chapter 11 Recursion.
Algorithm Efficiency and Sorting
Divide-and-Conquer 7 2  9 4   2   4   7
CSC 143 Java Sorting.
Last Class We Covered Recursion Stacks Parts of a recursive function:
CS203 Lecture 15.
“Human Sorting” It’s a “Problem Solving” game:
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Recursion COMP 112 2017T1 #6

Overview What is recursion? How dose iteration relate to recursion? Why use recursion? Make some problems are easier to solve Makes some problems quicker to solve Recursion is more important now we have Java 8 Ideally you should be able to decide if recursion is appropriate or not for the problems that you have to solve. Functions over infinite domains frequently need recursion.

Towers of Hanoi (run demo) Goal is to move discs to the right hand place Rules: Move only one disc at a time Do not place any disc on top of a smaller disc Try to design an algorithm that will work no mater how many discs there are but with out recursion (using iteration)

Recursion – what is it. A recursive method is a method that calls itself. Even with out recursion Java is functional complete. That is you do not need recursion to solve any problem. But thinking recursively can greatly help. Java maintains a call stack – see BlueJ debugging Calling method Move adds Move to the call stack If method Move calls itself a new call to Move is added to the stack before the initial call is removed

Divide and conquer A classic problem solving metaphor is The size of the TofH problem is n the number of discs. A solution for TofH-n can be constructed assuming a solution for TofH-(n-1). ----- Meeting Notes (16/03/14 10:52) ----- WE HAVE BROKEN DOWN A 4 DISC MOVE into two 3 disc moves and a one disc move ALSO we have broken a 3 disc move into two 2 disc moves and a one disc move M L R n = 4 Move 3 L discs to M Move 1 L disc to R Move 3 M discs to R

A constructive approach Divide and conquer decomposes a large problem into smaller problems Alternatively use solution to smaller problems to build solutions to larger problems Solution with 2 discs - Move 1 disc L2M and 1 disc L2R and 1 disc M2R Solution with 3 discs - Move 2 discs L2M and 1 disc L2R and 2 discs M2R Solution with 4 discs - Move 3 discs L2M and 1 disc L2R and 3 discs M2R See the pattern? Solution with n discs - Move n-1 discs L2M and 1 disc L2R and n-1 discs M2R

Plus a lot of boiler plate. Towers of Hanoi This problem can be solved in three lines of recursive code. Plus a lot of boiler plate. Note the only method that actually moves any disc is moveonedisc(); Method move() sets up when moveonedisc()is called.

Recursion for design and speed Hanoi recursion for easy of design Sorting recursion for speed

Big O notation - Order n For an array with n elements The algorithm to: read each element one at a time is O(n) read each element and then for each element read each element again is O(n2) As n becomes large n2 becomes very very large

Problem Size - Big O notation Given a 2D square array with n cells on each side. Adding the elements in each of the n2 cells requires n2 additions. complexity O(n2) “order n2” Adding the cells in the top right triangle cells requires n2/2 additions. If further you add one row of cells you have performed n2/2+n additions. We ignore both the constants ½ lower order factors +n

Divide and conquer for Speed Find an element in a sorted list of size n Look at each element in turn until you find the element. complexity O(n) Divide the list into two parts and search one of the shorter lists. Which depends on the value at the split. Do this recursively until there is one element. How many recursive calls can you make? n = (1/2)r or log(n) = r complexity O(log(n)) What is this log(n)? How much faster is log(n)/n ?

Log(n) for large n (data sizes) data with 1,099,511,627,776 elements can only be split in two 40 times. Recursive call stack depth 40. O(log(n)) algorithms become very very much faster than O(n) algorithms This is truly algorithm efficiency that is worth the effort! recursively splitting a problem in 2 can be VERY efficient m 2m 1 2 4 3 8 16 5 32 6 64 7 128 256 9 512 10 1024 15 32,768 20 1,048,576 30 11,073,741,824 40 1,099,511,627,776 log n n

Sort Bubble sort is of complexity O(n2) Repeat until no element switched For each element e=list[i] in list If e=list[i+1] switch e and list[i+1] Using divide and conquer design a faster O(nlog(n)) algorithm! Merge sort: Recursively split unsorted list into two parts until list is a one element sorted list. Then merge sorted lists and return.

Merge Sort (Wikipedia) 23 = 8 1 2 3

Summary Recursive methods call them selves Recursion can be used To solve some problems cleanly To solve some problems quickly Recursion can be implemented with iteration and a todo list. That is hand code the call stack.