Update comments
This commit is contained in:
parent
30765ae30d
commit
2dfdbfbb49
3 changed files with 55 additions and 31 deletions
|
@ -1,4 +1,11 @@
|
||||||
;; Create network nodes
|
;;;; Quick Find Algorithm
|
||||||
|
;;;; This algorithm solve dynamic connectivity
|
||||||
|
;;;; problem by providing a way to find if there
|
||||||
|
;;;; is a path between two nodes in a dynamic graph
|
||||||
|
|
||||||
|
|
||||||
|
;;; Base functions
|
||||||
|
|
||||||
(defun create-network (n)
|
(defun create-network (n)
|
||||||
"Build a quick-find network using a dynamic vector"
|
"Build a quick-find network using a dynamic vector"
|
||||||
(let ((nodes (make-array n :fill-pointer 0)))
|
(let ((nodes (make-array n :fill-pointer 0)))
|
||||||
|
@ -6,11 +13,6 @@
|
||||||
(vector-push id nodes))
|
(vector-push id nodes))
|
||||||
nodes))
|
nodes))
|
||||||
|
|
||||||
;; Check if two nodes are connected
|
|
||||||
(defmacro connected (network n1 n2)
|
|
||||||
" Return t if there is a path between n1 and n2, nil otherwise. connected represent the find operation of the Quick Find Algorithm"
|
|
||||||
`(= (elt ,network ,n1) (elt ,network ,n2)))
|
|
||||||
|
|
||||||
;; Link two nodes in the network
|
;; Link two nodes in the network
|
||||||
(defun union_ (network n1 n2)
|
(defun union_ (network n1 n2)
|
||||||
"Link two nodes in the quick-find network. union_ represent the union operation of the Quick Find Algorithm"
|
"Link two nodes in the quick-find network. union_ represent the union operation of the Quick Find Algorithm"
|
||||||
|
@ -21,9 +23,14 @@
|
||||||
(if (= (elt new-network n) v-n2) (setf (elt new-network n) v-n1)))
|
(if (= (elt new-network n) v-n2) (setf (elt new-network n) v-n1)))
|
||||||
new-network))
|
new-network))
|
||||||
|
|
||||||
;; Union consing version
|
;;; Macro definitions
|
||||||
|
|
||||||
|
(defmacro connected (network n1 n2)
|
||||||
|
" Return t if there is a path between n1 and n2, nil otherwise. connected represent the find operation of the Quick Find Algorithm"
|
||||||
|
`(= (elt ,network ,n1) (elt ,network ,n2)))
|
||||||
|
|
||||||
(defmacro nunion_ (network n1 n2)
|
(defmacro nunion_ (network n1 n2)
|
||||||
"A cosing version of union_"
|
"A consed version of union_"
|
||||||
`(setq ,network (union_ ,network ,n1 ,n2)))
|
`(setq ,network (union_ ,network ,n1 ,n2)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
;; Create network nodes
|
;;;; Quick Union Algorithm
|
||||||
|
;;;; This algorithm solve dynamic connectivity
|
||||||
|
;;;; problem by providing a way to find if there
|
||||||
|
;;;; is a path between two nodes in a dynamic graph.
|
||||||
|
;;;; It is an improved version of the Quick Find algorithm
|
||||||
|
;;;; It optimize the union function
|
||||||
|
|
||||||
|
|
||||||
|
;;; Base functions
|
||||||
|
|
||||||
(defun create-network (n)
|
(defun create-network (n)
|
||||||
"Build a quick-find network using a dynamic vector"
|
"Build a quick-find network using a dynamic vector"
|
||||||
(let ((nodes (make-array n :fill-pointer 0)))
|
(let ((nodes (make-array n :fill-pointer 0)))
|
||||||
|
@ -6,20 +15,12 @@
|
||||||
(vector-push id nodes))
|
(vector-push id nodes))
|
||||||
nodes))
|
nodes))
|
||||||
|
|
||||||
;; Find the root of a node
|
|
||||||
(defun find-root (network node)
|
(defun find-root (network node)
|
||||||
"Find the root of a sub-tree in the network."
|
"Find the root of a sub-tree in the network."
|
||||||
(do ((id node value)
|
(do ((id node value)
|
||||||
(value (elt network node) (elt network value)))
|
(value (elt network node) (elt network value)))
|
||||||
((= id value) id)))
|
((= id value) id)))
|
||||||
|
|
||||||
;; Check if two nodes are connected
|
|
||||||
(defmacro connected (network n1 n2)
|
|
||||||
"Return true if n1 and n2 are connected and nil otherwise. connection represent
|
|
||||||
the find operation on the Quick Union algorithm"
|
|
||||||
`(= (find-root ,network ,n1) (find-root ,network ,n2)))
|
|
||||||
|
|
||||||
;; Link two nodes together
|
|
||||||
(defun union_ (network n1 n2)
|
(defun union_ (network n1 n2)
|
||||||
"Connect to sub-tree together. union represent the union operation on the Quick Union algorithm"
|
"Connect to sub-tree together. union represent the union operation on the Quick Union algorithm"
|
||||||
(let ((new-network (copy-seq network)))
|
(let ((new-network (copy-seq network)))
|
||||||
|
@ -27,7 +28,15 @@ the find operation on the Quick Union algorithm"
|
||||||
(find-root new-network n2))
|
(find-root new-network n2))
|
||||||
new-network))
|
new-network))
|
||||||
|
|
||||||
;; A consed version of union_
|
|
||||||
|
;;; Macro definitions
|
||||||
|
|
||||||
|
(defmacro connected (network n1 n2)
|
||||||
|
"Return true if n1 and n2 are connected and nil otherwise. connection represent
|
||||||
|
the find operation on the Quick Union algorithm"
|
||||||
|
`(= (find-root ,network ,n1) (find-root ,network ,n2)))
|
||||||
|
|
||||||
(defmacro nunion_ (network n1 n2)
|
(defmacro nunion_ (network n1 n2)
|
||||||
|
"A consed version of union_"
|
||||||
`(setf ,network (union_ ,network ,n1 ,n2)))
|
`(setf ,network (union_ ,network ,n1 ,n2)))
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,28 @@
|
||||||
;; Create network nodes: A two dimensionnal array.
|
;;;; Weighted Quick Union Algorithm
|
||||||
;; 1st dimension = the network
|
;;;; This algorithm solve dynamic connectivity
|
||||||
;; 2nd dimension = each subtree node quantities
|
;;;; problem by providing a way to find if there
|
||||||
|
;;;; is a path between two nodes in a dynamic graph.
|
||||||
|
;;;; It is an improved version of the Quick Union algorithm
|
||||||
|
;;;; by improving the way that the union-tree is constructed
|
||||||
|
;;;; The algorithm try to reduce the deepness of the tree in
|
||||||
|
;;;; order to optimize the find-root function
|
||||||
|
|
||||||
|
;;; Base functions
|
||||||
|
|
||||||
(defun create-network (n)
|
(defun create-network (n)
|
||||||
"Build a quick-find network using a multi-dimensional dynamic vector"
|
"Build a quick-find network using a multi-dimensional dynamic vector:\n
|
||||||
|
1st dimension = the network\n 2nd dimension = each subtree node quantities"
|
||||||
(let ((network (make-array `(2 ,n) :initial-element 1)))
|
(let ((network (make-array `(2 ,n) :initial-element 1)))
|
||||||
(dotimes (id n)
|
(dotimes (id n)
|
||||||
(setf (aref network 0 id) id))
|
(setf (aref network 0 id) id))
|
||||||
network))
|
network))
|
||||||
|
|
||||||
;; Find the root of a node
|
|
||||||
(defun find-root (network node)
|
(defun find-root (network node)
|
||||||
"Find the root of a sub-tree in the network."
|
"Find the root of a sub-tree in the network."
|
||||||
(do ((id node value)
|
(do ((id node value)
|
||||||
(value (aref network 0 node) (aref network 0 value)))
|
(value (aref network 0 node) (aref network 0 value)))
|
||||||
((= id value) id)))
|
((= id value) id)))
|
||||||
|
|
||||||
;; Check if two nodes are connected
|
|
||||||
(defmacro connected (network n1 n2)
|
|
||||||
"Return true if n1 and n2 are connected and nil otherwise. connection represent
|
|
||||||
the find operation on the Quick Union algorithm"
|
|
||||||
`(= (find-root ,network ,n1) (find-root ,network ,n2)))
|
|
||||||
|
|
||||||
;; Link two nodes together
|
|
||||||
(defun union_ (network n1 n2)
|
(defun union_ (network n1 n2)
|
||||||
"Connect to sub-tree together. union represent the union operation on the Quick Union algorithm"
|
"Connect to sub-tree together. union represent the union operation on the Quick Union algorithm"
|
||||||
(let ((new-network (copy-tree network))) ; Duplicate network
|
(let ((new-network (copy-tree network))) ; Duplicate network
|
||||||
|
@ -39,9 +40,16 @@ the find operation on the Quick Union algorithm"
|
||||||
new-network)))
|
new-network)))
|
||||||
|
|
||||||
|
|
||||||
|
;;; Macro definitions
|
||||||
|
|
||||||
|
|
||||||
|
(defmacro connected (network n1 n2)
|
||||||
|
"Return true if n1 and n2 are connected and nil otherwise. connection represent
|
||||||
|
the find operation on the Quick Union algorithm"
|
||||||
|
`(= (find-root ,network ,n1) (find-root ,network ,n2)))
|
||||||
|
|
||||||
;; A consed version of union_
|
|
||||||
(defmacro nunion_ (network n1 n2)
|
(defmacro nunion_ (network n1 n2)
|
||||||
|
"A consed version of the union function."
|
||||||
`(setf ,network (union_ ,network ,n1 ,n2)))
|
`(setf ,network (union_ ,network ,n1 ,n2)))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue