strong.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey
  3. ; Code to find the strongly connected components of a graph.
  4. ; (TO <vertex>) are the vertices that have an edge to <vertex>.
  5. ; (SLOT <vertex>) and (SET-SLOT! <vertex> <value>) is a settable slot
  6. ; used by the algorithm.
  7. ;
  8. ; The components are returned in a backwards topologically sorted list.
  9. (define (strongly-connected-components vertices to slot set-slot!)
  10. (make-vertices vertices to slot set-slot!)
  11. (let loop ((to-do vertices) (index 0) (stack #t) (comps '()))
  12. (let ((to-do (find-next-vertex to-do slot)))
  13. (cond ((null? to-do)
  14. (for-each (lambda (n) (set-slot! n #f)) vertices)
  15. comps)
  16. (else
  17. (call-with-values
  18. (lambda ()
  19. (do-vertex (slot (car to-do)) index stack comps))
  20. (lambda (index stack comps)
  21. (loop to-do index stack comps))))))))
  22. (define (find-next-vertex vertices slot)
  23. (do ((vertices vertices (cdr vertices)))
  24. ((or (null? vertices)
  25. (= 0 (vertex-index (slot (car vertices)))))
  26. vertices)))
  27. (define-record-type vertex
  28. (data ; user's data
  29. )
  30. ((edges '()) ; list of vertices
  31. (stack #f) ; next vertex on the stack
  32. (index 0) ; time at which this vertex was reached in the traversal
  33. (parent #f) ; a vertex pointing to this one
  34. (lowpoint #f) ; lowest index in this vertices strongly connected component
  35. ))
  36. (define (make-vertices vertices to slot set-slot!)
  37. (let ((maybe-slot (lambda (n)
  38. (let ((s (slot n)))
  39. (if (vertex? s)
  40. s
  41. (error "graph edge points to non-vertex" n))))))
  42. (for-each (lambda (n)
  43. (set-slot! n (vertex-maker n)))
  44. vertices)
  45. (for-each (lambda (n)
  46. (set-vertex-edges! (slot n) (map maybe-slot (to n))))
  47. vertices)
  48. (values)))
  49. ; The numbers are the algorithm step numbers from page 65 of Graph Algorithms,
  50. ; Shimon Even, Computer Science Press, 1979.
  51. ; 2
  52. (define (do-vertex vertex index stack comps)
  53. (let ((index (+ index '1)))
  54. (set-vertex-index! vertex index)
  55. (set-vertex-lowpoint! vertex index)
  56. (set-vertex-stack! vertex stack)
  57. (get-strong vertex index vertex comps)))
  58. ; 3
  59. (define (get-strong vertex index stack comps)
  60. (if (null? (vertex-edges vertex))
  61. (end-vertex vertex index stack comps)
  62. (follow-edge vertex index stack comps)))
  63. ; 7
  64. (define (end-vertex vertex index stack comps)
  65. (call-with-values
  66. (lambda ()
  67. (if (= (vertex-index vertex) (vertex-lowpoint vertex))
  68. (unwind-stack vertex stack comps)
  69. (values stack comps)))
  70. (lambda (stack comps)
  71. (cond ((vertex-parent vertex)
  72. => (lambda (parent)
  73. (if (> (vertex-lowpoint parent) (vertex-lowpoint vertex))
  74. (set-vertex-lowpoint! parent (vertex-lowpoint vertex)))
  75. (get-strong parent index stack comps)))
  76. (else
  77. (values index stack comps))))))
  78. (define (unwind-stack vertex stack comps)
  79. (let loop ((n stack) (c '()))
  80. (let ((next (vertex-stack n))
  81. (c (cons (vertex-data n) c)))
  82. (set-vertex-stack! n #f)
  83. (if (eq? n vertex)
  84. (values next (cons c comps))
  85. (loop next c)))))
  86. ; 4
  87. (define (follow-edge vertex index stack comps)
  88. (let* ((next (pop-vertex-edge! vertex))
  89. (next-index (vertex-index next)))
  90. (cond ((= next-index 0)
  91. (set-vertex-parent! next vertex)
  92. (do-vertex next index stack comps))
  93. (else
  94. (if (and (< next-index (vertex-index vertex))
  95. (vertex-stack next)
  96. (< next-index (vertex-lowpoint vertex)))
  97. (set-vertex-lowpoint! vertex next-index))
  98. (get-strong vertex index stack comps)))))
  99. (define (pop-vertex-edge! vertex)
  100. (let ((edges (vertex-edges vertex)))
  101. (set-vertex-edges! vertex (cdr edges))
  102. (car edges)))
  103. ; GRAPH is ((<symbol> . <symbol>*)*)
  104. ;(define (test-strong graph)
  105. ; (let ((vertices (map (lambda (n)
  106. ; (vector (car n) #f #f))
  107. ; graph)))
  108. ; (for-each (lambda (data vertex)
  109. ; (vector-set! vertex 1 (map (lambda (s)
  110. ; (first (lambda (v)
  111. ; (eq? s (vector-ref v 0)))
  112. ; vertices))
  113. ; (cdr data))))
  114. ; graph
  115. ; vertices)
  116. ; (map (lambda (l)
  117. ; (map (lambda (n) (vector-ref n 0)) l))
  118. ; (strongly-connected-components vertices
  119. ; (lambda (v) (vector-ref v 1))
  120. ; (lambda (v) (vector-ref v 2))
  121. ; (lambda (v val)
  122. ; (vector-set! v 2 val))))))