-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.lisp
65 lines (62 loc) · 1.61 KB
/
dfs.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(defun dfs (graph sindex visitStatus)
(if (= (aref visitStatus (- sindex 1)) 0)
(progn
(terpri)
(format t "~D " sindex)
(setf (aref visitStatus (- sindex 1)) 1)
(dotimes (i (car (array-dimensions (aref graph (- sindex 1)))))
(dfs graph (aref (aref graph (- sindex 1)) i) visitStatus)
)
)
)
)
(format t "Enter N The total number of nodes in Graph")
(terpri)
(setf n (read))
(setf graph (make-array n))
(setf visitStatus (make-array n))
(dotimes (k n)
(setf (aref visitStatus k) 0)
)
(write visitStatus)
(format t "Enter The Adjancency Matrix")
(terpri)
(dotimes (i n)
(progn
(loop
(format t "Enter The number of nodes connected to ~Dth node in graph" (+ i 1))
(terpri)
(setf n1 (read))
(if (> n1 n)
(progn
(format t "Enter A Valid number of nodes connected")
(terpri)
)
(return)
)
)
(setf (aref graph i) (make-array n1))
(dotimes (j n1)
(loop
(format t "Enter ~Dth Number Node ~Dth node is connected to" (+ j 1) (+ i 1))
(terpri)
(setf (aref (aref graph i) j) (read) )
(if (> (aref (aref graph i) j) n)
(progn
(format t "Enter A Valid node number")
(terpri)
)
(return)
)
)
)
)
)
(write graph)
(terpri)
(format t "Enter the starting node for DFS traversal 1 --> ~D" n)
(terpri)
(setf startnode (read))
(format t "The DFS traversal of given Graph is")
(terpri)
(dfs graph startnode visitStatus)