Created
January 27, 2011 11:17
-
-
Save hristozov/798369 to your computer and use it in GitHub Desktop.
K2.scm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Georgi Ivanov Hristozov 80430 | |
(define g '( | |
(1 2 3) | |
(2 3 6) | |
(3 4 5) | |
(4 1 2 5) | |
(5 3) | |
(6 2))) | |
;Vrushta vsichki susedi na node | |
(define (getnbrs node g) | |
(let | |
((found (assq node g))) | |
(if found | |
(cdr found) | |
'()))) | |
;Vrushta vsichki putishta, zapochvashti ot node | |
;DFS | |
(define (getAllFrom node g way) | |
(if (or (null? node) (memq node way)) | |
() | |
(let* | |
((nbrs (getnbrs node g)) | |
(nbrs-filtered (filter (lambda (node) (not (memq node way))) nbrs)) | |
(nextways (apply append (map (lambda (newnode) (getAllFrom newnode g (cons node way))) nbrs-filtered))) | |
) | |
(if (null? nbrs-filtered) | |
(list (cons node way)) | |
nextways)))) | |
(define (paths-through node g) | |
(let* | |
((all-ways (map reverse (apply append (map (lambda (node) (getAllFrom node g ())) (map car g))))) | |
(all-ways-filtered (filter (lambda (way) (memq node way)) all-ways)) | |
) | |
all-ways-filtered)) | |
;testove | |
(paths-through 4 g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment