2 Commits
Author SHA1 Message Date
hasslesstech 7b5f6b93d8 lab3-rc2 2026-10-29 19:41:56 +02:00
hasslesstech 96a2614a9f improve efficiency 2026-10-29 16:55:51 +02:00
+33 -17
View File
@@ -7,6 +7,8 @@ write_files = False
n = 0 n = 0
nn_total_levels = 0 nn_total_levels = 0
edge_groups = {}
def y(i): def y(i):
# backwards compatibility # backwards compatibility
if i.__class__ == str: if i.__class__ == str:
@@ -45,7 +47,7 @@ def nn(*args):
return str(i(x, y)) + '-' + l return str(i(x, y)) + '-' + l
def join_nodes(g, a, b, target_degree = 6): def join_nodes(g, a, b, group = 'default', target_degree = 6):
if a == b: if a == b:
return return
@@ -54,6 +56,12 @@ def join_nodes(g, a, b, target_degree = 6):
if g.degree[a] > target_degree or g.degree[b] > target_degree: if g.degree[a] > target_degree or g.degree[b] > target_degree:
g.remove_edge(a, b) g.remove_edge(a, b)
return
if group not in edge_groups:
edge_groups[group] = []
edge_groups[group].append((a, b))
def start_file(p): def start_file(p):
if not write_files: if not write_files:
@@ -135,11 +143,11 @@ def build_network(l_n):
#print('---', i, start.x, y, step, y + step, n) #print('---', i, start.x, y, step, y + step, n)
if step == 0: if step == 0:
exit(1) exit(1)
join_nodes(g, nn(start.x, y, 'll'), nn(start.x - step, y + step, 'll')) join_nodes(g, nn(start.x, y, 'll'), nn(start.x - step, y + step, 'll'), f"gf{i}")
join_nodes(g, nn(start.x, y, 'rr'), nn(start.x + step, y + step, 'rr')) join_nodes(g, nn(start.x, y, 'rr'), nn(start.x + step, y + step, 'rr'), f"gf{i}")
y += step * 2 y += step * 2
join_nodes(g, nn(start.x, y, 'lr'), nn(start.x - step, y - step, 'lr')) join_nodes(g, nn(start.x, y, 'lr'), nn(start.x - step, y - step, 'lr'), f"gf{i}")
join_nodes(g, nn(start.x, y, 'rl'), nn(start.x + step, y - step, 'rl')) join_nodes(g, nn(start.x, y, 'rl'), nn(start.x + step, y - step, 'rl'), f"gf{i}")
start.x += step * 2 start.x += step * 2
@@ -152,17 +160,17 @@ def build_network(l_n):
# inline skips # inline skips
for x in range(n): for x in range(n):
for y in range(n): for y in range(n):
join_nodes(g, nn(x, y, 'ld'), nn((x + n//2) % n, y, 'ld')) join_nodes(g, nn(x, y, 'ld'), nn((x + n//2) % n, y, 'ld'), "is1")
join_nodes(g, nn(x, y, 'rd'), nn((x + n//3) % n, y, 'rd')) join_nodes(g, nn(x, y, 'rd'), nn((x + n//3) % n, y, 'rd'), "is2")
join_nodes(g, nn(x, y, 'lu'), nn(x, (y + n//5) % n, 'lu')) join_nodes(g, nn(x, y, 'lu'), nn(x, (y + n//5) % n, 'lu'), "is3")
join_nodes(g, nn(x, y, 'ru'), nn(x, (y + n//6) % n, 'ru')) join_nodes(g, nn(x, y, 'ru'), nn(x, (y + n//6) % n, 'ru'), "is4")
join_nodes(g, nn(x, y, 'll'), nn((x + round(n//3)) % n, (y + round(n//3)) % n, 'll')) join_nodes(g, nn(x, y, 'll'), nn((x + round(n//3)) % n, (y + round(n//3)) % n, 'll'), "is5")
join_nodes(g, nn(x, y, 'rr'), nn((x + round(n//2)) % n, (y + round(n//2)) % n, 'rr')) join_nodes(g, nn(x, y, 'rr'), nn((x + round(n//2)) % n, (y + round(n//2)) % n, 'rr'), "is6")
join_nodes(g, nn(x, y, 'lr'), nn((x + round(n//3)) % n, (y - round(n//3)) % n, 'lr')) join_nodes(g, nn(x, y, 'lr'), nn((x + round(n//5)) % n, (y - round(n//5)) % n, 'lr'), "is7")
join_nodes(g, nn(x, y, 'rl'), nn((x + round(n//2)) % n, (y - round(n//2)) % n, 'rl')) join_nodes(g, nn(x, y, 'rl'), nn((x + round(n//6)) % n, (y - round(n//6)) % n, 'rl'), "is8")
return g return g
@@ -188,6 +196,11 @@ def analyze(G: nx.Graph) -> dict:
def visualize(g): def visualize(g):
def draw_edge_groups():
colors = ["#FF0000", "#00FF00", "#0000FF", "#AAAA00", "#BB00BB", "#1ab278", "#ea44fc", "#c9c204", "#49af5a", "#692c8e"]
for i, v in enumerate(edge_groups.values()):
nx.draw_networkx_edges(g, exp_pos, edgelist = v, connectionstyle='arc3,rad=0.2', arrows=True, width = 2.0, edge_color = colors[i % len(colors)])
def draw_edges_between(a, b, x_min = 0, x_max = False, y_min = 0, y_max = False, color = 'black', width = 1.0, debug = False, direct = False, fmode = True, fmask = [1, 1, 1, 1], fsplit = False): def draw_edges_between(a, b, x_min = 0, x_max = False, y_min = 0, y_max = False, color = 'black', width = 1.0, debug = False, direct = False, fmode = True, fmask = [1, 1, 1, 1], fsplit = 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])] edgelist = [e for e in g.edges() if all([True if (a in i) or (b in i) else False for i in e])]
edgelist_filtered = [] edgelist_filtered = []
@@ -255,17 +268,19 @@ def visualize(g):
draw_edges_between('', '', x_max = 0, y_max = 0, color = '#888888', width = 0.5, direct = True) draw_edges_between('', '', x_max = 0, y_max = 0, color = '#888888', width = 0.5, direct = True)
# regular grid # regular grid
draw_edges_between('', '', x_min = 1, x_max = 1, y_min = 1, y_max = 1, color = '#FF0000', width = 2.0, fsplit = True) #draw_edges_between('', '', x_min = 1, x_max = 1, y_min = 1, y_max = 1, color = '#FF0000', width = 2.0, fsplit = True)
# anything (debug) # anything (debug)
draw_edges_between('', '', x_min = 1, y_min = 1, color = '#00FF00', width = 2.0, direct = True) #draw_edges_between('', '', x_min = 1, y_min = 1, color = '#00FF00', width = 2.0, direct = True)
draw_edge_groups()
plt.show() plt.show()
def main(): def main():
global write_files global write_files
write_files = False write_files = True
i = 0 i = 0
@@ -299,7 +314,8 @@ def test2():
print(set([v for i, v in g.degree()])) print(set([v for i, v in g.degree()]))
def test3(): def test3():
g = build_network(9) g = build_network(2)
print(analyze(g))
visualize(g) visualize(g)
if __name__=="__main__": if __name__=="__main__":