Recursion Recitation – 11/(6,7)/2008 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Recursion and Recursive Algorithms
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
HST 952 Computing for Biomedical Scientists Lecture 9.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
22C:21 Problem 4.3 Solution Outline. Trie Store characters in each node, not keys Use characters of the key to guide the search process retrievalFrom.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Design a Data Structure Suppose you wanted to build a web search engine, a la Alta Vista (so you can search for “banana slugs” or “zyzzyvas”) index say.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Contents What is a trie? When to use tries
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
Recursion what is it? how to build recursive algorithms
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.
Chapter 17 Recursion.
Mark Redekopp David Kempe
Introduction to Computer Science - Alice
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion Data Structures.
Recitation Outline C++ STL associative containers Examples
Search,Sort,Recursion.
Chapter 17 Recursion.
Running Time Exercises
Presentation transcript:

Recursion Recitation – 11/(6,7)/2008 CS 180 Department of Computer Science, Purdue University

Project 7 Now posted on the class webpage. Due Wed, Nov. 19 at 10 pm. Milestone Due Wed, Nov. 12 at 10 pm. Start early. All questions on the class newsgroup. There will be additional slides for project 7 today.

Iteration vs. Recursion Iteration:  We need to schedule everything in advance.  The compiler can only follow our instructions.  Since we are always clever, iterations are usually efficient. Recursion:  We tell the compiler how to make the problem smaller and/or which problem can be solved directly.  The compiler will generate the right process for us, but it is not so clever and may do some “meaningless” work.

Iteration vs. Recursion Sometimes, recursions are lovely. Example:  Towers of Hanoi

Iteration vs. Recursion Solution using recursion: void Hanoi (int n, char from, char dest, char by) { if (n == 1) { System.out.println(“Move the disk from ” + from + “ to “ + dest + “.”); } else { Hanoi(n – 1, from, by, dest); Hanoi(1, from, dest, by); Hanoi(n – 1, by, dest, from); } Solution using iteration?

Iteration vs. Recursion Sometimes, recursions are frustrating. Example:  Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, … F n =F n-1 +F n-2

Iteration vs. Recursion Solution using iteration: long Fibonacci (int n) { int i; long f0 = 1, f1 = 1, f2; if (n < 2) { return 1; } else { for (i = 2; i < n; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } return f2; }

Iteration vs. Recursion Solution using recursion: long Fibonacci (int n) { if (n < 2) { return 1; } else { return Fibonacci(n – 1) + Fibanacci(n – 2); } It seems simple and intriguing. Why is it very inefficient?

How to write recursions? Write the stop sign first. Think twice before you use a loop. Remember the differences between iterations and recursions. Take care of the order of your statements. Make good use of the parameters and notice their scopes. Try to simulate what your program may act like before letting it run.

How to write recursions? The importance of the statement order: void printSequence (int n) { if (n > 0) { printSequence(n – 1); System.out.println(n); } void printSequence (int n) { if (n > 0) { System.out.println(n); printSequence(n – 1); } What’s the stop sign?

How to write recursions? Practice the ability to cut a big problem into small pieces, which is essential to programming and helps solve all kinds of problems. Typically, cut a problem into two halves, solve them separately and then solve the whole problem. Programming with recursions can show this way of thinking explicitly.

Examples of recursions Binary search What’s the idea? Sample code Use pen and paper to simulate the recursion. Data: To find: 4, 5, 13, 19 You can also use only a loop to solve the problem.

Examples of recursions Merge sort – A recursive sorting method A divide-and-conquer algorithm  Array to be sorted is divided in half  The two halves are sorted by recursive calls  This produces two smaller, sorted arrays which are merged to a single sorted array Use an example to explain the idea

Examples of recursions Brief code: void mergeSort (int start, int end) { if (start < end) { mergeSort(start, (start + end) / 2); mergeSort((start + end) / 2 + 1, end); merge(start, end); } Advanced topic: How to write the merge method?

Dictionary Data Structure (Tries) Store this collection of words in the memory efficient way : {abacus, abode, dreaded, dust, dusty, planar, east} How to proceed?

We know Arrays!! aabcus aobde deradde dsut dsuty = 6 slots = 5 slots = 7 slots = 4 slots = 5 slots Can we use fewer slots? Let’s see how many redundant slots we are using here!

Idea? Can we come up with a solution where we can take advantage of the common letters in the words? It would be great if we can reduce the redundancy, and use the same letter for more than one word!

Trie Store characters in each node, not keys Use characters of the key to guide the search process retrieval From retrieval, but pronounced “ try ”

Applications Spell checkers Data compression Computational biology Routing tables for IP addresses Storing/Querying XML documents … and many other

Tries using Array of Pointers Nodes marked red indicate the end of the word. Each node has 26 children corresponding to 26 alphabets

Tries: Insertion

Tries: Project 7 requirements Insert – Load all words from words.txt (already implemented, you just have to read words from file and call insert of Trie.java) Delete – Change the marker at the end of the word to green from red Search – Traverse down the Trie recursively with each letter of the search word. If the end node is marked red, return success

Tries: Project 7 requirements Prefix Search - Find all words in the Trie which start with a given prefix (E.g. prefix : “ca” ; Output: print words car, cane, care) Jumble Search - Find the longest possible word present in the Trie that is formed from the given jumbled string (Depth-First-Search) E.g., jumble string: “bcgylonaa”; Output: analogy