Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Naren Nidamanuri

Assignment 3
BU id:248645
6.
Given the following adjacency list representation of a
weighted graph:

(define graph '(


(a (b 3) (c 1))
(b (a 3) (d 1))
(c (a 1) (d 2) (e 2))
(d (b 1) (c 2) (e 1) (g 2))
(e (c 2) (d 1) (f 3))
(f (e 3) (g 1))
(g (d 2) (f 1))
)
)

(a)
Write a function to perform breadth first (call your
function
bfs) search of the graph. The function should take as
arguments: a graph,
the starting node and the goal node. It should return as
output the path
from the start to the goal.

For example,
gt& (bfs graph 'a 'g) returns (a b d g)

Solution

(setq graph '(

(a (b c))

(b (a d))

(c (a d e))
(d (b c e g))

(e (c d f ))

(f (e g))

(g (d f))

(let ((predecessor_list (make-hash-table :size 20)))

(defun set_predecessor(x y)

(setf (gethash x predecessor_list) y)

(defun return_predecessor(x)

(gethash x predecessor_list)

(defun bfs(graph start goal)

(let ((open (list start)) closed)

(breadth open closed goal)

(set_predecessor start nil)

(let ((path (list goal)))

(build_solution goal path)

)
(defun breadth(open closed goal)

(cond

((null open) nil)

(t (let ((state (car open)))

(cond ((equal state goal) 'sucess)

(t (setq closed (cons state closed))

(setq list (generate-descendants state open closed goal))

(setq open (append (cdr open) (generate-descendants state open closed goal)))

(dolist(x list nil)

(set_predecessor x state)

(breadth open closed goal)

(defun generate-descendants(state open closed goal)

(let ((child (find-children state graph)))

(setq child (remove_closed child closed))

(setq child (remove_closed child open))

)
(defun find-children(state graph)

(cond ((null graph) nil)

((equal state (caar graph)) (car (cdr (car graph))))

(t (find-children state (cdr graph)))

(defun remove_closed(child closed)

(cond ((null child) nil)

((member (car child) closed) (remove_closed (cdr child) closed))

(t (setq child (cons (car child) (remove_closed (cdr child) closed))))

(defun build_solution(k path)

(cond ((null k) (cdr path))

(t

(setq path (cons (return_predecessor k) path))

(build_solution (car path) path)

)
(b) write a function which given a source node and a goal
node, it performs
a depth first search of the graph.
(setq graph '(
(a (b c))
(b (a d))
(c (a d e))
(d (b c e g))
(e (c d f ))
(f (e g))
(g (d f))
)
)

(let ((predecessor_list (make-hash-table :size 20)))


(defun set_predecessor(x y)
(setf (gethash x predecessor_list) y)
)
(defun return_predecessor(x)
(gethash x predecessor_list)
)
)

(defun dfs(graph start goal)


(let ((open (list start)) closed)
(depth open closed goal)
)
(set_predecessor start nil)
(let ((path (list goal)))

(build_solution goal path)

)
)

(defun depth(open closed goal)


(cond
((null open) nil)
(t (let ((state (car open)))

(cond ((equal state goal) 'sucess)


(t (setq closed (cons state closed))
(setq list (generate-descendants
state open closed goal))
(setq open (append (generate-
descendants state open closed goal) (cdr open)))

(set_predecessor (car open)


state)

(depth open closed goal)


)
)
)
)
)
)

(defun generate-descendants(state open closed goal)


(let ((child (find-children state graph)))
(setq child (remove_closed child closed))

)
)

(defun find-children(state graph)


(cond ((null graph) nil)
((equal state (caar graph)) (car (cdr (car
graph))))
(t (find-children state (cdr graph)))
)
)

(defun remove_closed(child closed)


(cond ((null child) nil)
((member (car child) closed) (remove_closed
(cdr child) closed))
(t (setq child (cons (car child)
(remove_closed (cdr child) closed))))
)
)

(defun build_solution(k path)


(cond ((null k) (cdr path))
(t
(setq path (cons (return_predecessor k) path))
(build_solution (car path) path)

)
)

You might also like