Example #1
0
from graphs.graphIO import loadgraph, writeDOT
from tristan.isomorphisms import bijection, colordict
from tristan.refinement import hopcroft

"""
Test if bijection works.
"""
l = loadgraph('../../test_grafen/colorref_smallexample_2_49.grl', readlist=True)
hopcroft(l[0][1])
hopcroft(l[0][2])
print(bijection(colordict(l[0][1]),colordict(l[0][2])))
def isomorphisms(g1t, g2t, count=False):
    """
    Counts or determines the isomorphisms between two graphs.
    :param g1t: The first graph
    :param g2t: The second graph
    :param count: Count the amount of isomorphisms or determine only if there are isomorphisms.
    :return:
    """
    # Generate copies of the graphs to avoid pointer collision
    g1 = copygraph(g1t)
    g2 = copygraph(g2t)

    # Union graphs g1 and g2
    union = graph()
    union.createUnion(g1, g2)

    # Compute coarsest stable coloring that refines (g1, g2)
    hopcroft(union)

    # Generate color dictionaries
    g1colors = colordict(g1.V())
    g2colors = colordict(g2.V())
    ucolors = colordict(union)

    # Check if the graphs are balanced
    if not balanced(g1colors, g2colors):
        return 0

    # Check if the graphs define a bijection
    if bijection(g1colors, g2colors):
        return 1

    # Choose a color class with |C| >= 4. The first element fulfills.
    for cc in g1colors:
        if len(g1colors[cc]) > 1 and len(g2colors[cc]) > 1:
            colorclass = cc
            break
    x = g1colors[colorclass][0]

    # Determine the maxvalue in the color dictionary
    for i in range(len(g1colors)+1):
        if i not in g1colors:
            maxvalue = i
            break

    # Set the new colorclass of x.
    xoldcolornum = x.getcolornum()
    x.setcolornum(maxvalue)
    g1colors = colordict(copygraph(g1).V())

    # Count number of automorphisms
    num = 0

    # Loop all y in C & g2.V()
    for y in g2colors[colorclass]:
        # Set the new colorclass of y, preserve the old color class
        yoldcolornum = y.getcolornum()
        y.setcolornum(maxvalue)
        g2colors = colordict(g2.V())

        if count:
            # Count all automorphisms
            num = num + isomorphisms(g1, g2, count)
        else:
            # Only one automorphism fulfills
            return isomorphisms(g1, g2)

        # Reset the colornum of y
        y.setcolornum(yoldcolornum)
    # Reset the colornum of x
    x.setcolornum(xoldcolornum)

    # Return the number of automorphisms
    return num