Thinking Recursively

2y ago
81 Views
2 Downloads
357.76 KB
138 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Evelyn Loftin
Transcription

Thinking Recursively

An Interesting Read“How to Train Your /how-to-train-your-robot/Teaching kids to program by having themprogram their parents!

Thinking Recursively

Recursive Problem-Solvingif (problem is sufficiently simple) {Directly solve the problem.Return the solution.} else {Split the problem up into one or more smallerproblems with the same structure as the original.Solve each of those smaller problems.Combine the results to get the overall solution.Return the overall solution.}

int digitalRoot(int value);int sumOfDigits(int value);int sumOfDigits(int value) {if (value 0) {return 0;} else {return sumOfDigits(value / 10) (value % 10);}}int digitalRoot(int value) {if (value 10) {return value;} else {return digitalRoot(sumOfDigits(value));}}

An Interesting ListenThis American Life:“Take The Money and Run For ives/episode/461/take-the-moneyand-run-for-office

Federal Campaign LimitsSource: html

Federal Campaign LimitsSource: html

Raising Money According to several sources(Bloomberg, the Wall Street Journal,Politico, etc.), the 2008 PresidentialElection cost about 1.5 billion.How can you raise that much moneyfrom private donors?

Raising Money 22,500 7,500 2,500 2,500 7,500 2,500 2,500 2,500 7,500 2,500 2,500 2,500 2,500

Recursive Problem-Solvingif (problem is sufficiently simple) {Directly solve the problem.Return the solution.} else {Split the problem up into one or more smallerproblems with the same structure as the original.Solve each of those smaller problems.Combine the results to get the overall solution.Return the overall solution.}

Recursive Problem-Solvingif (n is at most 2,500) {Directly solve the problem.Return the solution.} else {Split the problem up into one or more smallerproblems with the same structure as the original.Solve each of those smaller problems.Combine the results to get the overall solution.Return the overall solution.}

Recursive Problem-Solvingif (n is at most 2,500) {Donate n} else {Split the problem up into one or more smallerproblems with the same structure as the original.Solve each of those smaller problems.Combine the results to get the overall solution.Return the overall solution.}

Recursive Problem-Solvingif (n is at most 2,500) {Donate n} else {Find three other people.Tell them each to raise n / 3.}

What's Going On? Recursion solves a problem bycontinuously simplifying the problemuntil it becomes simple enough to besolved directly.The recursive decomposition makesthe problem slightly simpler at each step.The base case is what ultimately makesthe problem solvable – it guarantees thatwhen the problem is sufficiently simple,we can just solve it directly.

The Towers of Hanoi Problem

Towers of HanoiABC

Towers of HanoiMoveMovethisthistower.tower.ABC

Towers of pindle.spindle.C

Towers of pindle.spindle.C

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Towers of HanoiABC

Solving the Towers of Hanoi

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of HanoiABC

Solving the Towers of HanoiABC

Solving the Towers of HanoiABC

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of HanoiABC

Solving the Towers of HanoiABC

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

Solving the Towers of roverhere.here.

The Recursive Decomposition To get a tower of N 1 disks from spindleX to spindle Y, using Z as a temporary: Recursively move the top N disks fromspindle X to spindle Z, using Y as atemporary.Move the N 1st disk from X to Y.Recursively move the N disks from spindleZ to spindle Y, using X as a temporary.

The Base Case We need to find a very simple case thatwe can solve directly in order for therecursion to work.If the tower has size one, we can justmove that single disk from the source tothe destination.

And now, the solution.

void moveTower(int n, char from, char to, char temp) {if (n 1) {moveSingleDisk(from, to);} else {moveTower(n – 1, from, temp, to);moveSingleDisk(from, to);moveTower(n – 1, temp, to, from);}}

intint main()main() {{moveTower(3,moveTower(3, 'a','a', 'c','c', 'b');'b');}}ABC

intint main()main() {{moveTower(3,moveTower(3, 'a','a', 'c','c', 'b');'b');}}ABC

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromaBtocCtempb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromaBtocCtempb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromaBtocCtempb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromaBtocCtempb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromcBtobCtempa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromcBtobCtempa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromcBtobCtempa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else ingleDisk(from,to); to);moveTower(n– {1, from, temp,if(n 1)}moveSingleDisk(from,else{if(n 1){to);}moveSingleDisk(from,else moveSingleDisk(from,{to); lse {}}to); temp, rom);moveTower(n– temp,1, eDisk(from,to);}}moveSingleDisk(from, to);nfrom a–– 1,to btemp cmoveTower(n3}}moveTower(n1, temp,temp, to,to, from);from);}}nfrom ato ctemp b2}}n1AfromcBtobCtempa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAatoBbtempCc

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{ifif (n(n 1)1) {{moveSingleDisk(from,moveSingleDisk(from, to);to);}} else{else {moveTower(nmoveTower(n –– 1,1, from,from, temp,temp, m, to);moveTower(nmoveTower(n –– 1,1, temp,temp, to,to, from);from);}}}}n3fromAatoBctempCb

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAbtoBctempCa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAbtoBctempCa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAbtoBctempCa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{}} else{if(n 1)else Disk(from,else {to); temp, rom);moveTower(n– temp,1, isk(from,to);}}moveSingleDisk(from, to);moveTower(n}}moveTower(n –– 1,1, temp,temp, to,to, from);from);}}nfrom ato btemp c3}}n2fromAbtoBctempCa

intint main()main() );moveTower(intn,char}} voidvoid moveTower(int n, char from,from, charchar to,to, charchar temp)temp) {{if(n 1){{if(n charto,chartemp)moveSingleDisk(from,to);if(n 1){{

An Interesting Read “How to Train Your Robot” http://drtechniko.wordpress.com/2012/04/09/how-to-

Related Documents:

Critical Thinking Skills vs. Critical Thinking Disposition Critical Thinking Skills are the cognitive processes that are involved in critical thinking Critical Thinking Disposition is the attitudes, habits of mind or internal motivations that help us use critical thinking skills.

The Role of Critical Thinking in Problem Analysis Brian D. Egan, M.Sc., MBA, PMP Introduction Contrary to what the name implies, critical thinking is not thinking that is critical of others. It is “fundamental” or “vital” thinking. Critical thinking is thinking that drills down to the essence of a problem. It is introspective

2.2 Application of Critical Thinking in Nursing Practice 2.3 Traits of the Critical Thinker 2.4 Pitfalls in Critical Thinking 2.5 Critical Thinking Models 2.6 Critical Thinking Skills 2.6.1 Six Core Thinking Skills 2.6.2 Critical Thinking Skills in Nursing 2.6.3 Elements of Thoughts and the N

Organises the thinking "Thinking about the thinking" Sets the focus: Defines the problems and shapes the questions Mindwerx International mindwerx.com Manages the use of other hats Ensures Parallel Thinking rules are observed Summaries, overviews and conclusions de Bono's Six Thinking Hats Managing the Thinking Process

The Six Thinking Hats[6-10] Six Thinking Hats is a book by Edward de Bono which describes a tool for group discussion and individual thinking involving six colored hats. "Six Thinking Hats" and the associated idea parallel thinking provide a means for groups to plan thinking processes in a detailed and cohesive way, and in

F-IF.3 Recognize that sequences are functions, sometimes defined recursively, whose domain is a subset of the integers. For example, the Fibonacci sequence is defined recursively by f(0) f(1) 1, f(n 1) f(n) f(n – 1) for n 1. 8.1, 8.3 Interpret func

(Exercises for Chapter 9: Discrete Mathematics) E.9.2 9) Consider the sequence recursively defined by: k a 1 4 a 1 a k 10 ()k , k 1 . Find a 1, a 2, a 3, and a 4. This sequence is an arithmetic sequence, which we will discuss further in Section 9.2. (F) 10) Consider the sequence recursively defined by: a 1 2 a k 1 1 2 a k ()k , k 1 .

Design Thinking work and be effective in the banking sector is an open question. DESIGN THINKING VERSUS TRADITIONAL BUSINESS THINKING We will put forward what we believe are the key differences are between Design Thinking and traditional business thinking. Broadly, these differences can be grouped into three main categories: