当前位置: 首页>>代码示例>>Python>>正文


Python networkx.has_path方法代码示例

本文整理汇总了Python中networkx.has_path方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.has_path方法的具体用法?Python networkx.has_path怎么用?Python networkx.has_path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.has_path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: verify

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def verify(prog, src_name, dst_name):
    src = prog.subs.find(src_name)
    dst = prog.subs.find(dst_name)
    if src is None or dst is None:
        return None

    graphs = GraphsBuilder()
    graphs.run(prog)
    cg = graphs.callgraph

    if nx.has_path(cg, src.id.number, dst.id.number):
        return ('calls', nx.shortest_path(cg, src.id.number, dst.id.number))

    calls = CallsitesCollector(graphs.callgraph, src.id.number, dst.id.number)

    for sub in prog.subs:
        calls.run(sub)
        cfg = graphs.callgraph.nodes[sub.id.number]['cfg']
        for src in calls.srcs:
            for dst in calls.dsts:
                if src != dst and nx.has_path(cfg, src, dst):
                    return ('sites', nx.shortest_path(cfg, src, dst))
        calls.clear()

    return None 
开发者ID:BinaryAnalysisPlatform,项目名称:bap-tutorial,代码行数:27,代码来源:path_check.py

示例2: create_path_complete_condition

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def create_path_complete_condition(transmit_node_pairs):
    """
    This factory allows us to specify that there are valid directed paths between pairs of nodes.
    This returns a function that takes an graph argument (G) 
    and verifies that for the list of node pairs the graph meets those dependency conditions. 
    
    NB: This is useful for making known indirect dependencies explicit.
    
    Variables:
    node_list is a list of 2-tuples of nodes that will have valid direct paths 
    from the first of the nodes to the second.
    """

    def path_complete_condition(G):
        return all([nx.has_path(G,x,y) for x,y in transmit_node_pairs])
    return path_complete_condition 
开发者ID:mpacer,项目名称:Causal-Bayesian-NetworkX,代码行数:18,代码来源:scipy2015_cbnx_demo_code.py

示例3: check_solvable

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def check_solvable(self):
        # create the grid graph
        gf = nx.Graph()
        for r in xrange(self.world.height):
            for c in xrange(self.world.width):
                if c < self.world.width - 1:
                    gf.add_edge((r, c), (r, c + 1))
                if r < self.world.height - 1:
                    gf.add_edge((r, c), (r + 1, c))

        # remove nodes that are wall pixels
        for wall in self.world.objects['wall']:
            gf.remove_node(tuple(wall.state_index.tolist()))

        # find player and goal
        player = self.world.objects['self']
        goal = self.world.objects['goal']
        player_node = tuple(player.state_index.tolist())
        goal_node = tuple(goal.state_index.tolist())

        # check for existence of path
        self.assertTrue(nx.has_path(gf, player_node, goal_node)) 
开发者ID:vicariousinc,项目名称:pixelworld,代码行数:24,代码来源:test_mazes.py

示例4: test_one_edge

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def test_one_edge(nx):
    proc = Process(process_id=10, process_image="test.exe", command_line="test.exe /c foobar")
    other_proc = Process(process_id=12, process_image="best.exe", command_line="best.exe /c 123456")

    proc.launched[other_proc].append(timestamp=1)

    G = nx(nodes=[proc, other_proc])

    assert len(G.nodes()) == 2
    assert len(G.edges()) == 1

    u = hash(proc)
    v = hash(other_proc)

    assert networkx.has_path(G, u, v)
    assert "Launched" in G[u][v]
    assert {"timestamp": 1} == G[u][v]["Launched"]["data"][0] 
开发者ID:yampelo,项目名称:beagle,代码行数:19,代码来源:test_networkx.py

示例5: add_cycle_edges_by_path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def add_cycle_edges_by_path(g,number_of_edges,path_length = 5):
	number = 0
	num_nodes = g.number_of_nodes()
	nodes = g.nodes()
	extra_edges = []
	while number < number_of_edges:
		u,v = np.random.randint(0,num_nodes,2)
		u = nodes[u]
		v = nodes[v]
		if nx.has_path(g,u,v):
			length = nx.shortest_path_length(g,source = u,target = v)
			if length <= path_length:
				extra_edges.append((v,u))
				number += 1
		if nx.has_path(g,v,u):
			length = nx.shortest_path_length(g,source = v,target = u)
			if length <= path_length:
				extra_edges.append((u,v))
				number += 1
	print("# extra edges added with path length <= %d: %d" % (path_length,len(extra_edges)))
	return extra_edges 
开发者ID:zhenv5,项目名称:breaking_cycles_in_noisy_hierarchies,代码行数:23,代码来源:introduce_cycles_to_DAG.py

示例6: add_extra_edges

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def add_extra_edges(g,number_of_edges):
	number = 0
	num_nodes = g.number_of_nodes()
	nodes = g.nodes()
	extra_edges = set()
	while len(extra_edges) < number_of_edges:
		u,v = np.random.randint(0,num_nodes,2)
		u = nodes[u]
		v = nodes[v]
		if nx.has_path(g,u,v):
			if (v,u) not in extra_edges:
				extra_edges.add((v,u))	
		if nx.has_path(g,v,u):
			if (u,v) not in extra_edges:
				extra_edges.add((u,v))
	extra_edges = list(extra_edges)
	print("# extra edges added (path lenght unconstrainted): %d" % (len(extra_edges)))
	return extra_edges 
开发者ID:zhenv5,项目名称:breaking_cycles_in_noisy_hierarchies,代码行数:20,代码来源:introduce_cycles_to_DAG.py

示例7: hasLogicalError

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def hasLogicalError(self):
        for type in self.types:
            for charge_type in ['X','Z']:
                LogicalLattice = self.Primal.copy()
                for node in self.Primal.nodes():
                    if self.Primal.node[node]['charge'][charge_type] == 0:
                        LogicalLattice.remove_node(node)
                for node1 in LogicalLattice.nodes():
                    for node2 in LogicalLattice.nodes():
                        if node1 in self.Boundary[type] and node2 in self.Boundary[type]:
                            if self.Boundary[type][node1] != self.Boundary[type][node2]:
                                start, end = node1, node2
                                if start in LogicalLattice.nodes() and end in LogicalLattice.nodes():
                                    if nx.has_path(LogicalLattice, start, end):
                                        return True

        return False 
开发者ID:jacobmarks,项目名称:QTop,代码行数:19,代码来源:surface_codes.py

示例8: _optimize_propagation

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def _optimize_propagation(self):
        changed = False
        # remove redundant tensor nodes from graph
        for node in list(self.nodes()):
            if not isinstance(node, TensorNode):
                continue
            preds = node.predecessors
            succs = node.successors
            if not (len(preds) == len(succs) == 1):
                continue
            changed = True
            # remove current node as it is redundant
            self.remove_node(node)
            self.add_edge(preds[0], succs[0])
        # remove nodes not connected to output
        output_nodes = self.output_nodes()
        for node in list(self.nodes()):
            if not any(self.has_path(node, o) for o in output_nodes):
                self.remove_node(node)
        return changed 
开发者ID:deep-fry,项目名称:mayo,代码行数:22,代码来源:graph.py

示例9: test_topological_sort3

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def test_topological_sort3(self):
        DG = nx.DiGraph()
        DG.add_edges_from([(1, i) for i in range(2, 5)])
        DG.add_edges_from([(2, i) for i in range(5, 9)])
        DG.add_edges_from([(6, i) for i in range(9, 12)])
        DG.add_edges_from([(4, i) for i in range(12, 15)])

        def validate(order):
            ok_(isinstance(order, list))
            assert_equal(set(order), set(DG))
            for u, v in combinations(order, 2):
                assert_false(nx.has_path(DG, v, u))
        validate(nx.topological_sort_recursive(DG))
        validate(nx.topological_sort(DG))

        DG.add_edge(14, 1)
        assert_raises(nx.NetworkXUnfeasible, nx.topological_sort, DG)
        assert_raises(nx.NetworkXUnfeasible, nx.topological_sort_recursive, DG) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:20,代码来源:test_dag.py

示例10: test_topological_sort3

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def test_topological_sort3(self):
        DG = nx.DiGraph()
        DG.add_edges_from([(1, i) for i in range(2, 5)])
        DG.add_edges_from([(2, i) for i in range(5, 9)])
        DG.add_edges_from([(6, i) for i in range(9, 12)])
        DG.add_edges_from([(4, i) for i in range(12, 15)])

        def validate(order):
            ok_(isinstance(order, list))
            assert_equal(set(order), set(DG))
            for u, v in combinations(order, 2):
                assert_false(nx.has_path(DG, v, u))
        validate(list(nx.topological_sort(DG)))

        DG.add_edge(14, 1)
        assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG)) 
开发者ID:holzschu,项目名称:Carnets,代码行数:18,代码来源:test_dag.py

示例11: get_contigs_of_mates

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def get_contigs_of_mates(node, bamfile, G):
    """ retrieves set of nodes mapped to by read pairs
        having one mate on node; discards isolated nodes
        because they tend to reflect irrelevant alignments
    """
    mate_tigs = set([])
    if node[-1] == "'": node=node[:-1]
    try:    
        for hit in bamfile.fetch(node):
            nref = bamfile.getrname(hit.next_reference_id)
            if nref != node:
                mate_tigs.add(nref)

    except ValueError:
        pass
    source_name = node #re.sub('NODE_','EDGE_', node)

    # print "before removal", mate_tigs
    to_remove = set([])
    for nd in mate_tigs:
        # flip name from "NODE_" prefix back to "EDGE_"
        # differs between contigs set and graph node names
        nd_name = nd #re.sub('NODE_','EDGE_', nd)
        if (G.in_degree(nd_name)==0 and G.out_degree(nd_name)==0) or \
        (not G.has_node(nd_name)):
            to_remove.add(nd)
        # see if nd reachable by node or vice-versa
        # try both flipping to rc and switching source and target    
        elif G.has_node(rc_node(source_name)) and not any([nx.has_path(G, source_name, nd_name), nx.has_path(G, rc_node(source_name),nd_name), \
          nx.has_path(G, nd_name, source_name), nx.has_path(G, nd_name, rc_node(source_name))]):
            to_remove.add(nd)
        elif not any([nx.has_path(G, source_name, nd_name), nx.has_path(G, nd_name, source_name)]):
            to_remove.add(nd)            
    mate_tigs -= to_remove
    # print "after removal", mate_tigs

    return mate_tigs 
开发者ID:Shamir-Lab,项目名称:Recycler,代码行数:39,代码来源:utils.py

示例12: enter_Call

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def enter_Call(self,jmp):
        callee = direct(jmp.target[0])
        if callee:
            if nx.has_path(self.callgraph, callee.number, self.src):
                self.srcs.append(self.caller)
            if nx.has_path(self.callgraph, callee.number, self.dst):
                self.dsts.append(self.caller) 
开发者ID:BinaryAnalysisPlatform,项目名称:bap-tutorial,代码行数:9,代码来源:path_check.py

示例13: create_path_complete_condition

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def create_path_complete_condition(transmit_node_pairs):
    """
    This creates a closure that takes a graph as its input and returns a boolean value indicating whether the pairs of nodes in transmit_node_pairs are able to communicate from each tuple in transmit_node_pairs such that there is a path from transmit_node_pairs[i][0] to transmit_node_pairs[i][1]
    """

    def path_complete_condition(G):
        return all([nx.has_path(G,x,y) for x,y in transmit_node_pairs])
    return path_complete_condition 
开发者ID:mpacer,项目名称:Causal-Bayesian-NetworkX,代码行数:10,代码来源:graph_enumerator.py

示例14: get_random_path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def get_random_path(graph: BELGraph) -> List[BaseEntity]:
    """Get a random path from the graph as a list of nodes.

    :param graph: A BEL graph
    """
    wg = graph.to_undirected()

    nodes = wg.nodes()

    def pick_random_pair() -> Tuple[BaseEntity, BaseEntity]:
        """Get a pair of random nodes."""
        return random.sample(nodes, k=2)

    source, target = pick_random_pair()

    tries = 0
    sentinel_tries = 5

    while not nx.has_path(wg, source, target) and tries < sentinel_tries:
        tries += 1
        source, target = pick_random_pair()

    if tries == sentinel_tries:
        return [source]

    return nx.shortest_path(wg, source=source, target=target) 
开发者ID:pybel,项目名称:pybel,代码行数:28,代码来源:paths.py

示例15: op

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import has_path [as 别名]
def op(self, graph, a:List, b:NodeSpec):
		return [i for i in a if nx.has_path(graph.gnx, i["id"], b["id"])]


# --------------------------------------------------------------------------
# List operators
# -------------------------------------------------------------------------- 
开发者ID:Octavian-ai,项目名称:clevr-graph,代码行数:9,代码来源:functional.py


注:本文中的networkx.has_path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。