Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding and visualizing recursion

I referred to several questions here about recursion but I am not able to understand how recursion works for this particular problem: Recursive program to get all combination of characters in a string in Python:

st= []
def combi(prefix, s):
    if len(s)==0: return 
    else:
        st.append(prefix+s[0])        

        ''' printing values so that I can see what happens at each stage '''
        print "s[0]=",s[0]
        print "s[1:]=",s[1:]
        print "prefix=",prefix
        print "prefix+s[0]=",prefix+s[0]
        print "st=",st

        combi(prefix+s[0],s[1:])
        combi(prefix,s[1:])
        return st

print combi("",'abc')

I've made it print the values so that I can see what's happening. This is the output:

s[0]= a
s[1:]= bc
prefix= 
prefix+s[0]= a
st= ['a']
s[0]= b
s[1:]= c
prefix= a
prefix+s[0]= ab
st= ['a', 'ab']
s[0]= c
s[1:]= 
prefix= ab
prefix+s[0]= abc
st= ['a', 'ab', 'abc']
s[0]= c
s[1:]= 
prefix= a  ----> How did prefix become 'a' here. Shouldn't it be 'abc' ? 
prefix+s[0]= ac
st= ['a', 'ab', 'abc', 'ac']
.........
.........
['a', 'ab', 'abc', 'ac', 'b', 'bc', 'c'] # final output

Full output: http://pastebin.com/Lg3pLGtP

As I've shown in the output, how did prefix become 'ab'?

I tried to visualize the recursive calls for the combi(prefix+s[0],s[1:]). Am I understanding it right? Visualization of Recursion

like image 916
Bharat Avatar asked Feb 07 '12 03:02

Bharat


People also ask

Why is understanding recursion important?

Answer 4fd765800ef82b00030244ea. Recursive thinking is really important in programming. It helps you break down bit problems into smaller ones. Often, the recursive solution can be simpler to read than the iterative one.

How do you explain recursion?

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. The C programming language supports recursion, i.e., a function to call itself.


1 Answers

From this excellent browser based python recursion visualizer:

Paste your code as:

st= []
def combi(prefix, s):
    if len(s)==0: 
        return 
    else:
        st.append(prefix+s[0])     
        combi(prefix+s[0],s[1:])
        combi(prefix,s[1:])
        return st

And it generates this diagram which you can step through one call at a time. (The truly wonderful thing is the python is executed in your browser using web assembly!)

enter image description here

You could also look at a stand alone python module for that

rcviz output

Generated with:

from rcviz import callgraph, viz
st= []
@viz
def combi(prefix, s):
    if len(s)==0: 
        return 
    else:
        st.append(prefix+s[0])     
        combi.track(st = st) #track st in rcviz 
        combi(prefix+s[0],s[1:])
        combi(prefix,s[1:])
        return st

print combi("",'abc')
callgraph.render("combi.png")
like image 129
carlsborg Avatar answered Nov 01 '22 20:11

carlsborg