How to work out the enclosure sets of a set? I implemented it in Emacs-lisp. The source is as follow.
(setq empty-set (list nil))
(defun remove-dup (l)
(defun rm-dup (ls rs)
(cond ((null ls) rs)
((not (in-set (car ls) (cdr ls)))
(rm-dup (cdr ls) (cons (car ls) rs)))
(t (rm-dup (cdr ls) rs))))
(rm-dup l nil))
(defun removed-one-subsets (s)
(mapcar (lambda (e)
(remove e s)) s))
(defun removed-one-subsets-from-sets (ss)
(let ((rs nil))
(while (not (null ss))
(mapcar (lambda (x)
(setq rs (cons x rs)))
(removed-one-subsets (car ss)))
(setq ss (cdr ss)))
(remove-dup rs)))
(defun enclosure-sets (set)
(let ((n (length set))
(rs (list set))
(ss (removed-one-subsets set)))
(setq rs (append rs ss))
(while (> n (- (length set) 2))
(setq ss (removed-one-subsets-from-sets ss))
(setq rs (append rs ss))
(setq n (- n 1)))
(remove-dup (append rs empty-set))))
(enclosure-sets (list 1 2 3))
=> (nil (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
The codes is not beautiful. I discussed about the problem with my a workmates. He thinks it easy to
implement. But it does not seem to be easy in reality. If try to combine ones that is extracted from the
original set, you have to met lots of problems, such as how to get one or more elements from a set and how to
combine more elements. So, I adapted another way to remove one or more elements from the original set for
making a new sub-set. The sub-set can also repeat deleting its elements until there are all one element sets.
What is more, generally, a set doesn't include duplicated elements. However, during working out the
enclosure-sets, there are some temporary sets or set have duplicated elements. So, I wrote the function
remove-dup to make a new set or list from the original set or list that have not duplicated elements.
Finally, don't forget appending a empty-set and the original set into the enclosure-set.
(setq empty-set (list nil))
(defun remove-dup (l)
(defun rm-dup (ls rs)
(cond ((null ls) rs)
((not (in-set (car ls) (cdr ls)))
(rm-dup (cdr ls) (cons (car ls) rs)))
(t (rm-dup (cdr ls) rs))))
(rm-dup l nil))
(defun removed-one-subsets (s)
(mapcar (lambda (e)
(remove e s)) s))
(defun removed-one-subsets-from-sets (ss)
(let ((rs nil))
(while (not (null ss))
(mapcar (lambda (x)
(setq rs (cons x rs)))
(removed-one-subsets (car ss)))
(setq ss (cdr ss)))
(remove-dup rs)))
(defun enclosure-sets (set)
(let ((n (length set))
(rs (list set))
(ss (removed-one-subsets set)))
(setq rs (append rs ss))
(while (> n (- (length set) 2))
(setq ss (removed-one-subsets-from-sets ss))
(setq rs (append rs ss))
(setq n (- n 1)))
(remove-dup (append rs empty-set))))
(enclosure-sets (list 1 2 3))
=> (nil (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
The codes is not beautiful. I discussed about the problem with my a workmates. He thinks it easy to
implement. But it does not seem to be easy in reality. If try to combine ones that is extracted from the
original set, you have to met lots of problems, such as how to get one or more elements from a set and how to
combine more elements. So, I adapted another way to remove one or more elements from the original set for
making a new sub-set. The sub-set can also repeat deleting its elements until there are all one element sets.
What is more, generally, a set doesn't include duplicated elements. However, during working out the
enclosure-sets, there are some temporary sets or set have duplicated elements. So, I wrote the function
remove-dup to make a new set or list from the original set or list that have not duplicated elements.
Finally, don't forget appending a empty-set and the original set into the enclosure-set.