Trouble understanding recursion (Python)

Then you can try and replace the four "koch..." functions with just one function. In summary, the function koch(t,n) uses recursion to repeatedly call itself until it reaches the base case (n<3). It then returns and the execution continues, with more koch calls and more recursions. The recursion tree for this function can be visualized as a tree with branches and nodes, until the base case is reached and the tree bottoms out.
  • #1
mrcleanhands
I'm trying to understand a function which draws a koch curve using a library which draws (lt = left turn, fd = move forward, t represents an object class).

Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

So let's say I start with n = 60, then m=20 and it executes koch(t, 20), but it will never get below koch(t,m) as if the number is above 3 then it keep returning to execute itself again. This is the way I see the code:
Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
   DEAD CODE...

I'm confused what happens at this point. Does it continue executing the code right until the last line and then executes each koch(t,m) call separately?
 
Technology news on Phys.org
  • #2
That's what it looks like to me. Have you ran it?
 
  • #3
If n < 3, then koch() calls fd(), not koch(). Otherwise it sets m = n / 3, and makes calls to koch(m,...) (so m will eventually be < 3).
 
  • #4
mrcleanhands said:
I'm trying to understand a function which draws a koch curve using a library which draws (lt = left turn, fd = move forward, t represents an object class).

Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

So let's say I start with n = 60, then m=20 and it executes koch(t, 20), but it will never get below koch(t,m) as if the number is above 3 then it keep returning to execute itself again.


This is the way I see the code:
Code:
def koch(t, n):
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
   DEAD CODE...

I'm confused what happens at this point. Does it continue executing the code right until the last line and then executes each koch(t,m) call separately?

If n<3 is your "base case". It is not defined here what fd() does, but the important thing is that the recursion tree for Koch() stops here. During the execution of Koch() n will never grow, so you are guaranteed that the base case will be hit at some point.

The code below koch(t,m) is not dead code! The recursion tree for Koch(t,m) just needs to bottom out and hit its own base case before returning. Once it does the execution continues as expected, with more Koch() calls and more recursions. They will eventually all stop though.

Edit: To help you visualize what happens, draw a tree for the execution of Koch() with relatively simple values.

Let's say we start with n = 18. Your tree will have a top-most node, where n = 18. Below it will be four nodes connected with lines, each with n = 18/3 = 6. Now each of those four nodes will again have four children (so 4*4=16 children at level 2) and n = 2. Now the tree stops, since n < 3.

To follow the program flow, start at the top and trace the tree down. Start with the leftmost child of the root. Since this child has a leftmost child of its own, trace that line down too. Now you should be at level 2 of the tree in the far left part. What happens now is that the program hits its base case, and starts returning again. So you trace up one node, and follow the line of the second child (the right next to the one you visited before).
This continues until all the child nodes have been visited, in which case you trace back up one level and continue from there with the rest of the un-visited child nodes.
 
Last edited:
  • #5
I think you are confusing "calling the function recursively" with "jumping back to the top of the function". They are not the same thing.

You might want to try running something like this (I'm not a Python user so apologies for any syntax errors!)
Code:
def koch18(t, n):
    m = n/3.0
    koch6(t, m)
    lt(t, 60)
    koch6(t, m)
    rt(t, 120)
    koch6(t, m)
    lt(t, 60)
    koch6(t, m)
    return

def koch6(t, n):
    m = n/3.0
    koch2(t, m)
    lt(t, 60)
    koch2(t, m)
    rt(t, 120)
    koch2(t, m)
    lt(t, 60)
    koch2(t, m)
    return

def koch2(t, n):
   fd(t, n)
   return
That code is not recursive. Put some "print" statements so you can see how works when you do "koch18(t,18)".

That version of the code isn't very clever, because obviously "koch18" and "koch6" are almost exactly the same. The point of the recursive version is that you can replace them (and every other "koch..." function) with just ONE function, which calls itself.

Put some print statements in the original version, and compare what "koch(t, 18)" and "koch18(t,18)" do.
 

Related to Trouble understanding recursion (Python)

What is recursion in Python?

Recursion in Python is a programming technique where a function calls itself repeatedly until a certain condition is met. This allows for a solution to a problem to be broken down into smaller sub-problems, making it easier to solve.

Why is it important to understand recursion in Python?

Understanding recursion in Python is important because it allows for more efficient and elegant solutions to certain problems. It also helps in understanding other complex programming concepts such as data structures and algorithms.

What are the advantages of using recursion in Python?

Some advantages of using recursion in Python include simpler and cleaner code, the ability to solve complex problems more easily, and the potential for better performance in certain scenarios.

What are the common challenges when using recursion in Python?

One common challenge when using recursion in Python is understanding and managing the flow of the recursive function, as it can be difficult to visualize and debug. Other challenges include the potential for stack overflow errors and the possibility of creating an infinite loop.

How can I improve my understanding of recursion in Python?

To improve your understanding of recursion in Python, it is important to practice writing recursive functions and solving problems using recursion. You can also read articles and tutorials, watch videos, and seek help from more experienced programmers.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
11
Views
829
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
926
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top