unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: 58198@debbugs.gnu.org
Subject: bug#58198: topological-sort does not sort topologically in case of diamonds
Date: Fri, 30 Sep 2022 20:10:09 +0200	[thread overview]
Message-ID: <7e5cff81-debc-bc04-6cc0-9d88c5ea1d9b@telenet.be> (raw)


[-- Attachment #1.1.1: Type: text/plain, Size: 2511 bytes --]

Consider the following DAG (arrows are implicitly downwards):

top -> left, right
left,right -> bottom.

Or in ASCII art:

      top
     /    \
left      right
     \    /
      bottom

Currently, they are sorted incorrectly with topological-sort -- the 
exact resulting order depends on the order in which the dependencies are 
passed to 'topological-sort' (from (guix import utils)), but you can get 
the following:

right bottom left top

'bottom' and 'right' need to be switched.

(Background)
I would like to use a copy of 'topological-sort' for determining the 
order in which 'workspace' members need to be built in antioxidant, but 
currently it produces bogus results (at least for 'greetd').

Theoretically, it would also impact recursive imports (unverified) 
(topological-sort is used to emit them in topological order).

Code to reproduce the bug:

(use-modules (guix sets) (ice-9 match) (srfi srfi-1))

(define (topological-sort nodes
                           node-dependencies
                           node-name)
   "Perform a breadth-first traversal of the graph rooted at NODES, a 
list of
nodes, and return the list of nodes sorted in topological order.  Call
NODE-DEPENDENCIES to obtain the dependencies of a node, and NODE-NAME to
obtain a node's uniquely identifying \"key\"."
   (let loop ((nodes nodes)
              (result '())
              (visited (set)))
     (match nodes
       (()
        result)
       ((head . tail)
        (if (set-contains? visited (node-name head))
            (loop tail result visited)
            (let ((dependencies (node-dependencies head)))
              (loop (append dependencies tail)
                    (cons head result)
                    (set-insert (node-name head) visited))))))))

(define %dependencies
   '((top left right)
     (left bottom)
     (right bottom)
     (bottom)))
(define root-nodes '(top))
(define (node-dependencies node)
   (assoc-ref %dependencies node))
(define node-name identity)
(define sorted (topological-sort root-nodes node-dependencies node-name))
(write sorted)

;; Verify the dependencies have smaller indices
(define (node-index node)
   (list-index (lambda (x) (equal? node x)) sorted))
(define (check node)
   (unless (<= (apply max 0 (map node-index (node-dependencies node)))
	      (node-index node))
     (pk node)
     (error "incorrectly sorted!")))
(for-each check (map car %dependencies))


Greetings,
Maxime.

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

             reply	other threads:[~2022-09-30 18:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30 18:10 Maxime Devos [this message]
2022-10-05  8:42 ` bug#58198: topological-sort does not sort topologically in case of diamonds Maxime Devos
2022-10-08 18:13 ` Maxime Devos
2022-10-12 12:34   ` Maxime Devos
2022-10-17 19:01 ` Maxime Devos

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://guix.gnu.org/

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

  git send-email \
    --in-reply-to=7e5cff81-debc-bc04-6cc0-9d88c5ea1d9b@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=58198@debbugs.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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

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).