aboutsummaryrefslogtreecommitdiff
path: root/union-find/weighted-quick-union-path-compression.lisp
diff options
context:
space:
mode:
authorLoic Guegan <manzerberdes@gmx.com>2019-02-24 20:33:55 +0100
committerLoic Guegan <manzerberdes@gmx.com>2019-02-24 20:33:55 +0100
commitb256fc334a6c8868a6159f32adb6dba01fefca86 (patch)
treef98e6dcf0957b3f68502d7f7142e8c218596868d /union-find/weighted-quick-union-path-compression.lisp
parent5725987c8dfd55d4ee0282f0a37779e06052f3c6 (diff)
Add CLOS to union-findHEADmaster
Diffstat (limited to 'union-find/weighted-quick-union-path-compression.lisp')
-rw-r--r--union-find/weighted-quick-union-path-compression.lisp54
1 files changed, 30 insertions, 24 deletions
diff --git a/union-find/weighted-quick-union-path-compression.lisp b/union-find/weighted-quick-union-path-compression.lisp
index ee1fc31..7235cf2 100644
--- a/union-find/weighted-quick-union-path-compression.lisp
+++ b/union-find/weighted-quick-union-path-compression.lisp
@@ -11,15 +11,28 @@
(in-package :com.lisp-algo.union-find)
+
+(defclass weighted-quick-union-path-compression ()
+ ((nw-size
+ :initarg :network-size
+ :initform 10
+ :accessor network-size)
+ (nw
+ :initform nil
+ :accessor network)))
+
+
;;; Base functions
-(defun wqupc-create-network (n)
+(defmethod initialize-instance :after ((algo weighted-quick-union-path-compression) &key)
"Build a quick-find network using a multi-dimensional dynamic vector:\n
1st dimension = the network\n 2nd dimension = each subtree node quantities"
+ (with-slots ((n nw-size) (nw nw)) algo
(let ((network (make-array `(2 ,n) :initial-element 1)))
(dotimes (id n)
(setf (aref network 0 id) id))
- network))
+ (setf nw network))))
+
(defun wqupc-find-root (network node)
"Find the root of a sub-tree in the network. This is a destructive version of find-root that
@@ -29,33 +42,26 @@ include path compression"
((= id value) (progn (setf (aref network 0 node) id) ; Path compression
id))))
-(defun wqupc-union (network n1 n2)
+(defmethod union ((algo weighted-quick-union-path-compression) n1 n2)
"Connect to sub-tree together. union represent the union operation on the Quick Union algorithm"
- (let ((new-network (copy-tree network))) ; Duplicate network
- (let* ((n1-root (wqupc-find-root new-network n1))
- (n2-root (wqupc-find-root new-network n2))
- (n1-size (aref new-network 1 n1-root))
- (n2-size (aref new-network 1 n2-root)))
- (if (>= n1-size n2-size) ; Check which subtree is LARGER (not deeper)
- (progn (setf (aref new-network 0 n2-root) (aref new-network 0 n1-root)) ; Modify the second node
- (setf (aref new-network 1 n1-root) ; Update tree larger
- (+ (aref new-network 1 n1-root) (aref new-network 1 n2-root))))
+ (with-slots ((network nw)) algo
+ (let ((new-network (copy-tree network))) ; Duplicate network
+ (let* ((n1-root (wqupc-find-root new-network n1))
+ (n2-root (wqupc-find-root new-network n2))
+ (n1-size (aref new-network 1 n1-root))
+ (n2-size (aref new-network 1 n2-root)))
+ (if (>= n1-size n2-size) ; Check which subtree is LARGER (not deeper)
+ (progn (setf (aref new-network 0 n2-root) (aref new-network 0 n1-root)) ; Modify the second node
+ (setf (aref new-network 1 n1-root) ; Update tree larger
+ (+ (aref new-network 1 n1-root) (aref new-network 1 n2-root))))
(progn (setf (aref new-network 0 n1-root) (aref new-network 0 n2-root)) ; Otherwise modify the first node
(setf (aref new-network 1 n2-root) ; Update tree larger
(+ (aref new-network 1 n2-root) (aref new-network 1 n1-root)))))
- new-network)))
-
+ new-network))))
-;;; Macro definitions
-
-
-(defmacro wqupc-connected (network n1 n2)
+(defmethod connected ((algo weighted-quick-union-path-compression) n1 n2)
"Return true if n1 and n2 are connected and nil otherwise. connection represent
the find operation on the Quick Union algorithm"
- `(= (wqupc-find-root ,network ,n1) (wqupc-find-root ,network ,n2)))
-
-(defmacro wqupc-nunion (network n1 n2)
- "A destructive version of the union function."
- `(setf ,network (wqupc-union ,network ,n1 ,n2)))
-
+ (with-slots ((network nw)) algo
+ (= (wqupc-find-root network n1) (wqupc-find-root network n2))))