![]() ![]() The incorrect pseudocode is on slide 13, and it is clearly wrong since it does not correspond to the useful graphic showing the working of the algorithm on the immediately preceding slide. Interestingly, the pseudocode is basically derived from the slides from a talk Sedgewick gave in 2002 (reference 3 on the Wikipedia page, also currently available on Sedgewick's home page). The correct implementation of Heap's algorithm, and the source of the error This is precisely the output from your code, which is not surprising since your code is an accurate implementation of the algorithm presented. Which have three swaps between them (one of the swaps is a no-op). As you can see in the Wikipedia page, there are times when multiple swaps occur between generated permutations. However, the algorithm presented is not Heap's algorithm, and it does not guarantee that successive permutations will be the result of a single interchange. You have successfully implemented the algorithm presented. There's nothing wrong with your code (algorithmically), if you intended to implement the Wikipedia pseudocode. The current version may or may not be correct at the time of this edit (March 2022), the page contained both correct and incorrect versions. The version referred to by the question and original answer was incorrect you can see it in Wikipedia's change history. The Wikipedia article on Heap's algorithm has been corrected, defaced and corrected again several times since this answer was written. This seems fine until that last step, which isn't a swap of one pair. This produces permutations in the following order (for input of ): 0,1,2,3 I tried to write a generator for this in python: def heap_perm(A): The pseudocode is: procedure generate(n : integer, A : array of any): ![]() I found the Wikipedia article ( ) for Heap's algorithm, which is supposed to do this. The order has to be generated by swapping a pair of elements at each step. I need to iterate over permutations of a tuple of integers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |