Problem with implementing Depth First Search in Scheme?

You don't want to change the graph as you do the depth-first search, just the current node. If you want to do things purely functionally, have your function look like: (define (depth-first-search graph node dest path) (let dfs ((node node) (path path)) (let ((recur (lambda (node) (dfs node (cons node path))))) ; Write code here ; Recursive calls should use recur, not dfs or depth-first-search ...))) (returning a path or f as the result). You can use ormap (in Racket or SRFI-1) to iterate through all neighbors of a node, returning the first value that is not f.

You don't want to change the graph as you do the depth-first search, just the current node. If you want to do things purely functionally, have your function look like: (define (depth-first-search graph node dest path) (let dfs ((node node) (path path)) (let ((recur (lambda (node) (dfs node (cons node path))))) ; Write code here ; Recursive calls should use recur, not dfs or depth-first-search ...))) (returning a path or #f as the result). You can use ormap (in Racket or SRFI-1) to iterate through all neighbors of a node, returning the first value that is not #f.

Assuming node 'a has children '(b c d e), node 'b has children '(a c), node ... First you need a function that expands a node to its children. (define (expand graph node) (let ((c (assq node graph))) (if c (cdr c) '()))) Second: you have to remember all visited nodes. In general hat is different from the path (maybe it does not matter in this example).

Third: you need to remember all nodes you want to visit (result from the node expansion process). So define a helper function (define (dfs* graph visited border path dest) If there are no nodes left to visit, then no road exist. (cond ((null?

Border) #f) If the first element in border is equal to our destination, then we are happy ((eq? (car border) dest) (cons (car border) path)) Lets check all visited nodes. If the first node in border was visited before then proceed without node expansion ((memq (car border) visited) (dfs* graph visited (cdr border) path dest)) Otherwise expand the first node of border (else (dfs* graph (cons (car border) visited) (append (expand graph (car border)) (cdr border)) (cons (car border) path) dest)))) Call that helper function with starting values for visited, border and path: (define (dfs graph src dst) (dfs* graph '() (list src) '() dst) For breath first search: append the expanded node at the end of border Edit: a) visited and path are the same, you can drop one of them b) the path is returned in reverse order c) The procedure is not correct, path contains all visited nodes.

But a post processing of the result of dfs* will do the job.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions