Exemplo n.º 1
0
def HouseXGraph():
    """
    Returns a house X graph with 5 nodes.

    A house X graph is a house graph with two additional edges. The
    upper-right corner is connected to the lower-left. And the
    upper-left corner is connected to the lower-right.

    This constructor depends on NetworkX numeric labeling.

    PLOTTING: Upon construction, the position dictionary is filled to
    override the spring-layout algorithm. By convention, the house X
    graph is drawn with the first node in the lower-left corner of the
    house, the second in the lower-right corner of the house. The third
    node is in the upper-left corner connecting the roof to the wall,
    and the fourth is in the upper-right corner connecting the roof to
    the wall. The fifth node is the top of the roof, connected only to
    the third and fourth.

    EXAMPLES: Construct and show a house X graph

    ::

        sage: g = graphs.HouseXGraph()
        sage: g.show() # long time
    """
    pos_dict = {0: (-1, 0), 1: (1, 0), 2: (-1, 1), 3: (1, 1), 4: (0, 2)}
    import networkx
    G = networkx.house_x_graph()
    return graph.Graph(G, pos=pos_dict, name="House Graph")
Exemplo n.º 2
0
def HouseXGraph():
    """
    Returns a house X graph with 5 nodes.

    A house X graph is a house graph with two additional edges. The
    upper-right corner is connected to the lower-left. And the
    upper-left corner is connected to the lower-right.

    This constructor depends on NetworkX numeric labeling.

    PLOTTING: Upon construction, the position dictionary is filled to
    override the spring-layout algorithm. By convention, the house X
    graph is drawn with the first node in the lower-left corner of the
    house, the second in the lower-right corner of the house. The third
    node is in the upper-left corner connecting the roof to the wall,
    and the fourth is in the upper-right corner connecting the roof to
    the wall. The fifth node is the top of the roof, connected only to
    the third and fourth.

    EXAMPLES: Construct and show a house X graph

    ::

        sage: g = graphs.HouseXGraph()
        sage: g.show() # long time
    """
    pos_dict = {0:(-1,0),1:(1,0),2:(-1,1),3:(1,1),4:(0,2)}
    import networkx
    G = networkx.house_x_graph()
    return graph.Graph(G, pos=pos_dict, name="House Graph")
Exemplo n.º 3
0
def GenerateGame(num_players=2, num_mines=2, graph_type='small'):
    if graph_type == 'small':
        graph = nx.house_x_graph()
    elif graph_type == 'star':
        graph = nx.generators.classic.star_graph(num_players * num_mines * 25)
    elif graph_type == 'newman':
        graph = nx.generators.random_graphs.newman_watts_strogatz_graph(num_players * num_mines * 5, num_players * 2, 0.15)
    for node in graph:
        graph.node[node]['mine'] = 0
    print('Graph:', len(graph.nodes()), len(graph.edges()))
    # num_mines = 2
    # num_players = 2
    mine_nodes = np.random.choice(graph.nodes(), num_mines, replace=False)
    for mine in mine_nodes:
        graph.node[mine]['mine'] = 1
    for source, target in graph.edges_iter():
        graph[source][target]['claimed'] = -1
    game = dict()
    game['num_players'] = num_players
    game['current_player'] = 0
    game['player_mines'] = [[] for _ in range(num_players)]
    game['player_score'] = [0] * num_players
    game['player_reward'] = [0] * num_players
    game['player_paths'] = [dict() for _ in range(num_players)]
    game['latest_path_id'] = [0] * num_players
    game['graph'] = graph
    game['mines'] = mine_nodes
    CalcPoints(game)
    return game
def test_line_graph_batch():
    """
    Tests that `line_graph` works with multiple graphs in a batch.
    """
    # Arrange.
    # Create test graphs.
    graph1 = nx.house_graph()
    incidence1 = nx.incidence_matrix(graph1).toarray()
    # We will have to pad the first incidence matrix with two columns,
    # because the second graph has two extra edges.
    incidence1 = np.pad(incidence1, ((0, 0), (0, 2)))
    graph2 = nx.house_x_graph()
    incidence2 = nx.incidence_matrix(graph2).toarray()

    # Act.
    # Get the line graph for both graphs individually, and both together
    # as a batch.
    graph1_line = convolution.line_graph(incidence1)
    graph2_line = convolution.line_graph(incidence2)

    batch = np.stack([incidence1, incidence2], axis=0)
    line_graph_batch = convolution.line_graph(batch)

    # Assert.
    # Both ways of doing it should produce the same results.
    np.testing.assert_array_equal(graph1_line, line_graph_batch[0])
    np.testing.assert_array_equal(graph2_line, line_graph_batch[1])
def test_incidence_matrix_batch():
    """
    Tests that `incidence_matrix` works with multiple
    graphs in a batch.
    """
    # Arrange.
    # Create test graphs.
    graph1 = nx.to_numpy_array(nx.house_graph())
    graph2 = nx.to_numpy_array(nx.house_x_graph())

    # Act.
    # Get the incidence matrix for both graphs individually, and both together
    # as a batch.
    graph1_incidence = convolution.incidence_matrix(graph1)
    # The first graph will be padded with two extra rows columns so that it
    # can be batched with the other one, which has two more edges.
    graph1_incidence = np.pad(graph1_incidence, ((0, 0), (0, 2)))
    graph2_incidence = convolution.incidence_matrix(graph2)

    batch = np.stack([graph1, graph2], axis=0)
    batch_incidence = convolution.incidence_matrix(batch)

    # Assert.
    # Both ways of doing it should produce the same results.
    np.testing.assert_array_equal(graph1_incidence, batch_incidence[0])
    np.testing.assert_array_equal(graph2_incidence, batch_incidence[1])
Exemplo n.º 6
0
    def get_graph_list(num_factors):
        graph_list = []
        # 2, 3 bipartite graph
        g = nx.turan_graph(n=5, r=2)
        graph_list.append(nx.to_numpy_array(g))

        g = nx.house_x_graph()
        graph_list.append(nx.to_numpy_array(g))

        g = nx.balanced_tree(r=3, h=2)
        graph_list.append(nx.to_numpy_array(g))

        g = nx.grid_2d_graph(m=3, n=3)
        graph_list.append(nx.to_numpy_array(g))

        g = nx.hypercube_graph(n=3)
        graph_list.append(nx.to_numpy_array(g))

        g = nx.octahedral_graph()
        graph_list.append(nx.to_numpy_array(g))
        return graph_list[:num_factors]
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 18 16:39:14 2018

@author: Mustafa Hajij and @isMunim

"""
import networkx as nx
from unionfind import *
from kruskalsalgorithm import *

# Loading a graph example from networkx
graph=nx.house_x_graph()

print("The nodes of the graph are given by the list : ")
print(graph.nodes())
print("The edges of the graph are given by the list : ")
print(graph.edges())

# here we are giving weights to the edges
initweight=1
for edge in graph.edges():
    graph.add_edge(edge[0], edge[1], weight=initweight)
    initweight=initweight+1

# check the weights of the edges:
for edge in graph.edges():
    a=edge[0]
    b=edge[1]
    print("edge "+str(edge)+" has  weight " + str(graph[a][b]['weight']) )
    def test_properties_named_small_graphs(self):
        G = nx.bull_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 5
        assert sorted(d for n, d in G.degree()) == [1, 1, 2, 3, 3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 2

        G = nx.chvatal_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 12 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.cubical_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 8 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.desargues_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]

        G = nx.diamond_graph()
        assert G.number_of_nodes() == 4
        assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.dodecahedral_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]
        assert nx.diameter(G) == 5
        assert nx.radius(G) == 5

        G = nx.frucht_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]
        assert nx.diameter(G) == 4
        assert nx.radius(G) == 3

        G = nx.heawood_graph()
        assert G.number_of_nodes() == 14
        assert G.number_of_edges() == 21
        assert list(d for n, d in G.degree()) == 14 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.hoffman_singleton_graph()
        assert G.number_of_nodes() == 50
        assert G.number_of_edges() == 175
        assert list(d for n, d in G.degree()) == 50 * [7]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 6
        assert sorted(d for n, d in G.degree()) == [2, 2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_x_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 8
        assert sorted(d for n, d in G.degree()) == [2, 3, 3, 4, 4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.icosahedral_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 30
        assert (list(
            d for n, d in G.degree()) == [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.krackhardt_kite_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 18
        assert (sorted(
            d for n, d in G.degree()) == [1, 2, 3, 3, 3, 4, 4, 5, 5, 6])

        G = nx.moebius_kantor_graph()
        assert G.number_of_nodes() == 16
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 16 * [3]
        assert nx.diameter(G) == 4

        G = nx.octahedral_graph()
        assert G.number_of_nodes() == 6
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 6 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.pappus_graph()
        assert G.number_of_nodes() == 18
        assert G.number_of_edges() == 27
        assert list(d for n, d in G.degree()) == 18 * [3]
        assert nx.diameter(G) == 4

        G = nx.petersen_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 15
        assert list(d for n, d in G.degree()) == 10 * [3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.sedgewick_maze_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 10
        assert sorted(d for n, d in G.degree()) == [1, 2, 2, 2, 3, 3, 3, 4]

        G = nx.tetrahedral_graph()
        assert G.number_of_nodes() == 4
        assert G.number_of_edges() == 6
        assert list(d for n, d in G.degree()) == [3, 3, 3, 3]
        assert nx.diameter(G) == 1
        assert nx.radius(G) == 1

        G = nx.truncated_cube_graph()
        assert G.number_of_nodes() == 24
        assert G.number_of_edges() == 36
        assert list(d for n, d in G.degree()) == 24 * [3]

        G = nx.truncated_tetrahedron_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]

        G = nx.tutte_graph()
        assert G.number_of_nodes() == 46
        assert G.number_of_edges() == 69
        assert list(d for n, d in G.degree()) == 46 * [3]

        # Test create_using with directed or multigraphs on small graphs
        pytest.raises(nx.NetworkXError,
                      nx.tutte_graph,
                      create_using=nx.DiGraph)
        MG = nx.tutte_graph(create_using=nx.MultiGraph)
        assert sorted(MG.edges()) == sorted(G.edges())
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
    ("bull", nx.bull_graph()),
    ("chvatal", nx.chvatal_graph()),
    ("cubical", nx.cubical_graph()),
    ("desargues", nx.desargues_graph()),
    ("diamond", nx.diamond_graph()),
    ("dodecahedral", nx.dodecahedral_graph()),
    ("frucht", nx.frucht_graph()),
    ("heawood", nx.heawood_graph()),
    ("house", nx.house_graph()),
    ("house_x", nx.house_x_graph()),
    ("icosahedral", nx.icosahedral_graph()),
    ("krackhardt_kite", nx.krackhardt_kite_graph()),
    ("moebius_kantor", nx.moebius_kantor_graph()),
    ("octahedral", nx.octahedral_graph()),
    ("pappus", nx.pappus_graph()),
    ("petersen", nx.petersen_graph()),
    ("sedgewick_maze", nx.sedgewick_maze_graph()),
    ("tetrahedral", nx.tetrahedral_graph()),
    ("truncated_cube", nx.truncated_cube_graph()),
    ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')
Exemplo n.º 10
0
        if 'visited' in g[u][v]:
            del g[u][v]['visited']


if __name__ == '__main__':
    targets = {
        'bull': nx.bull_graph(),  # 1-connected planar
        'chvatal': nx.chvatal_graph(),  # 4-connected non-planar
        'cubical': nx.cubical_graph(),  # 3-connected planar
        'desargues': nx.desargues_graph(),  # 3-connected non-planar
        'diamond': nx.diamond_graph(),  # 2-connected planar
        'dodecahedral': nx.dodecahedral_graph(),  # 3-connected planar
        'frucht': nx.frucht_graph(),  # 3-connected planar
        'heawood': nx.heawood_graph(),  # 3-connected non-planar
        'house': nx.house_graph(),  # 2-connected planar
        'house_x': nx.house_x_graph(),  # 2-connected planar
        'icosahedral': nx.icosahedral_graph(),  # 5-connected planar
        'krackhardt': nx.krackhardt_kite_graph(),  # 1-connected planar
        'moebius': nx.moebius_kantor_graph(),  # non-planar
        'octahedral': nx.octahedral_graph(),  # 4-connected planar
        'pappus': nx.pappus_graph(),  # 3-connected non-planar
        'petersen': nx.petersen_graph(),  # 3-connected non-planar
        'sedgewick': nx.sedgewick_maze_graph(),  # 1-connected planar
        'tetrahedral': nx.tetrahedral_graph(),  # 3-connected planar
        'truncated_cube': nx.truncated_cube_graph(),  # 3-conn. planar
        'truncated_tetrahedron': nx.truncated_tetrahedron_graph(),
        # 3-connected planar
        'tutte': nx.tutte_graph()
    }  # 3-connected planar
    for g_name, g in targets.items():
        print g_name, is_planar(g)
Exemplo n.º 11
0
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
  ("bull", nx.bull_graph()),
  ("chvatal", nx.chvatal_graph()),
  ("cubical", nx.cubical_graph()),
  ("desargues", nx.desargues_graph()),
  ("diamond", nx.diamond_graph()),
  ("dodecahedral", nx.dodecahedral_graph()),
  ("frucht", nx.frucht_graph()),
  ("heawood", nx.heawood_graph()),
  ("house", nx.house_graph()),
  ("house_x", nx.house_x_graph()),
  ("icosahedral", nx.icosahedral_graph()),
  ("krackhardt_kite", nx.krackhardt_kite_graph()),
  ("moebius_kantor", nx.moebius_kantor_graph()),
  ("octahedral", nx.octahedral_graph()),
  ("pappus", nx.pappus_graph()),
  ("petersen", nx.petersen_graph()),
  ("sedgewick_maze", nx.sedgewick_maze_graph()),
  ("tetrahedral", nx.tetrahedral_graph()),
  ("truncated_cube", nx.truncated_cube_graph()),
  ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')

Exemplo n.º 12
0
 def test_house_x(self):
     expected = True
     actual = is_planar(nx.house_x_graph())
     self.assertEqual(expected, actual)
Exemplo n.º 13
0
def small_graphs():
    print("Make small graph")
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [1, 3], [2, 4], [1, 3]]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [3], [4], []]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["edgelist", "C_4", 4, [[1, 2], [3, 4], [2, 3], [4, 1]]])
    draw_graph(G)
    print("LCF graph")
    G = nx.LCF_graph(6, [3, -3], 3)
    draw_graph(G)
    G = nx.LCF_graph(14, [5, -5], 7)
    draw_graph(G)
    print("Bull graph")
    G = nx.bull_graph()
    draw_graph(G)
    print("Chvátal graph")
    G = nx.chvatal_graph()
    draw_graph(G)
    print("Cubical graph")
    G = nx.cubical_graph()
    draw_graph(G)
    print("Desargues graph")
    G = nx.desargues_graph()
    draw_graph(G)
    print("Diamond graph")
    G = nx.diamond_graph()
    draw_graph(G)
    print("Dodechaedral graph")
    G = nx.dodecahedral_graph()
    draw_graph(G)
    print("Frucht graph")
    G = nx.frucht_graph()
    draw_graph(G)
    print("Heawood graph")
    G = nx.heawood_graph()
    draw_graph(G)
    print("House graph")
    G = nx.house_graph()
    draw_graph(G)
    print("House X graph")
    G = nx.house_x_graph()
    draw_graph(G)
    print("Icosahedral graph")
    G = nx.icosahedral_graph()
    draw_graph(G)
    print("Krackhardt kite graph")
    G = nx.krackhardt_kite_graph()
    draw_graph(G)
    print("Moebius kantor graph")
    G = nx.moebius_kantor_graph()
    draw_graph(G)
    print("Octahedral graph")
    G = nx.octahedral_graph()
    draw_graph(G)
    print("Pappus graph")
    G = nx.pappus_graph()
    draw_graph(G)
    print("Petersen graph")
    G = nx.petersen_graph()
    draw_graph(G)
    print("Sedgewick maze graph")
    G = nx.sedgewick_maze_graph()
    draw_graph(G)
    print("Tetrahedral graph")
    G = nx.tetrahedral_graph()
    draw_graph(G)
    print("Truncated cube graph")
    G = nx.truncated_cube_graph()
    draw_graph(G)
    print("Truncated tetrahedron graph")
    G = nx.truncated_tetrahedron_graph()
    draw_graph(G)
    print("Tutte graph")
    G = nx.tutte_graph()
    draw_graph(G)