def test_biconnected_component_subgraphs_cycle(): G=nx.cycle_graph(3) G.add_cycle([1,3,4,5]) G.add_edge(1,3,eattr='red') # test copying of edge data G.node[1]['nattr']='blue' G.graph['gattr']='green' Gc = set(biconnected.biconnected_component_subgraphs(G)) assert_equal(len(Gc),2) g1,g2=Gc if 0 in g1: assert_true(nx.is_isomorphic(g1,nx.Graph([(0,1),(0,2),(1,2)]))) assert_true(nx.is_isomorphic(g2,nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) assert_equal(g2[1][3]['eattr'],'red') assert_equal(g2.node[1]['nattr'],'blue') assert_equal(g2.graph['gattr'],'green') g2[1][3]['eattr']='blue' assert_equal(g2[1][3]['eattr'],'blue') assert_equal(G[1][3]['eattr'],'red') else: assert_true(nx.is_isomorphic(g1,nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) assert_true(nx.is_isomorphic(g2,nx.Graph([(0,1),(0,2),(1,2)]))) assert_equal(g1[1][3]['eattr'],'red') assert_equal(g1.node[1]['nattr'],'blue') assert_equal(g1.graph['gattr'],'green') g1[1][3]['eattr']='blue' assert_equal(g1[1][3]['eattr'],'blue') assert_equal(G[1][3]['eattr'],'red')
def test_multigraph(self): G = nx.MultiGraph() G.add_edge(1, 2, key='first') G.add_edge(1, 2, key='second', color='blue') H = node_link_graph(node_link_data(G)) nx.is_isomorphic(G, H) assert_equal(H[1][2]['second']['color'], 'blue')
def test_multigraph(self): G = nx.MultiGraph() G.add_edge(1, 2, key="first") G.add_edge(1, 2, key="second", color="blue") H = adjacency_graph(adjacency_data(G)) nx.is_isomorphic(G, H) assert_equal(H[1][2]["second"]["color"], "blue")
def test_graph(self): G = nx.DiGraph() G.add_nodes_from([1, 2, 3], color='red') G.add_edge(1, 2, foo=7) G.add_edge(1, 3, foo=10) G.add_edge(3, 4, foo=10) H = tree_graph(tree_data(G, 1)) nx.is_isomorphic(G, H)
def test_node_input(self): G = nx.grid_2d_graph(4, 2, periodic=True) H = nx.grid_2d_graph(range(4), range(2), periodic=True) assert_true(nx.is_isomorphic(H, G)) H = nx.grid_2d_graph("abcd", "ef", periodic=True) assert_true(nx.is_isomorphic(H, G)) G = nx.grid_2d_graph(5, 6) H = nx.grid_2d_graph(range(5), range(6)) assert_edges_equal(H, G)
def test_empty_subgraph(self): # Subgraph of an empty graph is an empty graph. test 1 nullgraph = nx.null_graph() E5 = nx.empty_graph(5) E10 = nx.empty_graph(10) H = E10.subgraph([]) assert_true(nx.is_isomorphic(H, nullgraph)) H = E10.subgraph([1, 2, 3, 4, 5]) assert_true(nx.is_isomorphic(H, E5))
def test_triangle_graph(self): G = nx.complete_graph(3) H = nx.inverse_line_graph(G) alternative_solution = nx.Graph() alternative_solution.add_edges_from([[0, 1], [0, 2], [0, 3]]) # there are two alternative inverse line graphs for this case # so long as we get one of them the test should pass assert_true(nx.is_isomorphic(H, G) or nx.is_isomorphic(H, alternative_solution))
def test_expected_degree_graph(): # test that fixed seed delivers the same graph deg_seq = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] G1 = nx.expected_degree_graph(deg_seq, seed=1000) G2 = nx.expected_degree_graph(deg_seq, seed=1000) assert_true(nx.is_isomorphic(G1, G2)) G1 = nx.expected_degree_graph(deg_seq, seed=10) G2 = nx.expected_degree_graph(deg_seq, seed=10) assert_true(nx.is_isomorphic(G1, G2))
def test_remove_one_contig(self): my_graph = nx.Graph() my_graph.add_edges_from([('Contig1', 'Contig2'), ('Contig1', 'Contig3'), ('Contig1', 'Contig4')]) remove_contigs = RemoveContigs(my_graph) expected_graph = nx.Graph() expected_graph.add_nodes_from(['Contig2', 'Contig3', 'Contig4']) remove_contigs.remove_contigs_high_degree() self.assertTrue(nx.is_isomorphic(expected_graph, remove_contigs.graph)) remove_contigs.remove_extra_contigs() self.assertTrue(nx.is_isomorphic(nx.Graph(), remove_contigs.graph))
def test_ego(self): G=nx.star_graph(3) H=nx.ego_graph(G,0) assert_true(nx.is_isomorphic(G,H)) G.add_edge(1,11) G.add_edge(2,22) G.add_edge(3,33) H=nx.ego_graph(G,0) assert_true(nx.is_isomorphic(nx.star_graph(3),H)) G=nx.path_graph(3) H=nx.ego_graph(G,0) assert_equal(H.edges(), [(0, 1)])
def test_random_seed(self): """Tests that each call with the same random seed generates the same graph. """ deg_seq = [3] * 12 G1 = nx.configuration_model(deg_seq, seed=1000) G2 = nx.configuration_model(deg_seq, seed=1000) assert_true(nx.is_isomorphic(G1, G2)) G1 = nx.configuration_model(deg_seq, seed=10) G2 = nx.configuration_model(deg_seq, seed=10) assert_true(nx.is_isomorphic(G1, G2))
def test_cartesian_product_classic(): # test some classic product graphs P2 = nx.path_graph(2) P3 = nx.path_graph(3) # cube = 2-path X 2-path G=cartesian_product(P2,P2) G=cartesian_product(P2,G) assert_true(nx.is_isomorphic(G,nx.cubical_graph())) # 3x3 grid G=cartesian_product(P3,P3) assert_true(nx.is_isomorphic(G,nx.grid_2d_graph(3,3)))
def test_navigable_small_world(self): G = nx.navigable_small_world_graph(5, p=1, q=0) gg = nx.grid_2d_graph(5, 5).to_directed() assert_true(nx.is_isomorphic(G, gg)) G = nx.navigable_small_world_graph(5, p=1, q=0, dim=3) gg = nx.grid_graph([5, 5, 5]).to_directed() assert_true(nx.is_isomorphic(G, gg)) G = nx.navigable_small_world_graph(5, p=1, q=0, dim=1) gg = nx.grid_graph([5]).to_directed() assert_true(nx.is_isomorphic(G, gg))
def test_biconnected_component_subgraphs_cycle(): G=nx.cycle_graph(3) nx.add_cycle(G, [1, 3, 4, 5]) Gc = set(nx.biconnected_component_subgraphs(G)) assert_equal(len(Gc), 2) g1, g2=Gc if 0 in g1: assert_true(nx.is_isomorphic(g1, nx.Graph([(0,1),(0,2),(1,2)]))) assert_true(nx.is_isomorphic(g2, nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) else: assert_true(nx.is_isomorphic(g1, nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) assert_true(nx.is_isomorphic(g2, nx.Graph([(0,1),(0,2),(1,2)])))
def test_tensor_product_classic_result(): K2 = nx.complete_graph(2) G = nx.petersen_graph() G = tensor_product(G,K2) assert_true(nx.is_isomorphic(G,nx.desargues_graph())) G = nx.cycle_graph(5) G = tensor_product(G,K2) assert_true(nx.is_isomorphic(G,nx.cycle_graph(10))) G = nx.tetrahedral_graph() G = tensor_product(G,K2) assert_true(nx.is_isomorphic(G,nx.cubical_graph()))
def check_counterexample(G, sub_graph): """Raises an exception if the counterexample is wrong. Parameters ---------- G : NetworkX graph subdivision_nodes : set A set of nodes inducing a subgraph as a counterexample """ # 1. Create the sub graph sub_graph = nx.Graph(sub_graph) # 2. Remove self loops for u in sub_graph: if sub_graph.has_edge(u, u): sub_graph.remove_edge(u, u) # keep track of nodes we might need to contract contract = list(sub_graph) # 3. Contract Edges while len(contract) > 0: contract_node = contract.pop() if contract_node not in sub_graph: # Node was already contracted continue degree = sub_graph.degree[contract_node] # Check if we can remove the node if degree == 2: # Get the two neighbors neighbors = iter(sub_graph[contract_node]) u = next(neighbors) v = next(neighbors) # Save nodes for later contract.append(u) contract.append(v) # Contract edge sub_graph.remove_node(contract_node) sub_graph.add_edge(u, v) # 4. Check for isomorphism with K5 or K3_3 graphs if len(sub_graph) == 5: if not nx.is_isomorphic(nx.complete_graph(5), sub_graph): raise nx.NetworkXException("Bad counter example.") elif len(sub_graph) == 6: if not nx.is_isomorphic(nx.complete_bipartite_graph(3, 3), sub_graph): raise nx.NetworkXException("Bad counter example.") else: raise nx.NetworkXException("Bad counter example.")
def test_simple(): # 16 simple tests w = True rtol = 1e-6 atol = 1e-9 edges = [(0,0,1),(0,0,1.5),(0,1,2),(1,0,3)] for g1 in [nx.Graph(weighted=w), nx.DiGraph(weighted=w), nx.MultiGraph(weighted=w), nx.MultiDiGraph(weighted=w) ]: print g1.__class__ g1.add_weighted_edges_from(edges) g2 = g1.subgraph(g1.nodes()) assert_true( nx.is_isomorphic(g1,g2,True,rtol,atol) ) for mod1, mod2 in [(False, True), (True, False), (True, True)]: # mod1 tests a regular edge # mod2 tests a selfloop print "Modification:", mod1, mod2 if g2.is_multigraph(): if mod1: data1 = {0:{'weight':10}} if mod2: data2 = {0:{'weight':1},1:{'weight':2.5}} else: if mod1: data1 = {'weight':10} if mod2: data2 = {'weight':2.5} g2 = g1.subgraph(g1.nodes()) if mod1: if not g1.is_directed(): g2.adj[1][0] = data1 g2.adj[0][1] = data1 else: g2.succ[1][0] = data1 g2.pred[0][1] = data1 if mod2: if not g1.is_directed(): g2.adj[0][0] = data2 else: g2.succ[0][0] = data2 g2.pred[0][0] = data2 assert_false(nx.is_isomorphic(g1,g2,True,rtol,atol))
def test_simple(): # 16 simple tests w = 'weight' edges = [(0, 0, 1), (0, 0, 1.5), (0, 1, 2), (1, 0, 3)] for g1 in [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph(), ]: g1.add_weighted_edges_from(edges) g2 = g1.subgraph(g1.nodes()) if g1.is_multigraph(): em = iso.numerical_multiedge_match('weight', 1) else: em = iso.numerical_edge_match('weight', 1) assert_true( nx.is_isomorphic(g1, g2, edge_match=em) ) for mod1, mod2 in [(False, True), (True, False), (True, True)]: # mod1 tests a regular edge # mod2 tests a selfloop if g2.is_multigraph(): if mod1: data1 = {0: {'weight': 10}} if mod2: data2 = {0: {'weight': 1}, 1: {'weight': 2.5}} else: if mod1: data1 = {'weight': 10} if mod2: data2 = {'weight': 2.5} g2 = g1.subgraph(g1.nodes()).copy() if mod1: if not g1.is_directed(): g2._adj[1][0] = data1 g2._adj[0][1] = data1 else: g2._succ[1][0] = data1 g2._pred[0][1] = data1 if mod2: if not g1.is_directed(): g2._adj[0][0] = data2 else: g2._succ[0][0] = data2 g2._pred[0][0] = data2 assert_false(nx.is_isomorphic(g1, g2, edge_match=em))
def test_spectral_graph_forge(): numpy = 1 # nosetests attribute, use nosetests -a 'not numpy' to skip test scipy = 1 try: import numpy except ImportError: raise SkipTest('NumPy not available.') try: import scipy except ImportError: raise SkipTest("SciPy not available") G = karate_club_graph() seed = 54321 # common cases, just checking node number preserving and difference # between identity and modularity cases H = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed) assert_nodes_equal(G, H) I = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed) assert_nodes_equal(G, H) assert_true(is_isomorphic(I, H)) I = spectral_graph_forge(G, 0.1, transformation='modularity', seed=seed) assert_nodes_equal(G, I) assert_false(is_isomorphic(I, H)) # with all the eigenvectors, output graph is identical to the input one H = spectral_graph_forge(G, 1, transformation='modularity', seed=seed) assert_nodes_equal(G, H) assert_true(is_isomorphic(G, H)) # invalid alpha input value, it is silently truncated in [0,1] H = spectral_graph_forge(G, -1, transformation='identity', seed=seed) assert_nodes_equal(G, H) H = spectral_graph_forge(G, 10, transformation='identity', seed=seed) assert_nodes_equal(G, H) assert_true(is_isomorphic(G, H)) # invalid transformation mode, checking the error raising assert_raises(NetworkXError, spectral_graph_forge, G, 0.1, transformation='unknown', seed=seed)
def test_ego(self): G = nx.star_graph(3) H = nx.ego_graph(G, 0) assert_true(nx.is_isomorphic(G, H)) G.add_edge(1, 11) G.add_edge(2, 22) G.add_edge(3, 33) H = nx.ego_graph(G, 0) assert_true(nx.is_isomorphic(nx.star_graph(3), H)) G = nx.path_graph(3) H = nx.ego_graph(G, 0) assert_edges_equal(H.edges(), [(0, 1)]) H = nx.ego_graph(G, 0, undirected=True) assert_edges_equal(H.edges(), [(0, 1)]) H = nx.ego_graph(G, 0, center=False) assert_edges_equal(H.edges(), [])
def test_find_max_subgraph_to_delete(self): graph = nx.DiGraph() # *_d nodes have been deleted by glance # # a1_d # / \ # b1_d b2 # / \ \ # c1_d c2 c3_d # / # d1_d graph.add_path(['a1_d', 'b1_d','c1_d','d1_d']) graph.add_path(['b1_d', 'c2']) graph.add_path(['a1_d', 'b2', 'c3_d']) # The graphs to be deleted should be two: # c1_d c3_d # / # d1_d g1 = nx.DiGraph() g1.add_path(['c1_d', 'd1_d']) g1.add_node('c3_d') to_delete = find_subgraphs_to_delete(graph, delete_pattern='_d') self.assertTrue(nx.is_isomorphic(g1, to_delete), "Graphs are not isomorphic")
def test_undirected_edge_contraction(self): """Tests for edge contraction in an undirected graph.""" G = nx.cycle_graph(4) actual = nx.contracted_edge(G, (0, 1)) expected = nx.complete_graph(3) expected.add_edge(0, 0) assert_true(nx.is_isomorphic(actual, expected))
def test_caveman_graph(): G = nx.caveman_graph(4,3) assert_equal(len(G),12) G = nx.caveman_graph(1,5) K5 = nx.complete_graph(5) assert_true(nx.is_isomorphic(G,K5))
def get_connected_components(self, color_set): """ A generator for connected components given a specific color set :param color_set: The color set :return: A generator for connected components (subgraphs) induced by color_set """ # Make an empty set to store vertices v_set = set() # Find vertices that are colored with colors in color_set for index, color in enumerate(self.coloring): if color in color_set: v_set.add(index) cc_list = [] for new_cc in nx.connected_component_subgraphs(self.graph.subgraph(v_set)): found = False for n in new_cc.node: new_cc.node[n]['color'] = self.coloring[n] for i, cc in enumerate(cc_list): if nx.is_isomorphic(new_cc, cc, node_match=lambda n1, n2: n1['color'] == n2['color']): cc_list[i].occ += 1 found = True break if not found: new_cc.occ = 1 cc_list.append(new_cc) return cc_list
def test_stochastic(): G = nx.DiGraph() G.add_edge(0, 1) G.add_edge(0, 2) S = nx.stochastic_graph(G) assert_true(nx.is_isomorphic(G, S)) assert_equal(sorted(S.edges(data=True)), [(0, 1, {"weight": 0.5}), (0, 2, {"weight": 0.5})])
def test_weightkey(): g1 = nx.DiGraph() g2 = nx.DiGraph() g1.add_edge('A','B', weight=1) g2.add_edge('C','D', weight=0) assert_true( nx.is_isomorphic(g1, g2) ) em = iso.numerical_edge_match('nonexistent attribute', 1) assert_true( nx.is_isomorphic(g1, g2, edge_match=em) ) em = iso.numerical_edge_match('weight', 1) assert_false( nx.is_isomorphic(g1, g2, edge_match=em) ) g2 = nx.DiGraph() g2.add_edge('C','D') assert_true( nx.is_isomorphic(g1, g2, edge_match=em) )
def test_build_unique_fragments(self): edges = {(e[0], e[1]): None for e in self.pc_edges} mol_graph = MoleculeGraph.with_edges(self.pc, edges) unique_fragments = mol_graph.build_unique_fragments() self.assertEqual(len(unique_fragments), 295) nm = iso.categorical_node_match("specie", "ERROR") for ii in range(295): # Test that each fragment is unique for jj in range(ii + 1, 295): self.assertFalse( nx.is_isomorphic(unique_fragments[ii].graph, unique_fragments[jj].graph, node_match=nm)) # Test that each fragment correctly maps between Molecule and graph self.assertEqual(len(unique_fragments[ii].molecule), len(unique_fragments[ii].graph.nodes)) species = nx.get_node_attributes(unique_fragments[ii].graph, "specie") coords = nx.get_node_attributes(unique_fragments[ii].graph, "coords") mol = unique_fragments[ii].molecule for ss, site in enumerate(mol): self.assertEqual(str(species[ss]), str(site.specie)) self.assertEqual(coords[ss][0], site.coords[0]) self.assertEqual(coords[ss][1], site.coords[1]) self.assertEqual(coords[ss][2], site.coords[2]) # Test that each fragment is connected self.assertTrue(nx.is_connected(unique_fragments[ii].graph.to_undirected()))
def test_project_onto(): """ test the project_onto function of geograph """ net, nodes, projections = network_nodes_projections() new_net = net.project_onto(nodes) # check that the projected coords are in the new net neighbor_coords = {node: [list(new_net.coords[neigh]) for neigh in new_net.neighbors(node)] for node in projections.keys()} # need to 'cast' to list in case coords are numpy arrays tests = [projections[p] in neighbor_coords[p] for p in projections.keys()] assert all(tests), \ "expected projection does not match actual" # ensure that the rtree based projection results in the same network rtree = net.get_rtree_index() new_net_rt = net.project_onto(nodes, rtree_index=rtree) assert nx.is_isomorphic(new_net, new_net_rt) and \ [list(c) for c in new_net.coords.values()] == \ [list(c) for c in new_net_rt.coords.values()], \ "project_onto with and without rtree inputs don't match"
def check_iso(self): if len(self.graph_one.nodes_map) != len(self.graph_two.nodes_map): return "Graphs are not isomorphic - numbers of nodes are not equal" if len(self.graph_one.edges_map) != len(self.graph_two.edges_map): return "Graphs are not isomorphic - numbers of edges are not equal" neighbors_list_one = self.graph_one.get_neighbors() neighbors_list_two = self.graph_two.get_neighbors() G1 = nx.Graph() G2 = nx.Graph() for nodes in self.graph_one.nodes_map.values(): for k, v in nodes.items(): G1.add_node(v, id=v) for nodes in self.graph_two.nodes_map.values(): for k, v in nodes.items(): G2.add_node(v, id=v) for edges in self.graph_one.edges_map.values(): G1.add_edge(edges["v1"], edges["v2"]) for edges in self.graph_two.edges_map.values(): G2.add_edge(edges["v1"], edges["v2"]) result = nx.is_isomorphic(G1, G2) if result: return "Congrats! Graphs are isomorphic!" else: return "Too bad... Graphs are not isomorphic..."
def test_complete_subgraph(self): # Subgraph of a complete graph is a complete graph K1 = nx.complete_graph(1) K3 = nx.complete_graph(3) K5 = nx.complete_graph(5) H = K5.subgraph([1, 2, 3]) assert_true(nx.is_isomorphic(H, K3))
def test_path(self): G = nx.path_graph(5) L = nx.line_graph(G) assert nx.is_isomorphic(L, nx.path_graph(4))
def test_line(self): G = nx.path_graph(5) solution = nx.path_graph(6) H = nx.inverse_line_graph(G) assert nx.is_isomorphic(H, solution)
def test_K1(self): G = nx.complete_graph(1) H = nx.inverse_line_graph(G) solution = nx.path_graph(2) assert nx.is_isomorphic(H, solution)
def test_strong_product_null(): null = nx.null_graph() empty10 = nx.empty_graph(10) K3 = nx.complete_graph(3) K10 = nx.complete_graph(10) P3 = nx.path_graph(3) P10 = nx.path_graph(10) # null graph G = strong_product(null, null) assert_true(nx.is_isomorphic(G, null)) # null_graph X anything = null_graph and v.v. G = strong_product(null, empty10) assert_true(nx.is_isomorphic(G, null)) G = strong_product(null, K3) assert_true(nx.is_isomorphic(G, null)) G = strong_product(null, K10) assert_true(nx.is_isomorphic(G, null)) G = strong_product(null, P3) assert_true(nx.is_isomorphic(G, null)) G = strong_product(null, P10) assert_true(nx.is_isomorphic(G, null)) G = strong_product(empty10, null) assert_true(nx.is_isomorphic(G, null)) G = strong_product(K3, null) assert_true(nx.is_isomorphic(G, null)) G = strong_product(K10, null) assert_true(nx.is_isomorphic(G, null)) G = strong_product(P3, null) assert_true(nx.is_isomorphic(G, null)) G = strong_product(P10, null) assert_true(nx.is_isomorphic(G, null))
G.add_node('c') G.add_node('d') G.add_node('g') G.add_node('h') #Add Edges G.add_edge('a', 'g') G.add_edge('a', 'h') G.add_edge('a', 'i') G.add_edge('b', 'g') G.add_edge('b', 'h') G.add_edge('b', 'j') G.add_edge('c', 'g') G.add_edge('c', 'j') G.add_edge('c', 'i') G.add_edge('d', 'h') G.add_edge('d', 'j') G.add_edge('d', 'i') nx.draw(g, with_labels=1) plt.show() nx.draw(G, with_labels=1) plt.show() #Write Edge Information of 2 graphs into 2 different files nx.write_edgelist(g, 'edgeList1.txt') nx.write_edgelist(G, 'edgeList2.txt') #Check whether the Graphs are Eulerian print('Euler Graphs:') print('g: ', nx.is_eulerian(g)) print('G: ', nx.is_eulerian(G)) #Check whether the graphs are isomorphic print('Isomorphic:', nx.is_isomorphic(g, G))
def test_line_inverse_line_hypercube(self): G = nx.hypercube_graph(5) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)
def isomorphism(graph1,graph2): """Returns True if both graphs are isomorphic else False """ return n.is_isomorphic(graph1,graph2)
def test_construct_graph(self): """Test _construct_graph method.""" network_1, network_2 = self.networks_with_flow() network_1._construct_graph() network_2._construct_graph() # construct graph for network 1 G_1 = nx.MultiDiGraph() edge_data_1 = [(0, 1, { 'index': 0 }), (1, 2, { 'index': 1 }), (1, 3, { 'index': 2 }), (2, 4, { 'index': 3 }), (3, 4, { 'index': 4 }), (4, 5, { 'index': 5 })] G_1.add_edges_from(edge_data_1) # construct graph for network 2 G_2 = nx.MultiDiGraph() edge_data_2 = [(0, 1, { 'index': 0 }), (1, 2, { 'index': 1 }), (2, 3, { 'index': 2 }), (1, 4, { 'index': 3 }), (2, 5, { 'index': 4 }), (3, 6, { 'index': 5 }), (4, 5, { 'index': 6 }), (5, 6, { 'index': 7 }), (4, 7, { 'index': 8 }), (5, 8, { 'index': 9 }), (6, 9, { 'index': 10 }), (7, 8, { 'index': 11 }), (8, 9, { 'index': 12 }), (9, 10, { 'index': 13 })] G_2.add_edges_from(edge_data_2) # return True if graphs are the same is_isomorphic_1 = nx.is_isomorphic(network_1.graph, G_1) is_isomorphic_2 = nx.is_isomorphic(network_2.graph, G_2) self.assertTrue(is_isomorphic_1) self.assertTrue(is_isomorphic_2)
def test_cycle(self): G = nx.cycle_graph(5) L = nx.line_graph(G) assert nx.is_isomorphic(L, G)
def test_line_inverse_line_star(self): G = nx.star_graph(20) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)
def test_line_inverse_line_path(self): G = nx.path_graph(10) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)
def test_null_subgraph(self): # Subgraph of a null graph is a null graph nullgraph = nx.null_graph() G = nx.null_graph() H = G.subgraph([]) assert_true(nx.is_isomorphic(H, nullgraph))
def program_equivalence(prog1, prog2, compare_params=True, atol=1e-6, rtol=0): r"""Checks if two programs are equivalent. This function converts the program lists into directed acyclic graphs, and runs the NetworkX `is_isomorphic` graph function in order to determine if the two programs are equivalent. Note: when checking for parameter equality between two parameters :math:`a` and :math:`b`, we use the following formula: .. math:: |a - b| \leq (\texttt{atol} + \texttt{rtol}\times|b|) Args: prog1 (strawberryfields.program.Program): quantum program prog2 (strawberryfields.program.Program): quantum program compare_params (bool): Set to ``False`` to turn of comparing program parameters; equivalency will only take into account the operation order. atol (float): the absolute tolerance parameter for checking quantum operation parameter equality rtol (float): the relative tolerance parameter for checking quantum operation parameter equality Returns: bool: returns ``True`` if two quantum programs are equivalent """ DAG1 = list_to_DAG(prog1.circuit) DAG2 = list_to_DAG(prog2.circuit) circuit = [] for G in [DAG1, DAG2]: # relabel the DAG nodes to integers circuit.append(nx.convert_node_labels_to_integers(G)) # add node attributes to store the operation name and parameters name_mapping = {i: n.op.__class__.__name__ for i, n in enumerate(G.nodes())} parameter_mapping = {i: par_evaluate(n.op.p) for i, n in enumerate(G.nodes())} # CXgate and BSgate are not symmetric wrt permuting the order of the two # modes it acts on; i.e., the order of the wires matter wire_mapping = {} for i, n in enumerate(G.nodes()): if n.op.__class__.__name__ == "CXgate": if np.allclose(n.op.p[0], 0): # if the CXgate parameter is 0, wire order doesn't matter wire_mapping[i] = 0 else: # if the CXgate parameter is not 0, order matters wire_mapping[i] = [j.ind for j in n.reg] elif n.op.__class__.__name__ == "BSgate": if np.allclose([j % np.pi for j in par_evaluate(n.op.p)], [np.pi / 4, np.pi / 2]): # if the beamsplitter is *symmetric*, then the order of the # wires does not matter. wire_mapping[i] = 0 else: # beamsplitter is not symmetric, order matters wire_mapping[i] = [j.ind for j in n.reg] else: # not a CXgate or a BSgate, order of wires doesn't matter wire_mapping[i] = 0 # TODO: at the moment, we do not check for whether an empty # wire will match an operation with trivial parameters. # Maybe we can do this in future, but this is a subgraph # isomorphism problem and much harder. nx.set_node_attributes(circuit[-1], name_mapping, name="name") nx.set_node_attributes(circuit[-1], parameter_mapping, name="p") nx.set_node_attributes(circuit[-1], wire_mapping, name="w") def node_match(n1, n2): """Returns True if both nodes have the same name and same parameters, within a certain tolerance""" name_match = n1["name"] == n2["name"] p_match = np.allclose(n1["p"], n2["p"], atol=atol, rtol=rtol) wire_match = n1["w"] == n2["w"] if compare_params: return name_match and p_match and wire_match return name_match and wire_match # check if circuits are equivalent return nx.is_isomorphic(circuit[0], circuit[1], node_match)
def assert_equal(self, G1, G2): assert_true(nx.is_isomorphic(G1, G2, edge_match=lambda x, y: x == y))
def test_empty(self): G = nx.Graph() H = nx.inverse_line_graph(G) assert nx.is_isomorphic(H, nx.complete_graph(1))
def test_pair(self): G = nx.path_graph(2) H = nx.inverse_line_graph(G) solution = nx.path_graph(3) assert nx.is_isomorphic(H, solution)
def test_cycle(self): G = nx.cycle_graph(5) H = nx.inverse_line_graph(G) assert nx.is_isomorphic(H, G)
def test_line_inverse_line_dgm(self): G = nx.dorogovtsev_goltsev_mendes_graph(4) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)
def generate(cls, conf: config.Config) -> None: """Generates a family tree dataset based on the provided configuration. Args: conf (:class:`config.Config`): The configuration that specifies how to create the dataset. """ # a pattern that describes the base names of the created samples sample_name_pattern = "{:0" + str(len( str(conf.num_samples - 1))) + "d}" # numerous counters for computing data statistics tree_size_counts = [0] * (conf.max_tree_size + 2 ) # +2 rather than +1 -> adding of spouses total_relations_counts = [0] * (conf.max_branching_factor** conf.max_tree_depth) inferences_pos_relation_counts = {r: 0 for r in cls.RELATIONS} inferences_neg_relation_counts = {r: 0 for r in cls.RELATIONS} # create list for storing graph representations of all created samples (for checking isomorphism) sample_graphs = [] for sample_idx in range(conf.num_samples): print("creating sample #{}: ".format(sample_idx), end="") # use a fresh data context with dc.DataContext() as data_ctx: # reset person factory pf.PersonFactory.reset() total_start = time.time() # sample family tree print("sampling family tree", end="") start = time.time() done = False while not done: # randomly sample a tree family_tree = cls._sample_family_tree(conf) # create a graph that represents the structure of the created sample for checking isomorphism current_graph = nx.DiGraph() for p in family_tree: for parent in p.parents: current_graph.add_edge(p.index, parent.index) if p.married_to is not None: current_graph.add_edge(p.index, p.married_to.index) # check whether the new sample is isomorphic to any sample created earlier for existing_sample in sample_graphs: if nx.is_isomorphic(existing_sample, current_graph): data_ctx.clear() pf.PersonFactory.reset() break else: sample_graphs.append(current_graph) done = True print(" OK ({:.3f}s)".format(time.time() - start), end="") # run ASP solver to compute all inferences print(" | computing inferences", end="") start = time.time() data = cls._run_asp_solver(conf, family_tree) print(" OK ({:.3f}s)".format(time.time() - start), end="") # write sample to disk print(" | writing to disk", end="") start = time.time() cls._write_sample(conf, family_tree, data, sample_name_pattern.format(sample_idx)) print(" OK ({:.3f}s) | ".format(time.time() - start), end="") # update statistics tree_size_counts[len(family_tree)] += 1 total_relations_counts[sum( (len(p.children) for p in family_tree))] += 1 for i in data.inferences: if len(i.terms) == 2 and i.predicate in cls.RELATIONS: if i.positive: inferences_pos_relation_counts[i.predicate] += 1 else: inferences_neg_relation_counts[i.predicate] += 1 print("finished in {:.3f}s".format(time.time() - total_start)) print() # add an empty line to the output # prepare tree-size-statistics for printing title_format = "size={{:0{}d}}".format(len(str(conf.max_tree_size + 1))) tree_size_counts = { title_format.format(size): counts for size, counts in enumerate(tree_size_counts) if size > 1 } # prepare total-number-of-relations-statistics for printing max_relations = max( (size for size, counts in enumerate(total_relations_counts) if counts > 0)) title_format = "#relations={{:0{}d}}".format(len(str(max_relations))) total_relations_counts = { title_format.format(size): counts for size, counts in enumerate(total_relations_counts) if size <= max_relations } # print statistics print("DISTRIBUTION OF FAMILY TREE SIZES\n") cls._print_distribution(tree_size_counts) print("\nDISTRIBUTION OF TOTAL NUMBER OF RELATIONS PER SAMPLE\n") cls._print_distribution(total_relations_counts) print("\nINFERABLE RELATIONS\n") cls._print_stats(inferences_pos_relation_counts, inferences_neg_relation_counts) print("\nDISTRIBUTION OF POSITIVE RELATION INFERENCES\n") cls._print_distribution(inferences_pos_relation_counts) print("\nDISTRIBUTION OF NEGATIVE RELATION INFERENCES\n") cls._print_distribution(inferences_neg_relation_counts)
def test_star(self): G = nx.star_graph(5) L = nx.line_graph(G) assert nx.is_isomorphic(L, nx.complete_graph(5))
else: # if not in the group connect it to a dumb_node G.add_nodes_from([dumb_node], fill='dumb') G.add_weighted_edges_from([(dumb_node, name, new_qarg)]) dumb_node = chr(ord(dumb_node) - 1) index += 1 # compare with known structures in table_list em = iso.numerical_edge_match('weight', 10) nm = iso.categorical_node_match('fill', 'dumb') index = 0 found = False while index < len(dist_table): H = dist_table[index] if nx.is_isomorphic(G, H, node_match=nm, edge_match=em): dist_table_dict[index] = dist_table_dict[index] + 1 found = True break index += 1 if not found: # add a new structure dist_table.append(G) pos = len(dist_table) - 1 dist_table_dict[pos] = 1 dist_file_dict[pos] = parentPath iterator = 0 while iterator < len(dist_table): G = dist_table[iterator] suffix = str(iterator)
def test_line_inverse_line_multipartite(self): G = nx.complete_multipartite_graph(3, 4, 5) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)
def test_genGraphFromContactFile(commute_moves): graph = nx.read_edgelist(commute_moves, create_using=nx.DiGraph, delimiter=",", data=(("weight", float),)) assert nx.is_isomorphic(loaders.genGraphFromContactFile(commute_moves), graph)
def test_jit_round_trip(self): G = nx.Graph() d = nx.jit_data(G) H = jit_graph(json.loads(d)) K = jit_graph(d) assert_true(nx.is_isomorphic(H, K))
def test_from_biadjacency_roundtrip(self): B1 = nx.path_graph(5) M = bipartite.biadjacency_matrix(B1, [0, 2, 4]) B2 = bipartite.from_biadjacency_matrix(M) assert nx.is_isomorphic(B1, B2)
def test_digraph(self): G = nx.DiGraph() nx.add_path(G, [1, 2, 3]) H = cytoscape_graph(cytoscape_data(G)) assert H.is_directed() nx.is_isomorphic(G, H)
def test_basic(self): """Tests for joining multiple subtrees at a root node.""" trees = [(nx.full_rary_tree(2, 2**2 - 1), 0) for i in range(2)] actual = nx.join(trees) expected = nx.full_rary_tree(2, 2**3 - 1) assert_true(nx.is_isomorphic(actual, expected))
def test_graph(self): G = nx.path_graph(4) H = cytoscape_graph(cytoscape_data(G)) nx.is_isomorphic(G, H)
def is_isomorphic(graph1, graph2): return nx.is_isomorphic(graph1, graph2, node_match=_node_match)
def test_line_inverse_line_cycle(self): G = nx.cycle_graph(10) H = nx.line_graph(G) J = nx.inverse_line_graph(H) assert nx.is_isomorphic(G, J)