示例#1
0
 def test_random_gnm_directed(self):
     graph = retworkx.directed_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.directed_gnm_random_graph(20, 100, seed=10)
     graph_s2 = retworkx.directed_gnm_random_graph(20, 100, seed=10)
     self.assertEqual(graph_s1.edge_list(), graph_s2.edge_list())
示例#2
0
 def test_random_gnm_directed_empty_graph(self):
     graph = retworkx.directed_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.directed_gnm_random_graph(20, 0, 44)
     self.assertEqual(len(graph), 20)
     self.assertEqual(len(graph.edges()), 0)
示例#3
0
 def test_random_gnm_directed_complete_graph(self):
     n = 20
     max_m = n * (n - 1)
     # passing the max edges for the passed number of nodes
     graph = retworkx.directed_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) still returns the max edges
     graph = retworkx.directed_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.directed_gnm_random_graph(n, max_m, 55)
     self.assertEqual(len(graph), n)
     self.assertEqual(len(graph.edges()), max_m)
示例#4
0
 def setup(self, num_nodes, num_edges):
     random.seed(42)
     self.graph = retworkx.directed_gnm_random_graph(num_nodes, num_edges)
     for x in self.graph.node_indexes():
         self.graph[x] = x
     for edge in self.graph.edge_indices():
         self.graph.update_edge_by_index(edge, random.randint(1, 10000))
 def setup(self, num_nodes, num_edges, remove_nodes):
     random.seed(12345)
     self.graph_func = retworkx.digraph_adjacency_matrix
     self.graph = retworkx.directed_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))
示例#6
0
    def test_weight_fn_raises(self):
        path = os.path.join(tempfile.gettempdir(), "fail.txt")
        graph = retworkx.directed_gnm_random_graph(5, 4)

        def weight_fn(edge):
            raise KeyError

        self.addCleanup(os.remove, path)
        with self.assertRaises(KeyError):
            graph.write_edge_list(path, weight_fn=weight_fn)
示例#7
0
 def test_random_gnm_directed_invalid_num_edges(self):
     with self.assertRaises(ValueError):
         retworkx.directed_gnm_random_graph(23, -5)
示例#8
0
 def test_invalid_return_type_weight_fn(self):
     path = os.path.join(tempfile.gettempdir(), "fail.txt")
     graph = retworkx.directed_gnm_random_graph(5, 4)
     self.addCleanup(os.remove, path)
     with self.assertRaises(TypeError):
         graph.write_edge_list(path, weight_fn=lambda _: 4.5)
示例#9
0
import os
import tempfile
from typing import Sized

from PIL import Image
import pydot
import retworkx as rx
import igraph as ig
import numpy as np

G: rx.PyDiGraph = rx.directed_gnm_random_graph(1000, 2000, None)

components: list = rx.strongly_connected_components(G)

largest_component: Sized = max(components, key=len)

H: rx.PyGraph = G.subgraph(largest_component)

# have to fix pydot error
"""
dot = pydot.graph_from_dot_data(G.to_dot())[0]
with tempfile.TemporaryDirectory() as tmpdirname:
    tmp_path = os.path.join(tmpdirname, 'dag.png')
    dot.write_png(tmp_path)
    image = Image.open(tmp_path)
    os.remove(tmp_path)
image
"""
n1: np.ndarray = rx.digraph_adjacency_matrix(G, None)
g: ig.Graph = ig.Graph.Weighted_Adjacency(n1.tolist())
ig.plot(g)
示例#10
0
 def setup(self, num_nodes, num_edges):
     self.graph = retworkx.directed_gnm_random_graph(num_nodes,
                                                     num_edges,
                                                     seed=4242423)