Main Page       Index


graph.lsp


 graph.lsp
 Version 1.00  31 October 2004
 Author Steven Jones

 Contact jones57@swbell.net include the word "nyquist" in subject line  

 The contents of this file are released under the terms of the GNU General
 Public License. See the file LICENSE.txt

 Defines a function to display nodes on a graph. A graph consist of two
 sets; nodes and edges.  See node.lsp


function

graph:print

 (graph:print start [echo])
 Print graph representation where "A --(n)--> B"
 indicates that node A links to node B with frequency n
 
 start  - Object. An instance of node

 echo   - Boolean. If true dump results to standard out. Default true

 return - String | nil


example

Building a 5 node graph

;; @doc example Building a 5 node graph
;;
;;             [C]--
;;             /    \
;; [A]---[B]--<      >--[E]
;;             \    /
;;             [D]--
;;
;;
;; Define the nodes
;;

(setf A (send node :new 'APE :bidir nil)
      B (send node :new 'BAT :bidir nil)
      C (send node :new 'CAT :bidir nil)
      D (send node :new 'DOG :bidir nil)
      E (send node :new 'ELK :bidir nil))


;; Now interconnect them
;;

(send A :link B)
(send B :link C)
(send B :link D)
(send C :link E)
(send D :link E)


;; Print it, 
;;

(graph:print A)


View the Sourcecode :



;; graph.lsp
;; Version 1.00  31 October 2004
;; Author Steven Jones
;;
;; Contact jones57@swbell.net include the word "nyquist" in subject line  
;;
;; The contents of this file are released under the terms of the GNU General
;; Public License. See the file LICENSE.txt
;;
;; Defines a function to display nodes on a graph. A graph consist of two
;; sets; nodes and edges.  See node.lsp
;;

(require 'node)
(provide 'graph)
(current-file "graph")


;; @doc function graph:print
;; (graph:print start [echo])
;; Print graph representation where "A --(n)--> B"
;; indicates that node A links to node B with frequency n
;; 
;; start  - Object. An instance of node
;;
;; echo   - Boolean. If true dump results to standard out. Default true
;;
;; return - String | nil
;;

(defun graph:print (start-node &optional (echo t))
  (let (visited stack node acc local-visited)
    (setf visited '())
    (setf stack '())
    (setf acc "")
    (push start-node stack)
    (push start-node visited)
    (while (not (null stack))
      (setf node (car stack))
      (pop stack)
      (setf local-visited '())
      ;(setf acc (format nil "~a~%~a" acc (send node :label)))
      (dolist (other (send node :get-output-edges))
	(if (not (member other visited))
	    (progn 
	      (push other visited)
	      (push other stack)
	      (setf acc (format nil "~a ~%~a --(~a)--> ~a"
				acc
				(send node :label)
				(send node :frequency other)
				(send other :label)))
	      (push other local-visited))
	  (if (not (member other local-visited))
	      (setf acc (format nil "~a ~%~a --(~a)--> ~a"
				acc
				(send node :label)
				(send node :frequency other)
				(send other :label)))))))
	    
    (format echo "~%~a~%" acc)))


; ***************************************************************************  
;				 Example use
; ***************************************************************************  

#|

;; @doc example Building a 5 node graph
;;
;;             [C]--
;;             /    \
;; [A]---[B]--<      >--[E]
;;             \    /
;;             [D]--
;;
;;
;; Define the nodes
;;

(setf A (send node :new 'APE :bidir nil)
      B (send node :new 'BAT :bidir nil)
      C (send node :new 'CAT :bidir nil)
      D (send node :new 'DOG :bidir nil)
      E (send node :new 'ELK :bidir nil))


;; Now interconnect them
;;

(send A :link B)
(send B :link C)
(send B :link D)
(send C :link E)
(send D :link E)


;; Print it, 
;;

(graph:print A)


Main Page       Index