aboutsummaryrefslogtreecommitdiff
path: root/src/quick-find.lisp
diff options
context:
space:
mode:
authorLoic GUEGAN <loic.guegan@yahoo.fr>2019-01-27 19:34:56 +0100
committerLoic GUEGAN <loic.guegan@yahoo.fr>2019-01-27 19:34:56 +0100
commitee5bce70f59bd831ad8521972edc8c4194242e9a (patch)
treec1afb657c72d6fc86b3e5bc9041c0b61d5aab53c /src/quick-find.lisp
parent35bc8cbb154ce6ba28c22b8a6705ed9682df0f71 (diff)
Add some algo
Diffstat (limited to 'src/quick-find.lisp')
-rw-r--r--src/quick-find.lisp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/quick-find.lisp b/src/quick-find.lisp
new file mode 100644
index 0000000..dd371b8
--- /dev/null
+++ b/src/quick-find.lisp
@@ -0,0 +1,30 @@
+;; Create network nodes
+(defun create-network (n)
+ "Build a quick-find network using a dynamic vector"
+ (let ((nodes (make-array n :fill-pointer 0)))
+ (dotimes (id n)
+ (vector-push id 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
+(defun union_ (network n1 n2)
+ "Link two nodes in the quick-find network. union_ represent the union operation of the Quick Find Algorithm"
+ (let ((v-n1 (elt network n1))
+ (v-n2 (elt network n2))
+ (new-network (copy-seq network)))
+ (dotimes (n (length new-network))
+ (if (= (elt new-network n) v-n2) (setf (elt new-network n) v-n1)))
+ new-network))
+
+;; Union consing version
+(defmacro nunion_ (network n1 n2)
+ "A cosing version of union_"
+ `(setq ,network (union_ ,network ,n1 ,n2)))
+
+
+