Esempio n. 1
0
 def test_dfs(self):
     for i in range(0, len(self.primary)):
         primary = list(nx.dfs_edges(self.primary[i], source=0))
         followup = list(
             nx.dfs_edges(self.followup[i],
                          source=self.n_node_range[1] + 1))
         self.assertGreaterEqual(len(list(followup)), len(list(primary)))
Esempio n. 2
0
    def _get_relevant_vars(self, g):
        """
        Args
        ----
        g : nx.DiGraph
            A graph of variable dependencies.

        Returns
        -------
        dict
            Dictionary that maps a variable name to all other variables in the
            graph that are relevant to it.
        """
        succs = {}
        for nodes in self.inputs:
            for node in nodes:
                succs[node] = set([v for u, v in nx.dfs_edges(g, node)])
                succs[node].add(node)

        relevant = {}
        grev = g.reverse()
        for nodes in self.outputs:
            for node in nodes:
                relevant[node] = set()
                preds = set([v for u, v in nx.dfs_edges(grev, node)])
                preds.add(node)
                for inps in self.inputs:
                    for inp in inps:
                        common = preds.intersection(succs[inp])
                        relevant[node].update(common)
                        relevant.setdefault(inp, set()).update(common)

        return relevant
Esempio n. 3
0
    def _get_relevant_vars(self, g):
        """
        Args
        ----
        g : nx.DiGraph
            A graph of variable dependencies.

        Returns
        -------
        dict
            Dictionary that maps a variable name to all other variables in the
            graph that are relevant to it.
        """
        succs = {}
        for nodes in self.inputs:
            for node in nodes:
                succs[node] = set([v for u, v in nx.dfs_edges(g, node)])
                succs[node].add(node)

        relevant = {}
        grev = g.reverse()
        for nodes in self.outputs:
            for node in nodes:
                relevant[node] = set()
                preds = set([v for u, v in nx.dfs_edges(grev, node)])
                preds.add(node)
                for inps in self.inputs:
                    for inp in inps:
                        common = preds.intersection(succs[inp])
                        relevant[node].update(common)
                        relevant.setdefault(inp, set()).update(common)

        return relevant
Esempio n. 4
0
    def test_dfs(self):
        """
        Run DFS and verify that the list of edges returned for the
        followup case is not longer than the primary case.

        :return:
        """
        for i in range(0, len(self.primary)):
            # Choose a node that has at least degree one
            src = list(self.primary[i].out_edges)[0][0]
            primary = list(nx.dfs_edges(self.primary[i], source=src))
            followup = list(nx.dfs_edges(self.followup[i], source=src))
            self.assertGreaterEqual(len(list(followup)), len(list(primary)))
Esempio n. 5
0
def belief_propagation(graph, query_node=None):
    """Belief propagation.

    Perform exact inference on tree structured graphs.
    Return the belief of all query_nodes.

    """

    if query_node is None:  # pick random node
        query_node = choice(graph.get_vnodes())

    # Depth First Search to determine edges
    dfs = nx.dfs_edges(graph, query_node)

    # Convert tuple to reversed list
    backward_path = list(dfs)
    forward_path = reversed(backward_path)

    # Messages in forward phase
    for (v, u) in forward_path:  # Edge direction: u -> v
        msg = u.spa(v)
        graph[u][v]['object'].set_message(u, v, msg)

    # Messages in backward phase
    for (u, v) in backward_path:  # Edge direction: u -> v
        msg = u.spa(v)
        graph[u][v]['object'].set_message(u, v, msg)

    # Return marginal distribution
    return query_node.belief()
Esempio n. 6
0
    def root_tree(self, tree: nx.Graph, root_sample: str,
                  remaining_samples: List[str]):
        """Roots a tree produced by UPGMA.

        Adds the root at the top of the UPGMA reconstructed tree. By the
        ultrametric assumption, the root is placed as the parent to the last
        two unjoined nodes.

        Args:
            tree: Networkx object representing the tree topology
            root_sample: Ignored in this case, the root is known in this case
            remaining_samples: The last two unjoined nodes in the tree

        Returns:
            A rooted tree.
        """

        tree.add_node("root")
        tree.add_edges_from([("root", remaining_samples[0]),
                             ("root", remaining_samples[1])])

        rooted_tree = nx.DiGraph()
        for e in nx.dfs_edges(tree, source="root"):
            rooted_tree.add_edge(e[0], e[1])

        return rooted_tree
def linearize(filename):
    graph_name = filename.split('.')[0] + '.graphml'
    g = nx.read_graphml(graph_name)

    print nx.info(g)

    # get first strong connected component

    con = list(nx.strongly_connected_component_subgraphs(g))

    con.sort(key=lambda x: len(x), reverse=True)
    print[len(item) for item in con]

    print nx.info(con[0])

    dfs_edges = list(nx.dfs_edges(con[0]))

    dfs_edges.append((dfs_edges[-1][-1], dfs_edges[0][0]))

    #print dfs_edges

    with open(filename.split('.')[0] + '.linear.edges', 'w') as f:
        for item in dfs_edges:
            f.write(item[0] + ' ' + item[1] + ' ' +
                    str(con[0].edge[item[0]][item[1]]['ew']) + '\n')
Esempio n. 8
0
def monotone_draw(G, root, edge_length):
    """ take tree
  assign unique slope
  use tan-1 for slopes
  if path, may consider same slop
  run DFS
  """

    i = 0  # starting with zero angle

    vertexmanager.setCoordinate(G.node[root], 0.0, 0.0)

    for e in nx.dfs_edges(G, root):
        u, v = e
        slp = math.atan(i)

        x_u, y_u = vertexmanager.getCoordinate(G.node[u])

        x_v = x_u + math.cos(slp)
        y_v = y_u + math.sin(slp)

        vertexmanager.setCoordinate(G.node[v], x_v + edge_length,
                                    y_v + edge_length)

        i = i + 1

    return G
Esempio n. 9
0
def resolve(edges, relevant_nodes=None):
    relevant_nodes_provided = bool(relevant_nodes)
    relevant_nodes = relevant_nodes or []

    graph = nx.DiGraph()
    for e, v in edges:
        graph.add_edge(e, v)
        if not relevant_nodes_provided:
            relevant_nodes.append(e)
            relevant_nodes.append(v)

    condition = BelongsToNodes(relevant_nodes)

    ordered_paths = []
    for node in relevant_nodes:
        dfs_edges = nx.dfs_edges(graph, node)
        path = make_path(dfs_edges, condition)
        ordered_paths.append(path)
    ordered_paths.sort(key=lambda x: len(x))

    order = []
    for path in ordered_paths:
        for path_part in path:
            if path_part not in order:
                order.append(path_part)
    return order
def own_DFS(graph, s, dest=0, paths=False):
    visited = set()

    def dfs(G, source=None):
        if source is None:
            nodes = G
        else:
            nodes = [source]
        for start in nodes:
            if start in visited:
                continue
            visited.add(start)
            stack = [(start, iter(G[start]))]
            while stack:
                parent, children = stack[-1]
                try:
                    child = next(children)
                    if child not in visited:
                        yield parent, child
                        visited.add(child)
                        stack.append((child, iter(G[child])))
                except StopIteration:
                    stack.pop()

    path = []
    edges = list(nx.dfs_edges(graph, s))
    edges.reverse()
    for x in edges:
        if x[1] == dest:
            path.append(dest)
            dest = x[0]
    path.append(s)
    path.reverse()
    return path if paths else dfs(graph, s)
Esempio n. 11
0
def get_dependency_rules(graph, root_node=None,
                         node_attrib='label', edge_attrib='label'):
    """
    Given a graph, returns a set of its dependency rules. If root_node is
    given, returns only those rules from the subgraph rooted at that node.
    A dependency rules is represented by a
    (source node label, edge/relation label, target node label) triple, e.g.
    ('woman', 'dt', 'the').
    
    Returns
    -------
    rules : set of (str, str, str) tuples
        each dependency production rule is represented by a
        (source node label, edge/relation label, target node label)
        tuple
    """
    rules = set()

    if not root_node:
        # root node is the first element in a topological sort of the graph
        root_node = nx.topological_sort(graph)[0]

    for source, target in nx.dfs_edges(graph, root_node):
        rules.add( (ensure_utf8(graph.node[source].get(node_attrib, source)),
                    ensure_utf8(graph[source][target].get(edge_attrib, '')),
                    ensure_utf8(graph.node[target].get(node_attrib, target))) )
    return rules
Esempio n. 12
0
def find_all_paths(graph, start, end):
    # Think about the case where start is also an end
    all_paths = []
    current_path = []

    def append_path(p):
        tot_weight = 0
        for src, tgt in current_path:
            tot_weight += graph[src][tgt].get("weight", 1)
        all_paths.append((path_to_str(current_path), tot_weight))

    for e in nx.dfs_edges(nx.bfs_tree(graph, start)):
        if e[0] == start:  #We start a new path
            if len(current_path):
                # Do we end in an out node ?

                append_path(current_path)
                current_path = []
        if e[1] in end:  # We found a path
            append_path(current_path)

        current_path.append(e)
    if len(current_path):
        append_path(current_path)
    return all_paths
Esempio n. 13
0
def find_dfs_spanning(G):
  """Find edges of the DFS spanning tree for graph G

  This is used to visualize the spanning tree (spanning forest,
  tree cover) in example small graphs.  This function is also used
  to compute DFS intervals, among others in `find_dfs_intervals()`

  Parameters
  ----------
  G : NetworkX graph
      Graph to find spanning-tree in.

  Returns
  -------
  edges : list
      A list of edges in the depth-first-search.

  Examples
  --------
  >>> G = nx.path_graph(5)
  >>> find_dfs_spanning(G)
  [(0, 1), (1, 2), (2, 3), (3, 4)]

  Notes
  -----
  The source is chosen arbitrarily and repeatedly until all
  components in the graph are searched.
  """
  # this may not cover all nodes
  return list(nx.dfs_edges(G))
 def isConnected(self, G):
     isConnected = False
     for node in G.nodes:
         lista = list(nx.dfs_edges(G, source=node))
         if self.isConnectedLocal(node, lista, len(G.nodes)):
             return True
     return False
Esempio n. 15
0
def share_from_user(G, u, fl_dict):
    # compute BFS/DFS-tree with root u
    
#    tree_edges = nx.bfs_edges(G, u)        # use built-in
#    tree_edges = find_bfs_edges(G, u)        # use weighted
    
    tree_edges = nx.dfs_edges(G, u)
    
    
    # (exact) my friend list
    my_list = []
    for v in G.neighbors(u):
        my_list.append((u, v, 1.0))
    fl_dict[u].extend(my_list)

    # init
    list_at_node = [[] for v in G.nodes_iter()]
    
    # propagate
    for (v,w) in tree_edges:
#        print "v,w =", v, w 
        t_level = G.edge[v][w]['t']
        
        if v == u:  # prepare
            a_list = prepare_friend_list(G, u, t_level)
#            print "len(a_list) =", len(a_list)
            list_at_node[w] = a_list
        else:       # forward
            new_list = forward_friend_list(list_at_node[v], t_level)
#            print "len(new_list) =", len(new_list)
            list_at_node[w] = new_list
            
        # add new_list into fl_dict[w]
        fl_dict[w].extend(list_at_node[w])
Esempio n. 16
0
def SST(G):
    g = G.copy()
    for edge in g.edges():
        g.add_edge(edge[0], edge[1], score=0)
    N = 50
    nodes = G.nodes()
    reNodes = []
    flag = {}
    for node in nodes:
        flag[node] = 0
    while N > 0:
        N -= 1
        infected = random.choice(nodes)  # 初始为1个感染节点
        for edge in nx.dfs_edges(G, infected):
            g.add_edge(edge[0],
                       edge[1],
                       score=g.edge[edge[0]][edge[1]]['score'] + 1)
    edgesDict = nx.get_edge_attributes(g, 'score')
    edgeRank = sorted(edgesDict.iteritems(), key=lambda x: x[1], reverse=True)
    for edge in edgeRank:
        if flag[edge[0][0]] == 0:
            flag[edge[0][0]] = 1
            reNodes.add(edge[0][0])
        if flag[edge[0][1]] == 0:
            flag[edge[0][1]] = 1
            reNodes.add(edge[0][1])
    return reNodes
Esempio n. 17
0
 def est_num_matches_powerlaw_graph(self, subg):
     dfs_edges = list(nx.dfs_edges(subg))
     non_dfs_edges = self.set_minus(subg.edges(), dfs_edges)
     gTmp = nx.Graph()
     cur_num_matches = 1
     for e in dfs_edges:  #Compute case I
         if (cur_num_matches == 1):
             cur_num_matches *= (self.sum_of_power[1])
         else:
             node0, node1 = e[0], e[1]
             deg = self.get_degree(gTmp, node0)
             if (deg == 0):
                 deg = self.get_degree(gTmp, node1)
             if (deg == 0):
                 raise (
                     Exception('We do not expect both node has degree 0.'))
             r1 = self.cal_gamma1(deg)
             cur_num_matches *= r1
         gTmp.add_edge(*e)
     for e in non_dfs_edges:  #Compute case II
         node0, node1 = e[0], e[1]
         deg0 = self.get_degree(gTmp, node0)
         deg1 = self.get_degree(gTmp, node1)
         r2 = self.cal_gamma2(deg0, deg1)
         cur_num_matches *= r2
         gTmp.add_edge(*e)
     gTmp.clear()
     return cur_num_matches
Esempio n. 18
0
def join_count_tables(table_info, parsed_join_clauses, count_tables, join_how):
    g = make_join_graph(parsed_join_clauses)
    df_ret = None
    for edge in nx.dfs_edges(g, source=parsed_join_clauses[0][0]):
        t1, t2 = edge
        join_columns = g.edges[edge]["join_columns"]
        cs1 = join_columns[t1]  # columns to join
        cs2 = join_columns[t2]  # columns to join
        if df_ret is None:
            df_ret = count_tables[t1].add_prefix(t1 + ":")
        log.info("Joining {} and {} on {}".format(
            t1, t2, ", ".join([c1 + "=" + c2 for c1, c2 in zip(cs1, cs2)])))
        # NOTE: Since we are traversing the join graph in a DFS order, it is
        # guaranteed that at this point `df_ret` already contains `t1.c1`.
        df_ret = df_ret.merge(
            count_tables[t2].add_prefix(t2 + ":"),
            how=join_how,
            left_on=["{}:{}".format(t1, c) for c in cs1],
            right_on=["{}:{}".format(t2, c) for c in cs2],
        )

    # NOTE: `np.nanprod()` treats `np.nan` as 1.
    df_ret["cnt"] = np.nanprod([df_ret[f"{t}:cnt"] for t in table_info],
                               axis=0)
    return df_ret
Esempio n. 19
0
def downstream_edge_info(wg, n):
    downstream_edges = list(nx.dfs_edges(wg, n))
    down_switch_dict = {}
    e_file = gv.filepaths["edges"]
    for d in downstream_edges:
        # edge info:
        p = d[0].lstrip()
        q = d[1].lstrip()
        edge_of_path_name_1 = p + "_to_" + q
        edge_of_path_name_2 = q + "_to_" + p
        edge_search_result_1 = db._edge_search(edge_of_path_name_1)
        edge_search_result_2 = db._edge_search(edge_of_path_name_2)
        # try:
        with open(e_file, 'r+') as f:
            csvr = csv.reader(f)
            csvr = list(csvr)
            for row in csvr:
                if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                ) == edge_of_path_name_2:
                    if edge_search_result_1 != 0 or edge_search_result_2 != 0:
                        if row[1].lstrip() == "switch":
                            # print("[i] Downstream Switch: {}, Status: {}, Availability: {}".format(row[0], row[4],
                            # row[13]))
                            down_switch_dict[row[0]] = row[4]
                        if row[1].lstrip() == "transformer":
                            pass
                            # print("[i] Downstream Transformer: {}, Status: {}, Availability: {}".format(row[0],
                            # row[4],
                            # row[13]))

        # except:
        #     print("[x] Error in downstream edge search.")

    return down_switch_dict
Esempio n. 20
0
    def _dfs_edges(graph, source, max_steps=None):
        """
        Perform a depth-first search on the given DiGraph, with a limit on maximum steps.

        :param networkx.DiGraph graph:  The graph to traverse.
        :param Any source:              The source to begin traversal.
        :param int max_steps:           Maximum steps of the traversal, or None if not limiting steps.
        :return: An iterator of edges.
        """

        if max_steps is None:
            yield networkx.dfs_edges(graph, source)

        else:
            steps_map = defaultdict(int)
            traversed = {source}
            stack = [source]

            while stack:
                src = stack.pop()
                for dst in graph.successors(src):
                    if dst in traversed:
                        continue
                    traversed.add(dst)

                    dst_steps = max(steps_map[src] + 1, steps_map[dst])

                    if dst_steps > max_steps:
                        continue

                    yield src, dst

                    steps_map[dst] = dst_steps
                    stack.append(dst)
Esempio n. 21
0
def renumber_nodes(G, start=1):
    """Renumber nodes so that NeuronLand converter does not mess up.

    Returns: (new_graph, node_map)

    node_graph: a new DiGraph with the nodes numbered by bfs sequence.

    node_map: dict mapping node number in G to that in node_graph

    """
    ret = nx.DiGraph()
    node_map = {}

    # The nodes are connected as child->parent, we are starting from root,
    # hence reverse
    for ii, (n1, n2) in enumerate(nx.dfs_edges(G, start)):
        m1 = ii+1
        m2 = ii+2
        if n1 in node_map:
            m1 = node_map[n1]
        else:
            node_map[n1] = m1
            ret.add_node(m1)
            ret.node[m1].update(G.node[n1])
        node_map[n2] = m2
        ret.add_edge(m1, m2, length=G[n1][n2])
        ret.node[m2].update(G.node[n2])
        ret.node[m2]['p'] = m1
    ret.node[1]['p'] = -1
    return ret, node_map
Esempio n. 22
0
    def _dfs_edges(graph, source, max_steps=None):
        """
        Perform a depth-first search on the given DiGraph, with a limit on maximum steps.

        :param networkx.DiGraph graph:  The graph to traverse.
        :param Any source:              The source to begin traversal.
        :param int max_steps:           Maximum steps of the traversal, or None if not limiting steps.
        :return: An iterator of edges.
        """

        if max_steps is None:
            yield networkx.dfs_edges(graph, source)

        else:
            steps_map = defaultdict(int)
            traversed = { source }
            stack = [ source ]

            while stack:
                src = stack.pop()
                for dst in graph.successors(src):
                    if dst in traversed:
                        continue
                    traversed.add(dst)

                    dst_steps = max(steps_map[src] + 1, steps_map[dst])

                    if dst_steps > max_steps:
                        continue

                    yield src, dst

                    steps_map[dst] = dst_steps
                    stack.append(dst)
Esempio n. 23
0
	def bfs(self, source):		
		gen =  nx.dfs_edges(self.G, source, 2)
		expansion = []
		res = []
		paths = []

		while True:
			try:
				u, v = gen.next()
				path_from_source = nx.shortest_path(self.G, source, u)
				path_from_target = nx.shortest_path(self.G, source, v)
				likehood_source =  reduce(self.mul, map(self.get_clustering, path_from_source))
				likehood_target =  reduce(self.mul, map(self.get_clustering, path_from_target))
				if likehood_source > self.cut_off:
					if u not in self.term:
						res.append(u)
						paths.append(path_from_source)

				if likehood_source > self.cut_off:
					if v not in self.term:
						res.append(v)
						paths.append(path_from_target)

			except StopIteration as e:
				break
		return res, paths
Esempio n. 24
0
def compute_initial_guess(num_nodes, relative_rotations, relative_edges):
	graph = nx.Graph()
	graph.add_nodes_from(range(num_nodes))

	for (ind, edge) in enumerate(relative_edges):
		(n, theta) = so3.matrix_to_axis_angle(relative_rotations[ind])
		graph.add_edge(edge[0], edge[1], weight=theta, index=ind)

	tree = nx.minimum_spanning_tree(graph)

	global_rotation = []

	for i in range(num_nodes):
		global_rotation.append(numpy.identity(3))

	edges = nx.dfs_edges(tree, 0)

	for edge in edges:
		ind = graph[edge[0]][edge[1]]["index"]
		mat = relative_rotations[ind]

		if relative_edges[ind][0] == edge[0] and relative_edges[ind][1] == edge[1]:
			pass
		elif relative_edges[ind][0] == edge[1] and relative_edges[ind][1] == edge[0]:
			mat = mat.transpose()
		else:
			logging.error("GRAPH ERROR")

		global_rotation[edge[1]] = mat.dot(global_rotation[edge[0]])

	return global_rotation
Esempio n. 25
0
def work_trace(node):
    tracepoints = []
    basic_blocks = []
    syscalls = []
    interactions = []
    datapoints = []
    root = True
    node_ids = reversed([
        node.id,
        *(e[1]
          for e in nx.dfs_edges(context["cached_graph"].reverse(), node.id)),
    ])
    for current_node_id in node_ids:
        current_node = Node(current_node_id)
        if root:
            tracepoints.extend(current_node.tracepoints)
            root = False
        basic_blocks.extend(current_node.basic_blocks)
        syscalls.extend(current_node.syscalls)
        interactions.extend(current_node.interactions)
        datapoints.extend(current_node.datapoints)

    trace = json.dumps({
        "node_id": node.id,
        "tracepoints": tracepoints,
        "basic_blocks": basic_blocks,
        "syscalls": syscalls,
        "interactions": interactions,
        "datapoints": datapoints,
    })
    redis_client.rpush("work.trace", trace)
Esempio n. 26
0
def depths(dg):
    """Return the depths of nodes in a directed
  graph."""
    depths = defaultdict(int)
    for (u, v) in nx.dfs_edges(dg):
        depths[v] = max(depths[v], depths[u] + 1)
    return depths
Esempio n. 27
0
def get_tree_edges(mst, root):
    '''
  iterate over tree edges from the mst rooted at the specified node
  each edge is a tuple (a,b), which implies a -> b
  '''
    for edge in nx.dfs_edges(mst, root):
        yield edge
Esempio n. 28
0
    def _get_relevant_systems(self):
        """
        Given the dict that maps relevant vars to each VOI, find the mapping
        of each VOI to the set of systems that need to run.
        """
        relevant_systems = {}
        grev = self._sgraph.reverse()
        for voi, relvars in iteritems(self.relevant):
            rev = True if voi in self._outset else False
            if rev:
                voicomp = self._prom_to_abs[voi][0].rsplit('.', 1)[0]
                gpath = set([voicomp])
                gpath.update([v for u,v in nx.dfs_edges(grev, voicomp)])
            comps = set()
            for relvar in relvars:
                for absvar in self._prom_to_abs[relvar]:
                    parts = absvar.split('.')
                    for i in range(len(parts)-1):
                        cname = '.'.join(parts[:i+1])
                        # in rev mode, need to eliminate irrelevant systems that have shared promoted vars
                        if rev:
                            if cname in gpath:
                                comps.add(cname)
                        else:
                            comps.add(cname)
            relevant_systems[voi] = tuple(comps)

        return relevant_systems
Esempio n. 29
0
 def remove_symbol_definitions(self, symbols, statement):
     """Remove symbols and dependencies not used elsewhere. statement
     is the statement from which the symbol was removed
     """
     graph = self._create_dependency_graph()
     removed_ind = self.index(statement)
     # Statements defining symbols and dependencies
     candidates = set()
     for s in symbols:
         for i in range(removed_ind - 1, -1, -1):
             stat = self[i]
             if isinstance(stat, Assignment) and stat.symbol == s:
                 candidates.add(i)
                 break
     for i in candidates.copy():
         if i in graph:
             candidates |= set(nx.dfs_preorder_nodes(graph, i))
     # All statements needed for removed_ind
     if removed_ind in graph:
         keep = {down for _, down in nx.dfs_edges(graph, removed_ind)}
     else:
         keep = set()
     candidates -= keep
     # Other dependencies after removed_ind
     additional = {
         down
         for up, down in graph.edges
         if up > removed_ind and down in candidates
     }
     for add in additional.copy():
         if add in graph:
             additional |= set(nx.dfs_preorder_nodes(graph, add))
     remove = candidates - additional
     for i in reversed(sorted(remove)):
         del self[i]
Esempio n. 30
0
def layer_plot(tree, opt_path, buchi):
    """
    plot 3D layer graph
    :param tree: tree built by alg
    :param buchi: buchi state
    :return: none
    """
    path = list(nx.dfs_edges(tree))

    # 2D workspace in 3D
    fig = plt.figure(2)
    plt.rc('text', usetex=True)
    plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
    ax = fig.add_subplot(111, projection='3d')
    for i in range(len(path)):
        x_pre = np.array([path[i][0][0][0], path[i][1][0][0]])
        y_pre = np.array([path[i][0][0][1], path[i][1][0][1]])
        z_pre = np.asarray([buchi[path[i][0][1]], buchi[path[i][1][1]]])
        ax.plot(x_pre, y_pre, z_pre, 'b--d', markerfacecolor='None')

    x_pre = np.asarray([point[0][0] for point in opt_path[0]])
    y_pre = np.asarray([point[0][1] for point in opt_path[0]])
    z_pre = np.asarray([buchi[point[1]] for point in opt_path[0]])
    ax.plot(x_pre, y_pre, z_pre, 'r--d')
    # region_plot(regions, 'region', ax)
    # region_plot(obs, 'obs', ax)
    plt.savefig('3D.png', bbox_inches='tight', dpi=600)
Esempio n. 31
0
    def score_parsimony(self, cm=None):
        """
		Score the parsimony of the tree.
	
		:param cm:
			Character matrix, if the Cassiopeia_Tree object does not already have one stored.

		:return:
			An integer representing the number of mutations in the tree.
		"""

        if cm is not None:
            self.cm = cm

        assert self.cm is not None

        net = self.get_network().copy()
        copy_dict = {}
        for n in net:
            copy_dict[n] = copy.copy(n)

        net = nx.relabel_nodes(net, copy_dict)

        #net = fill_in_tree(net, cm)
        #net = tree_collapse(net)

        root = [n for n in net if net.in_degree(n) == 0][0]

        score = 0
        for e in nx.dfs_edges(net, source=root):
            score += e[0].get_mut_length(e[1])

        return score
    def _recursive_train_local_classifiers(self, X, y, node_id, progress):
        if self.graph_.node[node_id].get("classifier", None):
            # Already encountered this node, skip
            return

        progress.update(1)
        self._train_local_classifier(X, y, node_id)

        for child_node_id in self.graph_.successors(node_id):
            out_degree = self.graph_.out_degree(child_node_id)
            if not out_degree:
                # Terminal node, skip
                progress.update(1)
                continue

            if out_degree < 2:
                # If node has less than 2 children, no point training a local classifier
                self.logger.warning(
                    "*** Not enough children to train classifier for node %s, skipping (%s < 2)",
                    child_node_id,
                    self.graph_.out_degree(child_node_id),
                )
                # For tracking progress, count this node as well as all of its descendants
                progress.update(
                    1 + len(list(dfs_edges(self.graph_, child_node_id))))
                continue

            self._recursive_train_local_classifiers(
                X=X,
                y=y,
                node_id=child_node_id,
                progress=progress,
            )
Esempio n. 33
0
    def match_nodes_to_blocks(self, meta_molecule):
        """
        This function matches the nodes in the meta_molecule
        to the blocks in the force-field. It does the essential
        bookkeeping for two cases and populates the node_to_block,
        and node_to_fragment dicts as well as the fragments attribute.
        It distinguishes two cases:

        1) the node corresponds to a single residue block; here
           node_to_block entry is simply the resname of the block
        2) the node has the from_itp attribute; in this case
           the node is part of a multiresidue block in the FF,
           all nodes corresponding to that block form a fragment.
           All fragments are added to the fragments attribute, and
           the nodes in those fragments all have the entry node_to_block
           set to the block. In addition it is recorded to which fragment
           specifically the node belongs in the node_to_fragment dict.

        Parameters
        ----------
        meta_molecule: polyply.src.meta_molecule.MetaMolecule
            The meta molecule to process.

        """
        regular_graph = nx.Graph()
        restart_graph = nx.Graph()
        restart_attr = nx.get_node_attributes(meta_molecule, "from_itp")

        # in case we only have a single residue
        # we assume the user is sane and that residue is not from
        # an itp file
        if len(meta_molecule.nodes) == 1:
            regular_graph.add_nodes_from(meta_molecule.nodes)

        # this breaks down when to proteins are directly linked
        # because they would appear as one connected component
        # and not two seperate components referring to two molecules
        # but that is an edge-case we can worry about later
        for idx, jdx in nx.dfs_edges(meta_molecule):
            # the two nodes are restart nodes
            if idx in restart_attr and jdx in restart_attr:
                restart_graph.add_edge(idx, jdx)
            else:
                regular_graph.add_edge(idx, jdx)

        # regular nodes have to match a block in the force-field by resname
        for node in regular_graph.nodes:
            self.node_to_block[node] = meta_molecule.nodes[node]["resname"]

        # fragment nodes match parts of blocks, which describe molecules
        # with more than one residue
        for fragment in nx.connected_components(restart_graph):
            block_name = restart_attr[list(fragment)[0]]
            if all([restart_attr[node] == block_name  for node in fragment]):
                self.fragments.append(fragment)
                for node in fragment:
                    self.node_to_block[node] = block_name
                    self.node_to_fragment[node] = len(self.fragments) - 1
            else:
                raise IOError
Esempio n. 34
0
 def dfs_path(self, node, roadcode=None):
     if node not in self:
         raise nx.NetworkXError('source node %s not in graph' % node)
     path = [node]
     prev_node = node
     for child, prant in dfs_edges_reverse(self, node):
         if prev_node == child:
             path.append(prant)
             prev_node = prant
         else:
             raise nx.NetworkXError('Exist branches in the HWY main road.')
     path.reverse()
     prev_node = node
     if path:
         first_node = path[0]
     else:
         first_node = None
     for prant, child in nx.dfs_edges(self, node):
         if prev_node == prant:
             # Circle
             if first_node == child:
                 path.insert(0, prant)
                 break
             path.append(child)
             prev_node = child
         else:
             raise nx.NetworkXError('Exist branches in the HWY main road.')
     return path
Esempio n. 35
0
def resiliency(analysis='nodal'):
    """
    This function helps compute the resiliency metric of the network of a
    node, or a network
    :param kwargs:
    :return:
    """

    # 0. what's the event?

    event = gv.event

    # 1. get the graphs

    g1 = gv.graph_collection[0]
    g2 = gv.graph_collection[1]
    nodes = g2.nodes()
    edges = g2.edges()

    # 2. for which node and edge are you doing the analysis?
    wg = nx.DiGraph()  # wg: working graph
    wg.add_edges_from(edges)

    for n in nodes:
        # Finding Downstream Edges
        print("Downstream Edges of %s -->" % n)
        downstream_edges = list(nx.dfs_edges(wg, n))
        resiliency_downstream(g2, downstream_edges, event)

        # Finding Upstream Edges
        print("Upstream Edges of %s -->" % n)
        upstream_edges = list(nx.edge_dfs(wg, n, orientation='reverse'))
        resiliency_upstream(g2, upstream_edges, event)
Esempio n. 36
0
 def run(self):
     done = []
     for task in nx.dfs_edges(self.execution_graph):
         if not task[0] in done:
             self.__execute__(task[0].label, task[0].executable, task[0].parameters)
         done.append(task[0])
         if not task[1] in done:
             self.__execute__(task[1].label, task[1].executable, task[1].parameters)
         done.append(task[1])
Esempio n. 37
0
def transitive_reduction(G): 
    TR = nx.DiGraph()
    TR.add_nodes_from(G.nodes())
    for u in G:
        u_edges = set(G[u])
        for v in G[u]:
            u_edges -= {y for x, y in nx.dfs_edges(G, v)}
        TR.add_edges_from((u,v) for v in u_edges)
    return TR    
Esempio n. 38
0
    def _check_graph(self, out_stream=sys.stdout):
        # Cycles in group w/o solver
        cgraph = self.root._relevance._cgraph
        for grp in self.root.subgroups(recurse=True, include_self=True):
            path = [] if not grp.pathname else grp.pathname.split('.')
            graph = cgraph.subgraph([n for n in cgraph if n.startswith(grp.pathname)])
            renames = {}
            for node in graph.nodes_iter():
                renames[node] = '.'.join(node.split('.')[:len(path)+1])
                if renames[node] == node:
                    del renames[node]

            # get the graph of direct children of current group
            nx.relabel_nodes(graph, renames, copy=False)

            # remove self loops created by renaming
            graph.remove_edges_from([(u,v) for u,v in graph.edges()
                                         if u==v])

            strong = [s for s in nx.strongly_connected_components(graph)
                        if len(s)>1]

            if strong and isinstance(grp.nl_solver, RunOnce): # no solver, cycles BAD
                relstrong = []
                for slist in strong:
                    relstrong.append([])
                    for s in slist:
                        relstrong[-1].append(name_relative_to(grp.pathname, s))
                        relstrong[-1] = sorted(relstrong[-1])
                print("Group '%s' has the following cycles: %s" %
                     (grp.pathname, relstrong), file=out_stream)

            # Components/Systems/Groups are not in the right execution order
            subnames = [s.pathname for s in grp.subsystems()]
            while strong:
                # break cycles to check order
                lsys = [s for s in subnames if s in strong[0]]
                for p in graph.predecessors(lsys[0]):
                    if p in lsys:
                        graph.remove_edge(p, lsys[0])
                strong = [s for s in nx.strongly_connected_components(graph)
                            if len(s)>1]

            visited = set()
            out_of_order = set()
            for sub in grp.subsystems():
                visited.add(sub.pathname)
                for u,v in nx.dfs_edges(graph, sub.pathname):
                    if v in visited:
                        out_of_order.add(v)

            if out_of_order:
                print("In group '%s', the following subsystems are out-of-order: %s" %
                      (grp.pathname, sorted([name_relative_to(grp.pathname, n)
                                                for n in out_of_order])), file=out_stream)
Esempio n. 39
0
    def _get_relevant_vars(self, g):
        """
        Args
        ----
        g : nx.DiGraph
            A graph of variable dependencies.

        Returns
        -------
        dict
            Dictionary that maps a variable name to all other variables in the
            graph that are relevant to it.
        """

        relevant = {}
        succs = {}

        for nodes in self.inputs:
            for node in nodes:
                relevant[node] = set()
                succs[node] = set((node,))
                if node in g:
                    succs[node].update(v for u, v in nx.dfs_edges(g, node))

        grev = g.reverse()
        self._outset = set()
        for nodes in self.outputs:
            self._outset.update(nodes)
            for node in nodes:
                relevant[node] = set()
                if node in g:
                    preds = set(v for u, v in nx.dfs_edges(grev, node))
                    preds.add(node)
                    for inps in self.inputs:
                        for inp in inps:
                            if inp in g:
                                common = preds.intersection(succs[inp])
                                relevant[node].update(common)
                                relevant[inp].update(common)

        return relevant
Esempio n. 40
0
def normalize_step_weight(graph):
    "Changes the edge weights in the graph proportional to the longest path."
    longest_path_len = max(nx.shortest_path_length(graph, "ROOT").values())
    # add normalized path length as weight to edges.
    for category in "ABCEDFGHJKLMNPQRSTUVWXZ":
        # for each category, find out how long the longest path is.
        cat_longest_path_len = max(nx.shortest_path_length(graph, category).values()) + 1
        # normalize the stepsize
        stepsize = float(longest_path_len) / cat_longest_path_len
        # traverse tree for this category and assign stepsize to edges as weight attribute
        for a, b in nx.dfs_edges(graph, category):
            graph[a][b]["weight"] = stepsize
Esempio n. 41
0
def depth(dg):
    """Returns the depth of a directed graph.
  A single node has depth 0."""
    depths = defaultdict(int)
    for (u, v) in nx.dfs_edges(dg):
        depths[v] = max(depths[v], depths[u] + 1)
    if len(depths) > 0:
        return max(depths.values())
    elif len(dg.nodes()) == 0:  # No nodes in graph
        return None
    else:  # Nodes but no edges in graph
        return 0
Esempio n. 42
0
def hierarchy(G, edge_type=EdgeTypes.parent):
    """Produce child nodes in a depth-first order."""
    for root in toplevel(G):
        dq = deque()
        yield G.node[root], len(dq)  # Depth 0
        # Iterate over all children
        dq.append(root)
        for src, dest in nx.dfs_edges(G, source=root):
            while len(dq) > 0 and dq[-1] != src:
                dq.pop()  # Unwind the stack
            if G.edge[src][dest].get('type') == edge_type:
                yield G.node[dest], len(dq)
            dq.append(dest)
Esempio n. 43
0
 def findControllingNode(self, paramName):
     current, parent = self.currentContextName, self.currentContext.parentContext
     var = self.currentContext.parameters[paramName]
     child = None
     if not (parent and var.useParentValue):
         return None
     for child, parent in dfs_edges(self.dependencyGraph, parent):
         try:
             var = self.contextDict[parent].parameters[paramName]
             if not (var.useParentValue and var.hasParent):
                 return child
         except KeyError:
             return child
     return parent
Esempio n. 44
0
def cart_to_internal(molecule):
    """ Converts the coordinates of the molecule from cartesian to internal
    coordinates. """
    # First decide on a starting atom
    source_node = molecule.atoms.nodes()[0]
    # Argh, dictionaries aren't sorted...
    completed_bonds = {source_node: []}
    for edge in nx.dfs_edges(molecule.atoms, source_node):
        completed_bonds[edge[1]] = [edge[0]]
    # completed_bonds[2] += [x for x in completed_bonds[1][1:] if x not in completed_bonds[2]]
    # completed_bonds[3] += [x for x in completed_bonds[2][1:] if x not in completed_bonds[3]]

    for entry in completed_bonds:
        print(entry, completed_bonds[entry])
Esempio n. 45
0
	def get_bad_dest(self, b):
		# pick a random node near b
		count = 0
		ret = None
		for edge in nx.dfs_edges(self.graph, b):
			if count == 1000:
				break
			node = edge[1]
			count += 1
			if count == 1:
				ret = node
			i = randint(0, count - 1)
			if i == count - 1:
				ret = node
		return node
Esempio n. 46
0
def transitive_reduction(G):
    """ Returns transitive reduction of a directed graph

    The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
    for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
    in E and there is no path from v to w in G with length greater than 1.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    Returns
    -------
    NetworkX DiGraph
        The transitive reduction of `G`

    Raises
    ------
    NetworkXError
        If `G` is not a directed acyclic graph (DAG) transitive reduction is
        not uniquely defined and a :exc:`NetworkXError` exception is raised.

    References
    ----------
    https://en.wikipedia.org/wiki/Transitive_reduction

    """
    if not is_directed_acyclic_graph(G):
        msg = "Directed Acyclic Graph required for transitive_reduction"
        raise nx.NetworkXError(msg)
    TR = nx.DiGraph()
    TR.add_nodes_from(G.nodes())
    descendants = {}
    # count before removing set stored in descendants
    check_count = dict(G.in_degree)
    for u in G:
        u_nbrs = set(G[u])
        for v in G[u]:
            if v in u_nbrs:
                if v not in descendants:
                    descendants[v] = {y for x, y in nx.dfs_edges(G, v)}
                u_nbrs -= descendants[v]
            check_count[v] -= 1
            if check_count[v] == 0:
                del descendants[v]
        TR.add_edges_from((u, v) for v in u_nbrs)
    return TR
Esempio n. 47
0
def cluster(g):
    clustered = []
    clusters = {}
    i = 1
    for n in g.nodes():
        c = [n]
        if n in clustered: continue
        edges = list(nx.dfs_edges(g,n))
        for e in edges:
            n1,n2 = e
            clustered+=[n1,n2]
            c+=[n1,n2]
        c = list(set(c))
        clusters[i] = {'num': len(c), 'fams': c[:]}
        i+=1
    return clusters
Esempio n. 48
0
 def select(self, k=2, rand=False):
     if rand:
         nodes = []
         while len(nodes) != k:
             n = rd.randint(1, self.number_of_nodes())
             if not n in nodes:
                 nodes.append(n)
     else:
         w = int(self.number_of_nodes()/(k + 1))
         edges = nx.dfs_edges(self, 1)
         nodes = [1]
         for n in range(1, k):
             for e in range(w):
                 edge = edges.next()
             nodes.append(edge[1])
     return nodes
Esempio n. 49
0
def dfs_custom_tree(G, source=None):
    T = nx.Graph()
    if source is None:
        T.add_nodes_from(G)
    else: 
        T.add_node(source)
    #print "All those edges"
    #for e in nx.dfs_edges(G,source):
    #    print e
    T.add_edges_from(nx.dfs_edges(G,source))
    #FIXME assign weights back to the graph T
    for u,v,g in G.edges(data=True):
        try:
            T[u][v]['weight'] = G[u][v]['weight']
        except: continue
    return T
def SentimentAnalysis_RGO_Belief_Propagation(nxg):
	#Bayesian Pearl Belief Propagation is done by
	#assuming the senti scores as probabilities with positive
	#and negative signs and the Recursive Gloss Overlap
	#definition graph being the graphical model.
	#Sentiment as a belief potential is passed through 
	#the DFS tree of this graph.  
	dfs_positive_belief_propagated=1.0
	core_positive_belief_propagated=1.0
	dfs_negative_belief_propagated=1.0
	core_negative_belief_propagated=1.0
	core_xnegscore=core_xposscore=1.0
	dfs_knegscore=dfs_kposscore=dfs_vposscore=dfs_vnegscore=1.0
	sorted_core_nxg=sorted(nx.core_number(nxg).items(),key=operator.itemgetter(1), reverse=True)
	kcore_nxg=nx.k_core(nxg,6,nx.core_number(nxg))
	for x in sorted_core_nxg:
	      xsset = swn.senti_synsets(x[0])
	      if len(xsset) > 2:
	     		core_xnegscore = float(xsset[0].neg_score())*10.0
	      		core_xposscore = float(xsset[0].pos_score())*10.0
	      if core_xnegscore == 0.0:
			core_xnegscore = 1.0
	      if core_xposscore == 0.0:
			core_xposscore = 1.0
	      core_positive_belief_propagated *= float(core_xposscore)
	      core_negative_belief_propagated *= float(core_xnegscore)
	print "Core Number: RGO_sentiment_analysis_belief_propagation: %f, %f" % (float(core_positive_belief_propagated), float(core_negative_belief_propagated))
	#for k,v in nx.dfs_edges(nxg):
	for k,v in nx.dfs_edges(kcore_nxg):
	      ksynset = swn.senti_synsets(k)
	      vsynset = swn.senti_synsets(v)
	      if len(ksynset) > 2:
	     		dfs_knegscore = float(ksynset[0].neg_score())*10.0
	      		dfs_kposscore = float(ksynset[0].pos_score())*10.0
	      if len(vsynset) > 2:
			dfs_vnegscore = float(vsynset[0].neg_score())*10.0
			dfs_vposscore = float(vsynset[0].pos_score())*10.0
	      dfs_kposscore_vposscore = float(dfs_kposscore*dfs_vposscore)
	      dfs_knegscore_vnegscore = float(dfs_knegscore*dfs_vnegscore)
	      if dfs_kposscore_vposscore == 0.0:
		dfs_kposscore_vposscore = 1.0
	      if dfs_knegscore_vnegscore == 0.0:
		dfs_knegscore_vnegscore = 1.0
	      dfs_positive_belief_propagated *= float(dfs_kposscore_vposscore)
	      dfs_negative_belief_propagated *= float(dfs_knegscore_vnegscore)
	print "K-Core DFS: RGO_sentiment_analysis_belief_propagation: %f, %f" % (float(dfs_positive_belief_propagated),float(dfs_negative_belief_propagated))
	return (dfs_positive_belief_propagated, dfs_negative_belief_propagated, core_positive_belief_propagated, core_negative_belief_propagated)
Esempio n. 51
0
def get_tree_schedule(frcs, graph):
    """
    Find the most constrained tree in the graph and returns which messages to compute
    it.  This is the minimum spanning tree of the perturb_radius edge attribute.

    See forward_pass for parameters.

    Returns
    -------
    tree_schedules : numpy.ndarray of numpy.int
        Describes how to compute the max marginal for the most constrained tree.
        Nx3 2D array of (source pool_idx, target pool_idx, perturb radius), where
        each row represents a single outgoing factor message computation.
    """
    min_tree = nx.minimum_spanning_tree(graph, 'perturb_radius')
    return np.array([(target, source, graph.edge[source][target]['perturb_radius'])
                     for source, target in nx.dfs_edges(min_tree)])[::-1]
Esempio n. 52
0
def test_max_steps():

    binary_path = os.path.join(test_location, "x86_64", "fauxware")
    b = angr.Project(binary_path, load_options={'auto_load_libs': False})
    cfg = b.analyses.CFGAccurate(max_steps=5)

    dfs_edges = networkx.dfs_edges(cfg.graph)

    depth_map = {}
    for src, dst in dfs_edges:
        if src not in depth_map:
            depth_map[src] = 0
        if dst not in depth_map:
            depth_map[dst] = depth_map[src] + 1
        depth_map[dst] = max(depth_map[src] + 1, depth_map[dst])

    nose.tools.assert_less_equal(max(depth_map.itervalues()), 5)
Esempio n. 53
0
    def test_dag_execution(self):
        # build a DAG
        g = nx.Graph()
        t1 = Task("hello")
        t2 = Task("world")
        t3 = Task("!")
        g.add_edge(t1, t2)
        g.add_edge(t2, t3)

        # Arrange
        runner = Runner(g)
        with mock.patch('job_manager.runner.Runner.__execute__') as me:
            for edge in nx.dfs_edges(g):
                logging.warning("left: %s, right: %s", edge[0].label, edge[1].label)
            # Act
            runner.run()
            # Assert
            me.assert_any_call(t1.label, t1.executable, t1.parameters)
            me.assert_any_call(t2.label, t2.executable, t2.parameters)
            me.assert_any_call(t3.label, t3.executable, t3.parameters)
Esempio n. 54
0
def max_product(graph, query_node=None):
    """Max-product algorithm.

    Compute setting of variables with maximum probability on graphs
    that are tree structured.
    Return the setting of all query_nodes.

    """
    track = {}  # Setting of variables

    if query_node is None:  # pick random node
        query_node = choice(graph.get_vnodes())

    # Depth First Search to determine edges
    dfs = nx.dfs_edges(graph, query_node)

    # Convert tuple to reversed list
    backward_path = list(dfs)
    forward_path = reversed(backward_path)

    # Messages in forward phase
    for (v, u) in forward_path:  # Edge direction: u -> v
        msg = u.mpa(v)
        graph[u][v]['object'].set_message(u, v, msg)

    # Messages in backward phase
    for (u, v) in backward_path:  # Edge direction: u -> v
        msg = u.mpa(v)
        graph[u][v]['object'].set_message(u, v, msg)

    # Maximum argument for query node
    track[query_node] = query_node.argmax()

    # Back-tracking
    for (u, v) in backward_path:  # Edge direction: u -> v
        if v.type == nodes.NodeType.factor_node:
            for k in v.record[u].keys():  # Iterate over outgoing edges
                track[k] = v.record[u][k]

    # Return maximum probability for query node and setting of variable
    return query_node.maximum(), track
Esempio n. 55
0
def transitive_reduction(G):
    """ Returns transitive reduction of a directed graph

    The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
    for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
    in E and there is no path from v to w in G with length greater than 1.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    Returns
    -------
    NetworkX DiGraph
        The transitive reduction of `G`

    Raises
    ------
    NetworkXError
        If `G` is not a directed acyclic graph (DAG) transitive reduction is
        not uniquely defined and a :exc:`NetworkXError` exception is raised.

    References
    ----------
    https://en.wikipedia.org/wiki/Transitive_reduction

    """
    if not is_directed_acyclic_graph(G):
        raise nx.NetworkXError(
            "Transitive reduction only uniquely defined on directed acyclic graphs.")
    TR = nx.DiGraph()
    TR.add_nodes_from(G.nodes())
    for u in G:
        u_edges = set(G[u])
        for v in G[u]:
            u_edges -= {y for x, y in nx.dfs_edges(G, v)}
        TR.add_edges_from((u, v) for v in u_edges)
    return TR
Esempio n. 56
0
def dfs_edges_by_label(G, source=None, my_label=None):
    """Produce edges in a depth-first-search starting at source, 
    only following a given label.

    my_label : this is a dictionary, specifying an edge label condition to traverse by.

    For example, if edges have been added such as, g.add_edge(a, b, relationship='child')
    , then use my_label= {'relationship': 'child'} , as a condition for traversal.
    """
    if not my_label or my_label == {}:
        yield nx.dfs_edges(G, source=None)
    if type(my_label) != type({}):
        raise SyntaxError, "my_label should be a dictionary"
    label_, value_ = my_label.items()[0]
    if source is None:
        # produce edges for all components
        nodes = G
    else:
        # produce edges for components with source
        nodes = [source]
    visited = set()
    for start in nodes:
        if start in visited:
            continue
        visited.add(start)
        stack = [(start, iter(G[start]))]
        while stack:
            parent, children = stack[-1]
            try:
                child = next(children)
                if G[parent][child][label_] != value_:
                    continue
                if child not in visited:
                    yield parent, child
                    visited.add(child)
                    stack.append((child, iter(G[child])))
            except StopIteration:
                stack.pop()
Esempio n. 57
0
def strategy_connected_sequential(G, colors, traversal="bfs"):
    """
    Connected sequential ordering (CS). Yield nodes in such an order, that
    each node, except the first one, has at least one neighbour in the
    preceeding sequence. The sequence can be generated using both BFS and
    DFS search (using the strategy_connected_sequential_bfs and
    strategy_connected_sequential_dfs method). The default is bfs.
    """
    for component_graph in nx.connected_component_subgraphs(G):
        source = component_graph.nodes()[0]

        yield source  # Pick the first node as source

        if traversal == "bfs":
            tree = nx.bfs_edges(component_graph, source)
        elif traversal == "dfs":
            tree = nx.dfs_edges(component_graph, source)
        else:
            raise nx.NetworkXError("Please specify bfs or dfs for connected sequential ordering")

        for (_, end) in tree:
            # Then yield nodes in the order traversed by either BFS or DFS
            yield end