diff --git a/main.py b/main.py index 2c8e282..0017551 100644 --- a/main.py +++ b/main.py @@ -9,8 +9,11 @@ def nn(cluster_no, node_name): return str(cluster_no) + '-' + node_name return str(cluster_no % nn_total_levels) + '-' + node_name -def join_nodes(g, a, b): - if not (g.degree[a] == 6 or g.degree[b] == 6): +def lvl(node_name): + return int(node_name.split('-')[0]) + +def join_nodes(g, a, b, target_degree = 6): + if not (g.degree[a] == target_degree or g.degree[b] == target_degree): g.add_edge(a, b) def start_file(p): @@ -49,16 +52,16 @@ def build_network(n): for i in range(n): if n >= 2: if i % 2: - g.add_edge(nn(i, 'lu'), nn(i-1, 'lu')) - g.add_edge(nn(i, 'rd'), nn(i-1, 'rd')) + join_nodes(g, nn(i, 'lu'), nn(i-1, 'lu'), 4) + join_nodes(g, nn(i, 'rd'), nn(i-1, 'rd'), 4) else: - g.add_edge(nn(i, 'ld'), nn(i-1, 'ld')) - g.add_edge(nn(i, 'ru'), nn(i-1, 'ru')) + join_nodes(g, nn(i, 'ld'), nn(i-1, 'ld'), 4) + join_nodes(g, nn(i, 'ru'), nn(i-1, 'ru'), 4) g.add_edge(nn(i, 'ls'), nn(i-1, 'rs')) g.add_edge(nn(i, 'rs'), nn(i-1, 'ls')) - if n >= 1 * throw_dist: + if n > 1 * throw_dist: g.add_edge(nn(i, 'rs'), nn(i-(1*throw_dist), 'ls')) g.add_edge(nn(i, 'ls'), nn(i-(1*throw_dist), 'rs')) @@ -105,6 +108,103 @@ def analyze(G: nx.Graph) -> dict: 'C': C, } +def visualize(g): + def draw_edges_between(a, b, lvl_min = 0, lvl_max = False, color = 'black', width = 1.0, debug = False, direct = False): + edgelist = [e for e in g.edges() if all([True if (a in i) or (b in i) else False for i in e])] + + if debug: + print(edgelist) + + if lvl_max.__class__ == bool: + if debug: + print("Running 1 branch") + + edgelist = [(pa, pb) for pa, pb in edgelist if not (abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min)] + ''' + for p in edgelist: + if abs(lvl(pa) - lvl(pb)) < lvl_min: + edgelist.remove((pa, pb)) + ''' + else: + if debug: + print(f"Running 2 branch on {edgelist}") + + edgelist = [(pa, pb) for pa, pb in edgelist if not ((abs(lvl(pa) - lvl(pb)) < lvl_min or nn_total_levels - abs(lvl(pa) - lvl(pb)) < lvl_min) or (abs(lvl(pa) - lvl(pb)) > lvl_max and nn_total_levels - abs(lvl(pa) - lvl(pb)) > lvl_max))] + ''' + for pa, pb in edgelist: + if debug: + print(f"Processing ({pa}, {pb})") + + if abs(lvl(pa) - lvl(pb)) < lvl_min or abs(lvl(pa) - lvl(pb)) > lvl_max: + edgelist.remove((pa, pb)) + if debug: + print(f'Discarding {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})') + elif debug: + print(f'Leaving {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})') + + for pa, pb in edgelist: + if debug: + print(f"Processing ({pa}, {pb})") + + if abs(lvl(pa) - lvl(pb)) < lvl_min or abs(lvl(pa) - lvl(pb)) > lvl_max: + edgelist.remove((pa, pb)) + if debug: + print(f'Discarding {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})') + elif debug: + print(f'Leaving {pa} <-> {pb} (lvl(pa) == {lvl(pa)}, lvl(pb) == {lvl(pb)})') + ''' + + if debug: + print(edgelist) + + if direct: + nx.draw_networkx_edges(g, exp_pos, edgelist = edgelist, width = width, edge_color = color) + else: + nx.draw_networkx_edges(g, exp_pos, edgelist = edgelist, connectionstyle='arc3,rad=0.2', arrows=True, width = width, edge_color = color) + + pos = { + 'ls': (-2, 0), + 'lu': (-1, 1), + 'ld': (-1, -1), + 'm' : (0, 0), + 'ru': (1, 1), + 'rd': (1, -1), + 'rs': (2, 0) + } + + exp_pos = {} + + for i in range(nn_total_levels): + for j, v in pos.items(): + exp_pos[nn(i, j)] = (v[0], v[1] * 0.65 + 3 * i) + + nx.draw_networkx_nodes(g, exp_pos, node_size = 600) + nx.draw_networkx_labels(g, exp_pos, font_size = 8) + + #nx.draw_networkx_edges(g, exp_pos, edgelist = [e for e in g.edges() if all([True if ('ls' in i) or ('rs' in i) else False for i in e])], connectionstyle='arc3,rad=0.2', arrows=True) + + # cluster + draw_edges_between('', '', lvl_max = 0, color = '#888888', width = 0.5, direct = True) + + # S => 4 + draw_edges_between('ls', 'rs', lvl_max = 1, color = '#33AAAA', width = 2) + + draw_edges_between('lu', 'lu', lvl_max = 1, color = '#AAAA33', width = 1) + draw_edges_between('ru', 'ru', lvl_max = 1, color = '#AAAA33', width = 1) + draw_edges_between('ld', 'ld', lvl_max = 1, color = '#AAAA33', width = 1) + draw_edges_between('rd', 'rd', lvl_max = 1, color = '#AAAA33', width = 1) + + # S => 6 + draw_edges_between('lu', 'lu', lvl_min = 2, color = '#FF0000', width = 1.5) + draw_edges_between('ld', 'ld', lvl_min = 2, color = '#FFFF00', width = 1.5) + draw_edges_between('ru', 'ru', lvl_min = 2, color = '#00FF00', width = 1.5) + draw_edges_between('rd', 'rd', lvl_min = 2, color = '#00FFFF', width = 1.5) + draw_edges_between('ls', 'ls', lvl_min = 2, color = '#0000FF', width = 1.5) + draw_edges_between('rs', 'rs', lvl_min = 2, color = '#FF00FF', width = 1.5) + draw_edges_between('m' , 'm' , lvl_min = 2, color = '#000000', width = 1.5) + + plt.show() + def main(): i = 0 @@ -146,9 +246,14 @@ def test(): plt.show() def test2(): - g = build_network(150) + g = build_network(144) print(analyze(g)) print(set([v for i, v in g.degree()])) +def test3(): + g = build_network(3) + #print(g.adj['0-ls']) + visualize(g) + if __name__=="__main__": - main() + test3()