From 0ad2f8e7281e9a2e775a3e27fba0013f3af938e6 Mon Sep 17 00:00:00 2001 From: Stefan Pranger Date: Wed, 9 Jun 2021 17:34:51 +0200 Subject: [PATCH] WIP some further work --- st.py | 121 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 39 deletions(-) diff --git a/st.py b/st.py index e2d8b11..84ab760 100755 --- a/st.py +++ b/st.py @@ -20,6 +20,30 @@ def colour(index): array = ["red", "blue", "green"] return array[index % len(array)] +def sort_edges(edges): + return sorted(edges, key = lambda x: (x[0].index, x[1].index)) + +def sort_tuples_and_edges(edges): + sorted_tuples = [] + for e in edges: + if e[0].index == num_nodes - 1 and e[1].index == 0: + sorted_tuples.append(e) + elif e[0].index > e[1].index: + sorted_tuples.append((e[1], e[0])) + else: + sorted_tuples.append(e) + return sort_edges(sorted_tuples) + + +def to_indices(edge): + return (edge[0].index, edge[1].index) + +def list_to_indices(edges): + e = [] + for edge in edges: + e.append(to_indices(edge)) + return e + class Sxiv(ImageShow.UnixViewer): def get_command_ex(self, file, **options): return ("bspc rule -a \* -o state=floating focus=off && sxiv",) * 2 @@ -27,7 +51,7 @@ class Sxiv(ImageShow.UnixViewer): node_size = 10 m, n = 600, 600 nodes = [] -num_nodes = 9 +num_nodes = 6 assert(num_nodes % 3 == 0) @@ -58,6 +82,11 @@ for i,node in enumerate(nodes): draw.rectangle(node.rect, fill=node.colour) draw.text(node.pos, str(node.index),font=font, fill="black") +root = [] +for i in range(0, num_nodes - 1): + root.append((nodes[i], nodes[i+1])) +root_tuples = list_to_indices(root) + def connect_tuple(d, e): @@ -73,17 +102,6 @@ def draw_tree(edges): connect(d, e[0], e[1]) local.show() -def sort_edges(edges): - return sorted(edges, key = lambda x: (x[0].index, x[1].index)) - -def to_indices(edge): - return (edge[0].index, edge[1].index) - -def list_to_indices(edges): - e = [] - for edge in edges: - e.append(to_indices(edge)) - return e def has_crossing(edge, edges): new = to_indices(edge) @@ -129,9 +147,10 @@ def construct_neighbourhood(edges, index): i = 0 #print(list_to_indices(edges)) for e in edges: + if not is_from_root(to_indices(e)): continue start = e[0].index #print("Testing: " + str(to_indices(e))) - for new_target in range(start + 1, num_nodes): + for new_target in range(0, num_nodes): new_edge = (e[0], nodes[new_target]) if(e == new_edge): continue cropped_edges = edges[:] @@ -171,7 +190,6 @@ def is_from_root(edge): def get_largest_non_root_edge(edges): edges.reverse() - print(edges) for e in edges: if not is_from_root(e): return e @@ -181,17 +199,35 @@ def get_largest_missing_convex_edge(edges): difference = [e for e in root_tuples if e not in edges] return difference[-1] +def get_missing_convex_edge(edges, tup): + for i in range(tup[0], tup[1]): + if (i,i+1) not in list_to_indices(edges): + print("missing edge: " + str((i,i+1))) + return (nodes[i], nodes[i+1]) + +def get_e_bar(edges): + for e in sort_tuples_and_edges(edges): + if not is_from_root(to_indices(e)): + print("e_bar: " + str(to_indices(e))) + return e + return edges + + def get_parent(edges): - sorted_edges = list_to_indices(sort_edges(edges)) - e = get_largest_non_root_edge(sorted_edges) + sorted_edges = sort_edges(edges) + e = get_e_bar(sorted_edges) if type(e) is list: return list_to_indices(edges) - sorted_edges.remove(e) - sorted_edges.append(get_largest_missing_convex_edge(sorted_edges)) + try: + sorted_edges.remove(e) + except: + sorted_edges.remove((e[1], e[0])) + #sorted_edges.append(get_largest_missing_convex_edge(sorted_edges)) + sorted_edges.append(get_missing_convex_edge(sorted_edges, to_indices(e))) return sorted_edges def get_parent_in_nodes(edges): - parent = get_parent(edges) + parent = list_to_indices(get_parent(edges)) p = [] for e in parent: p.append((nodes[e[0]], nodes[e[1]])) @@ -199,35 +235,46 @@ def get_parent_in_nodes(edges): +# +#test = [] +#test.append((nodes[1], nodes[2])) +#test.append((nodes[2], nodes[3])) +#test.append((nodes[2], nodes[4])) +#test.append((nodes[4], nodes[0])) +#test.append((nodes[0], nodes[5])) +#test.append((nodes[5], nodes[6])) +#test.append((nodes[0], nodes[7])) +#test.append((nodes[8], nodes[8])) +#draw_tree(test) +#draw_tree(get_parent_in_nodes(test)) -root = [] -for i in range(0, num_nodes - 1): - root.append((nodes[i], nodes[i+1])) -root_tuples = list_to_indices(root) -draw_tree(root) - - -assert(False) v = root j = 0 count = 1 while True: - print("Current v: " + str(list_to_indices(v))) deg = degree(v) - print(" degree = " + str(deg)) + #print(" degree = " + str(deg)) while(j != deg): j = j + 1 - w = sort_edges(get_jth_neighbour(v, j - 1)) - if(sort_edges(get_parent_in_nodes(w)) == sort_edges(v)): - print("\t " + str(list_to_indices(w))) - print("\t parent: " + str(list_to_indices(get_parent_in_nodes(w)))) + w = sort_tuples_and_edges(get_jth_neighbour(v, j - 1)) + print("\t prob(v): " + str(list_to_indices(v))) + print("\t probing: " + str(list_to_indices(w))) + print("\t pred(w): " + str(list_to_indices(sort_tuples_and_edges(get_parent_in_nodes(w))))) + if(sort_tuples_and_edges(get_parent_in_nodes(w)) == sort_tuples_and_edges(v)): + print("!!! Added: " + str(list_to_indices(w))) v = w j = 0 count = count + 1 - print(" Current w:" + str(list_to_indices(v))) - print(" degree = " + str(deg) + " j = " + str(j)) + #print(" Current w:" + str(list_to_indices(v))) + #print(" degree = " + str(deg) + " j = " + str(j)) deg = degree(v) + #draw_tree(v) + #input() + #for proc in psutil.process_iter(): + # if proc.name() == "sxiv": + # proc.kill() + input() if(v != root): w = v @@ -244,10 +291,6 @@ while True: print(j) break - - - - #while True: # for tree in construct_neighbourhood(root, sys.maxsize): # im1 = im.copy()