Exemple #1
0
    def refineColors(self, g1, g2):
        """
        Refines the colors with the graphs
        :param g1: graph1
        :param g2: graph2
        :return: Nothing, this function acts on the current object
        """
        if self.refined:
            print("Warning! Refining a ColorCombination that is already flagged as refined!")
        self.applyToGraphs(g1, g2)
        g = disjointUnionMulti([g1, g2], True)

        p = generatePartitions(g, True)
        newColoring = dict()
        for c in range(len(p)):
            for v in p[c]:
                newColoring[v] = c

        # newColoring = refineColorsv2(g, True)

        newColoringG1 = dict()
        gV = g.V()
        g1V = g1.V()
        for i in range(len(g1V)):
            newColoringG1[g1V[i]] = newColoring[gV[i]]

        newColoringG2 = dict()
        g2V = g2.V()
        for i in range(len(g2V)):
            newColoringG2[g2V[i]] = newColoring[gV[i + len(g1V)]]

        self.generateCoreValues(newColoringG1, newColoringG2, True)
def countTreeAutomorphismsRS(G, visualize=False):
    p = generatePartitions(G)
    n = generateNeighbourList(G)
    degrees = dict()

    for v in G.V():
        degree = len(n[v])
        degreeSet = degrees.get(degree, 0)
        if degreeSet:
            degreeSet.add(v)
        else:
            degrees[degree] = {v}

    colorOf = dict()
    for color in p:
        for v in color:
            colorOf[v] = color

    queue = []
    done = set()
    automorphisms = 1
    for c in p:
        if len(c) == 1:
            queue.append((pickFromSet(c), None))
            break
    if not queue:
        for c in p:
            if len(c) == 2:
                r, l = c

                if True or l in n[r]:
                    queue.append((r, None))
                    break
    while queue:
        vertex, parent = queue[-1]
        color = colorOf[vertex]
        del queue[-1]
        for v in n[vertex] - done:
            if v not in done:
                queue.append((v, vertex))
                done |= colorOf[v]
        if parent:
            automorphisms *= math.factorial(len(n[parent] & color))**(len(colorOf[parent]))
        else:
            automorphisms *= math.factorial(len(color))
        if visualize:
            drawProgress(G, vertex, color, done, queue)
        done |= color
    if visualize:
        drawProgress(G, None, set(), done, queue)
    return automorphisms
Exemple #3
0
from utilities.graphIO import writeDOT
from utilities.graphUtil import createGraph
from week1.colorRefinement import refineColorsv2
from week3.gSnellerPartitioning import generatePartitions, writeColors

def generateTree(length):
    G = graph(length)
    done = {G.V()[0]}
    c = 0
    for i in range(1, len(G.V())):
        G.addedge(G.V()[i], G.V()[int((10000.0*time.time()+c)%i)])
        c += 1
        done.add(G.V()[i])
    return G

def createSimpleExample():
    return createGraph(11, [(0,4), (1,4), (4,6), (2, 5 ), (3,5), (5,6), (6,7), (7,8), (8,9), (8,10)])

# g = createGraph(22, [(0,2), (1,2), (2,9), (3,5), (4,5), (5,9), (6,8), (7,8), (8,9),
#                     (9, 10), (10, 11), (11, 12), (11, 13), (11,14), (13, 15), (10, 16), (10, 17), (17, 18), (17, 19), (17,20), (20,21)])

#g = createGraph(10, [(0,1), (1,2), (2,3), (3,4), (4,5), (0,6), (0,7), (5,8), (5,9)])
g = generateTree(3000)
# writeDOT(g, "testTree.dot")
# g.V()[2] = None
p = generatePartitions(g)
# refineColorsv2(g)5
print(countTreeAutomorphismsLS(g, True))
print(countTreeAutomorphismsRS(g, True))
writeColors(p)
writeDOT(g, "testTree.dot")
def countTreeAutomorphismsLS(G, visualize=False):
    p = generatePartitions(G)
    n = generateNeighbourList(G)
    degrees = dict()

    for v in G.V():
        degree = len(n[v])
        degreeSet = degrees.get(degree, 0)
        if degreeSet:
            degreeSet.add(v)
        else:
            degrees[degree] = {v}

    colorOf = dict()
    for color in p:
        for v in color:
            colorOf[v] = color

    queue = []
    done = set()

    for color in p:
        if len(color) > 1 and color <= degrees[1]:
            queue.append(color)
            done = done | color

    automorphisms = 1

    while queue: # is not empty
        newQueue = []
        for color in queue:
            v = pickFromSet(color)
            if visualize:
                drawProgress(G, v, color, done, queue)
            parent = None
            for neighbour in n[v]:
                if len(n[neighbour] & color) > 1 :
                    parent = neighbour
                    break

            if parent is None:
                unvisitedNeighbours = n[v] - done
                if len(unvisitedNeighbours) == 1:
                    parent = unvisitedNeighbours.pop()
                elif len(unvisitedNeighbours) > 1:
                    newQueue.append(color)
                    continue

            if parent:
                if len(colorOf[parent]) > 1 and parent not in done:
                    newQueue.append(colorOf[parent])
                automorphisms *= math.factorial(len(n[parent] & color))**(len(colorOf[parent]))
                done |= color
            else:
                if len(color) == 2 and len(color&n[v]) == 1:
                    automorphisms *= 2
        queue = newQueue
        for c in queue:
            done |= c

    if visualize:
        drawProgress(G, None, set(), done, queue)
    return automorphisms