System of equations and distinct result sets

Question on the below system of equations:

First off, in section 1 below, I had to manually play with that 4th parameter (300) so I can finally see that there are actually 96 sets of solutions. Is there a way that I do not provide that and Mathematica tells me there are 96? If I remove the parameter I only get 1 set. As I increase it, I eventually see that beyond 96, no matter how high I go it stops at 96.

Second, in section 2 below, I see the CountDistinct gives me the total number of sets. But if you pay attention there are only 4 unique sets (3,21,27,29), (9,11,27,33), (7,13,29,31), (11,13,19,37) – But there are 24 permutations for each set, since the 4 vars are interchangeable, hence 24*4=96 total). How can I ask Mathematica to also list these 4 unique sets?

eqn = FullSimplify[{w + x + y + z == 80, w^2 + x^2 + y^2 + z^2 == 2020}] 
Table[FindInstance[eqn, {w, x, y, z}, Integers, 300] ]  {{w -> 3, x -> 21, y -> 27, z -> 29}, {w -> 3, x -> 21, y -> 29,    z -> 27}, {w -> 3, x -> 27, y -> 21, z -> 29}, {w -> 3, x -> 27,    y -> 29, z -> 21}, {w -> 3, x -> 29, y -> 21, z -> 27}, {w -> 3,    x -> 29, y -> 27, z -> 21}, {w -> 7, x -> 13, y -> 29,    z -> 31}, {w -> 7, x -> 13, y -> 31, z -> 29}, {w -> 7, x -> 29,    y -> 13, z -> 31}, {w -> 7, x -> 29, y -> 31, z -> 13}, {w -> 7,    x -> 31, y -> 13, z -> 29}, {w -> 7, x -> 31, y -> 29,    z -> 13}, {w -> 9, x -> 11, y -> 27, z -> 33}, {w -> 9, x -> 11,    y -> 33, z -> 27}, {w -> 9, x -> 27, y -> 11, z -> 33}, {w -> 9,    x -> 27, y -> 33, z -> 11}, ..... ...(snip)....   {w -> 33, x -> 27, y -> 9, z -> 11}, {w -> 33, x -> 27, y -> 11,    z -> 9}, {w -> 37, x -> 11, y -> 13, z -> 19}, {w -> 37, x -> 11,    y -> 19, z -> 13}, {w -> 37, x -> 13, y -> 11, z -> 19}, {w -> 37,    x -> 13, y -> 19, z -> 11}, {w -> 37, x -> 19, y -> 11,    z -> 13}, {w -> 37, x -> 19, y -> 13, z -> 11}} 
CountDistinct[Table[FindInstance[eqn, {w, x, y, z}, Integers, 300] ]]  {96, 4} 

-Thanks

Saving sets of itemstacks in SQL

I’m currently programming a minecraft survivalgames minigame network and am currently setting up a database for it. I have decided to store the possible maps for my spigot instances that run on my physical servers in a table. They will contain a reference to a chest set. Every chest set will contain an itemset, which is a selection of itemstacks, that also are stored in a table. This will make it possible to make the items in the chests be of a great variety and have them pretty much unpredictable.

So in total I now have 4 tables. The first one looks like this:

enter image description here

The CHESTSET row has the ID of the CHESTSET-Table as foreign key. I do this to make every map have an associated chestset with it. The chestset-table also has a row with the ID of it, and 5 Item-Sets, so in total you can define up to 5 different types of chests. These chests will contain random items of the itemset’s that are referenced in them (the random selection will be done in my plugin, this is only about storing the selection possibilities).

enter image description here

Every itemset contains up to 10 items, that the contens of an individual chest on the map will be composed of.

enter image description here

The properties of the items themselves will be stored in yet another table, this table gives them an id, and then all the relevant properties, like the item-type, the display name, the subid, etc etc. This table gets pretty big, so I’ll just post a dummy, which has all relevant properties.

enter image description here

Now here starts my problem. I’ve been able to add the ID-field of my CHESTSET-Table as a foreign key to my MAPS-Table. This makes sense to me because every map will have a set of predefined chest candidates. I now planned to add the ID-column of my ITEMSETS-Table as foreign key to all 5 of my ITEMSET - X-rows, but I’m somehow not able to do that.

Am I thinking in the right way? For me, the multiple tables are a possibility to store multiple values in the place of one. So I store the items in a set, which can be combined to form chest groups, which then are selected for each map. I think of the foreign key’s as a way to basically "insert" the objects of another table in there. However, a column seems to be limited to a one time use as a foreign key.

What I want is that all of my 5 row’s in my CHESTSET-table are associated with an entry of the table ITEMSET, and likewise the entry’s of the row’s ITEM - x in my table ITEMSETS with an entry of the table CHESTITEMS. How can I achieve that, if I cant make all of them a foreign key linked to the respective ID? Also I’m fairly new to Database design, so if this is a really dumb question, I apologize in advance.

Devising an algorithm to remove “complete” sets of numbers from a list of numbers

Suppose I have a list of numbers consisting of numbers 1 through 5, where some numbers repeat. Define a set of numbers to be "complete" if it consists of each of the numbers 1-5 exactly once. Now, I want to pull out as many possible complete, sorted (lowest to highest) sets from this list. How can I do this (efficiently)?

Here is the solution I have in mind so far. Traverse through the list, and place each number into a pile of numbers that is the same number. For example, place all the 1s into a pile, all the 2s into a pile, and so on. Then, go through all of the piles and determine the minimum number of elements in a pile. For example, if the pile of 2s has five 2s and all other piles have greater than or equal to five elements, then 5 is the minimum number of elements. This minimum, call it min, will give us the number of complete sets in the list. Finally, go through the piles in order and pull out the elements one by one until you form a complete set, and again to form another complete set, and so on until min number of complete sets. Tada! You now have pulled out as many possible complete, sorted sets from this list.

Here is an example:

Let’s say the list is 3 2 1 2 2 3 4 5 5 4 4 3 4 1 1 2 5. We traverse the list and put them into piles:

333 2222 111 4444 555

The minimum number of elements in a pile is 3, so we have 3 complete sets. Now just go through the piles in order and pick up elements one by one:

12345 12345 12345

And tada! We have pulled out as many complete, sorted sets as possible.

Where I am lacking in my solution is figuring out how to efficiently go through the piles in order or make an efficient way of ordering the piles so that I can easily go through them in order.

I’d love to hear any feedback, especially other algorithms you might devise or ways to go about ordering. Thanks!

Spanning tree in a graph of intersecting sets

Consider $ n$ sets, $ X_i$ , each having $ n$ elements or fewer, drawn among a set of at most $ m \gt n$ elements. In other words $ $ \forall i \in [1 \ldots n],~|X_i| \le n~\wedge~\left|\bigcup_{i=1}^n X_i\right| \le m$ $

Consider the complete graph $ G$ formed by taking every $ X_i$ as a node, and weighing every edge $ (i,j)$ by the cardinal of the symmetric difference $ X_i \triangle X_j$ .

An immediate bound on the weight of the minimal spanning tree is $ \mathcal{O}(n^2)$ , since each edge is at most $ 2 n$ , but can we refine this to $ \mathcal{O}(m)$ ?

For illustration, consider $ 2 p$ sets, $ p$ of which contain the integers between $ 1$ and $ p$ and $ p$ of which contain the integers of between $ p+1$ and $ 2p$ . A minimal spanning tree has weight $ p$ but a poorly chose tree on this graph would have weight $ (p-1)p$ . Intuitively, if there are only $ m$ values to chose from, the sets can’t all be that different from one another.

Maximization problem on finite collection of finite sets


Problem

I am considering the following maximization problem:

  • Input is a finite collection of finite sets $ \mathcal{F} = \{ X_1, X_2, \ldots, X_n \}$ .
  • Goal is to find a subset $ G \subseteq \mathcal{F}$ that maximizes $ |G| \times |\bigcap G|$ where
    • $ |G|$ is the cardinality of the set $ G$ , and
    • $ \bigcap G = \bigcap \{X_{i_1}, X_{i_2}, \ldots, X_{i_m} \}$ .

As an example, for the collection $ $ \mathcal{F} = \{ \{a, b, c\}, \{a, b, c, x\}, \{b, c, y\}, \{a, b, c, z\} \}, $ $ the maximizing subset is $ G = \{ \{a, b, c\}, \{a, b, c, x\}, \{a, b, c, z\} \}$ and the score is $ 3 \times |\{a, b, c\}| = 9$ .

Note: the score of $ \mathcal{F}$ itself is $ 4 \times |\{b, c\}| = 8$ .

Question

I am planning to use a procedure of this problem for compressing data (represented by finite collections of finite sets). However, I don’t have any good idea to solve this problem efficiently. As yow know, we can solve this by enumerating all the collections of $ \mathcal{F}$ ; but, it’s too slow for practical use.

Is there a polynomial-time or some kind of efficient algorithm for this problem? Or, does this problem belong to the complexity class that cannot be solved in polynomial time?

Closure of disjoint sets

I am trying to solve the following problem but I cannot figure out two disjoint sets such that their closures are equal.
Find languages S and T over the alphabet {a, b} such that $ S \not\subset T $ and $ T \not\subset\ S $ (S is not contained in T and T is not contained in S) but S* = T*. It might be trivial, but I would appreciate some help. Thanks.

Clustering sets by set difference

Suppose you have $ n$ nonequal sets $ S_1, \ldots, S_n$ and some constant $ 0 \le k < n$ . The goal of set clustering is to find a partition of the set $ \mathbf{S} = \{S_1, \ldots, S_n\}$ such that the sum of the total distance for each subset of $ \mathbf{S}$ is minimized and such that $ \mathrm{cardinality}(\mathbf{S}) = k$ (in reality, this latter constraint is not quite so tight, but the size of $ \mathbf{S}$ must be less than $ k$ , and hopefully near it). The total distance $ d$ of a set $ X \in \mathbf{S}$ is $ d(X) = \sum_{A, B \in X} \mathrm{cardinality}(A \ominus B)$ where $ \ominus$ is symmetric difference. Assume for the purposes of the problem that the sets consume $ O(1)$ space (so symmetric difference can be computed in constant time).

Is there a good greedy linear(ithmic) heuristic for this problem? Is there any literature on this problem, or similar ones?


All I’ve come up with so far is an $ O(n^2 \log n)$ heuristic that looks like:

  1. Set $ I = \{1, \ldots, n\}$
  2. Choose some $ i \in I$
  3. Emit a cluster $ C \subset I$ containing all $ m \in I$ such that $ \mathrm{cardinality}(S_i \ominus S_m) \le d_\mathrm{max}$ .
  4. Set $ I = I – C$
  5. Go to step 2.

where this process occurs in each step of a binary search that finds the best value of $ d_\mathrm{max}$ for a given $ k$ .

I was thinking that if there were some way to sort the list of sets such that nearby sets in the list have small symmetric difference, then a linearithmic solution might be easy to write.