def copygraph(g):
    """
    Make a deepcopy of the graph.
    :param g: The graph to deepcopy
    :return:
    """
    f = graph(len(g.V()))
    for e in g.E():
        i = f.V()[e.tail().getlabel()]
        j = f.V()[e.head().getlabel()]
        f.addedge(i, j)

    for v in g.V():
        for u in f.V():
            if v.getlabel() == u.getlabel():
                u.setcolornum(v.getcolornum())
    return f
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
from graphs.graphIO import loadgraph, writeDOT
from graphs.basicgraphs import graph

"""
Test if the union of two graphs work
"""

l = loadgraph('../../test_grafen/colorref_smallexample_4_7.grl', readlist=True)
union = graph(0)
union.createUnion(l[0][0], l[0][1])
writeDOT(union, "output.dot")
print(union)