Program that generates finite topologies from a subbasis: Same Code different outputs

35 Views Asked by At

I have created a code that allows me to input a subbasis for a finite topology, in the example of the code I give the space is {A,B,C}. I believe the code is doing everything that I want, except sometimes it gives me duplicates of a set, and sometimes it doesn't.

For those of you that are unfamiliar with what a topology is let me quickly summarize the goal of this code. I give it a list of sets, L. From this list of sets, the code first forms all possible intersections of sets in this list, forming a new list (called a basis if we're thinking mathematically) B. Then using the list B, it forms all possible combinations of unions, and then outputs the final list of sets U which should be our topology.

It concerns me that the code gives multiple different outputs, because it makes me worry the code is doing something I didn't intend.

Here is the code:

import itertools

### compose your sets

x1=set(['A','B','C'])
x2=set([])
x3=set(['A'])
x4=set(['A','B'])
x5=set(['B','C'])

### L is your list of sets, this is your subbasis from which the code generates your topology.

L=[x1,x2,x3,x4,x5]
B=[]
#####get all intersections
for j in range(len(L)+1):
    for subset in itertools.combinations(L,j):
        f=set(['A','B','C'])
        for i in subset:
            f=f&i
        B.append(list(f))
        
####remove duplicates
B.sort()
B=list(B for B,_ in itertools.groupby(B))
newB=[]
for obj in B:
    newB.append(set(obj))

###B is the basis for the topology

###get all unions
U=[]
for j in range(len(newB)+1):
    for subset in itertools.combinations(newB,j):
        g=set([])
        for i in subset:
            g=g|i
        U.append(list(g))
        
### remove duplicates
U.sort()
U=list(U for U,_ in itertools.groupby(U))
print(U)

And an example of two different outputs are: Output 1

Output 2

I believe that there is something happening I don't understand in the itertools.combinations, or something underneath the ### remove duplicates section, in my code.

0

There are 0 best solutions below