COP3502_11_Recursion3 - More Recursion: Permutations...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida More Recursion: Permutations COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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!!!
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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?
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online