Example #1
0
 def test_random_gnm_undirected_empty_graph(self):
     graph = retworkx.undirected_gnm_random_graph(20, 0)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 0)
     # passing a seed when passing zero edges has no effect
     graph = retworkx.undirected_gnm_random_graph(20, 0, 44)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 0)
Example #2
0
 def test_random_gnm_undirected(self):
     graph = retworkx.undirected_gnm_random_graph(20, 100)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 100)
     # with other arguments equal, same seed results in same graph
     graph_s1 = retworkx.undirected_gnm_random_graph(20, 100, seed=10)
     graph_s2 = retworkx.undirected_gnm_random_graph(20, 100, seed=10)
     self.assertEqual(graph_s1.edge_list(), graph_s2.edge_list())
Example #3
0
 def test_random_gnm_undirected_complete_graph(self):
     n = 20
     max_m = n * (n - 1) // 2
     # passing the max edges for the passed number of nodes
     graph = retworkx.undirected_gnm_random_graph(n, max_m)
     self.assertEqual(len(graph), n)
     self.assertEqual(len(graph.edges()), max_m)
     # passing m > the max edges n(n-1)/2 still returns the max edges
     graph = retworkx.undirected_gnm_random_graph(n, max_m + 1)
     self.assertEqual(len(graph), n)
     self.assertEqual(len(graph.edges()), max_m)
     # passing a seed when passing max edges has no effect
     graph = retworkx.undirected_gnm_random_graph(n, max_m, 55)
     self.assertEqual(len(graph), n)
     self.assertEqual(len(graph.edges()), max_m)
Example #4
0
 def test_gnm_random_against_networkx(self):
     rx_graph = retworkx.undirected_gnm_random_graph(10, 13, seed=42)
     nx_graph = networkx.Graph(list(rx_graph.edge_list()))
     nx_matches = networkx.max_weight_matching(nx_graph)
     rx_matches = retworkx.max_weight_matching(rx_graph,
                                               verify_optimum=True)
     self.compare_rx_nx_sets(rx_graph, rx_matches, nx_matches, 42, nx_graph)
    def test_large_partial_random(self) -> None:
        """Test a random (partial) mapping on a large randomly generated graph"""
        size = 100
        # Note that graph may have "gaps" in the node counts, i.e. the numbering is noncontiguous.
        graph = rx.undirected_gnm_random_graph(size, size**2 // 10)
        for i in graph.node_indexes():
            try:
                graph.remove_edge(i, i)  # Remove self-loops.
            except rx.NoEdgeBetweenNodes:
                continue
        # Make sure the graph is connected by adding C_n
        graph.add_edges_from_no_data([(i, i + 1)
                                      for i in range(len(graph) - 1)])
        swapper = ApproximateTokenSwapper(
            graph)  # type: ApproximateTokenSwapper[int]

        # Generate a randomized permutation.
        rand_perm = random.permutation(graph.nodes())
        permutation = dict(zip(graph.nodes(), rand_perm))
        mapping = dict(itertools.islice(permutation.items(), 0, size,
                                        2))  # Drop every 2nd element.

        out = list(swapper.map(mapping, trials=40))
        util.swap_permutation([out], mapping, allow_missing_keys=True)
        self.assertEqual({i: i for i in mapping.values()}, mapping)
 def setup(self, num_nodes, num_edges):
     random.seed(1234)
     self.graph = retworkx.undirected_gnm_random_graph(num_nodes,
                                                       num_edges,
                                                       seed=4242)
     for edge in self.graph.edge_indices():
         self.graph.update_edge_by_index(edge, random.randint(0, 20000))
Example #7
0
    def test_random_gnm_induced_subgraph_isomorphism(self):
        graph = retworkx.undirected_gnm_random_graph(50, 150)
        nodes = random.sample(range(50), 25)
        subgraph = graph.subgraph(nodes)

        self.assertTrue(
            retworkx.is_subgraph_isomorphic(graph,
                                            subgraph,
                                            id_order=True,
                                            induced=True))
Example #8
0
    def test_weight_fn_raises(self):
        path = os.path.join(tempfile.gettempdir(), "fail.txt")
        graph = retworkx.undirected_gnm_random_graph(5, 4)

        def weight_fn(edge):
            raise KeyError

        self.addCleanup(cleanup_file, path)
        with self.assertRaises(KeyError):
            graph.write_edge_list(path, weight_fn=weight_fn)
Example #9
0
    def test_random_gnm_non_induced_subgraph_isomorphism(self):
        graph = retworkx.undirected_gnm_random_graph(50, 150)
        nodes = random.sample(range(50), 25)
        subgraph = graph.subgraph(nodes)

        indexes = list(subgraph.edge_indices())
        for idx in random.sample(indexes, len(indexes) // 2):
            subgraph.remove_edge_from_index(idx)

        self.assertTrue(
            retworkx.is_subgraph_isomorphic(graph,
                                            subgraph,
                                            id_order=True,
                                            induced=False))
Example #10
0
 def test_random_gnm_undirected_invalid_probability(self):
     with self.assertRaises(ValueError):
         retworkx.undirected_gnm_random_graph(23, -5)
Example #11
0
 def test_random_gnm_undirected_invalid_num_nodes(self):
     with self.assertRaises(ValueError):
         retworkx.undirected_gnm_random_graph(-23, 5)
Example #12
0
 def test_invalid_return_type_weight_fn(self):
     path = os.path.join(tempfile.gettempdir(), "fail.txt")
     graph = retworkx.undirected_gnm_random_graph(5, 4)
     self.addCleanup(cleanup_file, path)
     with self.assertRaises(TypeError):
         graph.write_edge_list(path, weight_fn=lambda _: 4.5)
Example #13
0
 def setup(self, num_nodes, num_edges):
     self.graph = retworkx.undirected_gnm_random_graph(num_nodes,
                                                       num_edges,
                                                       seed=42)