Python networkx.random_regular_graph() Examples

The following are 7 code examples of networkx.random_regular_graph(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: generate_data.py    From ggnn.pytorch with MIT License 5 votes vote down vote up
def generate_eulerian_circuit_data(options):

    while True:
        num_nodes = options["num_entities"]
        g = nx.random_regular_graph(2, num_nodes)

        try:
            path = list(nxalg.eulerian_circuit(g))
        except:
            continue

        # print path

        break

    for edge in g.edges():
        print "%s connected-to %s" % (edge[0], edge[1])
        print "%s connected-to %s" % (edge[1], edge[0])

    first_edge = path[0]

    node_list = [str(edge[0]) for edge in path]
    print "eval eulerian-circuit %s %s\t%s" % (first_edge[0], first_edge[1],
                                               ",".join(node_list))


##################### noisy data ####################### 
Example #2
Source File: generate_data.py    From ggnn.pytorch with MIT License 5 votes vote down vote up
def generate_noisy_eulerian_circuit_data(options):
    """
    This is a noisy version of the eularian circuit problem.
    """
    while True:
        num_nodes = options["num_entities"]
        g = nx.random_regular_graph(2, num_nodes)

        try:
            path = list(nxalg.eulerian_circuit(g))
        except:
            continue
        break

    edges = g.edges()

    # generate another misleading cycle
    num_confusing = options["num_confusing"]
    if num_confusing > 0:
        g_confusing = nx.random_regular_graph(2, num_confusing)

        for e in g_confusing.edges():
            edges.append((e[0] + num_nodes, e[1] + num_nodes))

    random.shuffle(edges)

    # randomize index
    idx = _generate_random_node_index(num_nodes + num_confusing)
    new_edges = _relabel_nodes_in_edges(edges, idx)
    new_path = _relabel_nodes_in_edges(path, idx)

    for edge in new_edges:
        print "%s connected-to %s" % (edge[0], edge[1])
        print "%s connected-to %s" % (edge[1], edge[0])

    first_edge = new_path[0]

    node_list = [str(edge[0]) for edge in new_path]
    print "eval eulerian-circuit %s %s\t%s" % (first_edge[0], first_edge[1],
                                               ",".join(node_list)) 
Example #3
Source File: generate_data.py    From ggnn.pytorch with MIT License 5 votes vote down vote up
def generate_eulerian_circuit_data(options):

    while True:
        num_nodes = options["num_entities"]
        g = nx.random_regular_graph(2, num_nodes)

        try:
            path = list(nxalg.eulerian_circuit(g))
        except:
            continue

        # print path

        break

    for edge in g.edges():
        print "%s connected-to %s" % (edge[0], edge[1])
        print "%s connected-to %s" % (edge[1], edge[0])

    first_edge = path[0]

    node_list = [str(edge[0]) for edge in path]
    print "eval eulerian-circuit %s %s\t%s" % (first_edge[0], first_edge[1],
                                               ",".join(node_list))


##################### noisy data ####################### 
Example #4
Source File: generate_data.py    From ggnn.pytorch with MIT License 5 votes vote down vote up
def generate_noisy_eulerian_circuit_data(options):
    """
    This is a noisy version of the eularian circuit problem.
    """
    while True:
        num_nodes = options["num_entities"]
        g = nx.random_regular_graph(2, num_nodes)

        try:
            path = list(nxalg.eulerian_circuit(g))
        except:
            continue
        break

    edges = g.edges()

    # generate another misleading cycle
    num_confusing = options["num_confusing"]
    if num_confusing > 0:
        g_confusing = nx.random_regular_graph(2, num_confusing)

        for e in g_confusing.edges():
            edges.append((e[0] + num_nodes, e[1] + num_nodes))

    random.shuffle(edges)

    # randomize index
    idx = _generate_random_node_index(num_nodes + num_confusing)
    new_edges = _relabel_nodes_in_edges(edges, idx)
    new_path = _relabel_nodes_in_edges(path, idx)

    for edge in new_edges:
        print "%s connected-to %s" % (edge[0], edge[1])
        print "%s connected-to %s" % (edge[1], edge[0])

    first_edge = new_path[0]

    node_list = [str(edge[0]) for edge in new_path]
    print "eval eulerian-circuit %s %s\t%s" % (first_edge[0], first_edge[1],
                                               ",".join(node_list)) 
Example #5
Source File: test_utils.py    From quantumflow with Apache License 2.0 5 votes vote down vote up
def test_graph6():
    graph0 = nx.random_regular_graph(4, 10)
    g0 = to_graph6(graph0)
    graph1 = from_graph6(g0)
    g1 = to_graph6(graph1)

    assert g0 == g1 
Example #6
Source File: graph_generate.py    From quantumflow with Apache License 2.0 4 votes vote down vote up
def _cli():

    parser = argparse.ArgumentParser(
        description=__description__)

    parser.add_argument('--version', action='version', version=__version__)

    parser.add_argument('-d', '--degree', type=float, action='store',
                        help='Degree')

    parser.add_argument('--family', type=str, action='store', default='er',
                        help='Graph family')

    parser.add_argument('N', type=int, action='store', help='Nodes')

    parser.add_argument('S', type=int, action='store', help='Samples')

    parser.add_argument('fout', action='store',
                        metavar='OUT_FILE', help='Write graphs to file')

    opts = vars(parser.parse_args())
    N = opts.pop('N')
    S = opts.pop('S')
    fout = opts.pop('fout')

    degree = opts.pop('degree')
    family = opts.pop('family')
    assert family in {'er', 'reg'}

    if family == 'reg':
        assert degree is not None
        degree = int(degree)

    with open(fout, 'w') as file:
        for _ in range(S):
            if family == 'er':
                graph = nx.gnp_random_graph(N, 0.5)
            elif family == 'reg':
                graph = nx.random_regular_graph(int(degree), N)
            else:
                assert False
            file.write(to_graph6(graph))
            file.write('\n') 
Example #7
Source File: utilities.py    From entropica_qaoa with Apache License 2.0 3 votes vote down vote up
def random_k_regular_graph(degree: int,
                           nodes: List[Union[int, QubitPlaceholder]],
                           seed: int = None,
                           weighted: bool = False,
                           biases: bool = False) -> nx.Graph:
    """
    Produces a random graph with specified number of nodes, each having degree k.

    Parameters
    ----------
    degree:
        Desired degree for the nodes
    nodes:
        The node set of the graph. Can be anything that works as a qubit for
        PauliSums.
    seed:
        A seed for the random number generator
    weighted:
        Whether the edge weights should be uniform or different. If false, all weights are set to 1.
        If true, the weight is set to a random number drawn from the uniform distribution in the interval 0 to 1.
        If true, the weight is set to a random number drawn from the uniform
        distribution in the interval 0 to 1.
    biases:
        Whether or not the graph nodes should be assigned a weight.
        If true, the weight is set to a random number drawn from the uniform
        distribution in the interval 0 to 1.

    Returns
    -------
    nx.Graph:
        A graph with the properties as specified.

    """
    np.random.seed(seed=seed)

    # create a random regular graph on the nodes
    G = nx.random_regular_graph(degree, len(nodes), seed)
    nx.relabel_nodes(G, {i: n for i, n in enumerate(nodes)})

    for edge in G.edges():
        if not weighted:
            G[edge[0]][edge[1]]['weight'] = 1
        else:
            G[edge[0]][edge[1]]['weight'] = np.random.rand()

    if biases:
        for node in G.nodes():
            G.node[node]['weight'] = np.random.rand()

    return G