Exemple #1
0
    def test_simple_graphs(self):
        for dest, source in [
            (to_dict_of_dicts, from_dict_of_dicts),
            (to_dict_of_lists, from_dict_of_lists),
        ]:
            G = barbell_graph(10, 3)
            G.graph = {}
            dod = dest(G)

            # Dict of [dicts, lists]
            GG = source(dod)
            assert_graphs_equal(G, GG)
            GW = to_networkx_graph(dod)
            assert_graphs_equal(G, GW)
            GI = nx.Graph(dod)
            assert_graphs_equal(G, GI)

            # With nodelist keyword
            P4 = nx.path_graph(4)
            P3 = nx.path_graph(3)
            P4.graph = {}
            P3.graph = {}
            dod = dest(P4, nodelist=[0, 1, 2])
            Gdod = nx.Graph(dod)
            assert_graphs_equal(Gdod, P3)
Exemple #2
0
    def test_good_partition(self):
        """Tests that a good partition has a high performance measure.

        """
        G = barbell_graph(3, 0)
        partition = [{0, 1, 2}, {3, 4, 5}]
        assert_almost_equal(14 / 15, performance(G, partition))
Exemple #3
0
    def test_good_partition(self):
        """Tests that a good partition has a high performance measure.

        """
        G = barbell_graph(3, 0)
        partition = [{0, 1, 2}, {3, 4, 5}]
        assert_almost_equal(14 / 15, performance(G, partition))
Exemple #4
0
    def test_modularity(self):
        # test that empty graph converts fine for all options
        G = barbell_graph(3, 0)
        m = modularity(G, [{0, 1, 2}, {3, 4, 5}])

        G = karate_club_graph()
        c = list(greedy_modularity_communities(G, gamma=1.8))
        print(sorted(c[0]))
Exemple #5
0
def get_barbell_graph(left, right, **kwargs):
    # kwargs are ignored
    if left != right:
        raise ValueError(
            "Barbell graph requires both communities to be of the same size!")
    size = left
    print('Generating {}x{} barbell graph'.format(size, size))
    G = barbell_graph(size, 0)
    solution_bitstring = [-1] * size + [1] * size
    return G, solution_bitstring
Exemple #6
0
    def test_simple_graphs(self):
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts),
                             (to_dict_of_lists, from_dict_of_lists)]:
            G = barbell_graph(10, 3)
            G.graph = {}
            dod = dest(G)

            # Dict of [dicts, lists]
            GG = source(dod)
            assert_graphs_equal(G, GG)
            GW = to_networkx_graph(dod)
            assert_graphs_equal(G, GW)
            GI = nx.Graph(dod)
            assert_graphs_equal(G, GI)

            # With nodelist keyword
            P4 = nx.path_graph(4)
            P3 = nx.path_graph(3)
            P4.graph = {}
            P3.graph = {}
            dod = dest(P4, nodelist=[0, 1, 2])
            Gdod = nx.Graph(dod)
            assert_graphs_equal(Gdod, P3)
    def test_simple_graphs(self):
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts), (to_dict_of_lists, from_dict_of_lists)]:
            G = barbell_graph(10, 3)
            dod = dest(G)

            # Dict of [dicts, lists]
            GG = source(dod)
            assert_equal(sorted(G.nodes()), sorted(GG.nodes()))
            assert_equal(sorted(G.edges()), sorted(GG.edges()))
            GW = from_whatever(dod)
            assert_equal(sorted(G.nodes()), sorted(GW.nodes()))
            assert_equal(sorted(G.edges()), sorted(GW.edges()))
            GI = Graph(dod)
            assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
            assert_equal(sorted(G.edges()), sorted(GI.edges()))

            # With nodelist keyword
            P4 = path_graph(4)
            P3 = path_graph(3)
            dod = dest(P4, nodelist=[0, 1, 2])
            Gdod = Graph(dod)
            assert_equal(sorted(Gdod.nodes()), sorted(P3.nodes()))
            assert_equal(sorted(Gdod.edges()), sorted(P3.edges()))
Exemple #8
0
    def test_simple_graphs(self):
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts),
                             (to_dict_of_lists, from_dict_of_lists)]:
            G=barbell_graph(10,3)
            dod=dest(G)

            # Dict of [dicts, lists]
            GG=source(dod)
            assert_equal(sorted(G.nodes()), sorted(GG.nodes()))
            assert_equal(sorted(G.edges()), sorted(GG.edges()))
            GW=to_networkx_graph(dod)
            assert_equal(sorted(G.nodes()), sorted(GW.nodes()))
            assert_equal(sorted(G.edges()), sorted(GW.edges()))
            GI=Graph(dod)
            assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
            assert_equal(sorted(G.edges()), sorted(GI.edges()))

            # With nodelist keyword
            P4=path_graph(4)
            P3=path_graph(3)
            dod=dest(P4,nodelist=[0,1,2])
            Gdod=Graph(dod)
            assert_equal(sorted(Gdod.nodes()), sorted(P3.nodes()))
            assert_equal(sorted(Gdod.edges()), sorted(P3.edges()))
Exemple #9
0
    def setup_method(self):
        self.G1 = barbell_graph(10, 3)
        self.G2 = cycle_graph(10, create_using=nx.DiGraph)

        self.G3 = self.create_weighted(nx.Graph())
        self.G4 = self.create_weighted(nx.DiGraph())
Exemple #10
0
def test_correctness():
    # note: in the future can restrict all graphs to have the same number of nodes, for analysis
    graphs = [lambda: basic_graph(), lambda: complete_graph(10), lambda: balanced_tree(3, 5), lambda: barbell_graph(6, 7),
              lambda: binomial_tree(10), lambda: cycle_graph(50),
              lambda: path_graph(200), lambda: star_graph(200)]
    names = ["basic_graph", "complete_graph", "balanced_tree", "barbell_graph", "binomial_tree", "cycle_graph",
             "path_graph", "star_graph"]
    for graph, name in zip(graphs, names):
        print(f"Testing graph {name}...")
        
        # Initialize both graphs
        G_dinitz = graph()
        G_gr = graph()

        # Set random capacities of graph edges
        for u, v in G_dinitz.edges:
            cap = randint(1, 20)
            G_dinitz.edges[u, v]["capacity"] = cap
            G_gr.edges[u, v]["capacity"] = cap

        # Pick random start and end node
        start_node = randint(0, len(G_dinitz.nodes)-1)
        end_node = randint(0, len(G_dinitz.nodes)-1)
        while start_node == end_node: end_node = randint(0, len(G_dinitz.nodes)-1)

        # Run max-flow
        R_dinitz = dinitz(G_dinitz, start_node, end_node)
        R_gr = goldberg_rao(G_gr, start_node, end_node)

        # Check correctness
        d_mf = R_dinitz.graph["flow_value"]
        gr_mf = R_gr.graph["flow_value"]
        assert d_mf == gr_mf, f"Computed max flow in {name} graph is {d_mf}, but goldberg_rao function computed {gr_mf}"
Exemple #11
0
 def test_bad_partition(self):
     """Tests that a poor partition has a low performance measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 4}, {2, 3, 5}]
     assert_almost_equal(8 / 15, performance(G, partition))
Exemple #12
0
 def test_bad_partition(self):
     """Tests that a poor partition has a low coverage measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 4}, {2, 3, 5}]
     assert_almost_equal(3 / 7, coverage(G, partition))
Exemple #13
0
 def test_bad_partition(self):
     """Tests that a poor partition has a low performance measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 4}, {2, 3, 5}]
     assert_almost_equal(8 / 15, performance(G, partition))
Exemple #14
0
 def test_good_partition(self):
     """Tests that a good partition has a high coverage measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 2}, {3, 4, 5}]
     assert_almost_equal(6 / 7, coverage(G, partition))
Exemple #15
0
 def test_bad_partition(self):
     """Tests that a poor partition has a low coverage measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 4}, {2, 3, 5}]
     assert_almost_equal(3 / 7, coverage(G, partition))
    def __init__(self):
        self.G1 = barbell_graph(10, 3)
        self.G2 = cycle_graph(10, create_using=nx.DiGraph())

        self.G3 = self.create_weighted(nx.Graph())
        self.G4 = self.create_weighted(nx.DiGraph())
    def __init__(self):
        self.G1 = barbell_graph(10, 3)
        self.G2 = cycle_graph(10, create_using=nx.DiGraph())

        self.G3 = self.create_weighted(nx.Graph())
        self.G4 = self.create_weighted(nx.DiGraph())
Exemple #18
0
 def test_good_partition(self):
     """Tests that a good partition has a high coverage measure."""
     G = barbell_graph(3, 0)
     partition = [{0, 1, 2}, {3, 4, 5}]
     assert_almost_equal(6 / 7, coverage(G, partition))