예제 #1
0
 def bfs_test_predecessor(self):
     assert (dict(nx.bfs_predecessors(self.G, source=1,
                                      depth_limit=3)) ==
             {0: 1, 2: 1, 3: 2, 4: 3, 7: 2, 8: 7})
     assert (dict(nx.bfs_predecessors(self.D, source=7,
                                      depth_limit=2)) ==
             {2: 7, 3: 2, 8: 7, 9: 8})
예제 #2
0
 def bfs_test_predecessor(self):
     assert_equal(dict(nx.bfs_predecessors(self.G, source=1,
                                           depth_limit=3)),
                  {0: 1, 2: 1, 3: 2, 4: 3, 7: 2, 8: 7})
     assert_equal(dict(nx.bfs_predecessors(self.D, source=7,
                                           depth_limit=2)),
                  {2: 7, 3: 2, 8: 7, 9: 8})
예제 #3
0
def find_lowest_common_ancestor(graph, a, b):
    assert nx.is_directed_acyclic_graph(
        graph), "Graph has to be acyclic and directed."

    preds_1 = nx.bfs_predecessors(graph, a)
    preds_2 = nx.bfs_predecessors(graph, b)

    common_preds = set([n for n in preds_1
                        ]).intersection(set([n for n in preds_2]))

    if len(list(preds_1)) > 0 and len(common_preds) > 0:
        min(common_preds, key=lambda n: preds_1[n])
    else:
        return -1
예제 #4
0
 def test_limited_bfs_predecessor(self):
     assert dict(nx.bfs_predecessors(self.G, source=1, depth_limit=3)) == {
         0: 1,
         2: 1,
         3: 2,
         4: 3,
         7: 2,
         8: 7,
     }
     assert dict(nx.bfs_predecessors(self.D, source=7, depth_limit=2)) == {
         2: 7,
         3: 2,
         8: 7,
         9: 8,
     }
예제 #5
0
def subtree(graph: nx.Graph,
            nodes: Iterable[Node],
            predecessors: Mapping[Node, Node] = None) -> nx.Graph:
    """Extract a subtree with the specified nodes.

    Parameters
    ----------
    graph : nx.Graph
        Full tree graph.
    nodes : Iterable[Node]
        Nodes to include.
    predecessors : Mapping[Node, Node], optional
        Mapping of node to parent node, by default None

    Returns
    -------
    nx.Graph
        A proper tree containing at least the specified nodes.
    """
    if predecessors is None:
        (root, ), nodes = _peek(iter(nodes))
        predecessors = dict(nx.bfs_predecessors(graph, root))

    base = set(nodes)
    parent_nodes = (parents(node, predecessors) for node in base)
    result = set(itertools.chain.from_iterable(parent_nodes))
    result.update(base)
    return graph.subgraph(result)
예제 #6
0
def get_bfs(source):
    edge_local = dict()
    for i in g.edges:
        edge_local[tuple(sorted(i))] = 0
    bfs_output = nx.Graph(nx.bfs_tree(g, source))
    leaf_nodes_list = get_leaf_nodes(bfs_output, source)
    parent_nodes_list = list(nx.bfs_predecessors(bfs_output, source))
    children_nodes_list = list(nx.bfs_successors(bfs_output, source))
    # Initializing the queue with the leaf nodes
    queue = list(leaf_nodes_list)
    while len(queue) > 0:
        current_node = queue.pop(0)
        if current_node in leaf_nodes_list:
            parents = get_parent_nodes(parent_nodes_list, current_node)
            #leaf nodes will have only one parent
            temp = (current_node, parents[0])
            edge_local[tuple(sorted(temp))] = 1
        else:
            parents = get_parent_nodes(parent_nodes_list, current_node)
            children = get_child_nodes(children_nodes_list, current_node)
            forward_val = 1

            for c in children:
                temp = (current_node, c)
                forward_val = forward_val + edge_local[tuple(sorted(temp))]

            if len(parents) > 0:
                temp = (current_node, parents[0])
                edge_local[tuple(sorted(temp))] = forward_val

        if len(parents) > 0 and parents[0] not in queue:
            queue.append(parents[0])

    return edge_local
예제 #7
0
    def find_lca(self, term_a, term_b):
        """
        Determine the lowest common ancestor for a two terms

        :param term_a: HPO term A.
        :param term_b: HPO term B.
        :return: Least Common Ancestor for two terms, "HP:0000001"
        """
        # if either term is HP:0000001 return it
        if any(term == 'HP:0000001' for term in [term_a, term_b]):
            return 'HP:0000001'

        # if one of the terms is a child of the other return the parent
        if self.hpo_network.has_edge(term_a, term_b):
            return term_b
        if self.hpo_network.has_edge(term_b, term_a):
            return term_a

        # find common breadth-first-search predecessors
        parents = []
        for i, term in enumerate([term_a, term_b]):
            parents.append(
                {p[0] for p in nx.bfs_predecessors(self.hpo_network, term)})
            parents[i].add(term)
        common_parents = parents[0].intersection(
            parents[1])
        # lca node
        # find the ancestor with the highest IC
        # break ties by choosing the node with the greatest depth
        return max(common_parents, key=lambda n: (self.hpo_network.nodes[n]['ic'], self.hpo_network.nodes[n]['depth']))
예제 #8
0
파일: extra.py 프로젝트: wlyxpf/STREAM
def bfs_edges_modified(tree, source, preference=None):
    visited, queue = [], [source]
    bfs_tree = nx.bfs_tree(tree, source=source)
    predecessors = dict(nx.bfs_predecessors(bfs_tree, source))
    edges = []
    while queue:
        vertex = queue.pop()
        if vertex not in visited:
            visited.append(vertex)
            if (vertex in predecessors.keys()):
                edges.append((predecessors[vertex], vertex))
            unvisited = set(tree[vertex]) - set(visited)
            if (preference != None):
                weights = list()
                for x in unvisited:
                    successors = dict(nx.bfs_successors(bfs_tree, source=x))
                    successors_nodes = list(
                        itertools.chain.from_iterable(successors.values()))
                    weights.append(
                        min([
                            preference.index(s)
                            if s in preference else len(preference)
                            for s in successors_nodes + [x]
                        ]))
                unvisited = [
                    x for _, x in sorted(zip(weights, unvisited),
                                         reverse=True,
                                         key=lambda x: x[0])
                ]
            queue.extend(unvisited)
    return edges
예제 #9
0
 def test_predecessor(self):
     assert_equal(nx.bfs_predecessors(self.G, source=0), {
         1: 0,
         2: 1,
         3: 1,
         4: 2
     })
예제 #10
0
    def find_lca(self, term_a, term_b):
        """
        Determine the lowest common ancestor for a two terms

        :param term_a: HPO term A.
        :param term_b: HPO term B.
        :return: Least Common Ancestor for two terms, "HP:0000001"
        """
        # if either term is HP:0000001 return it
        if any(term == 'HP:0000001' for term in [term_a, term_b]):
            return 'HP:0000001'

        # if one of the terms is a child of the other return the parent
        if self.hpo_network.has_edge(term_a, term_b):
            return term_b
        if self.hpo_network.has_edge(term_b, term_a):
            return term_a

        # find common breadth-first-search predecessors
        parents = []
        for term in [term_a, term_b]:
            parents.append(
                {p[0]
                 for p in nx.bfs_predecessors(self.hpo_network, term)})
        common_parents = parents[0].intersection(parents[1])
        # lca node
        return max(common_parents,
                   key=lambda n: self.hpo_network.node[n]['depth'])
예제 #11
0
def BFS(root, permissions, graph):
    common = []

    if len(permissions) == 0 or root not in permissions or root in common:
        return common

    common.append(root)
    permissions.remove(root)

    nodes = [
        u for u, _ in nx.bfs_predecessors(graph, source=root, depth_limit=1)
    ]
    neighbors = sortNeighbor(root, nodes, permissions, graph)
    '''
    广度优先筛选符合条件的permissions
    若满足条件,选取为common
    否则, 跳过
    '''
    #print(neighbors)
    for key, values in neighbors:
        if freqPermission(graph.graph['name'], key) < 0.05 or weightProportion(
                root, key, graph) < 0.2:
            break
        elif key in permissions:
            common.append(key)
            permissions.remove(key)

    for node in common[1:]:
        common.extend(BFS(node, permissions, graph))

    return common
예제 #12
0
 def test_predecessor(self):
     assert dict(nx.bfs_predecessors(self.G, source=0)) == {
         1: 0,
         2: 1,
         3: 1,
         4: 2
     }
예제 #13
0
def calculate_pseudotime(adata):
    flat_tree = adata.uns['flat_tree']
    dict_edge_len = nx.get_edge_attributes(flat_tree,'len')
    adata.obs = adata.obs[adata.obs.columns.drop(list(adata.obs.filter(regex='_pseudotime')))].copy()
    # dict_nodes_pseudotime = dict()
    for root_node in flat_tree.nodes():
        df_pseudotime = pd.Series(index=adata.obs.index)
        list_bfs_edges = list(nx.bfs_edges(flat_tree,source=root_node))
        dict_bfs_predecessors = dict(nx.bfs_predecessors(flat_tree,source=root_node))
        for edge in list_bfs_edges:
            list_pre_edges = list()
            pre_node = edge[0]
            while(pre_node in dict_bfs_predecessors.keys()):
                pre_edge = (dict_bfs_predecessors[pre_node],pre_node)
                list_pre_edges.append(pre_edge)
                pre_node = dict_bfs_predecessors[pre_node]
            len_pre_edges = sum([flat_tree.edges[x]['len'] for x in list_pre_edges]) 
            indices = adata.obs[(adata.obs['branch_id'] == edge) | (adata.obs['branch_id'] == (edge[1],edge[0]))].index
            if(edge==flat_tree.edges[edge]['id']):
                df_pseudotime[indices] = len_pre_edges + adata.obs.loc[indices,'branch_lam']
            else:
                df_pseudotime[indices] = len_pre_edges + (flat_tree.edges[edge]['len']-adata.obs.loc[indices,'branch_lam'])
        adata.obs[flat_tree.node[root_node]['label']+'_pseudotime'] = df_pseudotime
        # dict_nodes_pseudotime[root_node] = df_pseudotime
    # nx.set_node_attributes(flat_tree,values=dict_nodes_pseudotime,name='pseudotime')
    # adata.uns['flat_tree'] = flat_tree
    return None
예제 #14
0
def DFS(root, permissions, graph):

    common = []
    common.append(root)

    permissions.remove(root)

    nodes = [
        u for u, _ in nx.bfs_predecessors(graph, source=root, depth_limit=1)
    ]
    #print(nodes)
    neighbors = sortNeighbor(root, nodes, permissions, graph)
    #print(neighbors)
    '''
    判断排序后的第一个是否满足要求,
    若满足,选取该点作为common,
    否则,结束
    '''
    if freqPermission(graph.graph['name'],
                      neighbors[0][0]) < 0.05 or weightProportion(
                          root, neighbors[0][0], graph) < 0.2:
        return common
    elif neighbors[0][0] in permissions:
        common.extend(DFS(neighbors[0][0], permissions, graph))
        return common
    else:
        return common
예제 #15
0
    def evaluate(self, learner):
        prufer, err = self.decode(learner.subjects)
        # print(prufer)
        learner.seq = prufer
        if err:
            learner.fitness = 1e20
            return
        tree = self.decode_tree(
            prufer)  # actually not prufer tho, well whatever :)
        learner.tree = tree
        # cost1 = 0
        # for x1,x2 in combinations(range(self.dim),2):
        # 	route = list(nx.all_simple_paths(tree, source=x1, target=x2))[0]
        # 	route_cost = 0
        # 	for x in range(len(route)-1):
        # 		n1 = route[x]
        # 		n2 = route[x+1]
        # 		route_cost = route_cost + self.distance[n1,n2]
        # 	cost1 = cost1 + route_cost
        # learner.fitness = cost1
        cost = 0
        w = np.ones(self.dim)
        k = list(nx.bfs_predecessors(tree, 0))
        l = dict(k)
        # print(k)
        h = [a for a, b in k][::-1]
        for z in h:
            w[l[z]] = w[l[z]] + w[z]
            cost = cost + w[z] * (self.dim - w[z]) * self.distance[z, l[z]]

        learner.fitness = cost
        # print(len(tree.edges))
        return cost
예제 #16
0
def calculate_shift_distance(adata,root='S0',percentile=95, factor=2.0, preference=None):
    flat_tree = adata.uns['flat_tree']
    dict_label_node = {value: key for key,value in nx.get_node_attributes(flat_tree,'label').items()}  
    root_node = dict_label_node[root]
    ##shift distance for each branch
    dict_edge_shift_dist = dict()
    max_dist = np.percentile(adata.obs['branch_dist'],percentile) ## maximum distance from cells to branch
    leaves = [k for k,v in flat_tree.degree() if v==1]
    n_nonroot_leaves = len(list(set(leaves) - set([root_node])))
    dict_bfs_pre = dict(nx.bfs_predecessors(flat_tree,root_node))
    dict_bfs_suc = dict(nx.bfs_successors(flat_tree,root_node))
    #depth first search
    if(preference != None):
        preference_nodes = [dict_label_node[x] for x in preference]
    else:
        preference_nodes = None
    dfs_nodes = dfs_nodes_modified(flat_tree,root_node,preference=preference_nodes)
    dfs_nodes_copy = deepcopy(dfs_nodes)
    id_leaf = 0
    while(len(dfs_nodes_copy)>1):
        node = dfs_nodes_copy.pop()
        pre_node = dict_bfs_pre[node]
        if(node in leaves):
            dict_edge_shift_dist[(pre_node,node)] = factor*max_dist*(id_leaf-(n_nonroot_leaves/2.0))
            id_leaf = id_leaf+1
        else:
            suc_nodes = dict_bfs_suc[node]
            dict_edge_shift_dist[(pre_node,node)] = (sum([dict_edge_shift_dist[(node,sn)] for sn in suc_nodes]))/float(len(suc_nodes))            
    return dict_edge_shift_dist
예제 #17
0
def get_paths_for_nodes_bfs(g, node_list, sparse=False):
    # make sure there are no duplicates in node_list
    assert len(node_list) == np.unique(node_list).size
    num_nodes = len(node_list)
    if sparse:
        path_mat = scisparse.csr_matrix((num_nodes, num_nodes))
    else:
        path_mat = np.zeros((num_nodes, num_nodes))

    for i in range(num_nodes):
        source = node_list[i]
        pred_map = dict(nx.bfs_predecessors(g, source))
        for j in range(num_nodes):
            # skip self loops
            if i == j:
                continue
            target = node_list[j]
            # if target is unreachable, leave path lenght as zero
            if target not in pred_map:
                continue
            # traverse back from each target towards the source and count the
            # number of steps
            while target != source:
                target = pred_map[target]
                path_mat[i, j] += 1
    return path_mat
예제 #18
0
파일: extract.py 프로젝트: cns-iu/map4sci
def get_layers(graph: nx.Graph,
               fractions: Iterable[float],
               maximum=True,
               weight: WeightSelector = 'weight',
               algorithm='prim') -> Iterator[nx.Graph]:
    """Extracts multiple layers from a graph.

    Parameters
    ----------
    graph
        The base graph.
    fractions
        Percentage of nodes in each layer.
    maximum
        Whether to use a maximum spanning tree.
    weight
        Key for the weight attribute.
    algorithm
        Algorithm used to find spanning tree.

    Yields
    -------
    layers
        Each layer as its own graph.
    """
    nweight, eweight = _weight_selectors(weight)
    tree = graph
    if algorithm != 'null':
        tree = tree_util.spanning_tree(graph, maximum, eweight, algorithm)
    sorted_nodes = tree_util.sorted_nodes(tree, nweight, maximum)
    root = sorted_nodes[0]
    predecessors = dict(nx.bfs_predecessors(tree, root))

    level_counts = dict(
        (level, int(frac * len(tree))) for level, frac in enumerate(fractions))
    level_counts = {
        0: 100,
        1: 300,
        2: 900,
        3: 2700,
        4: 8100,
        5: 24300,
        6: len(sorted_nodes)
    }

    for level in range(len(level_counts)):
        count = level_counts[level]
        nodes = _take_n(iter(sorted_nodes), min(len(sorted_nodes), count))
        subtree = tree_util.subtree(tree, nodes, predecessors)

        for _n, data in subtree.nodes.data():
            if _n in sorted_nodes[:count]:
                data.setdefault('level', level + 1)
        for _u, _v, data in subtree.edges.data():
            data.setdefault('level', level + 1)

        yield subtree
    yield tree
예제 #19
0
    def __init__(self, path, name, tree_graph, root):
                
        '''
        Input: treegraph of the VascGraph.GeomGraph.DiGraph class
        '''
        self.path=path
        self.name=name
        
        # check tree
        if nx.is_tree(tree_graph): pass
        else: 
            print('Cannot wirte non- tree graph!')
            return
        
        
        def fix_indexing(g_old, root):
            
            bfs=list(nx.bfs_predecessors(treegraph, root))
            old_indices=[root]
            old_indices.extend([i[0] for i in bfs])
            
            new_indices=range(len(old_indices))
            mapping={old_indices[i]:new_indices[i] for i in new_indices}
           
            g=DiGraph()
            g.add_nodes_from(new_indices)
            
            for i in old_indices:
                g.node[mapping[i]]['pos']=g_old.node[i]['pos']
                g.node[mapping[i]]['r']=g_old.node[i]['r']
            
            edges_old=g_old.GetEdges()
            edges_new=[[mapping[e[0]], mapping[e[1]]] for e in edges_old]
            
            g.add_edges_from(edges_new)

            return g
            
        # fix indixing to start with 0 at root node
        treegraph=tree_graph.copy()        
        treegraph=fix_indexing(treegraph, root)
        
        # get info
        pos=treegraph.GetNodesPos()
        radii=treegraph.GetRadii()
       
        bfs=dict(nx.bfs_predecessors(treegraph, 0))        
        
        e1=treegraph.GetNodes()
        e2=[]
        
        for i in e1:
            if i==0:
                e2.append(-1)
            else:
                e2.append(bfs[i])                   

        self.__write_graph(e1, e2, pos, radii)
예제 #20
0
 def propogate_score(self, source, score, loaner, already_passed):
     self.modify_score(source, score, 0, loaner)
     bfs = bfs_predecessors(self, source)
     for guy in bfs:
         if guy in already_passed:
             continue
         already_passed.add(guy)
         dist = shortest_path_length(self, source, guy)
         self.modify_score(guy,score,dist, loaner)
예제 #21
0
def llf_route(G,origin,destination,nodes_proj):
    
    import networkx as nx
    
    max_depth = len(nx.dijkstra_path(G,origin,destination,weight = 'length'))
    #print('Max search depth is '+str(max_depth))
    edge_list = list(nx.bfs_predecessors(G, origin, depth_limit=max_depth))
    #node_list = list(nx.bfs_tree(G, origin, depth_limit=max_depth))
    nt_edges = list(nx.bfs_predecessors(G, origin, depth_limit=1))
    #print('Initial edges '+str(nt_edges))
    nt_nodes = list(nx.bfs_tree(G, origin, depth_limit=1))
    for i in range(len(nt_nodes)-1,len(edge_list)):
        if edge_list[i][1] in nt_nodes:
            deflection = abs(bearing(G,edge_list[i][0],edge_list[i][1],nodes_proj) - bearing(G,edge_list[i][1],origin,nodes_proj))
            if deflection < 45.0 :
                nt_nodes.append(edge_list[i][0])
                nt_edges.append(edge_list[i])
    
    if destination in nt_nodes:
        return nx.dijkstra_path(G,origin,destination,weight = 'length')
    
    last_leg = 1000000000.0
    for j in range(0,len(nt_nodes)):
        leg = nx.dijkstra_path_length(G,destination,nt_nodes[j],weight = 'length')       
        if leg < last_leg:
            last_leg = leg
            route_node = nt_nodes[j]    
    #print('Nodes are '+str(nt_nodes))
    #print('Edges are '+str(nt_edges))
    #print('Node with least distance from destination is '+str(route_node))
      
    prev_node = route_node
    route = nx.dijkstra_path(G,destination,route_node,weight = 'length')
    #print('Last route segment '+str(route))
    while prev_node != origin:
        for i in range(0,len(nt_edges)):
            if nt_edges[i][0] == prev_node:
                prev_node = nt_edges[i][1]
                route.append(prev_node)
                #print('Updated route '+str(route[::-1]))
                break
    return route[::-1] #reversing the route
예제 #22
0
    def ShowTree(self, lib='HV'):
        import networkx as nx
        import inspect
        G = self.__G__

        # reverse graph used to calculate depth of node
        H = nx.MultiDiGraph()
        H.add_nodes_from(G)
        H.add_edges_from([(e[1], e[0]) for e in G.edges])
        for n in H.nodes():
            lev = set(
                [val[1] for val in dict(nx.bfs_predecessors(H, n)).items()])
            lev = len(lev)
            G.nodes[n]['depth'] = lev

        h = {i: 0 for i in range(100)}
        for n in G.nodes():
            G.nodes[n]['height'] = h[G.nodes[n]['depth']] * 1.5
            G.nodes[n]['pos'] = (float(G.nodes[n]['depth']),
                                 float(G.nodes[n]['height']))
            h[G.nodes[n]['depth']] = h[G.nodes[n]['depth']] + 1

        if lib.lower() == 'matplotlib':
            import matplotlib.pyplot as plt
            plt.figure(figsize=(6, 6))
            #pos= [key for key in nx.get_node_attributes(G,'pos').keys()] #  nx.spring_layout(G,scale=2)
            pos = nx.get_node_attributes(G,
                                         'pos')  #  nx.spring_layout(G,scale=2)

            color_map = [G.nodes[g]['color'] for g in G.nodes]
            nx.draw(G,
                    pos,
                    node_color=color_map,
                    with_labels=True,
                    node_size=1000,
                    connectionstyle='arc3, rad = 0.1')
        if lib.lower() == 'hv':
            import holoviews as hv
            hv.extension('bokeh')
            graph = hv.Graph.from_networkx(
                G, nx.layout.fruchterman_reingold_layout).opts(
                    width=800,
                    height=400,
                    xaxis=None,
                    yaxis=None,
                    legend_position='top_left',
                    directed=True,
                    node_size=50,
                    inspection_policy='edges',
                    arrowhead_length=0.01,
                    node_color='color')
            labels = hv.Labels(graph.nodes, ['x', 'y'], 'name')
            return graph * labels
예제 #23
0
def straighten_edges(graph):
    """Merges all nodes which only have 1 child with their parent"""
    predecessors = dict(nx.bfs_predecessors(graph, "0"))
    only_single_successor = [(n, *s) for n, s in nx.bfs_successors(graph, "0")
                             if len(s) == 1]
    nodes_to_be_removed = []
    cant_be_removed = {"0"}
    for node, successor in only_single_successor:
        if node not in cant_be_removed:
            predecessor = predecessors[node]
            nodes_to_be_removed.append(node)
            merge_edges(graph, predecessor, node, successor)
            cant_be_removed.add(successor)
    remove_nodes(graph, nodes_to_be_removed)
def main():
    # create graph from problem statement input
    # keeping track of where the solution location should be in the graph
    (original_graph, solution_location) = makeGraph("input.txt")
    #draw our graph 'corr'
    drawVanillaGraph(original_graph,
                     pos=nx.kamada_kawai_layout(original_graph))
    # refurbish graph to better fit our needs
    # our needs are to find if a path exists to 'end' from the starting location
    # making new graph R(efurbished) U(ltimate) G(raph)
    RUG = refurbish(original_graph, solution_location, 0, 1)
    drawRefurbishedGraph(RUG, pos=nx.fruchterman_reingold_layout(RUG))
    # now print a bfs traversal of the refurbished graph
    # bfs predecessors and parents stored in y
    # Rocket starts at 0 (A) and Lucky starts at 1 (B)
    y = list(nx.bfs_predecessors(RUG, State(R=0, L=1)))
    # now traverse bfs tree and output solution (or lack thereof)
    traverse_bfs_tree(y, solution_location)
예제 #25
0
 def dependencies(self, symbol):
     """Find all dependencies of a symbol"""
     for i in range(len(self) - 1, -1, -1):
         if (isinstance(self[i], Assignment) and self[i].symbol == symbol
                 or isinstance(self[i], ODESystem)
                 and symbol in self[i].amounts):
             break
     else:
         raise KeyError(f"Could not find symbol {symbol}")
     g = self._create_dependency_graph()
     symbs = self[i].rhs_symbols
     for j, _ in nx.bfs_predecessors(
             g, i, sort_neighbors=lambda x: reversed(sorted(x))):
         if isinstance(self[j], Assignment):
             symbs -= {self[j].symbol}
         elif isinstance(self[j], ODESystem):
             symbs -= set(self[j].amounts)
         symbs |= self[j].rhs_symbols
     return symbs
예제 #26
0
        def fix_indexing(g_old, root):
            
            bfs=list(nx.bfs_predecessors(treegraph, root))
            old_indices=[root]
            old_indices.extend([i[0] for i in bfs])
            
            new_indices=range(len(old_indices))
            mapping={old_indices[i]:new_indices[i] for i in new_indices}
           
            g=DiGraph()
            g.add_nodes_from(new_indices)
            
            for i in old_indices:
                g.node[mapping[i]]['pos']=g_old.node[i]['pos']
                g.node[mapping[i]]['r']=g_old.node[i]['r']
            
            edges_old=g_old.GetEdges()
            edges_new=[[mapping[e[0]], mapping[e[1]]] for e in edges_old]
            
            g.add_edges_from(edges_new)

            return g
예제 #27
0
파일: main.py 프로젝트: Daiver/jff
def main():
    g = nx.Graph()
    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(3, 1)
    g.add_edge(4, 2)

    print("Start search")
    bfs_res = nx.bfs_predecessors(g, 0)
    for x in bfs_res:
        print(x)
    print("Start search")
    bfs_res = nx.bfs_successors(g, 0)
    for x in bfs_res:
        print(x)
    print("Start search")
    bfs_res = nx.bfs_tree(g, 0, depth_limit=1)
    for x in bfs_res:
        print(x)

    nx.draw(g)
    plt.show()
예제 #28
0
파일: symd.py 프로젝트: xym-tool/symd
def get_dependent_modules():
    print('\n===Dependent Modules===')
    for node_name in G.nodes_iter():
        dependents = nx.bfs_predecessors(G, node_name)
        if len(dependents):
            print(dependents)
def tree_part2(graph, pop_col, pop_target, epsilon,node_repeats):
    
    w=graph.copy()
    for ed in w.edges():
        w.add_edge(ed[0],ed[1],weight=random.random())
    
    T = tree.maximum_spanning_edges(w, algorithm='kruskal', data=False)
    ST= nx.Graph()
    ST.add_edges_from(list(T))
    #nx.draw(ST)
    h=ST.copy()
    
    #nx.draw(ST,layout='tree')
    
    #root = random.choice(list(h.nodes()))
    root = random.choice([x for x in ST.nodes() if ST.degree(x)>1])#this used to be greater than 2 but failed on small grids:(
    #print(root)
    predbfs=nx.bfs_predecessors(h, root)#was dfs
    pred={}
    for ed in predbfs:
        pred[ed[0]]=ed[1]

    pops={x:[{x},graph.nodes[x][pop_col]] for x in graph.nodes()}
    
    leaves=[]
    t=0
    layer=0
    restarts=0
    while 1==1:
        if restarts==node_repeats:
        
        
            w=graph.copy()
            for ed in w.edges():
                w.add_edge(ed[0],ed[1],weight=random.random())
    
            T = tree.maximum_spanning_edges(w, algorithm='kruskal', data=False)
            ST= nx.Graph()
            ST.add_edges_from(list(T))
            #nx.draw(ST)
            h=ST.copy()
    
        #nx.draw(ST,layout='tree')
    
            #root = random.choice(list(h.nodes()))
            root = random.choice([x for x in ST.nodes() if ST.degree(x)>1])#this used to be greater than 2 but failed on small grids:(
            #print(root)
            predbfs=nx.bfs_predecessors(h, root)#was dfs
            pred={}
            for ed in predbfs:
                pred[ed[0]]=ed[1]

            pops={x:[{x},graph.nodes[x][pop_col]] for x in graph.nodes()}
    
            leaves=[]
            t=0
            layer=0
            restarts=0
            #print("Bad tree -- rebuilding")
            
        if len(list(h.nodes()))==1:
            h=ST.copy()
            root = random.choice([x for x in ST.nodes() if ST.degree(x)>1])#this used to be greater than 2 but failed on small grids:(
            #print(root)
            #pred=nx.bfs_predecessors(h, root)#was dfs
            predbfs=nx.bfs_predecessors(h, root)#was dfs
            pred={}
            for ed in predbfs:
                pred[ed[0]]=ed[1]
            pops={x:[{x},graph.nodes[x][pop_col]] for x in graph.nodes()}
            #print("bad root --- restarting",restarts)
            restarts+=1
            layer=0
            leaves=[]
            
        if leaves == []:
        
            leaves = [x for x in h.nodes() if h.degree(x)==1]
            layer=layer+1
            
            if len(leaves) == len(list(h.nodes()))-1:
                tsum = pops[root][1]
                for r in range(2,len(leaves)):
                    for s in itertools.combinations(leaves,r):
                        for node in s:
                            tsum+=pops[node][1]
                    if abs(tsum-pop_target)<epsilon*pop_target:
                        print(pops[leaf][1]/pop_target)
                        clusters={}
                        clusters[1]=list(pops[leaf][0])
                        clusters[-1]=[]
                        for nh in graph.nodes():
                            if nh not in clusters[1]:
                                clusters[-1].append(nh)
                        return clusters

            if root in leaves: #this was in an else before but is still apparently necessary?
                    leaves.remove(root)
                
            #if layer %10==0:
                #print("Layer",layer)
                
            

            
        for leaf in leaves:
            if layer>1 and abs(pops[leaf][1]-pop_target) < pop_target * epsilon:
                #print(pops[leaf][1]/pop_target)
                #ST.remove_edge(leaf,pred[leaf])#One option but slow
                #parts=list(nx.connected_components(h)) #ST here too
                #print(layer, len(parts))

                #part=parts[random.random()<.5]
                clusters={}
                clusters[1]=list(pops[leaf][0])
                clusters[-1]=[]
                for nh in graph.nodes():
                    if nh not in clusters[1]:
                        clusters[-1].append(nh)
                return clusters

            parent = pred[leaf]
            
            pops[parent][1]+=pops[leaf][1]
            pops[parent][0]=pops[parent][0].union(pops[leaf][0])
            #h = nx.contracted_edge(h,(parent,leaf),self_loops = False)#too slow on big graphs
            h.remove_node(leaf)
            leaves.remove(leaf)
            t=t+1
예제 #30
0
def _run_bfswpf(ppc, options, **kwargs):
    """
    SPARSE version of distribution power flow solution according to [1]
    :References:
    [1] Jen-Hao Teng, "A Direct Approach for Distribution System Load Flow Solutions", IEEE Transactions on Power Delivery, vol. 18, no. 3, pp. 882-887, July 2003.

    :param ppc: matpower-style case data
    :return: results (pypower style), success (flag about PF convergence)
    """
    time_start = time()  # starting pf calculation timing

    tap_shift = ppc['branch'][:, SHIFT].real

    enforce_q_lims, tolerance_kva, max_iteration, calculate_voltage_angles, numba = _get_options(
        options)

    numba, makeYbus = _import_numba_extensions_if_flag_is_true(numba)

    ppci = ppc

    baseMVA, bus, gen, branch = \
        ppci["baseMVA"], ppci["bus"], ppci["gen"], ppci["branch"]
    nbus = bus.shape[0]
    # generate results for original bus ordering
    Ybus, Yf, Yt = makeYbus(baseMVA, bus, branch)

    # get bus index lists of each type of bus
    ref, pv, pq = bustypes(bus, gen)

    # creating networkx graph from list of branches
    G = nx.Graph()
    G.add_edges_from((int(fb), int(tb), {
        "shift": float(shift)
    }) for fb, tb, shift in list(
        zip(branch[:, F_BUS].real, branch[:, T_BUS].real, tap_shift)))
    if not nx.is_connected(G):
        Graphs = list(nx.connected_component_subgraphs(G))
    else:
        Graphs = [G]

    V_final = np.zeros(nbus, dtype=complex)
    V_tapshifts = np.zeros(nbus)
    for subi, G in enumerate(Graphs):

        ppci_sub = _cut_ppc(ppci, G.nodes())
        nbus_sub = len(G)

        # depth-first-search bus ordering and generating Direct Load Flow matrix DLF = BCBV * BIBC
        DLF, ppc_bfsw, buses_ordered_bfsw = _bibc_bcbv(ppci_sub, G)
        ppc_bfsw['branch'][:, SHIFT] = 0

        baseMVA_bfsw, bus_bfsw, gen_bfsw, branch_bfsw, ref_bfsw, pv_bfsw, pq_bfsw,\
        on, gbus, V0 = _get_pf_variables_from_ppci(ppc_bfsw)

        Sbus_bfsw = makeSbus(baseMVA_bfsw, bus_bfsw, gen_bfsw)

        Ybus_bfsw, Yf_bfsw, Yt_bfsw = makeYbus(baseMVA_bfsw, bus_bfsw,
                                               branch_bfsw)

        # #-----  run the power flow  -----
        V_res, success = _bfswpf(DLF, bus_bfsw, gen_bfsw, branch_bfsw, baseMVA,
                                 Ybus_bfsw, buses_ordered_bfsw, Sbus_bfsw, V0,
                                 ref_bfsw, pv_bfsw, pq_bfsw, enforce_q_lims,
                                 tolerance_kva, max_iteration, **kwargs)

        V_final[
            buses_ordered_bfsw] = V_res  # return bus voltages in original bus order
        # TODO: find the better way to consider transformer phase shift and remove this workaround
        if calculate_voltage_angles:
            predecessors = nx.bfs_predecessors(G, ref[subi])
            branches = list(zip(branch[:, F_BUS].real, branch[:, T_BUS].real))
            for bus_start in predecessors.iterkeys():
                bus_pred = bus_start
                bus_next = bus_start
                while predecessors.get(bus_next) is not None:
                    bus_next = predecessors.get(bus_pred)
                    shift_angle = G.get_edge_data(bus_pred, bus_next)['shift']
                    if (bus_pred, bus_next) in branches:
                        V_tapshifts[bus_start] += shift_angle
                    else:
                        V_tapshifts[bus_start] -= shift_angle
                    bus_pred = bus_next

            V_final *= np.exp(1j * np.pi / 180 * V_tapshifts)

    # #----- output results to ppc ------
    ppci["et"] = time() - time_start  # pf time end

    bus, gen, branch = pfsoln(baseMVA, bus, gen, branch, Ybus, Yf, Yt, V_final,
                              ref, pv, pq)

    ppci["success"] = success

    ppci["bus"], ppci["gen"], ppci["branch"] = bus, gen, branch

    return ppci, success
예제 #31
0
	def run(self, G):
		s = random.randint(0, G.number_of_nodes())
		networkx.bfs_predecessors(G, s)
예제 #32
0
파일: pdcalc.py 프로젝트: gfon/fuefit
def _all_predecessors(graph, nodes):
    return [k for node in nodes for k in nx.bfs_predecessors(graph, node).keys()]
예제 #33
0
파일: tree.py 프로젝트: maxhully/GerryChain
def predecessors(h, root):
    return {a: b for a, b in nx.bfs_predecessors(h, root)}
예제 #34
0
def get_dependent_modules():
    print('\n===Dependent Modules===')
    for node_name in G.nodes():
        dependents = nx.bfs_predecessors(G, node_name)
        if len(dependents):
            print(dependents)
def bfs(graph, root, max_depth):
    """
    Perform breadth-first search to compute the shortest paths from a root node to all
    other nodes in the graph. To reduce running time, the max_depth parameter ends
    the search after the specified depth.
    E.g., if max_depth=2, only paths of length 2 or less will be considered.
    This means that nodes greather than max_depth distance from the root will not
    appear in the result.

    You may use these two classes to help with this implementation:
      https://docs.python.org/3.5/library/collections.html#collections.defaultdict
      https://docs.python.org/3.5/library/collections.html#collections.deque

    Params:
      graph.......A networkx Graph
      root........The root node in the search graph (a string). We are computing
                  shortest paths from this node to all others.
      max_depth...An integer representing the maximum depth to search.

    Returns:
      node2distances...dict from each node to the length of the shortest path from
                       the root node
      node2num_paths...dict from each node to the number of shortest paths from the
                       root node that pass through this node.
      node2parents.....dict from each node to the list of its parents in the search
                       tree

    In the doctests below, we first try with max_depth=5, then max_depth=2.

    >>> node2distances, node2num_paths, node2parents = bfs(example_graph(), 'E', 5)
    >>> sorted(node2distances.items())
    [('A', 3), ('B', 2), ('C', 3), ('D', 1), ('E', 0), ('F', 1), ('G', 2)]
    >>> sorted(node2num_paths.items())
    [('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 2)]
    >>> sorted((node, sorted(parents)) for node, parents in node2parents.items())
    [('A', ['B']), ('B', ['D']), ('C', ['B']), ('D', ['E']), ('F', ['E']), ('G', ['D', 'F'])]
    >>> node2distances, node2num_paths, node2parents = bfs(example_graph(), 'E', 2)
    >>> sorted(node2distances.items())
    [('B', 2), ('D', 1), ('E', 0), ('F', 1), ('G', 2)]
    >>> sorted(node2num_paths.items())
    [('B', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 2)]
    >>> sorted((node, sorted(parents)) for node, parents in node2parents.items())
    [('B', ['D']), ('D', ['E']), ('F', ['E']), ('G', ['D', 'F'])]
    """
    ###TODO
    node2distances = {}
    node2num_paths = {}
    node2parents = {}

    q = deque()
    q.append(root)
    seen = set()
    res = []
    noOf_shortest_path = []
    count = 0
    while len(q) > 0:
        n = q.popleft()
        if n not in seen:
            res.append(n)
            seen.add(n)
            count += 1
        for nn in graph.neighbors(n):
            if nn not in seen:
                q.append(nn)
    paths = []

    for i in range(len(res)):
        paths.append(nx.single_source_shortest_path(graph, res[i]))
    for i in range(len(paths)):
        for k, v in paths[i].items():
            if k == root:
                node2distances[res[i]] = len(v)

    node2distances = sorted(node2distances.items())
    #print(node2distances)
    n_spaths = dict.fromkeys(graph, 0.0)
    s_paths = nx.all_pairs_shortest_path(graph)
    for source in graph:
        for path in s_paths[source].values():
            for node in path[1:]:  # ignore firs element (source == node)
                n_spaths[node] += 1  # this path passes through `node`
    j = 0
    bak = []
    for i in range(len(res)):
        for k, v in s_paths.items():
            if k == res[i]:
                node2num_paths[res[i]] = len(v)

    node2num_paths = sorted(node2num_paths.items())
    #print(node2num_paths)

    l = []
    for i in range(len(res)):
        pred = nx.bfs_predecessors(graph, res[i])
    for i in range(len(res)):
        for k, v in pred.items():
            if k == res[i]:
                node2parents[res[i]] = v
                l.append(v)

    blah = []
    for k, v in node2parents.items():
        temp = tuple((k, list([v])))
        blah.append(temp)
    node2parents = blah
    #print(node2parents)
    gaandu = bottom_up(root, node2distances, node2num_paths, node2parents)
    return gaandu

    pass
예제 #36
0
 def test_predecessor(self):
     assert_equal(nx.bfs_predecessors(self.G,source=0),
                  {1: 0, 2: 1, 3: 1, 4: 2})