unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: uzibalqa <uzibalqa@proton.me>
To: uzibalqa via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Too many permutations computed
Date: Tue, 01 Aug 2023 18:45:08 +0000	[thread overview]
Message-ID: <Ug13lSJeexzDXPneYyL05De7rscBvpDaUTim67wfYWqxZXVdHe_WPbRaO-2yPZaeG44o2IQC45AABu0SWfWXoaPCUJ8dxXfcbi7T8Yt9Ktg=@proton.me> (raw)

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




             reply	other threads:[~2023-08-01 18:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-01 18:45 uzibalqa [this message]
2023-08-02 10:13 ` Too many permutations computed 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='Ug13lSJeexzDXPneYyL05De7rscBvpDaUTim67wfYWqxZXVdHe_WPbRaO-2yPZaeG44o2IQC45AABu0SWfWXoaPCUJ8dxXfcbi7T8Yt9Ktg=@proton.me' \
    --to=uzibalqa@proton.me \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).