all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Too many permutations computed
@ 2023-08-01 18:45 uzibalqa
  2023-08-02 10:13 ` Emanuel Berg
  2023-08-03 18:23 ` tpeplt
  0 siblings, 2 replies; 13+ messages in thread
From: uzibalqa @ 2023-08-01 18:45 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor

I have implemented the computations of permutations of a string using the heap algorithm.

But I am getting repetitions, which should not happen.  There could be some problems in the 
way  the recursive calls are handled, and the result list is not being updated correctly.

(defun permute-strg-heap (strg h &optional result)
  "Generate all permutations of string STRG using recursive
backtracking."

  (if (null result) (setq result '()))

  (if (= h 1)

      (progn
        (push (copy-sequence strg) result)  ; Output the permutation
        (message "%s" strg))

    (progn

      (setq result
        (permute-strg-heap strg (1- h) result))

      ;; Generate permutations for i-th swapped with each i-1 initial
      (let ( (j 0) )
        (while (< j h)

          ;; Swap choice based upon h (even or odd)
          (if (evenp h)
              (swap-chars strg j (1- h))
            (swap-chars strg 0 (1- h)))

          (setq result
            (permute-strg-heap strg (1- h) result))

          (setq j (1+ j))) )))

  result)


This should be tho algorithm


procedure generate(k : integer, A : array of any):
    if k = 1 then
        output(A)
    else
        // Generate permutations with k-th unaltered
        // Initially k = length(A)
        generate(k - 1, A)

        // Generate permutations for k-th swapped with each k-1 initial
        for i := 0; i < k-1; i += 1 do
            // Swap choice dependent on parity of k (even or odd)
            if k is even then
                swap(A[i], A[k-1]) // zero-indexed, the k-th is at k-1
            else
                swap(A[0], A[k-1])
            end if
            generate(k - 1, A)
        end for
    end if




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-08-06 13:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-01 18:45 Too many permutations computed uzibalqa
2023-08-02 10:13 ` Emanuel Berg
2023-08-03 15:42   ` uzibalqa
2023-08-03 16:49     ` uzibalqa
2023-08-03 22:13       ` Emanuel Berg
2023-08-04 18:44         ` Heime
2023-08-03 15:52   ` uzibalqa
2023-08-03 16:08     ` Emanuel Berg
2023-08-04 18:39       ` Heime
2023-08-03 18:23 ` tpeplt
2023-08-03 23:25   ` Emanuel Berg
2023-08-04 21:45     ` tpeplt
2023-08-06 13:32       ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.