def solveE(G, even_nodes, odd_nodes, h, pe, po): all_nodes = G.get_nodes() amount_nodes = len(all_nodes) if amount_nodes == 0 or pe <= 1 or h < 0: return [] pe = min(amount_nodes, pe) po = min(amount_nodes, po) while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE, _ = atr(G, even_nodes, Nh, 0) if contain_all(G, ATRE, h): Nh_2 = [node for node in G.get_nodes() if node.get_priority() == h-2] Nh += Nh_2 ATRE, _ = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po//2, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) if equal: # testing W0 == ATR0 break #Nh = [node for node in G.get_nodes() if node.get_priority() == h] #ATRE, _ = atr(G, even_nodes, Nh, 0) #nodes, edges = G.remove_nodes(ATRE) #H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) while not(equal): Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE, _ = atr(G, even_nodes, Nh, 0) if contain_all(G, ATRE, h): Nh_2 = [node for node in G.get_nodes() if node.get_priority() == h-2] Nh += Nh_2 ATRE, _ = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po//2, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) WE = G.get_nodes() return WE
def solveO(G, even_nodes, odd_nodes, h, po, pe): all_nodes = G.get_nodes() if len(all_nodes) == 0 or po <= 1 or h < 0: return [] while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRO, _ = atr(G, odd_nodes, Nh, 1) if contain_all(G, ATRO, h): Nh_2 = [ node for node in G.get_nodes() if node.get_priority() == h - 2 ] Nh += Nh_2 ATRO, _ = atr(G, odd_nodes, Nh, 1) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h - 1, pe // 2, po) ATRE, equal = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) if equal: # testing WE == ATRE break Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRO, _ = atr(G, odd_nodes, Nh, 1) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h - 1, pe, po) ATRE, equal = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) while not (equal): Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRO, _ = atr(G, odd_nodes, Nh, 1) if contain_all(G, ATRO, h): Nh_2 = [ node for node in G.get_nodes() if node.get_priority() == h - 2 ] Nh += Nh_2 ATRO, _ = atr(G, odd_nodes, Nh, 1) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h - 1, pe // 2, po) ATRE, equal = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) WO = G.get_nodes() return WO
def solveO(G, even_nodes, odd_nodes, h): all_nodes = G.get_nodes() if len(all_nodes) == 0 or h < 0: return [] while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRO = atr(G, odd_nodes, Nh, 1) if contain_all(G, ATRO, h): Nh_2 = [ node for node in G.get_nodes() if node.get_priority() == h - 2 ] Nh += Nh_2 ATRO = atr(G, odd_nodes, Nh, 1) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h - 1) ATRE = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) if len(WE) == 0: # testing W0 emptiness break WO = G.get_nodes() return WO
def solveE(G, even_nodes, odd_nodes, h, pe, po): all_nodes = G.get_nodes() if len(all_nodes) == 0 or pe <= 1 or h < 0: return [] while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po//2, pe) ATRO = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) if len(WO) == 0: # testing W0 emptiness break Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po, pe) ATRO = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) while len(WO) != 0: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h-1, po//2, pe) ATRO = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) WE = G.get_nodes() return WE
def solveE(G, even_nodes, odd_nodes, h, pe, po): all_nodes = G.get_nodes() if len(all_nodes) == 0 or pe <= 1 or h < 0: return [] while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE, equal = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h - 1, po // 2, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) if equal: # testing W0 == ATR0 break Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE, _ = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h - 1, po, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) while not (equal): Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRE, _ = atr(G, even_nodes, Nh, 0) nodes, edges = G.remove_nodes(ATRE) H = Graph(nodes, edges) WO = solveO(H, even_nodes, odd_nodes, h - 1, po // 2, pe) ATRO, equal = atr(G, odd_nodes, WO, 1) nodes, edges = G.remove_nodes(ATRO) G = Graph(nodes, edges) WE = G.get_nodes() return WE
def solveO(G, even_nodes, odd_nodes, h): all_nodes = G.get_nodes() if len(all_nodes) == 0 or h < 0: return [] while True: Nh = [node for node in G.get_nodes() if node.get_priority() == h] ATRO, _ = atr(G, odd_nodes, Nh, 1) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h-1) ATRE, equal = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) if equal: # testing WE == ATRE break WO = G.get_nodes() return WO
def main(input_file, output_file): # player 0: circular - even # player 1: rectangular - odd # creating the graph from this webpage: https://en.wikipedia.org/wiki/Parity_game even_nodes = [] odd_nodes = [] G = Graph() higher = -1 number = None with open(input_file) as reader: data = reader.read().splitlines(True) number = data[0].split() number = number[1] number = number.replace(";", "") data = data[1:] for line in data: uuid, p, owner, edges, name = line.split() uuid = int(uuid) p = int(p) owner = int(owner) name = name.replace("\"", "") name = name.replace(";", "") node = Node(p, owner, uuid, name) edges = edges.split(',') G.insert_node(node) for edge in edges: edge = int(edge) G.insert_edge(uuid, edge) if owner % 2 == 0: even_nodes.append(uuid) else: odd_nodes.append(uuid) if p > higher: higher = p if higher % 2 == 1: higher = higher + 1 WE = solveE(G, even_nodes, odd_nodes, higher) nodes = G.get_nodes() for node in WE: node.set_winner(0) # 0 means even player for node in nodes: if node.get_winner() == -1: node.set_winner(1) # 1 means odd player with open(output_file, 'w') as writter: writter.write("parity " + number + ";\n") for node in nodes: writter.write(str(node.get_uuid()) + " " + str(node.get_winner()) + ";\n")
def main(input_file, output_file): # player 0: circular - even # player 1: rectangular - odd even_nodes = [] odd_nodes = [] G = Graph() higher = -1 number_nodes = None with open(input_file) as reader: data = reader.read().splitlines(True) number_nodes = data[0].split() number_nodes = number_nodes[1] number_nodes = number_nodes.replace(";", "") data = data[1:] for line in data: uuid, p, owner, edges, name = line.split() uuid = int(uuid) p = int(p) owner = int(owner) name = name.replace("\"", "") name = name.replace(";", "") node = Node(p, owner, uuid, name) edges = edges.split(',') G.insert_node(node) for edge in edges: edge = int(edge) G.insert_edge(uuid, edge) if owner % 2 == 0: even_nodes.append(uuid) else: odd_nodes.append(uuid) if p > higher: higher = p if higher % 2 == 1: higher = higher + 1 # it's number_nodes + 1 because it's index in 0 WE = solveE(G, even_nodes, odd_nodes, higher, int(number_nodes) + 1, int(number_nodes) + 1) nodes = G.get_nodes() for node in WE: node.set_winner(0) # 0 means even player for node in nodes: if node.get_winner() == -1: node.set_winner(1) # 1 means odd player with open(output_file, 'w') as writter: writter.write("parity " + number_nodes + ";\n") for node in nodes: writter.write(str(node.get_uuid()) + " " + str(node.get_winner()) + ";\n")