Commit 7e66edd0 by Yoann Dufresne

### bugfix on graph manipulation for merging and test to verify it

parent 309388be
 ... ... @@ -34,14 +34,15 @@ def merge_nodes(G, node1, node2): new_node = f"{node1}_{node2}" if node1 < node2 else f"{node2}_{node1}" G.add_node(new_node) # Add the edges from previous nodes # List the edges to add neighbors = [] for edge in G.edges(node1): neighbor = edge[0] if edge[0] != node1 else edge[1] if neighbor == node2: continue G.add_edge(neighbor, new_node) neighbors.append(neighbor) for edge in G.edges(node2): neighbor = edge[0] if edge[0] != node2 else edge[1] ... ... @@ -49,6 +50,13 @@ def merge_nodes(G, node1, node2): if neighbor == node1: continue neighbors.append(neighbor) # De-replicate neighbors neighbors = set(neighbors) # Add neighbors for neighbor in neighbors: G.add_edge(neighbor, new_node) # Remove previous nodes from the graph ... ...
 import unittest from d_graph import Dgraph import graph_manipulator as gm class TestGraphManipulation(unittest.TestCase): def test_dg_to_list(self): center, h1, h2, G = unit_d_graph dg = Dgraph(center) dg.put_halves(h1, h2, G) lst = dg.to_ordered_lists() self.assertEquals([['A0'], ['A1'], ['A2'], ['C'], ['B2'], ['B1'], ['B0']], lst) def test_generate_d_graph_chain(self): for size in range(10, 100, 10): for d in range(1, 4): G = gm.generate_d_graph_chain(size, d) # Test the size self.assertEquals(len(G.nodes()), size) # Test arities for idx in range(d): self.assertEquals(len(G.edges(idx)), d+idx) self.assertEquals(len(G.edges(size-idx-1)), d+idx) for idx in range(d, size-d): self.assertEquals(len(G.edges(idx)), d*2) def test_merging_node(self): size = 15 d = 3 G = gm.generate_d_graph_chain(size, d) for gap in range(1, d+1): H = G.copy() gm.merge_nodes(H, d+1, d+gap+1) self.assertEquals(len(H.nodes()), size-1) g_nodes = list(G.nodes()) for node in H.nodes(): if node in g_nodes: g_neighbors = list(G.neighbors(node)) h_neighbors = list(H.neighbors(node)) if (d+1) in g_neighbors and (d+gap+1) in g_neighbors: self.assertEquals(len(g_neighbors), len(h_neighbors)+1) else: self.assertEquals(len(g_neighbors), len(list(h_neighbors))) else: cumulative_neighbors = set(list(G.neighbors(d+1)) + list(G.neighbors(d+gap+1))) h_neighbors = H.neighbors(f"{d+1}_{d+gap+1}") self.assertEquals(len(cumulative_neighbors)-2, len(list(h_neighbors))) ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!