File: topological-sort.scm

package info (click to toggle)
guile-lib 0.2.7-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,904 kB
  • sloc: lisp: 7,014; sh: 3,986; makefile: 191
file content (82 lines) | stat: -rw-r--r-- 2,905 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
;; (graph topological-sort) -- topological sorting
;; Written 1995 by Mikael Durfeldt.

;; This file is based on tsort.scm from SLIB, and is in the public domain.

;;; Commentary:
;; The algorithm is inspired by Cormen, Leiserson and Rivest (1990)
;; ``Introduction to Algorithms'', chapter 23.
;;; Code:

(define-module (graph topological-sort)
  #:export (topological-sort
            topological-sortq
            topological-sortv)
  #:use-module (math primes))

(define (topological-sort-helper dag insert lookup)
  (if (null? dag)
      '()
      (let* ((adj-table (make-hash-table
			 (car (primes> (length dag) 1))))
	     (sorted '()))
	(letrec ((visit
		  (lambda (u adj-list)
		    ;; Color vertex u
		    (insert adj-table u 'colored)
		    ;; Visit uncolored vertices which u connects to
		    (for-each (lambda (v)
				(let ((val (lookup adj-table v)))
				  (if (not (eq? val 'colored))
				      (visit v (or val '())))))
			      adj-list)
		    ;; Since all vertices downstream u are visited
		    ;; by now, we can safely put u on the output list
		    (set! sorted (cons u sorted)))))
	  ;; Hash adjacency lists
	  (for-each (lambda (def)
		      (insert adj-table (car def) (cdr def)))
		    (cdr dag))
	  ;; Visit vertices
	  (visit (caar dag) (cdar dag))
	  (for-each (lambda (def)
		      (let ((val (lookup adj-table (car def))))
			(if (not (eq? val 'colored))
			    (visit (car def) (cdr def)))))
		    (cdr dag)))
	sorted)))

(define (topological-sort dag)
"Returns a list of the objects in the directed acyclic graph, @var{dag}, topologically sorted.  Objects are
compared using @code{equal?}.  The graph has the form:
@lisp
 (list (obj1 . (dependents-of-obj1)) 
       (obj2 . (dependents-of-obj2)) ...)
@end lisp
...specifying, for example, that @code{obj1} must come before all the objects in @code{(dependents-of-obj1)} in
the sort."
  (topological-sort-helper dag hash-set! hash-ref))

(define (topological-sortq dag)
"Returns a list of the objects in the directed acyclic graph, @var{dag}, topologically sorted.  Objects are
compared using @code{eq?}.  The graph has the form:
@lisp
 (list (obj1 . (dependents-of-obj1)) 
       (obj2 . (dependents-of-obj2)) ...)
@end lisp
...specifying, for example, that @code{obj1} must come before all the objects in @code{(dependents-of-obj1)} in
the sort."
  (topological-sort-helper dag hashq-set! hashq-ref))

(define (topological-sortv dag)
"Returns a list of the objects in the directed acyclic graph, @var{dag}, topologically sorted.  Objects are
compared using @code{eqv?}.  The graph has the form:
@lisp
 (list (obj1 . (dependents-of-obj1)) 
       (obj2 . (dependents-of-obj2)) ...)
@end lisp
...specifying, for example, that @code{obj1} must come before all the objects in @code{(dependents-of-obj1)} in
the sort."
  (topological-sort-helper dag hashv-set! hashv-ref))

;;; arch-tag: 9ef30b53-688a-43fc-b208-df78d5b38c74