strong.scm 4.7 KB

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