def random_connected_graph(n, m):
    """ Return the random connected graph G_{n,m}.

    Gives a graph picked randomly out of the set of all graphs
    with n nodes and m edges.

    Parameters
    ----------
    n : int
        The number of nodes.
    m : int
        The number of edges.

    """
    G = Graph()
    V = G + n  # add n vertices

    max_edges = int((n * (n - 1.0)) / 2.0)
    m = min(m, max_edges)


    # add the first connection line, (n-1) edges, assuring a connected graph
    for u, v in pairwise(V):
        G.add_edge(u, v)

    AddEdge = G.add_edge
    E_star = set_copy(combinations(G.vertices, 2))

    for u, v in random.sample(E_star - G.edges, m - n + 1):
        AddEdge(u, v)

    return G
Example #2
0
def load_from_edge_list(strFileName, G = None):
    if G is None: G = Graph()

    with open(strFileName, 'r') as f:
        lines = f.readlines()

    if not G: G = Graph()
    v_list = dict()

    for line in lines:
        if len(line) == 0 or line == '\n': continue
        if line == "": continue
        fields = line.split()
        if not len(fields): continue
        if fields[0] == 'e':
            u = int(fields[1])
            v = int(fields[2])

            if u not in v_list: v_list[u] = G.add_vertex()
            if v not in v_list: v_list[v] = G.add_vertex()

            x, y = v_list[u], v_list[v]
            G.add_edge(x, y)



    return G
def complete_graph(N):
    G = Graph()
    G + N  # add N vertices

    for u, v in combinations(G.Vertices(), 2):
        G.add_edge(u, v)

    return G
Example #4
0
def from_nx_graph(G):
    """
    Convert a graph from networkx graph to a custom class
    """

    from planegraphs import Graph
    H = Graph()
    for v in G: H.add_named_vertex(v)
    for u, v in G.edges_iter(): H.add_edge(u, v)

    return H
def octahedron():
    G = Graph()

    v, w, x, y, z1, z2 = (G.add_vertex() for i in xrange(6))

    for i, j in pairwise((v, w, x, y, v)):
        G.add_edge(i, j)
        G.add_edge(i, z1)
        G.add_edge(i, z2)

    G.is_planar()
    return G
def random_briggs_graph(n, m):
    """Return the random graph G_{n,m}.

    Gives a graph picked randomly out of the set of all graphs
    with n nodes and m edges.

    Parameters
    ----------
    n : int
        The number of nodes.
    m : int
        The number of edges.

    Notes
    -----
    Algorithm by Keith M. Briggs Mar 31, 2006.
    Inspired by Knuth's Algorithm S (Selection sampling technique),
    in section 3.4.2 of

    References
    ----------
    .. [1] Donald E. Knuth,
        The Art of Computer Programming,
        Volume 2 / Seminumerical algorithms
        Third Edition, Addison-Wesley, 1997.

    """
    mmax = n * (n - 1) / 2
    if m >= mmax:
        G = complete_graph(n)
    else:
        G = Graph()
        G + n

    if n == 1 or m >= mmax:
        return G

    u = 0
    v = 1
    t = 0
    k = 0
    while True:
        if random.randrange(mmax - t) < m - k:
            G.add_edge(u, v)
            k += 1
            if k == m: return G
        t += 1
        v += 1
        if v == n:  # go to next row of adjacency matrix
            u += 1
            v = u + 1
Example #7
0
def prepare_grid(G):
    # ensure that k is odd
    if len(G) % 2 == 0:
        G.add_named_vertex('dummykcol')
        for v in G.vertices - {'dummykcol'}: G.add_edge('dummykcol', v)

    H = Graph()
    # Create the color blue
    H.add_named_vertex('b')

    H.add_named_vertex('g')
    H.add_edge('b', 'g')

    return G, H
def random_planar_graph(n, m):
    G = Graph()

    for _v in xrange(n):
        G.add_vertex()

    Edges = get_maximal_planar_edges(G, n, 0)

    for _i in xrange(m):
        pos = random.randint(0, len(Edges) - 1)
        G.add_edge(Edges[pos][0], Edges[pos][1])
        del Edges[pos]

    return G
Example #9
0
def load_from_edge_list_named(strFileName, G = None):
    with open(strFileName, 'r') as f:
        lines = f.readlines()

    if G is None: G = Graph()

    for line in lines:
        if len(line) == 0 or line == '\n': continue
        fields = line.split()
        if not len(fields): continue
        if fields[0] == 'e':
            u, v = nvertex(fields[1]), nvertex(fields[2])
            if u not in V(G):
                G.add_named_vertex(u)
            if v not in V(G):
                G.add_named_vertex(v)
            G.add_edge(u, v)


    G.set_vertex_index()
    return G
Example #10
0
def reduction_from_kcol_to_3col(G, k):
    # add the main gadget

    T = lambda *x: "dummy(" + ",".join(map(str, x)) + ')'
    V = lambda v, c: str(v) + '(' + str(c) + ')'
    if k == 3: return G
    H = Graph()
    # Create the color blue
    H.add_named_vertex('c1')
    H.add_named_vertex('c2')
    H.add_named_vertex('c3')
    H.add_edge('c1', 'c2')
    H.add_edge('c1', 'c3')
    H.add_edge('c2', 'c3')

    # add vertices
    for v in G:
        H.add_named_vertex(V(v, 0))
        H.add_edge(V(v, 0), 'c1')
        H.add_edge(V(v, 0), 'c3')

        H.add_named_vertex(V(v, k))
        H.add_edge(V(v, k), 'c2')
        H.add_edge(V(v, k), 'c3')

        for i in range(1, k):
            H.add_named_vertex(V(v, i))
            H.add_edge('c3', V(v, i))

    for u, v in G.edges:
        for i in range(1, k + 1):
            H.add_named_vertex(T(u, v, u, i))
            H.add_edge(V(u, i - 1), T(u, v, u, i))
            H.add_edge(V(u, i), T(u, v, u, i))

            H.add_named_vertex(T(u, v, v, i))
            H.add_edge(V(v, i - 1), T(u, v, v, i))
            H.add_edge(V(v, i), T(u, v, v, i))
            H.add_edge(T(u, v, u, i), T(u, v, v, i))

    H.set_vertex_index()

    return H
Example #11
0
def reduce_3sat_to_3col(instance, G = None):
    """reduces a 3sat instance to a graph 3-coloring instance
       receives a graph G
        
      for each clause (a,b,c)
      gadget:
      (-a)----(a)---(g1)
                     | \
                     | (g3)---(g4)    (X)             
                     | /       | \   / |
      (-b)----(b)---(g2)       |  (T)  |
                               | /   \ |
      (-c)----(c)-------------(g5)    (F)
      
      X is adjacent to all variables
    """
    G = Graph() if G is None else G

    # add common gadget
    G.add_named_vertex('T')
    G.add_named_vertex('F')
    G.add_named_vertex('X')
    G.add_edge('T', 'F')
    G.add_edge('F', 'X')
    G.add_edge('X', 'T')


    # add gadget for variables
    variables = sorted(set([abs(v) for clause in instance for v in clause]))

    for v in variables:
        G.add_named_vertex(v)
        G.add_named_vertex(-v)
        G.add_edge('X', v)
        G.add_edge('X', -v)
        G.add_edge(v, -v)

    G.set_vertex_index(max(variables) + 1)
    # add the clause gadgets
    for a, b, c in instance:
        g1, g2, g3, g4, g5 = [G.add_vertex() for _i in range(5)]

        # triangle 1,2,3
        G.add_edge(g1, g2)  # 1
        G.add_edge(g2, g3)  # 2
        G.add_edge(g3, g1)  # 3

        # bridge betwen triangle 1,2,3T  and 4,5,T
        G.add_edge(g3, g4)  # 4

        # triangle 3,4,5
        G.add_edge(g4, g5)  # 5
        G.add_edge(g5, 'T')  # 6
        G.add_edge('T', g4)  # 7

        # edges for clause a,b,c
        G.add_edge(a, g1)  # 8
        G.add_edge(b, g2)  # 9
        G.add_edge(c, g5)  # 10

    return G