Commit 7e66edd0 authored by Yoann Dufresne's avatar Yoann Dufresne
Browse files

bugfix on graph manipulation for merging and test to verify it

parent 309388be
...@@ -34,14 +34,15 @@ def merge_nodes(G, node1, node2): ...@@ -34,14 +34,15 @@ def merge_nodes(G, node1, node2):
new_node = f"{node1}_{node2}" if node1 < node2 else f"{node2}_{node1}" new_node = f"{node1}_{node2}" if node1 < node2 else f"{node2}_{node1}"
G.add_node(new_node) G.add_node(new_node)
# Add the edges from previous nodes # List the edges to add
neighbors = []
for edge in G.edges(node1): for edge in G.edges(node1):
neighbor = edge[0] if edge[0] != node1 else edge[1] neighbor = edge[0] if edge[0] != node1 else edge[1]
if neighbor == node2: if neighbor == node2:
continue continue
G.add_edge(neighbor, new_node) neighbors.append(neighbor)
for edge in G.edges(node2): for edge in G.edges(node2):
neighbor = edge[0] if edge[0] != node2 else edge[1] neighbor = edge[0] if edge[0] != node2 else edge[1]
...@@ -49,6 +50,13 @@ def merge_nodes(G, node1, node2): ...@@ -49,6 +50,13 @@ def merge_nodes(G, node1, node2):
if neighbor == node1: if neighbor == node1:
continue continue
neighbors.append(neighbor)
# De-replicate neighbors
neighbors = set(neighbors)
# Add neighbors
for neighbor in neighbors:
G.add_edge(neighbor, new_node) G.add_edge(neighbor, new_node)
# Remove previous nodes from the graph # Remove previous nodes from the graph
......
import unittest import unittest
from d_graph import Dgraph import graph_manipulator as gm
class TestGraphManipulation(unittest.TestCase): class TestGraphManipulation(unittest.TestCase):
def test_dg_to_list(self): def test_generate_d_graph_chain(self):
center, h1, h2, G = unit_d_graph for size in range(10, 100, 10):
dg = Dgraph(center) for d in range(1, 4):
dg.put_halves(h1, h2, G) G = gm.generate_d_graph_chain(size, d)
lst = dg.to_ordered_lists()
# Test the size
self.assertEquals([['A0'], ['A1'], ['A2'], ['C'], ['B2'], ['B1'], ['B0']], lst) 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!
Please register or to comment