Exemplo n.º 1
0
def get_pos(g, N, i):
    is_planar, certificate = check_planarity(g, counterexample=True)
    pos = get_tutte_pos(g, certificate)
    scale_factor = 1.0 / N
    t = np.array([i % N, i // N]) * scale_factor
    pos = {v: 0.35 * scale_factor * p + t for v, p in pos.items()}
    return pos
Exemplo n.º 2
0
def check_graph_planarity(path):
    (v_num, edges) = loadWeightedGraph(path)

    graph = nx.Graph()

    nodes = [i for i in range(1, v_num+1)]
    graph.add_nodes_from(nodes)

    graph.add_weighted_edges_from(edges)

    print(1) if check_planarity(graph)[0] else print(0)
Exemplo n.º 3
0
def random_planar(n=100, trials=300, seed=0):
    """ A random graph generator based on Markov chain:
        (Ref.) A. Denise, M. Vasconcellos, D. J. A. Welsh, (1996).
                "The Random Planar Graph", Congressus Numerantium.
    """
    random.seed(seed)
    g = nx.empty_graph(n)
    for i in xrange(trials):
        f = random.sample(xrange(n), 2)
        if g.has_edge(*f):
            g.remove_edge(*f)
            yield g
        else:
            g.add_edge(*f)
            is_planar, _ = check_planarity(g)
            if not is_planar:
                g.remove_edge(*f)
            else:
                yield g
Exemplo n.º 4
0
def vs_check_planarity(trials=100):
    N = [
        5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600,
        700, 800, 900, 1000
    ]
    #    N = [10, 50, 100, 500, 1000, 1500, 5000, 10000, 50000, 100000]
    ch_p, is_p = [], []
    for n in N:
        print(n)
        cp, ip = 0, 0
        p = 0
        for i in range(trials):
            g = nx.fast_gnp_random_graph(n, 5. / n)

            s = time.time()
            actual = is_planar(g)
            e = time.time()
            expected, _ = check_planarity(g)
            cp += time.time() - e
            ip += e - s

            if expected != actual:
                nx.write_gml(g, 'g.gml')
                print(expected, actual)
            if expected:
                p += 1
            g.clear()
        ch_p.append(float(cp) / trials)
        is_p.append(float(ip) / trials)


#        print float(p) / trials
    plt.plot(N, ch_p, label='check_planarity', marker='o')
    plt.plot(N, is_p, label='is_planar', marker='o')
    plt.legend(loc='best')
    plt.xscale('log')
    plt.tight_layout()
    #    plt.savefig('vs_check_planarity_1.png', bbox_inches='tight')
    plt.show()
Exemplo n.º 5
0
    key = [edge for edge in multiple_edges if edge['color'] == color]
    graph.remove_edge(u, v, key=key)

    return

G = nx.MultiGraph()
# the_colored_graph.add_edge(v1, v2, "red")
G.add_edge(1, 2, key="blue")
G.add_edge(2, 1, key="red")
G.add_edge(2, 3, key="red")
G.add_edge(4, 2, key="green")
G.add_edge(2, 4, key="blue")

print (G.edges(data=True, keys=True))

G.remove_edge(2, 4, key="blue")

print (G.edges(data=True, keys=True))

# Does not work G[1][2]['red']="aaa"

print (G.edges(data=True, keys=True))

expected, aaa = check_planarity(G)
print("---")
print(expected, aaa)
print("---")

nx.draw(G)
pyplot.show()
Exemplo n.º 6
0
import networkx as nx
from lab7.dimacs import loadWeightedGraph
from networkx.algorithms.planarity import check_planarity
import os


def toNxGraph(name):
    myG = loadWeightedGraph(name)
    G = nx.Graph()
    G.add_nodes_from([x for x in range(1, myG[0] + 1)])
    for edge in myG[1]:
        G.add_edge(edge[0], edge[1])
    return G


graphs = []
for name in os.listdir("graphs/plnar"):
    graphs.append(f"graphs/plnar/{name}")

for graph in graphs:
    G = toNxGraph(graph)

    res = check_planarity(G)[0]
    print(graph, "\t", res)


Exemplo n.º 7
0
        line2 = line.replace(";", "")
        vertex = int(line2.split(":")[0])
        edges = line2.split(":")[1].split(" ")

        for j in range(1, len(edges)):
            q = int(edges[j])
            if vertex < q:
                G.add_edge(vertex, q)
        i = i + 1

    print "Graph = ", graph
    print G.number_of_nodes()
    print G.number_of_edges()

    isPlanar, PG = planarity.check_planarity(G)
    #    print isPlanar
    #    print PG.check_structure()
    CCW = PG.get_data()

    #
    # This code takes the radial of G and stores it in the graph I.
    #
    I.clear()
    # Number of edges in original graph. We will be adding additional points to
    # take the radial.
    n = G.number_of_nodes()
    marked_edges = set()

    for v in CCW:
        for w in CCW[v]:
Exemplo n.º 8
0
import networkx as nx
from dimacs import loadWeightedGraph
import matplotlib.pyplot as plt

G = nx.Graph()

filename = 'graphs-lab6/plnar/clique20'

(V, L) = loadWeightedGraph(filename)

v = [i for i in range(V + 1)]

G.add_nodes_from(v)

for a, b, w in L:
    G.add_edge(a, b)

from networkx.algorithms.planarity import check_planarity

print(check_planarity(G)[0])

pos = nx.spring_layout(G)

nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
plt.show()
#!/usr/bin/env python
import matplotlib.pyplot as pyplot
import networkx as nx
from networkx.algorithms.planarity import check_planarity

G = nx.MultiGraph()
# the_colored_graph.add_edge(v1, v2, "red")
G.add_edge(1, 2, key="red")
G.add_edge(1, 3, key="green")
G.add_edge(1, 4, key="blue")

G.add_edge(2, 3, key="blue")
G.add_edge(3, 4, key="red")
G.add_edge(4, 2, key="green")

print(G.edges(data=True, keys=True))

is_it_planar, embedding = check_planarity(G, counterexample=True)

print("---")
print(embedding.edges, "---", G.degree)
print("---")

nx.draw(G)
pyplot.show()
Exemplo n.º 10
0
def relax(pos, cycle):
    n_rounds, d_threshold = 200, 0.001
    pos = pos + [[100, 100], [100, -100], [-100, 0]]
    for i in range(n_rounds):
        if centroidal(pos, cycle) < d_threshold:
            break
    return pos[:-3]


def getpos(g, cid=0):
    c = get_cycle(g, cid)
    fix_outer_cycle_pos(g, c)
    return relax(fix_all_pos(g), c)


if __name__ == '__main__':
    g = nx.chvatal_graph()
    pos = getpos(g)
    nx.draw(g, pos, alpha=0.25)

    is_planar, certificate = check_planarity(g, counterexample=True)
    if is_planar:
        print 'planar'
    else:
        print 'non planar'
        nx.draw_networkx(certificate, pos, node_color='#99ff99')

    plt.gca().set_aspect('equal')
    plt.savefig('ex_nx_check_planarity_1.png', bbox_inches='tight')
    plt.show()
Exemplo n.º 11
0
def check_planar(G):
    return check_planarity(G)[0]