Recursion Recitation 11/(6,7)/2008 CS 180 Department of Computer - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion Recitation 11/(6,7)/2008 CS 180 Department of Computer - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Recursion

Recitation – 11/(6,7)/2008

CS 180 Department of Computer Science, Purdue University

slide-2
SLIDE 2

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.

slide-3
SLIDE 3

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.

slide-4
SLIDE 4

Iteration vs. Recursion

Sometimes, recursions are lovely. Example:

Towers of Hanoi

slide-5
SLIDE 5

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?

slide-6
SLIDE 6

Iteration vs. Recursion

Sometimes, recursions are frustrating. Example:

Fibonacci sequence

1, 1, 2, 3, 5, 8, 13, 21, … Fn=Fn-1+Fn-2

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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?

slide-9
SLIDE 9

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.

slide-10
SLIDE 10

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?

slide-11
SLIDE 11

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.

slide-12
SLIDE 12

Examples of recursions

Binary search What’s the idea? Sample code 11.5 Use pen and paper to simulate the

recursion.

Data:

1 2 3 4 6 7 8 10 12 13 14 15 18 20 25

To find:

4, 5, 13, 19

You can also use only a loop to solve the

problem.

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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?

slide-15
SLIDE 15

Dictionary Data Structure (Tries) Dictionary Data Structure (Tries)

Store this collection of words in the memory efficient way :

{abacus, abode, dreaded, dust, dusty, planar, east} {abacus, abode, dreaded, dust, dusty, planar, east}

How to pr

  • ceed?
slide-16
SLIDE 16

We know Arrays!! We know Arrays!!

a a b c u s a

  • b

d e d e r a d d e d s u t d s u t y

= 6 slo ts = 5 slo ts = 7 slo ts = 4 slo ts = 5 slo ts

Can we use fewer slo ts? L e t’s se e ho w many re dundant slo ts we are using he re !

slide-17
SLIDE 17

Idea? 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!

slide-18
SLIDE 18

Trie Trie

Store characters in each node, not keys Use characters of the key to guide the

search process

From re

retrie trieval val, but pronounced “try”

slide-19
SLIDE 19

Applications Applications

Spell checkers Data compression Computational biology Routing tables for IP addresses Storing/Querying XML documents

… and many other

slide-20
SLIDE 20

Tries using Array of Pointers

  • Nodes marked red indicate the end of the word.
  • Each node has 26 children corresponding to 26 alphabets
slide-21
SLIDE 21

Tries: Insertion

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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