def merge(cls, g1, g2):
        inspector1 = InductionEngine(g1)
        inspector2 = InductionEngine(g2)
        (face1, cycle1) = inspector1.find_girth()
        (face2, cycle2) = inspector2.find_girth()
        if len(face1) != len(face2):
            print "two face not of same kind"
            return None
        
        graph = MultiGraph()
        v_count = 1
        v1_map = {}
        v2_map = {}
        for u in g1.vertices:
            v1_map[u.id] = v_count
            graph.add_vertex(v_count)
            v_count += 1
        for u in g2.vertices:
            v2_map[u.id] = v_count
            graph.add_vertex(v_count)
            v_count += 1
        
        for edge in g1.edges:
            (a, b) = edge.get_endpoints()
            if a in cycle1 and b in cycle1:
                continue
            graph.add_edge(v1_map[a], v1_map[b])
        
        for edge in g2.edges:
            (a, b) = edge.get_endpoints()
            if a in cycle2 and b in cycle2:
                continue
            graph.add_edge(v2_map[a], v2_map[b])
        
        k = min(len(cycle1), len(cycle2))
        cycle1.append(cycle1[0])
        cycle2.append(cycle2[0])
        for i in xrange(k):
            x, y = tuple(map(v1_map.get, cycle1[i:i + 2]))
            u, v = tuple(map(v2_map.get, cycle2[i:i + 2]))
            p = v_count
            q = v_count + 1
            v_count += 2
            graph.add_vertex(p)
            graph.add_vertex(q)
            graph.add_edge(x, p)
            graph.add_edge(p, y)
            graph.add_edge(p, q)
            graph.add_edge(u, q)
            graph.add_edge(q, v)

        for i in xrange(k, len(cycle1) - 1):
            x, y = tuple(map(v1_map.get, cycle1[i:i + 2]))
            if graph.multiplicity(x, y) == 0:
                graph.add_edge(x, y)

        for i in xrange(k, len(cycle2) - 1):
            x, y = tuple(map(v2_map.get, cycle2[i:i + 2]))
            if graph.multiplicity(x, y) == 0:
                graph.add_edge(x, y)
        return graph
Example #2
0
    def merge(cls, g1, g2):
        inspector1 = InductionEngine(g1)
        inspector2 = InductionEngine(g2)
        (face1, cycle1) = inspector1.find_girth()
        (face2, cycle2) = inspector2.find_girth()
        if len(face1) != len(face2):
            print "two face not of same kind"
            return None

        graph = MultiGraph()
        v_count = 1
        v1_map = {}
        v2_map = {}
        for u in g1.vertices:
            v1_map[u.id] = v_count
            graph.add_vertex(v_count)
            v_count += 1
        for u in g2.vertices:
            v2_map[u.id] = v_count
            graph.add_vertex(v_count)
            v_count += 1

        for edge in g1.edges:
            (a, b) = edge.get_endpoints()
            if a in cycle1 and b in cycle1:
                continue
            graph.add_edge(v1_map[a], v1_map[b])

        for edge in g2.edges:
            (a, b) = edge.get_endpoints()
            if a in cycle2 and b in cycle2:
                continue
            graph.add_edge(v2_map[a], v2_map[b])

        k = min(len(cycle1), len(cycle2))
        cycle1.append(cycle1[0])
        cycle2.append(cycle2[0])
        for i in xrange(k):
            x, y = tuple(map(v1_map.get, cycle1[i:i + 2]))
            u, v = tuple(map(v2_map.get, cycle2[i:i + 2]))
            p = v_count
            q = v_count + 1
            v_count += 2
            graph.add_vertex(p)
            graph.add_vertex(q)
            graph.add_edge(x, p)
            graph.add_edge(p, y)
            graph.add_edge(p, q)
            graph.add_edge(u, q)
            graph.add_edge(q, v)

        for i in xrange(k, len(cycle1) - 1):
            x, y = tuple(map(v1_map.get, cycle1[i:i + 2]))
            if graph.multiplicity(x, y) == 0:
                graph.add_edge(x, y)

        for i in xrange(k, len(cycle2) - 1):
            x, y = tuple(map(v2_map.get, cycle2[i:i + 2]))
            if graph.multiplicity(x, y) == 0:
                graph.add_edge(x, y)
        return graph