COP3502_11_Recursion3

COP3502_11_Recursion3 - More Recursion Permutations...

This preview shows pages 1–9. Sign up to view the full content.

Computer Science Department University of Central Florida More Recursion: Permutations COP 3502 – Computer Science I

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
More Recursion: Permutations page 2 Permutations The Permutation problem: Given a list of items, List ALL the possible orderings of those items Often, we work with permutations of letters For example: Here are all the permutations of the letters CAT: The question: can we write a program to do this? CAT CTA ACT ATC TAC TCA
More Recursion: Permutations page 3 Permutations The Permutation algorithm: There are several different permutation algorithms Since recursion is an emphasis of the course, we will present a recursive algorithm to solve this Permutations of the letters CAT: CAT CTA ACT ATC TAC TCA

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
More Recursion: Permutations page 4 Permutations The Permutation algorithm: The idea is as follows: We want to list ALL the permutations of CAT So we split our work into 3 groups of permutations: 1) Permutations that start with C 2) Permutations that start with A 3) Permutations that start with T
More Recursion: Permutations page 5 Permutations The Permutation algorithm: The idea is as follows: Notice what happens: What can we say about ALL of the permutations that start with the letter C? Think about recursion… Think about the idea of wanting to reduce your problem to a smaller problem of the same form ALL of the permutations that start with the letter C, Are SIMPLY three-character strings that are formed by attaching C to the front of ALL permutations of “AT” So this is nothing but another, smaller permutation problem!!!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
More Recursion: Permutations page 6 Permutations – Recursive Calls The Permutation algorithm: The # of recursive calls needed: General “rule of thumb” for recursion: “recursive functions don’t have loops” cuz we use recursion! Either you have iteration, hence loops Or recursion…no need for loops However, this rule of thumb is just that It’s not always true One exception is this permutation algorithm
More Recursion: Permutations page 7 Permutations – Recursive Calls The Permutation algorithm: The # of recursive calls needed: Look at the example with three letters, CAT We need THREE recursive calls, one for each letter Remember, we said we split the work into three groups: 1) Permutations that start with C 2) Permutations that start with A 3) Permutations that start with T But what if we were permuting the letters of the word “computer” EIGHT recursive calls would be needed 1 for each possible starting letter

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
More Recursion: Permutations page 8 Permutations – Recursive Calls The Permutation algorithm: The # of recursive calls needed: So we see the need for a loop in our algorithm: Now, what is the terminating condition?
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 35

COP3502_11_Recursion3 - More Recursion Permutations...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online