Guest User

graph-alg.lisp

a guest
Nov 5th, 2010
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.83 KB | None | 0 0
  1. ;; Alghoritm to searching subgraph in graph
  2. ;; rewrite:
  3. ;; (get-v-outs graph vertex)
  4. ;; (same-vertex? v1 v2)
  5.  
  6. (load "pscat.lisp")
  7.  
  8. (defpackage graph-alg
  9.   (:use :cl :pscat))
  10.  
  11. (in-package graph-alg)
  12.  
  13. ;; #################################
  14. ;; generic functions to work with graphs
  15. (defgeneric verticies (graph))
  16.  
  17. (defgeneric get-v-outs (graph vertex))
  18.  
  19. (defgeneric same-vertex? (v1 v2))
  20.  
  21.  
  22. ;; #################################
  23. ;; simply graph system
  24. (defstruct (simply-graph (:constructor make-simply-graph ())
  25.              (:conc-name simply-graph-))
  26.   (verticies nil)
  27.   (branchs nil))
  28.  
  29. ;; Methods to special cases
  30. (defmethod verticies ((graph simply-graph))
  31.   (simply-graph-verticies graph))
  32.  
  33. (defmethod get-v-outs ((graph simply-graph) vertex)
  34.   (with-slots (branchs) graph
  35.     (loop for branch in branchs
  36.        with result = (list)
  37.        do (multiple-value-bind (el id)
  38.           (pscat:list-find-and-id vertex branch)
  39.         (if el
  40.         (if (> id 0)
  41.             (push (car branch) result)
  42.             (setf result (append result (cdr branch))))))
  43.        finally (return (delete-duplicates result)))))
  44.  
  45. (defmethod same-vertex? ((v1 symbol) (v2 symbol))
  46.   (equal v1 v2))
  47.  
  48. ;; Testing
  49. (defparameter *sg-g* (let ((sg (make-simply-graph))
  50.                (g (make-simply-graph)))
  51.                (setf (simply-graph-verticies sg) '(a b c))
  52.                (setf (simply-graph-verticies g) '(a b c d))
  53.                (setf (simply-graph-branchs sg) '((a b c) (b a)))
  54.                (setf (simply-graph-branchs g) '((a d) (a b c)))
  55.                (cons sg g)))
  56.  
  57.  
  58. ;; #################################
  59. ;; To save subgraph variants
  60. (defstruct (variant (:constructor make-variant)
  61.             (:conc-name variant-))
  62.   pairs)
  63.  
  64.  
  65. ;; #################################
  66. ;; Searching
  67. (defun search-subgraph-in-graph (subgraph graph)
  68.   (let ((all-variants '()))
  69.     (labels ((search-iter (subgraph-vertex graph-vertex)
  70.            (let ((subgraph-v-out-vs (get-v-outs subgraph subgraph-vertex))
  71.              (graph-v-out-vs (get-v-outs graph graph-vertex)))
  72.          (let* ((interception (get-interception subgraph-v-out-vs graph-v-out-vs #'same-vertex?))
  73.             (alls-next-paths (set-product (mapcar #'cdr interception))))
  74.            
  75.            ;; all vertexes in subgraph and graph that go in one vertex on next level
  76.            (print (vertexes-that-go-in-one-on-next-level subgraph subgraph-v-out-vs #'same-vertex?))
  77.            (print (vertexes-that-go-in-one-on-next-level graph graph-v-out-vs #'same-vertex?))
  78.            ))))
  79.       (loop for sv in (verticies subgraph)
  80.      do (loop for v in (verticies graph)
  81.            do (when (same-vertex? v sv)
  82.             (search-iter sv v)))))))
  83.            
  84.  
  85. (defun get-interception (l1 l2 &optional (equalp #'equalp))
  86.   ;;conformuty between graph and subgrap vertexies
  87.   (loop for el1 in l1
  88.      with interceptions
  89.      do (loop for el2 in l2
  90.        when (funcall equalp el2 el1) collect el2 into accum
  91.        finally (when accum
  92.              (push (cons el1 accum) interceptions)))
  93.      finally (return interceptions)))
  94.        
  95. (defun vertexes-that-go-in-one-on-next-level (graph vertexes &optional (equalp #'equalp))
  96.   (loop for v in vertexes
  97.      collect (cons v (get-v-outs graph v)) into accum
  98.      finally (lists-with-commons accum equalp)))
  99.  
  100. (defun lists-with-commons (ls &optional (equalp #'equalp))
  101.   (loop for l1 in ls
  102.      for i from 1
  103.      with ls-with-common-els
  104.      do (loop for l2 in (subseq ls i)
  105.        when (get-interception (cdr l1) (cdr l2) equalp)
  106.        do (push (cons (car l1) (car l2)) ls-with-common-els))
  107.      finally (return ls-with-common-els)))
  108.  
  109. (defun set-product (ls)
  110.   (labels ((set-iter (set ls)
  111.          (if ls
  112.          (set-iter (product set (car ls)) (cdr ls))
  113.          set))
  114.        (product (set l)
  115.          (loop for el in l
  116.         append (loop for s in set
  117.               for s-list = (in-list s)
  118.               collect (cons el s-list)))))
  119.     (set-iter (car ls) (cdr ls))))
  120.        
  121. (defun in-list (l)
  122.   (if (listp l) l (list l)))
Advertisement
Add Comment
Please, Sign In to add comment