# Generating a topological space diagram for an n-element set

Over on StackOverflow I asked a similar question for the n=3 case, but the answer given doesn't easily generalize.

How can I make a diagram such as this:

But for a general n-element space instead?

-
It is unclear how such diagrams would work for n>=4: you face a problem similar to that of drawing Venn diagrams. What solution do you have in mind to make clear diagrams? (One idea would be to limit the marked subsets to a basis for the topology rather than attempting to show every open set in it.) – whuber Jan 17 '12 at 22:38
Using just the basis is a good solution. It also occurred to me that this is a special case of visualizing HyperGraphs. – tlehman Jan 17 '12 at 22:52

I suspect this is a fundamentally difficult problem, because as you pointed out, it boils down to drawing hypergraphs which, it seems, Mathematica does not have built in support for.

It isn't hard though, to automatically generate some kind of visualization of topologies.

I used the elegant topologyQ function proposed by kguler over at the stackoverflow question you linked to:

topologyQ[x_List] := Intersection[x, #] === # &[
Union[{Union @@ x}, Intersection @@@ Rest@#, Union @@@ #] &@
Subsets@x]


And then a little bit of (substantially less elegant) code I wrote myself:

RandomTopoTable[n_] :=
Module[{Sets =
Reverse[Sort[
RandomSample[#, RandomChoice[Range[Length[#]]]] &[
Subsets[Range[n]]]]],
GenerateRow =
Function[{set}, {#, Background -> If[MemberQ[set, #], Red, White],
Frame -> True} & /@ Range[n]]},
{If[topologyQ[Sets] && MemberQ[Sets, Range[n]],