def merge_enclosed(graph, segments): """Merge nodes of the given graph that are on edges that do not intersects with the given segments.""" i = 0 while i < len(graph.keys()): node = graph.keys()[i] j = 0 altered = False while j < len(graph[node]): neighbour = graph[node][j] assert (neighbour in graph) edge = (node, neighbour) if not any( geometry.segment_intersection(edge, seg) for seg in segments): graph = merge_nodes(graph, edge[0], edge[1], geometry.middle(*edge)) altered = True LOG(".") break else: j += 1 continue if altered: i = 0 else: i += 1 return graph
def merge_enclosed( graph, segments ): """Merge nodes of the given graph that are on edges that do not intersects with the given segments.""" i=0 while i < len(graph.keys()): node = graph.keys()[i] j=0 altered = False while j < len(graph[node]): neighbour = graph[node][j] assert( neighbour in graph ) edge = (node,neighbour) if not any( geometry.segment_intersection(edge,seg) for seg in segments ): graph = merge_nodes( graph, edge[0], edge[1], geometry.middle(*edge) ) altered = True LOG(".") break else: j+=1 continue if altered: i = 0 else: i+=1 return graph
def MST_Prim(graph, start): """ compute MST (minimum spanning tree) Prim's algorithm high-level description: starting from one arbitrary vertex, iterate through vertexes, in each iteration, add one vertex to MST which has the minimual local costs, then update spanning vertexes ready for next iteration similar to shortest paths (Dijkstra's algo) deal with indirected graph, directed graph Graph Structure: dicts whose keys are vertexes, values are lists of tuples of (adjacent vertexes, costs) """ V = set(graph.keys()) # all vertexes X = set() # explored vertexes pq = [ (0, start) ] # priority queue to proceess min cost one at a time in iteration, also spanning vertexes qith edges (vertexes with cross-edges between X and V-X) total_cost = 0 i = 0 while X != V: cur_cost, cur_v = minheap.pop(pq) if cur_v in X: # ignore old spanned vertexes still in queue continue X.add(cur_v) total_cost += cur_cost for neighbor, cost in graph[cur_v]: # update spanning vertexes if neighbor in X: continue minheap.add(pq, (cost, neighbor)) return total_cost
def check_TPS(graph, tps): """ Takes a out-edge graph dictionary and a list of integers for topological ordering and checks if that topological ordering is correct. """ for i in reversed(range(len(tps))): for j in range(i): if tps[j] in graph[tps[i]]: print("Fault: There is a backward edge from ", tps[i], " to ", tps[j]) return False if len(graph.keys()) != len(tps): return False return True
def create_and_initialize_intelligent_water_drops(n_iwd, graph, init_vel): """ Creates and Initializes all the intelligent water drops. In this algorithm, number of intelligent water drops = number of nodes in graph. Returns intelligent water drop. """ iwd = {} for iwd_number in range(n_iwd): iwd[iwd_number] = intelligent_water_drop(iwd_number, init_vel) iwd[iwd_number].current = random.choice(graph.keys()) iwd[iwd_number].visited.append(iwd[iwd_number].current) return iwd
def rectangle(self, *args, **kwargs): israndom = kwargs.get('rand') if len(self.graph) >= 4: rect = self.randsample(4) temp = rect graph = {i: None for i in rect} while len(rect) > 0: first = rect.pop() rect.reverse() last =rect.pop() rect.reverse() graph[first] = [last] graph[last] = [first] keys = list(graph.keys()) graph[keys[0]].append(keys[1]) graph[keys[1]].append(keys[0]) graph[keys[2]].append(keys[3]) graph[keys[3]].append(keys[2]) return graph