def edge_probabilities(g, cpt, threshold=False): # Given cpt and the graph: # P(x_1~x_2) = sum_{parents}{P(x_p~x_1, x_1~x_2)} # For the CPT we have the keys as ((x_p~x_1), (x_1~x_2)) : P((x_1~x_2) | (x_p~x_1)) # Thus all the parents are all key[0] where key[1] == (x_1~x_2) g = g.copy() if threshold: ept = {(u, v): max(edge_score, float('1e-5')) for u, v, edge_score in g.edges(data='score')} # Some incoming values were very close to the smallest possible float # which would get converted to nan else: ept = {(u, v): edge_score for u, v, edge_score in g.edges(data='score')} # Initialize a dict of the form (u~v) : p(u~v) with the scores for edge in nx.edge_bfs(g, 's'): # Breadth first search starting at s for efficiency ept[edge] = edge_probability(cpt, edge, ept, 0) # Find P(edge), gets recursive Whoa!!! if threshold: # May want to remove values below a certain cutoff ept = {key: max(value, float('1e-5')) for key, value in ept.items()} return g, cpt, ept
def solve(client): client.end() client.start() vote = votes(client) newG = nx.Graph() for e in list(client.G.edges): wt = client.G.edges[e[0], e[1]]['weight'] wt = wt * (1 - vote[e[0] - 1] / client.k) newG.add_edge(e[0], e[1], weight=wt) MST = nx.minimum_spanning_tree(newG) # print(sorted(MST.edges(data = True))) bfs_edge = list(nx.edge_bfs(MST, source=client.home)) # print(dfs_edge) foundBot = 0 for i in range(client.v - 2, -1, -1): if (foundBot < client.l): foundBot = foundBot - client.bot_count[bfs_edge[i][1]] + client.remote(bfs_edge[i][1], bfs_edge[i][0]) else: if (client.bot_count[bfs_edge[i][1]] > 0): client.remote(bfs_edge[i][1], bfs_edge[i][0]) score = client.end() print("The input was: V", client.v, " E: ", client.e, " L: ", client.l, " K: ", client.k) return score
def sub_schema_graph(self, source, include_parents=True, include_children=True, size=None): """Visualize a sub-graph of the schema based on a specific node""" scls = SchemaClass(source, self) paths = scls.parent_classes parents = [] for _path in paths: elements = [] for _ele in _path: elements.append(_ele.name) parents.append(elements) # handle cases where user want to get all children if include_parents is False and include_children: edges = list( nx.edge_bfs(self.full_class_only_graph, [self.cls_converter.get_uri(source)])) # handle cases where user want to get all parents elif include_parents and include_children is False: edges = [] for _path in parents: _path.append(self.cls_converter.get_label(source)) for i in range(0, len(_path) - 1): edges.append((_path[i], _path[i + 1])) # handle cases where user want to get both parents and children elif include_parents and include_children: edges = list( nx.edge_bfs(self.full_class_only_graph, [self.cls_converter.get_uri(source)])) for _path in parents: _path.append(self.cls_converter.get_label(source)) for i in range(0, len(_path) - 1): edges.append((_path[i], _path[i + 1])) else: raise ValueError( "At least one of include_parents and include_children parameter need to be set to True" ) curie_edges = [] for _edge in edges: curie_edges.append((self.cls_converter.get_label(_edge[0]), self.cls_converter.get_label(_edge[1]))) return visualize(curie_edges, size=size)
def test_digraph_ignore(self): G = nx.DiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes, orientation="ignore")) x_ = [ (0, 1, FORWARD), (1, 0, REVERSE), (2, 0, REVERSE), (2, 1, REVERSE), (3, 1, REVERSE), ] assert x == x_
def test_digraph_rev(self): G = nx.DiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes, orientation="reverse")) x_ = [ (1, 0, REVERSE), (2, 0, REVERSE), (0, 1, REVERSE), (2, 1, REVERSE), (3, 1, REVERSE), ] assert x == x_
def test_multigraph(self): G = nx.MultiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes)) x_ = [(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (1, 2, 0), (1, 3, 0)] # This is an example of where hash randomization can break. # There are 3! * 2 alternative outputs, such as: # [(0, 1, 1), (1, 0, 0), (0, 1, 2), (1, 3, 0), (1, 2, 0)] # But note, the edges (1,2,0) and (1,3,0) always follow the (0,1,k) # edges. So the algorithm only guarantees a partial order. A total # order is guaranteed only if the graph data structures are ordered. assert x == x_
def test_digraph_orientation_original(self): G = nx.DiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes, orientation="original")) x_ = [ (0, 1, FORWARD), (1, 0, FORWARD), (2, 0, FORWARD), (2, 1, FORWARD), (3, 1, FORWARD), ] assert x == x_
def sub_schema_graph(self, source, direction, size=None): if direction == 'down': edges = list(nx.edge_bfs(self.schema_nx, [source])) return visualize(edges, size=size) elif direction == 'up': paths = self.find_parent_classes(source) edges = [] for _path in paths: _path.append(source) for i in range(0, len(_path) - 1): edges.append((_path[i], _path[i + 1])) return visualize(edges, size=size) elif direction == "both": paths = self.find_parent_classes(source) edges = list(nx.edge_bfs(self.schema_nx, [source])) for _path in paths: _path.append(source) for i in range(0, len(_path) - 1): edges.append((_path[i], _path[i + 1])) return visualize(edges, size=size)
def find_ordering(labels, order_type, root_node): edges = [] added_edges = [] for label in labels: x, name, y = label.split('.') i = -1 if (x, y) in added_edges: i = added_edges.index((x, y)) elif (y, x) in added_edges: i = added_edges.index((y, x)) if i != -1: edges[i][2]['name'].append(name) else: edges.append((x, y, {'name': [name]})) added_edges.append((x, y)) G = nx.Graph() G.add_edges_from(edges) if root_node == 'none': degrees = nx.degree_centrality(G) root_node = max(degrees, key=degrees.get) if order_type == 'bfs': new_labels = nx.edge_bfs(G, root_node) if order_type == 'dfs': new_labels = nx.edge_dfs(G, root_node) names = nx.get_edge_attributes(G, 'name') ordered_labels = [] for label in new_labels: x, y = label if label in names: edge_names = names[label] else: edge_names = names[(y, x)] for name in edge_names: new_label = '.'.join([x, name, y]) if new_label in labels: ordered_labels.append(new_label) else: new_label = '.'.join([y, name, x]) ordered_labels.append(new_label) return ordered_labels
def main(argv): parser = ArgumentParser() parser.add_argument('-i', '--input_file', help='Input .dot file', required=True) parser.add_argument('-s', '--start_id', help='Start ID (inclusive)', required=True) parser.add_argument('-f', '--finish_id', help='Finish ID (inclusive)', required=True) parser.add_argument('-o', '--output_file', help='Output .dot file', required=True) args = parser.parse_args(args=argv) graph = nx.DiGraph(nx.drawing.nx_pydot.read_dot(args.input_file)) new_graph = nx.DiGraph() start_key = None for node_key in nx.lexicographical_topological_sort(graph): id_portion = node_key.split()[0] has_id = id_portion.isdigit() if has_id: curr_id = int(id_portion) if curr_id == int(args.start_id): start_key = node_key break if start_key is None: raise RuntimeError("Could not find the node with ID {} to start from!".format(args.start_id)) for edge in nx.edge_bfs(graph, start_key, orientation='ignore'): from_key, to_key, _ = edge id_portion = from_key.split()[0] has_id = id_portion.isdigit() end_key = from_key if has_id: curr_id = int(id_portion) if curr_id >= int(args.finish_id): break node_data = graph.nodes[from_key] new_graph.add_node(from_key, **node_data) edge_data = graph.edges[from_key, to_key] new_graph.add_edge(from_key, to_key, **edge_data) # for edge in nx.edge_bfs(graph, end_key, reverse=True): # from_key, to_key = edge # if from_key == start_key: # break # node_data = graph.nodes[from_key] # new_graph.add_node(from_key, **node_data) # edge_data = graph.edges[from_key, to_key] # new_graph.add_edge(from_key, to_key, **edge_data) nx.drawing.nx_pydot.write_dot(new_graph, args.output_file)
def generate_candidate_compressions(nlp, edge_model, vectorizer, sentence): doc = nlp(sentence) root = get_root(doc) groups = get_groups(edge_model, vectorizer, doc) candidates = set() for path in get_possible_paths(groups): path_graph = nx.DiGraph() for edge, label in path: path_graph.add_edge(edge.head.i, edge.i) for head, modifier in list(nx.edge_bfs(path_graph, root.i)): label = [l for e, l in path if e.i == modifier][0] if path_graph.has_node(modifier) and label == "del_l": subtree = list(nx.edge_bfs(path_graph, modifier)) path_graph.remove_edges_from([(head, modifier), *subtree]) elif path_graph.has_node(modifier) and label == "del_u": subtree = list(nx.edge_bfs(path_graph, modifier)) path_graph = nx.DiGraph() path_graph.add_edges_from(subtree) if len(path_graph.edges) > 0: candidates.add(" ".join([ doc[n].text for n in sorted( list(set([n for e in path_graph.edges for n in e]))) ])) return candidates
def bfs(self,*args): G = self.generate_graph(args[0]) nodelist = G.nodes(); print "Printing the nodes" print nodelist print "Printing the edges" print G.edges() print "Hello Printing bfs" print list(nx.edge_bfs(G,nodelist)) new_edges = list(nx.bfs_edges(G,source=args[1],depth_limit=3)) print new_edges new_Graph = nx.Graph() new_Graph.add_edges_from(new_edges) print list(islice(nx.shortest_simple_paths(new_Graph,args[1],args[2]),100))
def solve(client): client.end() client.start() MST = nx.minimum_spanning_tree(client.G) # print(sorted(MST.edges(data = True))) bfs_edge = list(nx.edge_bfs(MST, source = client.home)) # print(bfs_edge) foundBot = 0 for i in range(client.v - 2, -1, -1): if (foundBot < client.l): foundBot = foundBot - client.bot_count[bfs_edge[i][1]] + client.remote(bfs_edge[i][1], bfs_edge[i][0]) else: if (client.bot_count[bfs_edge[i][1]] > 0): client.remote(bfs_edge[i][1], bfs_edge[i][0]) client.end() print("The input was: V", client.v ," E: ", client.e, " L: ", client.l, " K: ", client.k)
def remoteKnownBotHome(client): knownBots = [client.h] for i in range(1, client.v + 1): if i == client.h: continue if (client.bot_count[i] > 0): knownBots.append(i) newG = nx.Graph() for i in range(len(knownBots)): newG.add_node(knownBots[i]) for (u, v, wt) in client.G.edges.data('weight'): if u in knownBots and v in knownBots: newG.add_edge(u, v, weight=wt) MST = nx.minimum_spanning_tree(newG) bfs_edge = list(nx.edge_bfs(MST, source=client.home)) r_bfs_edge = reversed(bfs_edge) for edge in r_bfs_edge: client.remote(edge[1], edge[0])
def graph_to_coords(g): """Topologically sorts graph and returns a Dict of structure: nodeId: {x: , y: } Note Nodes will be spaced such that the gap between nodes is a multiple of height/width. This constant is defined in GraphConstants.NODE_SPACING_FACTOR Graphs are sorted downwards, and nodes are centre justitifed, like this 1 2 3 4 5 6 7 """ levels = [] for node in topological_sort(g): upstream_nodes = list(edge_bfs(g, node, orientation="reverse")) ind = 0 for up_node, _, _ in upstream_nodes: for level_index, level in enumerate(levels): if up_node in level: ind = max(ind, level_index + 1) if len(levels) < ind + 1: levels.append([node]) else: levels[ind].append(node) max_width = max([len(l) for l in levels]) dx = GraphConstants.NODE_WIDTH dy = GraphConstants.NODE_HEIGHT s = GraphConstants.NODE_SPACING_FACTOR nodes = {} for l_index, level in enumerate(levels): spacing = (max_width + 1) / (len(level) + 1) for n_index, node in enumerate(level): nodes[node] = { "x": (n_index + 1) * spacing * dx * s, "y": (l_index + 1) * dy * s, } return nodes
def shortest_path_amount_solution(path_to_graph: str, source: int) -> List[int]: """ Finds for each node the amount of different possible paths from source to it that have the shortest path :param path_to_graph: path to the pickle file with the graph input :param source: the number od the wanted source :return: array with the amount of shortest paths """ """ idea: we iterate the graph from source by BFS. when we find a new shortest path to node, to set its path amount to be the same ad the node we arrived from, and when we find a new path with same distance as the current shortest path we update the counter by the amount of paths to node we arrived from. """ g = nx.read_gpickle(os.path.join(os.getcwd(), path_to_graph)) n = len(g.nodes) dists = np.tile(np.inf, n) # length of shortest path to each node so far dists[source] = 0 amount = np.zeros( n ) # amount of paths found with the same length as the current shortest path amount[source] = 1 # Pay attention that we used edge_bfs and not bfs_edges. that's because bfs_edges will remember the visited nodes # and will not contains another edges towards them - and using it will give us a perfect dists array but amount # array will be the same as np.ones(n). But edge_bfs iterate over *all* edges in a bfs order, thus helps us. for u, v in nx.edge_bfs(g, source): if dists[u] + 1 < dists[v]: dists[v] = dists[u] + 1 amount[v] = amount[u] elif dists[u] + 1 == dists[v]: amount[v] += amount[u] return list(amount)
def test_digraph_rev2(self): G = nx.DiGraph() nx.add_path(G, range(4)) x = list(nx.edge_bfs(G, [3], orientation="reverse")) x_ = [(2, 3, REVERSE), (1, 2, REVERSE), (0, 1, REVERSE)] assert x == x_
def test_digraph2(self): G = nx.DiGraph() nx.add_path(G, range(4)) x = list(nx.edge_bfs(G, [0])) x_ = [(0, 1), (1, 2), (2, 3)] assert x == x_
def test_digraph_orientation_none(self): G = nx.DiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes, orientation=None)) x_ = [(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)] assert x == x_
def test_graph(self): G = nx.Graph(self.edges) x = list(nx.edge_bfs(G, self.nodes)) x_ = [(0, 1), (0, 2), (1, 2), (1, 3)] assert x == x_
def test_digraph_orientation_invalid(self): G = nx.DiGraph(self.edges) edge_iterator = nx.edge_bfs(G, self.nodes, orientation="hello") pytest.raises(nx.NetworkXError, list, edge_iterator)
def test_graph_single_source(self): G = nx.Graph(self.edges) G.add_edge(4, 5) x = list(nx.edge_bfs(G, [0])) x_ = [(0, 1), (0, 2), (1, 2), (1, 3)] assert x == x_
def julia(self): #self.julia2() #print("--------- END JULIA2------------") # *** IDENTIFY STREAMING NODES *** # find all streaming nodes: these are all nodes # explicitly marked OutputType.STREAM or having # at least one input channel with more than one connection multisource = set() for node in self.nodes.values(): runtype = node.identify_runtype() if runtype == OutputType.STREAM: multisource.add(node) # all downstream nodes from streaming nodes are # marked as streaming nodes as well, because they # will be consuming streams! # # TODO: identify nodes that can be packed inside # the streaming node. # for node in multisource: for source, target in nx.edge_bfs(graph.dg, source=node.id): self.nodes[target].produce_type = OutputType.STREAM # *** IDENTIFY ENDPOINTS *** # identify all nodes predecessing each multisource node; # these nodes should output their results to channels. endpoints = set() for node in multisource: for predecessor in self.dg.predecessors(node.id): other = self.nodes[predecessor] if other.get_runtype() == OutputType.SINGLE: endpoints.add(predecessor) print(endpoints) # downstream nodes without output connections are also endpoints for node in self.nodes.values(): if node.get_runtype() == OutputType.SINGLE and self.dg.out_degree( node.id) == 0: endpoints.add(node.id) print(endpoints) # from the set of all identified potential endpoint nodes, # some may be reacheable from other endpoint nodes. Hence, # only those not reacheable from other endpoints should be retained. uniq_endpoints = [] for node in endpoints: if sum(nx.has_path(self.dg, node, other) for other in endpoints) == 1: uniq_endpoints.append(node) endpoints = uniq_endpoints # write code for each upstream path starting # at each endpoint node for endpoint in endpoints: path = [endpoint] for edge in nx.edge_dfs(self.dg, source=endpoint, orientation='reverse'): path.insert(0, edge[0]) print('# ===================================================') print('# ', ' → '.join(path)) print('# ===================================================') for nid in path: node = self.nodes[nid] # scope.update({p.get_name():p.get_varname() for p in node.get_inports('channel') if p.is_valid()}) # scope.update({p.get_name():p.get_varname() for p in node.oports if p.is_valid()}) scope = node.get_config() scope.update(node.get_channels_scope()) scope.update(node.get_callbacks_scope()) scope.update(node.get_components_scope()) # callbacks_scope = node.get_callbacks_scope() # scope.update(callbacks_scope) # components_scope = node.get_components_scope() # scope.update(components_scope) code = node.get_template() print(code.render(**scope)) print('') # Generate taskable functions tasks = [] for node in self.nodes.values(): if node.get_runtype( ) == OutputType.SINGLE or node.type_ != NodeType.MODULE: continue scope = {} inchannels = [] outchannels = [] for port in node.get_inports('channel'): # if this port has a single connection, and it comes # from a serial node, then it is a variable if len(port.connections) == 1: source_node = port.others[0].parent # source,target = port.connections[0].split('-') # source_node = self.ports[source].parent if source_node.produce_type == OutputType.SINGLE: scope[port.get_name()] = Port.format_varname(source) else: scope[port.get_name()] = port.get_name() inchannels.append((port, Port.format_channel(port.id))) else: scope[port.get_name()] = port.get_name() inchannels.append((port, Port.format_channel(port.id))) # scope.update({p.get_name():p.get_varname() for p in node.iports if p.category!='callback' and p.is_valid()}) for port in node.get_outports('channel'): # for target in port.others: # outchannels.append((port, Port.format_channel(target.id))) for connection in port.connections: source, target = connection.split('-') outchannels.append((port, Port.format_channel(target))) scope[port.get_name()] = port.get_name() # code = CODE[node.name]['code'] code = node.get_template() scope.update(node.get_components_scope()) scope.update(node.get_callbacks_scope()) scope['scope'] = scope scope.update(node.get_config()) body = code.render(**scope) #, **node.get_config()) print('# ===================================================') print(f'# {node.name} [ID: {node.id}]') print('# ===================================================') print( jenv.get_template('task.j2').render(node=node, inchannels=inchannels, outchannels=outchannels, body=body)) print('') args = ','.join(c for p, c in inchannels + outchannels) task = f'task_{node.id} = @task {node.get_instance_name()}({args})' tasks.append((f'task_{node.id}', task)) print('') for taskname, task in tasks: print(task) print('') for taskname, task in tasks: print(f'schedule({taskname})') # dump multisource nodes for node in multisource: for port in node.get_inports('channel'): # data fed through ports with a single connection are # also inlined if the connection source is single_sourced if len(port.connections) == 1: continue for connection in port.connections: source, target = connection.split('-') source_node = self.ports[source].parent if source_node.produce_type != OutputType.SINGLE: continue channel = Port.format_channel(port.id) print("put!({channel}, {var})".format( channel=channel, var=Port.format_varname(source)))
num = int(child.strip()[:1]) bag_name = child.strip()[2:] bag.add_child(bag_name, num) B.add_edge(parent, bag_name, weight=num) bags.append(bag) node_name = 'shiny gold' colours = { node for node in nx.bfs_edges(B, node_name, reverse=True) if node != node_name } print(len(colours)) no_of_bags = [node for node in nx.edge_bfs(B, node_name)] bag_map = dict() for a, b, d in B.edges.data(): if ( a, b, ) in no_of_bags: if a in bag_map.keys(): bag_map[a] += [b] * d['weight'] else: bag_map[a] = [b] * d['weight'] bag_list = []
def _edge_bfs_by_depth(G, source_nodes, orientation=None): yield from _group_by_sources(nx.edge_bfs(G, source_nodes, orientation), set(source_nodes))
def get_edge_bfs_dir(g, from_node, direction): return nx.edge_bfs(g, source=from_node, orientation=direction.orientation)
def test_multidigraph(self): G = nx.MultiDiGraph(self.edges) x = list(nx.edge_bfs(G, self.nodes)) x_ = [(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 1, 0), (3, 1, 0)] assert x == x_
def test_empty(self): G = nx.Graph() edges = list(nx.edge_bfs(G)) assert edges == []
def _get_node_predecessors(self, node, graph=None): if graph is None: graph = self.graph edges = reversed([e[:-1] for e in nx.edge_bfs(graph, node, 'reverse')]) return edges
def test_digraph_ignore2(self): G = nx.DiGraph() nx.add_path(G, range(4)) x = list(nx.edge_bfs(G, [0], orientation="ignore")) x_ = [(0, 1, FORWARD), (1, 2, FORWARD), (2, 3, FORWARD)] assert x == x_