def test_number_strongly_connected_big(self): G = retworkx.PyDiGraph() for i in range(100000): node = G.add_node(i) G.add_child(node, str(i), {}) self.assertEqual(len(retworkx.strongly_connected_components(G)), 200000)
def test_number_strongly_connected(self): G = retworkx.PyDiGraph() node_a = G.add_node(1) node_b = G.add_child(node_a, 2, {}) node_c = G.add_node(3) self.assertEqual(retworkx.strongly_connected_components(G), [[node_c], [node_b], [node_a]])
def test_stongly_connected_no_linear(self): G = retworkx.PyDiGraph() G.add_nodes_from(list(range(8))) G.add_edges_from_no_data([ (0, 1), (1, 2), (1, 7), (2, 3), (2, 6), (3, 4), (4, 2), (4, 5), (6, 3), (6, 5), (7, 0), (7, 6), ]) expected = [[5], [2, 3, 4, 6], [0, 1, 7]] components = retworkx.strongly_connected_components(G) self.assertEqual(components, expected)
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)
def largest_connected_component(self): """Return a set of qubits in the largest connected component.""" return max(rx.strongly_connected_components(self.graph), key=len)
def time_strongly_connected_components(self): retworkx.strongly_connected_components(self.graph)