예제 #1
0
 def test_single_node(self):
     # TODO Is this really the intended behavior for providing a
     # single node as the argument `nodes`? Shouldn't the function
     # just return the connectivity value itself?
     G = nx.trivial_graph()
     conn = nx.average_degree_connectivity(G, nodes=0)
     assert conn == {0: 0}
예제 #2
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     independent_set, cliques = clique_removal(G)
     assert is_independent_set(G, independent_set)
     assert all(is_clique(G, clique) for clique in cliques)
     # In fact, we should only have 1-cliques, that is, singleton nodes.
     assert all(len(clique) == 1 for clique in cliques)
예제 #3
0
    def test_trivial_path(self):
        """Tests that the trivial path, a path of length one, is
        considered a simple path in a graph.

        """
        G = nx.trivial_graph()
        assert_true(nx.is_simple_path(G, [0]))
예제 #4
0
def test_ramsey():
    # this should only find the complete graph
    graph = nx.complete_graph(10)
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    assert cdens == 1.0, "clique not correctly found by ramsey!"
    idens = nx.density(graph.subgraph(i))
    assert idens == 0.0, "i-set not correctly found by ramsey!"

    # this trival graph has no cliques. should just find i-sets
    graph = nx.trivial_graph()
    c, i = apxa.ramsey_R2(graph)
    assert c == {0}, "clique not correctly found by ramsey!"
    assert i == {0}, "i-set not correctly found by ramsey!"

    graph = nx.barbell_graph(10, 5, nx.Graph())
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    assert cdens == 1.0, "clique not correctly found by ramsey!"
    idens = nx.density(graph.subgraph(i))
    assert idens == 0.0, "i-set not correctly found by ramsey!"

    # add self-loops and test again
    graph.add_edges_from([(n, n) for n in range(0, len(graph), 2)])
    cc, ii = apxa.ramsey_R2(graph)
    assert cc == c
    assert ii == i
예제 #5
0
    def test_trivial_nonpath(self):
        """Tests that a list whose sole element is an object not in the
        graph is not considered a simple path.

        """
        G = nx.trivial_graph()
        assert_false(nx.is_simple_path(G, ['not a node']))
예제 #6
0
def test_clique_removal():
    graph = nx.complete_graph(10)
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by clique_removal!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")

    graph = nx.trivial_graph(nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    # we should only have 1-cliques. Just singleton nodes.
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 0.0, "clique not found by clique_removal!")

    graph = nx.barbell_graph(10, 5, nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")
예제 #7
0
    def test_trivial_path(self):
        """Tests that the trivial path, a path of length one, is
        considered a simple path in a graph.

        """
        G = nx.trivial_graph()
        assert_true(nx.is_simple_path(G, [0]))
예제 #8
0
    def test_trivial_nonpath(self):
        """Tests that a list whose sole element is an object not in the
        graph is not considered a simple path.

        """
        G = nx.trivial_graph()
        assert_false(nx.is_simple_path(G, ['not a node']))
예제 #9
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     independent_set, cliques = clique_removal(G)
     assert_true(is_independent_set(G, independent_set))
     assert_true(all(is_clique(G, clique) for clique in cliques))
     # In fact, we should only have 1-cliques, that is, singleton nodes.
     assert_true(all(len(clique) == 1 for clique in cliques))
예제 #10
0
 def test_single_node(self):
     # TODO Is this really the intended behavior for providing a
     # single node as the argument `nodes`? Shouldn't the function
     # just return the connectivity value itself?
     G = nx.trivial_graph()
     conn = nx.average_degree_connectivity(G, nodes=0)
     assert_equal(conn, {0: 0})
예제 #11
0
def test_clique_removal():
    graph = nx.complete_graph(10)
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by clique_removal!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")

    graph = nx.trivial_graph(nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    # we should only have 1-cliques. Just singleton nodes.
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 0.0, "clique not found by clique_removal!")

    graph = nx.barbell_graph(10, 5, nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")
예제 #12
0
def classic_graphs():
    print("Balanced Tree")
    BG = nx.balanced_tree(3, 2)
    draw_graph(BG)
    print("Barbell Graph")
    BBG = nx.barbell_graph(3, 2)
    draw_graph(BBG)
    print("Complete Graph")
    CG = nx.complete_graph(10)
    draw_graph(CG)
    print("Complete Multipartite Graph")
    CMG = nx.complete_multipartite_graph(1, 2, 10)
    print([CMG.node[u]['block'] for u in CMG])
    print(CMG.edges(0))
    print(CMG.edges(2))
    print(CMG.edges(4))
    draw_graph(CMG)
    print("Circular Ladder Graph")
    CLG = nx.circular_ladder_graph(5)
    draw_graph(CLG)
    print("Dorogovtsev Goltsev Mendes Graph")
    DGMG = nx.dorogovtsev_goltsev_mendes_graph(3)
    draw_graph(DGMG)
    print("Empty Graph")
    EG = nx.empty_graph(5, create_using=nx.DiGraph())
    draw_graph(EG)
    print("Grid 2D Graph")
    G2DG = nx.grid_2d_graph(5, 6)
    draw_graph(G2DG)
    print("Grid Graph")
    GDG = nx.grid_graph(dim=[5, 2])
    draw_graph(GDG)
    print("Hypercube Graph")
    HG = nx.hypercube_graph(3)
    draw_graph(HG)
    print("Ladder Graph")
    LG = nx.ladder_graph(8)
    draw_graph(LG)
    print("Ladder Graph")
    LG = nx.ladder_graph(8)
    draw_graph(LG)
    print("Lollipop Graph")
    LPG = nx.lollipop_graph(n=6, m=4)
    draw_graph(LPG)
    print("Null Graph")
    NG = nx.null_graph()
    draw_graph(NG)
    print("Path Graph")
    PG = nx.path_graph(16)
    draw_graph(PG)
    print("Star Graph")
    SG = nx.star_graph(16)
    draw_graph(SG)
    print("Trivial Graph")
    TG = nx.trivial_graph()
    draw_graph(TG)
    print("Wheel Graph")
    WG = nx.wheel_graph(n=18)
    draw_graph(WG)
예제 #13
0
    def test_empty_list(self):
        """Tests that the empty list is not a valid path, since there
        should be a one-to-one correspondence between paths as lists of
        nodes and paths as lists of edges.

        """
        G = nx.trivial_graph()
        assert_false(nx.is_simple_path(G, []))
예제 #14
0
    def test_empty_list(self):
        """Tests that the empty list is not a valid path, since there
        should be a one-to-one correspondence between paths as lists of
        nodes and paths as lists of edges.

        """
        G = nx.trivial_graph()
        assert_false(nx.is_simple_path(G, []))
예제 #15
0
    def test_trivial_graph(self):
        """Tests that the trivial graph has average path length zero,
        since there is exactly one path of length zero in the trivial
        graph.

        For more information, see issue #1960.

        """
        G = nx.trivial_graph()
        assert_equal(nx.average_shortest_path_length(G), 0)
예제 #16
0
    def test_trivial_graph(self):
        """Tests that the trivial graph has average path length zero,
        since there is exactly one path of length zero in the trivial
        graph.

        For more information, see issue #1960.

        """
        G = nx.trivial_graph()
        assert_equal(nx.average_shortest_path_length(G), 0)
예제 #17
0
    def crossover_two_graph(self, g1, g2, pr=0.5):

        g3 = nx.trivial_graph()
        g3.add_nodes_from(g1.nodes())
        g3.add_edges_from(g1.edges() & g2.edges())

        diff_g1_g2 = g1.edges() - g2.edges()
        diff_g2_g1 = g2.edges() - g2.edges()
        g3.add_edges_from(random.sample(diff_g1_g2, int(len(diff_g1_g2) * pr)))
        g3.add_edges_from(random.sample(diff_g2_g1, int(len(diff_g2_g1) * pr)))

        return g3
예제 #18
0
파일: graph.py 프로젝트: gharib85/qsim
def ring_graph(n, node_weight=1, edge_weight=1, return_mis=False):
    g = nx.Graph()
    g.add_nodes_from(np.arange(0, n), weight=node_weight)
    if n == 1:
        return nx.trivial_graph()
    else:
        for i in range(n - 1):
            g.add_edge(i, i + 1, weight=edge_weight)
        g.add_edge(0, n - 1, weight=edge_weight)
    if return_mis:
        return Graph(g), np.floor(n / 2)
    return Graph(g)
예제 #19
0
 def test_all_invariants_with_trivial_graph(self):
     trivial = nx.trivial_graph()
     for inv in inv_num.InvariantNum().all:
         self.assertTrue(
             isinstance(inv.calculate(trivial),
                        (float, int, numpy.int32, numpy.int64)))
     for inv in inv_bool.InvariantBool().all:
         self.assertTrue(isinstance(inv.calculate(trivial), bool))
     for inv in inv_other.InvariantOther().all:
         self.assertTrue(
             isinstance(inv.calculate(trivial),
                        (numpy.ndarray, list, tuple, dict, str, set)))
     for op in oper.GraphOperations().all:
         self.assertTrue(isinstance(op.calculate(trivial), nx.Graph))
예제 #20
0
def test_ramsey():
    # this should only find the complete nxgraph
    graph = nx.complete_graph(10)
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    eq_(cdens, 1.0, "clique not found by ramsey!")
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")

    # this trival nxgraph has no cliques. should just find i-sets
    graph = nx.trivial_graph(nx.Graph())
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    eq_(cdens, 0.0, "clique not found by ramsey!")
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")

    graph = nx.barbell_graph(10, 5, nx.Graph())
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    eq_(cdens, 1.0, "clique not found by ramsey!")
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
예제 #21
0
def test_ramsey():
    # this should only find the complete graph
    graph = nx.complete_graph(10)
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    assert cdens == 1.0, "clique not found by ramsey!"
    idens = nx.density(graph.subgraph(i))
    assert idens == 0.0, "i-set not found by ramsey!"

    # this trival graph has no cliques. should just find i-sets
    graph = nx.trivial_graph(nx.Graph())
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    assert cdens == 0.0, "clique not found by ramsey!"
    idens = nx.density(graph.subgraph(i))
    assert idens == 0.0, "i-set not found by ramsey!"

    graph = nx.barbell_graph(10, 5, nx.Graph())
    c, i = apxa.ramsey_R2(graph)
    cdens = nx.density(graph.subgraph(c))
    assert cdens == 1.0, "clique not found by ramsey!"
    idens = nx.density(graph.subgraph(i))
    assert idens == 0.0, "i-set not found by ramsey!"
예제 #22
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     result = BytesIO()
     nx.write_sparse6(G, result)
     self.assertEqual(result.getvalue(), b'>>sparse6<<:@\n')
예제 #23
0
    def trivialGraph(self):
        self.figure.clf()
        self.the_graph = nx.trivial_graph()

        nx.draw(self.the_graph, with_labels=True)
        self.canvas.draw_idle()
예제 #24
0
     'name': 'Star Graph',
     'args': ('n', ),
     'argtypes': (int, ),
     'argvals': (3, ),
     'gen': lambda n: nx.star_graph(n),
     'description_fn': 'star_graph(n)',
     'description': 'Return the star graph',
 },
 'trivial_graph': {
     'name':
     'Trivial Graph',
     'args': (),
     'argtypes': (),
     'argvals': (),
     'gen':
     nx.trivial_graph(),
     'description_fn':
     'trivial_graph()',
     'description':
     'Return the Trivial graph with one node (with label 0) and no edges.',
 },
 'turan_graph': {
     'name': 'Turan Graph',
     'args': ('n', 'r'),
     'argtypes': (
         int,
         int,
     ),
     'argvals': (3, 3),
     'gen': lambda n, r: nx.turan_graph(n, r),
     'description_fn': 'turan_graph(n, r)',
예제 #25
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     result = BytesIO()
     nx.write_sparse6(G, result)
     assert result.getvalue() == b'>>sparse6<<:@\n'
예제 #26
0
 def test_trivial_graph(self):
     nx.to_prufer_sequence(nx.trivial_graph())
예제 #27
0
 def test_trivial(self):
     expected = True
     actual = is_planar(nx.trivial_graph())
     self.assertEqual(expected, actual)
예제 #28
0
 def test_trivial_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.trivial_graph(), result)
     assert result.getvalue() == b">>graph6<<@\n"
예제 #29
0
 def test_trivial_graph(self):
     with pytest.raises(nx.NetworkXPointlessConcept):
         nx.to_prufer_sequence(nx.trivial_graph())
예제 #30
0
n = 6
hypercube_n = 4
m = 6
r = 2
h = 3
dim = [2, 3]

# left out dorogovtsev_goltsev_mendes_graph and null_graph

graphs = [
    ("balanced_tree", nx.balanced_tree(r, h)),
    ("barbell_graph", nx.barbell_graph(n, m)),
    ("complete_graph", nx.complete_graph(n)),
    ("complete_bipartite_graph", nx.complete_bipartite_graph(n, m)),
    ("circular_ladder_graph", nx.circular_ladder_graph(n)),
    ("cycle_graph", nx.cycle_graph(n)),
    ("empty_graph", nx.empty_graph(n)),
    ("grid_2d_graph", nx.grid_2d_graph(m, n)),
    ("grid_graph", nx.grid_graph(dim)),
    ("hypercube_graph", nx.hypercube_graph(hypercube_n)),
    ("ladder_graph", nx.ladder_graph(n)),
    ("lollipop_graph", nx.lollipop_graph(m, n)),
    ("path_graph", nx.path_graph(n)),
    ("star_graph", nx.star_graph(n)),
    ("trivial_graph", nx.trivial_graph()),
    ("wheel_graph", nx.wheel_graph(n)),
]

plot_multigraph(graphs, 4, 4, node_size=50)
plt.savefig("graphs/classic.png")
예제 #31
0
 def test_trivial_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.trivial_graph(), result)
     self.assertEqual(result.getvalue(), b'>>graph6<<@\n')
예제 #32
0
 def test_result_trivial_graph(self):
     assert (calc_and_compare(NX.trivial_graph()))
예제 #33
0
 def test_trivial_graph(self):
     assert nx.number_of_nodes(nx.trivial_graph()) == 1
예제 #34
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     result = BytesIO()
     nx.write_sparse6(G, result)
     self.assertEqual(result.getvalue(), b'>>sparse6<<:@\n')
n = 6
hypercube_n = 4
m = 6
r = 2
h = 3
dim = [2, 3]

# left out dorogovtsev_goltsev_mendes_graph and null_graph

graphs = [
    ("balanced_tree", nx.balanced_tree(r, h)),
    ("barbell_graph", nx.barbell_graph(n, m)),
    ("complete_graph", nx.complete_graph(n)),
    ("complete_bipartite_graph", nx.complete_bipartite_graph(n, m)),
    ("circular_ladder_graph", nx.circular_ladder_graph(n)),
    ("cycle_graph", nx.cycle_graph(n)),
    ("empty_graph", nx.empty_graph(n)),
    ("grid_2d_graph", nx.grid_2d_graph(m, n)),
    ("grid_graph", nx.grid_graph(dim)),
    ("hypercube_graph", nx.hypercube_graph(hypercube_n)),
    ("ladder_graph", nx.ladder_graph(n)),
    ("lollipop_graph", nx.lollipop_graph(m, n)),
    ("path_graph", nx.path_graph(n)),
    ("star_graph", nx.star_graph(n)),
    ("trivial_graph", nx.trivial_graph()),
    ("wheel_graph", nx.wheel_graph(n)),
]

plot_multigraph(graphs, 4, 4, node_size=50)
plt.savefig('graphs/classic.png')
if not os.path.exists(args.output):
    os.makedirs(args.output)

with open(args.output + '/output.csv', 'w') as output:
	helper(output, nx.balanced_tree(2, 5), "balanced_tree") # branching factor, height
	helper(output, nx.barbell_graph(50, 50), "barbell_graph")
	helper(output, nx.complete_graph(50), "complete_graph")
	helper(output, nx.complete_bipartite_graph(50, 50), "complete_bipartite_graph")
	helper(output, nx.circular_ladder_graph(50), "circular_ladder_graph")
	helper(output, nx.cycle_graph(50), "cycle_graph")
	helper(output, nx.dorogovtsev_goltsev_mendes_graph(5), "dorogovtsev_goltsev_mendes_graph")
	helper(output, nx.empty_graph(50), "empty_graph")
	helper(output, nx.grid_2d_graph(5, 20), "grid_2d_graph")
	helper(output, nx.grid_graph([2, 3]), "grid_graph")
	helper(output, nx.hypercube_graph(3), "hypercube_graph")
	helper(output, nx.ladder_graph(50), "ladder_graph")
	helper(output, nx.lollipop_graph(5, 20), "lollipop_graph")
	helper(output, nx.path_graph(50), "path_graph")
	helper(output, nx.star_graph(50), "star_graph")
	helper(output, nx.trivial_graph(), "trivial_graph")
	helper(output, nx.wheel_graph(50), "wheel_graph")
	
	helper(output, nx.random_regular_graph(1, 50, 678995), "random_regular_graph_1")
	helper(output, nx.random_regular_graph(3, 50, 683559), "random_regular_graph_3")
	helper(output, nx.random_regular_graph(5, 50, 515871), "random_regular_graph_5")
	helper(output, nx.random_regular_graph(8, 50, 243579), "random_regular_graph_8")
	helper(output, nx.random_regular_graph(13, 50, 568324), "random_regular_graph_13")
	
	
	helper(output, nx.diamond_graph(), "diamond_graph")
예제 #37
0
 def test_trivial_graph(self):
     nx.to_prufer_sequence(nx.trivial_graph())
예제 #38
0
파일: scenario.py 프로젝트: kit-tm/fdeval
    def create_topo(self):
        self.log("")
        self.log("create topology")

        # special case: a single node
        if self.param_topo_num_switches == 1:
            self.log("  use single node topology")
            self.g = nx.trivial_graph()
            self.hosts_of_switch = {}
            self.flows_of_switch = {}
            self.flows_of_switch[0] = []
            self.hosts_of_switch[0] = []
            # all hosts are assigned to the one switch
            for host in range(0, self.param_topo_num_hosts):
                self.hosts_of_switch[0].append(host)
            return

        # use the Barabasi–Albert model to create scale-free topologies
        if self.param_topo_scenario_generator == 1:
            self.log("  use Barabasi–Albert model with m=%d" %
                     self.param_topo_scenario_ba_modelparam)
            # first step is to create the topology of the switches
            seed = None
            if self.param_topo_seed >= 0:
                seed = self.param_topo_seed

            # n = Number of nodes
            # m = Number of edges to attach from a new node to existing nodes
            # seed = Seed for random number generator (default=None)
            self.g = nx.barabasi_albert_graph(
                self.param_topo_num_switches,
                self.param_topo_scenario_ba_modelparam,
                seed=seed)

            # next, the hosts are attached (stored in hosts_of_switch, i.e., separate from g)
            self.hosts_of_switch = {}
            self.flows_of_switch = {}
            for switch in self.g.nodes():
                self.flows_of_switch[switch] = []
                self.hosts_of_switch[switch] = []

            # assign each host to one switch (randomly)
            for host in range(0, self.param_topo_num_hosts):
                use_switch = random.randint(0,
                                            self.param_topo_num_switches - 1)
                self.hosts_of_switch[use_switch].append(host)

            self.log("  nodes", len(self.g.nodes()))
            self.log("  edges", len(self.g.edges()))
            self.log("  node degrees", self.g.degree())
            self.log(
                "  average degree",
                sum(d
                    for n, d in self.g.degree()) / float(len(self.g.nodes())))

            # done
            return

        # invalid parameter
        raise Exception("param_topo_scenario_generator = %d is not supported" %
                        self.param_topo_scenario_generator)