(define r (lambda ;; param map ;; param start represents the start address/node (map start) (display (string-append "address/nodes: " (car map))) (newline) (display "------") (newline) (display (list-ref map 1)) (newline) (display "------") (newline) (newline) (newline) (newline) (newline) (newline) (set! idx (let ((idx0 -1)) (do ((j 0 (+ j 1))) ((>= j (length (car map)))) (if (eqv? (list-ref (car map) j) start) (set! idx0 j))) idx0)) ;; handle invalid start address/node. (if ;(not ; (member ; start (car map))) (= idx -1) (begin (display (string-append "start address/node: " start " is invalid.")) (newline) (throw (IllegalArgumentException. (string-append "start address/node: " start " is invalid."))))) (let* ((s (list idx)) ; (a (car map)) ; addresses/nodes (p (list-ref map 1)) ; (dis (let ; each of t is a tuple: (dis, s, p), ; dis represents the dis between the start address/node and the specific address/node. ; s indicates whether the dis is the final shortest or not. 1 represents yes and initial 0 represents not. ; p represents the path between the start address/node and the specific address/node. ; ; for example: #((0 1 ()) #null (10 0 ()) #null (30 0 ()) (100 0 ()) #null) ((t (list))) (for-each (lambda (x) (display (string-append "address/node: " x)) (newline) (if (.equals start x) ;; x == start, then dis 0, and is the final shortest, and the path specific as an empty-list (begin (let ((d (list 0 1 (list)))) (display (string-append "initial distance to address/node " x ":" d)) (newline) (set! t (append t (list d))))) ;; (set! t (append t (let ((t0 (list #null))) (for-each (lambda (e) (if (and (.equals (list-ref e 0) start) (.equals (list-ref e 1) x)) (set! t0 (list (list (list-ref e 2) 0 (list)))))) p) (display (string-append "initial distance to address/node " x ":" t0)) (newline) t0))))) a) (list->vector t)))) (display "------") (newline) (display dis) (newline) (display "------") (newline) (newline) (newline) (newline) (do ((n 0 (+ n 1))) ((or (>= (length s) (length a)) (>= n 30))) (let ((m #null)) (do ((i 0 (+ i 1))) ((>= i (vector-length dis))) (if (and (not (eqv? (list-ref a i) start)) (not (eqv? (vector-ref dis i) #null)) (eqv? (list-ref (vector-ref dis i) 1) 0)) (if (eqv? m #null) (set! m (vector i (vector-ref dis i))) (if (> (list-ref (vector-ref m 1) 0) (list-ref (vector-ref dis i) 0)) (set! m (vector i (vector-ref dis i))) ) ) ) ) (display m) (newline) (if (not (eqv? m #null)) (begin (display (string-append "path searched for " (list-ref a (vector-ref m 0)) ":" (vector-ref m 1))) (newline) (newline) (vector-set! dis (vector-ref m 0) (list (list-ref (vector-ref m 1) 0) 1 (append (list-ref (vector-ref m 1) 2) ;(list-ref ; (vector-ref dis ; (list-ref s ; (- ; (length s) 1))) 2) (list (list-ref a (vector-ref m 0)))))) (let ((o (list))) (for-each (lambda (e) (if (eqv? (list-ref a (vector-ref m 0)) (car e)) (begin (set! o (append o e)) (let ((idx0 -1)) (do ((j 0 (+ j 1))) ((>= j (length a))) (if (eqv? (list-ref a j) (list-ref e 1)) (set! idx0 j))) (if (not (= idx0 -1)) (let ((t (+ (list-ref e 2) (list-ref (vector-ref m 1) 0)))) (if (eqv? (vector-ref dis idx0) #null) (vector-set! dis idx0 (list t 0 (list (list-ref a (vector-ref m 0)))) ) (if (> (list-ref (vector-ref dis idx0) 0) t) (vector-set! dis idx0 (list t 0 (append (list-ref (vector-ref dis idx0) 2) (list (list-ref a (vector-ref m 0)))))))) ))))) ) p) ) (let ((c (vector-ref m 0))) (set! s (append s (list c)))) ) ) m) (display "stats: ") (newline) (display (string-append "stats: " dis)) (newline) (display (string-append "stats: " s)) (newline)) (display (string-append "result: " dis)) (newline) dis) ) )
;; ;; (#'a', #'b', #'c', #'d', #'e', #'f', #'g') ;; ;; (#'a', #'c', 10 ) ;; (#'a', #'e', 30 ) ;; (#'a', #'f', 100) ;; (#'b', #'c', 5 ) ;; (#'c', #'d', 50 ) ;; (#'d', #'f', 10 ) ;; (#'e', #'d', 20 ) ;; (#'e', #'f', 60 ) ;; (define g (list (list #'a' #'b' #'c' #'d' #'e' #'f' #'g') (list (list #'a' #'c' 10 ) (list #'a' #'e' 30 ) (list #'a' #'f' 100) (list #'b' #'c' 5 ) (list #'c' #'d' 50 ) (list #'d' #'f' 10 ) (list #'e' #'d' 20 ) (list #'e' #'f' 60 ))))
> (r g #'a') address/nodes: (a b c d e f g) ------ ((a c 10) (a e 30) (a f 100) (b c 5) (c d 50) (d f 10) (e d 20) (e f 60)) ------ address/node: a initial distance to address/node a:(0 1 ()) address/node: b initial distance to address/node b:(#null) address/node: c initial distance to address/node c:((10 0 ())) address/node: d initial distance to address/node d:(#null) address/node: e initial distance to address/node e:((30 0 ())) address/node: f initial distance to address/node f:((100 0 ())) address/node: g initial distance to address/node g:(#null) ------ #((0 1 ()) #null (10 0 ()) #null (30 0 ()) (100 0 ()) #null) ------ #(2 (10 0 ())) path searched for c:(10 0 ()) stats: stats: #((0 1 ()) #null (10 1 (c)) (60 0 ()) (30 0 ()) (100 0 ()) #null) stats: (0 2) #(4 (30 0 ())) path searched for e:(30 0 ()) stats: stats: #((0 1 ()) #null (10 1 (c)) (50 0 (e)) (30 1 (e)) (90 0 (e)) #null) stats: (0 2 4) #(3 (50 0 (e))) path searched for d:(50 0 (e)) stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 0 (e d)) #null) stats: (0 2 4 3) #(5 (60 0 (e d))) path searched for f:(60 0 (e d)) stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) stats: (0 2 4 3 5) result: #((0 1 ()) #null (10 1 (c)) (50 1 (e d)) (30 1 (e)) (60 1 (e d f)) #null) $13 = #((0 1 ()) #null (10 1 (#'c')) (50 1 (#'e' #'d')) (30 1 (#'e')) (60 1 (#'e' #'d' #'f')) #null)
;; ;; (#'a', #'b', #'c', #'d', #'e', #'f', #'g') ;; ;; (#'a', #'c', 10 ) ;; (#'a', #'e', 500 ) ;; (#'a', #'f', 100) ;; (#'b', #'c', 5 ) ;; (#'c', #'d', 50 ) ;; (#'d', #'f', 100 ) ;; (#'e', #'d', 20 ) ;; (#'e', #'f', 60 ) ;; (define g (list (list #'a' #'b' #'c' #'d' #'e' #'f' #'g') (list (list #'a' #'c' 10 ) (list #'a' #'e' 500 ) (list #'a' #'f' 100) (list #'b' #'c' 5 ) (list #'c' #'d' 50 ) (list #'d' #'f' 100 ) (list #'e' #'d' 20 ) (list #'e' #'f' 60 ))))
> (r g #'a') address/nodes: (a b c d e f g) ------ ((a c 10) (a e 500) (a f 100) (b c 5) (c d 50) (d f 100) (e d 20) (e f 60)) ------ address/node: a initial distance to address/node a:(0 1 ()) address/node: b initial distance to address/node b:(#null) address/node: c initial distance to address/node c:((10 0 ())) address/node: d initial distance to address/node d:(#null) address/node: e initial distance to address/node e:((500 0 ())) address/node: f initial distance to address/node f:((100 0 ())) address/node: g initial distance to address/node g:(#null) ------ #((0 1 ()) #null (10 0 ()) #null (500 0 ()) (100 0 ()) #null) ------ #(2 (10 0 ())) path searched for c:(10 0 ()) stats: stats: #((0 1 ()) #null (10 1 (c)) (60 0 (c)) (500 0 ()) (100 0 ()) #null) stats: (0 2) #(3 (60 0 (c))) path searched for d:(60 0 (c)) stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 0 ()) (100 0 ()) #null) stats: (0 2 3) #(5 (100 0 ())) path searched for f:(100 0 ()) stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 0 ()) (100 1 (f)) #null) stats: (0 2 3 5) #(4 (500 0 ())) path searched for e:(500 0 ()) stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) #null stats: stats: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) stats: (0 2 3 5 4) result: #((0 1 ()) #null (10 1 (c)) (60 1 (c d)) (500 1 (e)) (100 1 (f)) #null) $22 = #((0 1 ()) #null (10 1 (#'c')) (60 1 (#'c' #'d')) (500 1 (#'e')) (100 1 (#'f')) #null)