Skip to content

Commit 06e94d3

Browse files
committed
LISP:dfs+bfs
1 parent 0474200 commit 06e94d3

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

BFS/bfs.lisp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(defparameter *G* (make-hash-table))
2+
(setf (gethash 'Arad *G*) '( (Zerind 75) (Sibiu 140) (Timisoara 118) ))
3+
(setf (gethash 'Bucharest *G*) '( (Pitesti 101) (Giurgiu 90) (Fagaras 221) (Urziceni 85) ))
4+
(setf (gethash 'Craiova *G*) '( (Dobreta 120) (Rimnichu-Vilcea 146) (Pitesti 138) ))
5+
(setf (gethash 'Dobreta *G*) '( (Mehadia 75) (Craiova 120) ))
6+
(setf (gethash 'Eforie *G*) '( (Hirsova 86) ))
7+
(setf (gethash 'Fagaras *G*) '( (Sibiu 99) (Bucharest 211) ))
8+
(setf (gethash 'Giurgiu *G*) '( (Bucharest 90) ))
9+
(setf (gethash 'Hirsova *G*) '( (Urziceni 98) (Eforie 86) ))
10+
(setf (gethash 'Iasi *G*) '( (Neamt 87) (Vaslui 92) ))
11+
(setf (gethash 'Lugoj *G*) '( (Timisoara 111) (Mehadia 70) ))
12+
(setf (gethash 'Mehadia *G*) '( (Lugoj 70) (Dobreta 75) ))
13+
(setf (gethash 'Neamt *G*) '( (Iasi 87) ))
14+
(setf (gethash 'Oradea *G*) '( (Zerind 71) (Sibiu 151) ))
15+
(setf (gethash 'Pitesti *G*) '( (Craiova 138) (Rimnichu-Vilcea 97) (Bucharest 101) ))
16+
(setf (gethash 'Rimnichu-Vilcea *G*) '( (Craiova 146) (Sibiu 80) (Pitesti 97) ))
17+
(setf (gethash 'Sibiu *G*) '( (Oradea 151) (Arad 140) (Fagaras 99) (Rimnichu-Vilcea 80) ))
18+
(setf (gethash 'Timisoara *G*) '( (Arad 118) (Lugoj 111) ))
19+
(setf (gethash 'Urziceni *G*) '( (Bucharest 85) (Vaslui 142) (Hirsova 98) ))
20+
(setf (gethash 'Vaslui *G*) '( (Urziceni 142) (Iasi 92) ))
21+
(setf (gethash 'Zerind *G*) '( (Arad 75) (Oradea 71) ))
22+
(setf cities '(Arad Bucharest Craiova Dobreta Eforie Fagaras Giurgiu
23+
Hirsova Iasi Lugoj Mehadia Neamt Oradea Pitesti
24+
Rimnichu-Vilcea Sibiu Timisoara Urziceni Vaslui Zerind ))
25+
26+
(setf N 20)
27+
(setf *used* (make-hash-table :size N))
28+
; (loop for node in cities do
29+
; (format t "~% ~d ~d ~d" node (gethash node *used*) (gethash node *G*))
30+
; )
31+
32+
(defvar path '())
33+
(defvar *S* 'Bucharest)
34+
(defvar *T* 'Pitesti)
35+
36+
(defun bfs(tree)
37+
(let ( (Q (list (list tree 0)) ) )
38+
(loop while (not (null Q)) do
39+
(let ((node (car Q)))
40+
(print Q)
41+
(setf Q (cdr Q))
42+
(loop for to in (gethash (car node) *G*) do
43+
(when (null (gethash (car to) *used*))
44+
(setf Q (append Q (list (list (car to) (+ (cadr to) (cadr node))))))
45+
(setf (gethash (car to) *used*) t)
46+
; (format t "~% ~S->~S" to Q)
47+
)
48+
)
49+
)
50+
)
51+
)
52+
)
53+
54+
(bfs 'Bucharest)

DFS/dfs.lisp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(defparameter *G* (make-hash-table))
2+
(setf (gethash 'Arad *G*) '( (Zerind 75) (Sibiu 140) (Timisoara 118) ))
3+
(setf (gethash 'Bucharest *G*) '( (Pitesti 101) (Giurgiu 90) (Fagaras 221) (Urziceni 85) ))
4+
(setf (gethash 'Craiova *G*) '( (Dobreta 120) (Rimnichu-Vilcea 146) (Pitesti 138) ))
5+
(setf (gethash 'Dobreta *G*) '( (Mehadia 75) (Craiova 120) ))
6+
(setf (gethash 'Eforie *G*) '( (Hirsova 86) ))
7+
(setf (gethash 'Fagaras *G*) '( (Sibiu 99) (Bucharest 211) ))
8+
(setf (gethash 'Giurgiu *G*) '( (Bucharest 90) ))
9+
(setf (gethash 'Hirsova *G*) '( (Urziceni 98) (Eforie 86) ))
10+
(setf (gethash 'Iasi *G*) '( (Neamt 87) (Vaslui 92) ))
11+
(setf (gethash 'Lugoj *G*) '( (Timisoara 111) (Mehadia 70) ))
12+
(setf (gethash 'Mehadia *G*) '( (Lugoj 70) (Dobreta 75) ))
13+
(setf (gethash 'Neamt *G*) '( (Iasi 87) ))
14+
(setf (gethash 'Oradea *G*) '( (Zerind 71) (Sibiu 151) ))
15+
(setf (gethash 'Pitesti *G*) '( (Craiova 138) (Rimnichu-Vilcea 97) (Bucharest 101) ))
16+
(setf (gethash 'Rimnichu-Vilcea *G*) '( (Craiova 146) (Sibiu 80) (Pitesti 97) ))
17+
(setf (gethash 'Sibiu *G*) '( (Oradea 151) (Arad 140) (Fagaras 99) (Rimnichu-Vilcea 80) ))
18+
(setf (gethash 'Timisoara *G*) '( (Arad 118) (Lugoj 111) ))
19+
(setf (gethash 'Urziceni *G*) '( (Bucharest 85) (Vaslui 142) (Hirsova 98) ))
20+
(setf (gethash 'Vaslui *G*) '( (Urziceni 142) (Iasi 92) ))
21+
(setf (gethash 'Zerind *G*) '( (Arad 75) (Oradea 71) ))
22+
(setf cities '(Arad Bucharest Craiova Dobreta Eforie Fagaras Giurgiu
23+
Hirsova Iasi Lugoj Mehadia Neamt Oradea Pitesti
24+
Rimnichu-Vilcea Sibiu Timisoara Urziceni Vaslui Zerind ))
25+
26+
(setf N 20)
27+
(setf used (make-hash-table :size N))
28+
; (loop for node in cities do
29+
; (format t "~% ~d ~d ~d" node (gethash node used) (gethash node *G*))
30+
; )
31+
32+
(defvar path '())
33+
(defvar *S* 'Bucharest)
34+
(defvar *T* 'Pitesti)
35+
36+
(defun dfs(node)
37+
(when (gethash node used) (return-from dfs))
38+
(setf (gethash node used) t)
39+
(setf path (cons node path))
40+
(when (eq node *T*) (format t "~% full_path:~S" path))
41+
; (print path)
42+
(loop for to in (gethash node *G*) do
43+
(dfs (car to))
44+
)
45+
(setf path (cdr path))
46+
(setf (gethash node used) NIL)
47+
)
48+
49+
(dfs 'Bucharest)

0 commit comments

Comments
 (0)