Help with Heap's Algorithm

Hey guys I’m having a problem understanding Heaps Algorithm for the No Repeat Challenge in the Advanced Algorithm section. I understand the block I have enclosed in red is a recursive function (a function that calls itself) after doing some research, but when I read it, it looks like a loop that accomplishes nothing (enclosed in purple). I pulled this pseudocode from Wikipedia, can someone help me understand how the recursive function works in this algorithm? Because I’m reading and executing in my mind line by line and it just seems like it will restart the function each time.

To understand this piece of code, you will need to know more than recusion. Because the heap algorithm is actually a tree like data structure.

The algorithm breaks down the permutation like a tree with every character is one level deep. It is difficult to explain in words. Here is a link that shows the visual tree like breakdown.

https://www.google.ca/search?q=the+thinking+behind+heap+algorithm+permutation&biw=360&bih=560&tbm=isch&prmd=vin&source=lnms&sa=X&ved=0ahUKEwiSzY-v657SAhXL64MKHVPCC0QQ_AUIBigC&dpr=3#imgrc=sHEtlHw3ChfqxM:

I highly recommend that you try doing this using brute force method first before attempting to use an algorithm. It helps reinforce your thinking in terms of complex problems and putting them into codes. As you get better at it, you’ll see nifty patterns to do it more efficiently.

1 Like