Beispiel #1
0
def thickness_merge(g):
    if planarity_test.is_planar(g):
        return 1

    partition = [[edge] for edge in g.edges()]
    thickness_until_now = len(partition)  # the cardinality of the partition
    return recursive_thickness(g, partition, thickness_until_now)
Beispiel #2
0
def recursive_thickness(g, partition, thickness_until_now):
    p2 = partition.copy(
    )  # we cant add and delete things to the partition while iterating, so we need a copy
    # of the partition for that
    # print(partition)
    # print(len(partition), end="")
    for s1 in partition:  # s stands for subset, it is a subset of the graph
        for s2 in partition:
            if s1 != s2:
                s_new = s1 + s2
                g2 = nx.Graph()
                g2.add_edges_from(s_new)
                if planarity_test.is_planar(g2):
                    p2.remove(s1)
                    p2.remove(s2)
                    p2.append(s_new)
                    thickness_until_now = recursive_thickness(
                        g, p2, thickness_until_now)
                    p2.remove(s_new)
                    p2.append(s1)
                    p2.append(s2)
                else:
                    thickness_until_now = min(
                        thickness_until_now,
                        len(partition))  # Omdat we al hebben gecheckt of de
                    # graaf panair is, weten we zeker dat dit statement sowieso 1 keer uitgevoerd wordt.
                    # Als we dat niet zouden checken, en de graaf zou planair zijn, dan wordt de thickness gelijk aan
                    # het aantal nodes in de graaf.
    return thickness_until_now
Beispiel #3
0
def thickness_could_be(part_size, g):
    for partition in partition_gen(g.edges().copy(), part_size):
        all_planar = True
        for s in partition:
            g2 = nx.Graph()
            g2.add_edges_from(
                s)  # todo: possibly implement dynamic programming
            if not planarity_test.is_planar(g2):
                all_planar = False
                break
        if all_planar:
            return True
    return False
Beispiel #4
0
def thickness_bisection(g):
    if planarity_test.is_planar(g):  # shortcut if graph is planar
        return 1

    minn = 0
    maxx = math.ceil(g.number_of_edges() / 8)
    while minn + 1 != maxx:
        avg = math.floor((minn + maxx) / 2)
        if thickness_could_be(avg, g):
            maxx = avg
        else:
            minn = avg
    return maxx
Beispiel #5
0
def draw(G, output):
    planarity.draw(G)
    plt.axis('off')
    plt.savefig(output)


if __name__ == '__main__':
    arguments = docopt(__doc__)
    M = int(arguments['M'])
    N = int(arguments['N'])
    output = arguments['OUTPUT']
    # G = nx.complete_multipartite_graph(M, N)
    # G = nx.complete_graph(M)
    G = nx.gnm_random_graph(M, N)

    if planarity_test.is_planar(G):
        print("Graph is planar")
        try:
            draw(G, output)
            print("Planar graph drawn!")
        except:
            print("Could not draw a planar graph...")
            nx.draw_random(G)
            plt.axis('off')
            plt.savefig(output)
    else:
        if planarity.is_planar(G):
            draw(G, output)
            print("Graph is unexpectedly planar...")
        else:
            nx.draw_random(G)
matplotlib.use('TkAgg')  # nopep8
import matplotlib.pyplot as plt
import planarity
import thickness
import planarity_test as p
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__)
    M = int(arguments['M'])
    N = int(arguments['N'])
    output = arguments['OUTPUT']

    G = nx.gnm_random_graph(M, N)

    try:
        print("Graph is planar")
        planarity.draw(G)
        plt.axis('off')
        plt.savefig(output)
    except:
        print("Graph not planar!")
        nx.draw_random(G)
        plt.axis('off')
        plt.savefig(output)
    if (p.is_planar(G)):
        print("Our algorithm says that the graph is planar")
    else:
        print("thickness:")
        print(thickness.thickness(G))
Beispiel #7
0
def is_planar(g):
    return planarity_test.is_planar(g)
def main():

    times = list()
    vertices = list()
    edges = list()
    N = 50
    M = pg.edges_from_connectivity(N)
    for i in range(100):
        G = pg.random_planar_g(10, 20)

        # D1 = G.dual()

        edges = is_planar(G)
        print "faces"
        for f in G.faces_iterator():
            print f

        print

        G.is_planar()
        print "faces2"
        for f in G.faces_iterator():
            print f


        G.coordinates = FPP_planar_coords(G)
        DrawMyGraph(G, G.coordinates)







# #        G = pg.random_connected_graph(N, M)
# #        #G = pg.load_from_edge_list("test_cross.col")
# #        #G = pg.load_pickled("problem.pic")
# #
# #        #G,x,xp,y,yp = pg.planar_gadget(G)
# #        #print x,xp,y,yp
# #        #G.coordinates = FPP_planar_coords(G)
# #        #DrawMyGraph(G, G.coordinates)
# #
# #        #D = G.dual()
# #        #D.coordinates = FPP_planar_coords(D)
# #
# #        #start, dest = random.sample(D.Vertices(),2)
# #        #print 'from',start,'to',dest
# #        #print shortest_path(D,start,dest)
# #        #print
# #        #DrawMyGraph(D, D.coordinates)
# #
# #        t1 = time.clock()
# #        P = pg.reduce_to_planar_3_coloring(G)
# #        t2 = time.clock()
# #        #if not P: continue
# #
# #        print "size", G.Order(), G.Size(), P.Order(), P.Size(), 'time:', t2 - t1, pg.graph_connectivity(N, M)
# #        times.append(t2 - t1)
# #        vertices.append(P.Order())
# #        edges.append(P.Size())

    # DrawMyGraph(G,pos)
    return times, vertices, edges