6.001, Spring Semester, 2005--Pro ject 3 (define (graph? graph) boolean (and (pair? graph)(eq? 'graph (car graph)))) (define (graph-elements graph Graph - list<Graph-Element> (if (not (graph? graph)) (error object not a graph: "graph) (cdr graph))) (define (graph-r oot graph) oh - Node null (let ((elements ( graph-elements graph))) (if (null? elements) (gr aph-element->node (car elements))))) In the above implement ation, we will arbitrarily consider the first graph-element to hold the"root for the graph. The procedure graph-root ret urns t he root node Given these abstractions, we can construct the graph in Figure 2(with node a as the root )using (define test (make-graph (1 (make-graph-element ]a '(b i m)'(some words)) (make-graph-element 'b '(c d e h),(more words)) (make-graph-element 'c 1('(at c node some words ) (make-graph-element e ,(f g) make-gr ap g0,0) (make-graph-element ' h 10)10) make-graph-element ]i '(j k 1)'(more words yet)) (make-graph-element j1(10) Note that several of t he no des have no children . and t hat several have no contents We would like to have some accessors to get connect ivity and content s information out of the graph Find the specified node in the graph (define (find-graph-element graph node) Graph, Node - Graph-Element Inull (define(find el cond((null? elements)o) ((eq? (graph-element->node (car elements)) node) (car elements)) (else (find (cdr elements))))) (find(graph-elements graph))) We are often more interested in the node children or node content, rat her than the graph-element The find-node-children and find-node-contents accessor pro cedures can be implemented follows
( "" & ( )( ' & % & ( *+ $ * & , ( - & + ,2 & 1 1 ,* % + >% += + + /0 + +! + # % + ! 2 +% ,%%& 1 % + + ' 81+ % + 9 %) ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' . ' ' ' ' ' ' ' ' ' ' '+ ' " '+ ' ' ' ' ' ' ' ' + %2 + % +2 +& + %2 +2 %! 3 1 4 +2 % %%% 2* % + +! 3 >% > > += +& 2 + 8!! + %*, + >% + 9) / & , ( ' )( & 3 % + + %& + + + +=! + %% % , % 1%)
6.001, Spring Semester, 2005--Pro ject 3 Find the children of the specified node in the graph (define(find-node-children graph node) Graph, Node -> list<Node>Inull (let ((element (find-graph-element graph node))) (if (not (null? (gr aph -element->children element) ))) ind the contents of the specified node in the graph (define(find-node-contents graph node) Graph, N ->contents Inull grap (if (not (null? element)) (graph -element->cont ents In our represent ation above, we use node names(Node symbol) to reference a graph-element in a graph; the children of a no de are represented as a list of other node names. An alternat ive to this approach would be to make the node it self a full abstract dat a type, so that a node object would have ident ity, and the children of a node could be, for example, a list of the act ual children node object s. The tradeoff would be more work in building the graph(e.g. to link toget her actual node objects as nodes and edges are added to a graph), but subst ant ial savings when nodes are requested from the graph(i.e. by avoiding a linear search of the graph-elements for the mat ching node name). With such an alternative abstract ion, when requesting a child no de one can achieve const ant time access(in the size of the graph), as opposed to linear time access as in the current imp lementat ior 2. Searching a Graph How can we search a graph? The basic idea is that we need to st art at some node and traverse the graph in some fashion look ing for some goal. The search might succeed (meaning that some goal is found), or it might fail (meaning that some goal was not found). This very basic and abstract search behav ior can be capt ured in the following procedure search: Node, (Node->Boolean),(Graph, Node - List<Node>) Boolean (define ( h initial-state goal' initial-state is the start state of the search hat d reached the goal successors computes from the current state all succes sor states Deterge combines new states with the set of states still to explore ine (s inner still-to-do) (if (null? still-to-do) (let ((current (car still-to-do)) (if *sear ch-debugk
G / & % &, ( & ' / & , ( & ' % ,2& 1 % % 8' ! %$ 9 # # F + + % % % + %! 2 +% + 1 , 4 + % ,% *& % + 1 +2 *& + + ,& .& % + + , -%! + H 1 , 14 , + + 8!! 4 + , -% % % % +9& , %,% %2% 1+ % I% + + 8!! ,* 2 %+ + +=% + + 9! 3+ %+ 2 ,%& 1+ I% + +2 % %% 8 + %E + +9& % % %% % + ! 1 1 %+ +J + ,% % + 1 % % 2% + + % %+ 4 % ! + %+ + % 8 + % % 9& + 8 + % 1% 9! +% 2* ,% ,% %+ ,+2 , + 1 ) $ &0 & % & % & % & & % & & 0 ( ( . ( - 11