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 create_graph(list_of_seq): """Creates an overlap graph based on a list of sequences.""" super_string_graph = Graph() num_seq = len(list_of_seq) i = 0 while i < num_seq: first = list_of_seq[i] j = 0 while j < num_seq: if i != j: second = list_of_seq[j] max_overlap = min( len(first), len(second)) # Maximum overlength is shortest sequence min_overlap = max( len(first), len(second) ) // 2 # Minimum overlap is half of the largest sequence overlap = max_overlap found = False while overlap >= min_overlap and not found: if suffix(first, overlap) == prefix(second, overlap): super_string_graph.add_edge(first, second, weight=overlap) found = True overlap -= 1 j += 1 i += 1 return super_string_graph
def __init__(self, *args, **kwargs): # These are needed (and the normal way to override from a python class) Sofa.Core.Controller.__init__(self, *args, **kwargs) self.init_done = False self.mesh = None edges = [] if kwargs["mesh"]: self.mesh = kwargs["mesh"] for edge in self.mesh.edges.value: edges += [(edge[0], edge[1], 1)] skel_graph = Graph() for edge in edges: skel_graph.add_edge(*edge) self.startNode = kwargs["startNode"] self.goalNode = kwargs["goalNode"] self.path = dijkstra(skel_graph, self.startNode, self.goalNode) print("shortest path from {} to {} is {}".format( self.startNode, self.goalNode, self.path)) self.skelMO = None if kwargs["skelMO"]: self.skelMO = kwargs["skelMO"] self.goalMO = None if kwargs["goalMO"]: self.goalMO = kwargs["goalMO"] self.it = 0 self.max_it = 1000
def run(G: Graph) -> Result: memory: int = 0 k: int = 0 W: float = 0.0 path: List[Edge] = [] vertices: List[int] = G.vertices() memory += sizeof(vertices) i: int = randrange(len(vertices)) start: int = vertices[i] del vertices[i] u: int = start while len(vertices) > 0: i = randrange(len(vertices)) v: int = vertices[i] del vertices[i] e = G.get_edge(u, v) path.append(e) W += e.w k += 1 u = v e = G.get_edge(u, start) path.append(e) W += e.w k += 1 return Result(k, W, memory, path=path)
def run(G: Graph) -> Result: c: Counter = Counter() mst, _ = prim(G, 1) tree = Graph(G.N, directed=False) tree.add_edges(mst) visited: List[bool] = [False for _ in range(G.N + 1)] walk: List[int] = [] def DFS(u: int): visited[u] = True walk.append(u) c.inc() for e in tree.get_incident_edges(u): v = e.snd if not visited[v]: DFS(v) DFS(1) path: List[Edge] = [] w: float = 0.0 first: int = walk[0] u: int = first walk.append(first) for v in walk[1:]: c.inc() e = G.get_edge(u, v) path.append(e) w += e.w u = v memory: int = 0 memory += sizeof(mst) memory += sizeof(tree) memory += sizeof(visited) memory += sizeof(walk) return Result(c.get(), w, memory, path=path)
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 solveE(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] 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) 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 WE = G.get_nodes() return WE
def day_10(): values = get_input() values.sort() values.insert(0, 0) values.append(values[-1] + 3) subgraphs = [] choke_point = 3 i = 0 itr = 1 for itr in range(len(values)): if values[itr] - values[itr - 1] == choke_point: subgraphs.append(values[i:itr]) i = itr total = 1 for sub in subgraphs: dict = {} for i in range(len(sub)): dict[sub[i]] = [] itr = i + 1 while itr < i + 4: try: if sub[itr] - sub[i] < 4: dict[sub[i]].append(sub[itr]) except: pass itr += 1 graph = Graph(dict) total *= graph.countAllPaths(sub[0], sub[-1]) return total
def run(G: Graph) -> Result: memory: int = 0 k: int = 0 W: float = 0.0 path: List[Edge] = [] vertices: List[int] = G.vertices() memory += sizeof(vertices) i: int = randrange(len(vertices)) start: int = vertices[i] del vertices[i] G.order_edges() u: int = start while len(vertices) > 0: next_e: Optional[Edge] = next( (e for e in G.get_incident_edges(u) if e.snd in vertices), None) if next_e is None: raise Exception('None') v: int = next_e.snd vertices.remove(v) e = G.get_edge(u, v) path.append(e) W += e.w k += 1 u = v e = G.get_edge(u, start) path.append(e) W += e.w k += 1 return Result(k, W, memory, path=path)
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")
def solveO(G, even_nodes, odd_nodes, h, po, pe): all_nodes = G.get_nodes() amount_nodes = len(all_nodes) if amount_nodes == 0 or po <= 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] 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
elif argv[1] == '-k': algorithm = 'kruskal' elif argv[1] == "-p": algorithm = 'prim' else: print('Unknown algorithm') exit(-1) n: int = 0 try: n = int(stdin.readline()) except ValueError: print("Wrong \'n\' input format") exit(-1) G: Graph = Graph(n, directed=False) m: int = 0 try: m = int(stdin.readline()) except ValueError: print("Wrong \'m\' input format") exit(-1) try: for _ in range(m): line = stdin.readline() u, v, w = get_edge(line) edge = Edge(u, v, w) G.add_edge(edge) except ValueError:
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) nodes, edges = G.remove_nodes(ATRO) H = Graph(nodes, edges) WE = solveE(H, even_nodes, odd_nodes, h-1, pe//2, po) 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 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 = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) while len(WE) != 0: 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//2, po) ATRE = atr(G, even_nodes, WE, 0) nodes, edges = G.remove_nodes(ATRE) G = Graph(nodes, edges) 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) 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