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


Python networkx.all_pairs_shortest_path函数代码示例

本文整理汇总了Python中networkx.all_pairs_shortest_path函数的典型用法代码示例。如果您正苦于以下问题:Python all_pairs_shortest_path函数的具体用法?Python all_pairs_shortest_path怎么用?Python all_pairs_shortest_path使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: next_move

 def next_move(self, who):
     all_paths = nx.all_pairs_shortest_path(who.graph.clear_networkx_graph(self.cannottouch))
     all_paths2 = nx.all_pairs_shortest_path(who.graph.networkx_graph())
     temp = []
     for i in self.graph.graph[who.position]:
         if i in all_paths:
             temp.append(i)
     if not temp == []:
         return random.choice(temp)
开发者ID:madeleine789,项目名称:SAO-cops-and-robbers,代码行数:9,代码来源:Strategies.py

示例2: shortest_path_policy

def shortest_path_policy(graph1, graph2):
    # Take two graphs as arguments, one with all the switches and one
    # with some removed. For each host, flip a coin to decide if it
    # will be routed using the full or partial graph.
    # Because hosts are single-homed, we can save a lot of computation
    # by computing shortest paths on the subgraph of switches instead
    # the full graph of switches and hosts
    raw = defaultdict(lambda:[])
    sub_graph1 = graph1.copy()
    sub_graph1.remove_nodes_from(sub_graph1.hosts())
    sub_graph2 = graph2.copy()
    sub_graph2.remove_nodes_from(sub_graph2.hosts())
    paths1 = nx.all_pairs_shortest_path(sub_graph1)
    paths2 = nx.all_pairs_shortest_path(sub_graph2)
    hosts = graph1.hosts()
    for src in hosts:
        for dst in hosts:
            if src != dst:
                if hash(src + dst) % 5:
                    paths = paths1
                    graph = graph1
                else:
                    paths = paths2
                    graph = graph2
                # Grab hosts' switches
                try:
                    if len(graph1.neighbors(src)) > 1:
                        log.debug(str(src) + " nbrs: " + str(graph1.neighbors(src)))
                    src_sw = graph1.neighbors(src)[0]
                except IndexError:
                    # log.debug("Index error! src host " + str(src))
                    break
                try:                    
                    dst_sw = graph1.neighbors(dst)[0]
                except IndexError:
                    # log.debug("Index error! dst host " + str(dst))
                    continue
                try:
                    path = [src] + paths[src_sw][dst_sw] + [dst]
                except KeyError:
                    # log.debug("Key error! switches " + str(src_sw) + " and " + str(dst_sw))
                    continue
                last = path.pop(0)
                curr = path.pop(0)
                for next in path:
                    inport = graph1.node[curr]['ports'][last]
                    outport = graph1.node[curr]['ports'][next]
                    pat = { IN_PORT:inport, DL_TYPE:0x800, \
                            NW_SRC:ip(src), NW_DST:ip(dst) }
                    acts = [forward(outport)]
                    raw[curr].append((pat,acts))
                    last = curr
                    curr = next
    return policy_of_dict(raw)
开发者ID:XianliangJ,项目名称:collections,代码行数:54,代码来源:routing_policies.py

示例3: getPathFrequencies

def getPathFrequencies(g, label_name='label'):
	apsp = nx.all_pairs_shortest_path(g)

	l = [v0 for v in apsp.itervalues() for v0 in v.itervalues()]
	l_label = [[g.node[i][label_name] for i in item]  for item in l]


	prepared_l_label0 = [zip(path, ['/' for i in range(len(path))]) for path in l_label]
	prepared_l_label1 = [[el for subsublist in sublist for el in subsublist][:-1] for sublist in prepared_l_label0]

	all_paths = ['//' + ''.join(str_list) for str_list in prepared_l_label1]
	all_paths.sort()


	result = [[all_paths[0], 1]]

	for i in range(1, len(all_paths)):
		if all_paths[i] == all_paths[i-1]:
			result[-1][1] += 1
		else:
			result.append([all_paths[i], 1])

	freq = {}
	for el in result:
		freq[el[0]] = el[1]

	return freq
开发者ID:bolod,项目名称:bioEuler,代码行数:27,代码来源:t1.py

示例4: initialize_graph

def initialize_graph(size):
    points_x = []
    points_y = []
    terminals = []
    for i in range(size):
        points_x.append(random.uniform(-100, 100))
        points_y.append(random.uniform(-100, 100))
    for i in range(size):
        if i % 2 == 0:
            terminals.append(i)

    h = nx.erdos_renyi_graph(size, 0.3)
    g = nx.Graph()
    for i in range(size):
        g.add_node(i)

    for i in range(size):
        for j in range(i, size):
            if h.has_edge(i, j):
                g.add_edge(i, j)
                # weight=manhattan_distance((points_x[i], points_y[i]), (points_x[j], points_y[j])))

    length_shortest_paths = nx.all_pairs_dijkstra_path_length(g)
    shortest_paths = nx.all_pairs_shortest_path(g)

    metric_closure = nx.Graph()
    for i in range(size):
        metric_closure.add_node(i)
    for i in range(size):
        for j in range(size):
            metric_closure.add_edge(i, j, weight=length_shortest_paths[i][j])

    print metric_closure
    return (terminals, shortest_paths, metric_closure, points_x, points_y, g)
开发者ID:AliakseiSemchankau,项目名称:study,代码行数:34,代码来源:test.py

示例5: add_switch

 def add_switch(self, dpid, stats):
     if not dpid in self.topology.nodes():
         self.topology.add_node(dpid)
         self.topology.node[dpid]['rules'] = {}
         self.topology.node[dpid]['nActives'] = 0
     if not self.__setting_up__: self.paths_dict = nx.all_pairs_shortest_path(self.topology)
     return
开发者ID:asgard-lab,项目名称:CastFlow,代码行数:7,代码来源:overmind.py

示例6: rem_switch

 def rem_switch(self, dpid):
     if dpid in self.topology.nodes():
         self.topology.remove_node(dpid)
         for mac in self.switch_to_host[dpid]:
             del host_to_switch[mac]
         del switch_to_host[dpid]
     if not self.__setting_up__: self.paths_dict = nx.all_pairs_shortest_path(self.topology)
     return
开发者ID:asgard-lab,项目名称:CastFlow,代码行数:8,代码来源:overmind.py

示例7: compareToBinary

def compareToBinary(complexSentencesById, classifications, exampleBuilder, options):
    # Load corpus and make sentence graphs
    print >> sys.stderr, "Calculating performance on binary corpus"
    classificationsBySentence = {}
    for classification in classifications:
        example = classification[0][0]
        sentenceId = example[0].rsplit(".",1)[0]
        sentenceOrigId = complexSentencesById[sentenceId].sentence.attrib["origId"]
        if not classificationsBySentence.has_key(sentenceOrigId):
            classificationsBySentence[sentenceOrigId] = []
        classificationsBySentence[sentenceOrigId].append(classification)
    
    print >> sys.stderr, "Loading Binary corpus"
    binaryCorpusElements = loadCorpus(options.binaryCorpus)
    binaryClassifications = []
    counter = ProgressCounter(len(binaryCorpusElements.sentences), "Build binary classifications")
    for binarySentence in binaryCorpusElements.sentences:
        counter.update(1, "Building binary classifications ("+binarySentence.sentence.attrib["id"]+"): ")
        if(classificationsBySentence.has_key(binarySentence.sentence.attrib["origId"])):
            complexClassificationGraph = NX.XGraph(multiedges = multiedges)
            for token in binarySentence.sentenceGraph.tokens:
                complexClassificationGraph.add_node(token)
            for classification in classificationsBySentence[binarySentence.sentence.attrib["origId"]]:
                if classification[1] > 0:
                    example = classification[0][0]       
                    t1 = example[3]["t1"]
                    t2 = example[3]["t2"]
                    t1Binary = None
                    for token in binarySentence.sentenceGraph.tokens:
                        if token.attrib["charOffset"] == t1.attrib["charOffset"]:
                            t1Binary = token
                    t2Binary = None
                    for token in binarySentence.sentenceGraph.tokens:
                        if token.attrib["charOffset"] == t2.attrib["charOffset"]:
                            t2Binary = token
                    assert(t1Binary != None and t2Binary != None)
                    complexClassificationGraph.add_edge(t1Binary, t2Binary)
            paths = NX.all_pairs_shortest_path(complexClassificationGraph, cutoff=999)
            for pair in binarySentence.pairs:
                t1 = binarySentence.sentenceGraph.entityHeadTokenByEntity[pair.attrib["e1"]]
                t2 = binarySentence.sentenceGraph.entityHeadTokenByEntity[pair.attrib["e2"]]
                assert(pair.attrib["interaction"] == "True" or pair.attrib["interaction"] == "False")
                if pair.attrib["interaction"] == "True":
                    pairClass = 1
                else:
                    pairClass = -1
                extra = {"xtype":"edge","type":"i","t1":t1,"t2":t2}
                if paths.has_key(t1) and paths[t1].has_key(t2):
                    binaryClassifications.append( [[pair.attrib["id"], pairClass, None, extra], 1, "binary"] )
                else:
                    binaryClassifications.append( [[pair.attrib["id"], pairClass, None, extra], -1, "binary"] )
    print >> sys.stderr, "Evaluating binary classifications"
    evaluation = Evaluation(predictions, classSet=exampleBuilder.classSet)
    print >> sys.stderr, evaluation.toStringConcise()
    if options.output != None:
        evaluation.saveCSV(options.output + "/binary_comparison_results.csv")                    
开发者ID:jbjorne,项目名称:Tdevel,代码行数:56,代码来源:SplitAnalysis.py

示例8: buildExamples

    def buildExamples(self, sentenceGraph):
        self.makeGSEvents(sentenceGraph)

        eventNodes = []
        nameNodes = []
        for entity in sentenceGraph.entities:
            if entity.get("type") == "neg":
                continue
            if entity.get("isName") == "True":
                nameNodes.append(entity)
            else:
                eventNodes.append(entity)
        allNodes = eventNodes + nameNodes

        examples = []
        exampleIndex = 0

        undirected = sentenceGraph.dependencyGraph.to_undirected()
        paths = NX.all_pairs_shortest_path(undirected, cutoff=999)

        for eventNode in eventNodes:
            eventType = eventNode.get("type")
            if eventType in [
                "Gene_expression",
                "Transcription",
                "Protein_catabolism",
                "Localization",
                "Phosphorylation",
            ]:
                for nameNode in nameNodes:
                    if self.isPotentialGeniaInteraction(eventNode, nameNode):
                        examples.append(self.buildExample(exampleIndex, sentenceGraph, paths, eventNode, nameNode))
                        exampleIndex += 1
            elif eventType in ["Regulation", "Positive_regulation", "Negative_regulation"]:
                combinations = combine.combine(allNodes + [None], allNodes + [None])
                for combination in combinations:
                    if combination[0] == combination[1]:
                        continue
                    if combination[0] == eventNode or combination[1] == eventNode:
                        continue
                    if combination[0] != None and not self.isPotentialGeniaInteraction(eventNode, combination[0]):
                        continue
                    if combination[1] != None and not self.isPotentialGeniaInteraction(eventNode, combination[1]):
                        continue
                    examples.append(
                        self.buildExample(exampleIndex, sentenceGraph, paths, eventNode, combination[0], combination[1])
                    )
                    exampleIndex += 1
            elif eventType in ["Binding"]:
                continue
            else:
                assert False, eventType

        self.gsEvents = None
        return examples
开发者ID:jbjorne,项目名称:Tdevel,代码行数:55,代码来源:EventExampleBuilder.py

示例9: __init__

    def __init__(self, topology, shortest_path=None):
        """Constructors
        
        Parameters
        ----------
        topology : fnss.Topology
            The topology object
        shortest_path : dict of dict, optional
            The all-pair shortest paths of the network
        """
        # Filter inputs
        if not isinstance(topology, fnss.Topology):
            raise ValueError('The topology argument must be an instance of '
                             'fnss.Topology or any of its subclasses.')
        
        # Shortest paths of the network
        self.shortest_path = shortest_path if shortest_path is not None \
                             else nx.all_pairs_shortest_path(topology)
        
        # Network topology
        self.topology = topology
        
        # Dictionary mapping each content object to its source
        # dict of location of contents keyed by content ID
        self.content_source = {}
        
        # Dictionary of cache sizes keyed by node
        self.cache_size = {}
        
        self.colla_table = {}

        # Dictionary of link types (internal/external)
        self.link_type = nx.get_edge_attributes(topology.to_directed(), 'type')
        
        self.link_delay = fnss.get_delays(topology.to_directed())
        
        policy_name = topology.graph['cache_policy']
        # Initialize attributes
        for node in topology.nodes_iter():
            stack_name, stack_props = fnss.get_stack(topology, node)
            if stack_name == 'cache':
                self.cache_size[node] = stack_props['size']
                self.colla_table[node] = {}
            elif stack_name == 'source':
                contents = stack_props['contents']
                for content in contents:
                    self.content_source[content] = node
        cache_size = dict((node, fnss.get_stack(topology, node)[1]['size'])
                          for node in topology.nodes_iter()
                          if fnss.get_stack(topology, node)[0] == 'cache')
        # The actual cache object storing the content
        self.caches = dict((node, cache_policy_register[policy_name](cache_size[node]))
                            for node in cache_size)
开发者ID:Jeswang,项目名称:icarus,代码行数:53,代码来源:network.py

示例10: test_all_pairs_shortest_path

 def test_all_pairs_shortest_path(self):
     p=nx.shortest_path(self.cycle)
     assert_equal(p[0][3],[0,1,2,3])
     assert_equal(p,nx.all_pairs_shortest_path(self.cycle))
     p=nx.shortest_path(self.grid)
     assert_equal(p[1][12],[1, 2, 3, 4, 8, 12])
     # now with weights
     p=nx.shortest_path(self.cycle,weighted=True)
     assert_equal(p[0][3],[0,1,2,3])
     assert_equal(p,nx.all_pairs_dijkstra_path(self.cycle))
     p=nx.shortest_path(self.grid,weighted=True)
     assert_equal(p[1][12],[1, 2, 3, 4, 8, 12])
开发者ID:c0ns0le,项目名称:zenoss-4,代码行数:12,代码来源:test_generic.py

示例11: run

 def run(self):
      """here we wait for the topology to be "complete", then calculate what is needed and stop the thread"""
      self.__cur__ = self.__init_idle__ = time.time()
      self.__setting_up__ = True
      while (self.__cur__ < self.__init_idle__ + 5):
         self.__cur__ = time.time()
         #print 'Detecting topology, idle for ',self.__cur__ - self.__init_idle__
         time.sleep(0.001)
      print 'Finished detecting topology, creating data...'
      self.paths_dict = nx.all_pairs_shortest_path(self.topology)
      self.__setting_up__ = False
      print 'Finished creating data'
开发者ID:asgard-lab,项目名称:CastFlow,代码行数:12,代码来源:overmind.py

示例12: test_all_pairs_shortest_path

 def test_all_pairs_shortest_path(self):
     p=nx.shortest_path(self.cycle)
     assert_equal(p[0][3],[0,1,2,3])
     assert_equal(p,nx.all_pairs_shortest_path(self.cycle))
     p=nx.shortest_path(self.grid)
     validate_grid_path(4, 4, 1, 12, p[1][12])
     # now with weights
     p=nx.shortest_path(self.cycle,weight='weight')
     assert_equal(p[0][3],[0,1,2,3])
     assert_equal(p,nx.all_pairs_dijkstra_path(self.cycle))
     p=nx.shortest_path(self.grid,weight='weight')
     validate_grid_path(4, 4, 1, 12, p[1][12])
开发者ID:rvu95,项目名称:networkx,代码行数:12,代码来源:test_generic.py

示例13: transitveReduction

 def transitveReduction(self, digraph):
     paths = dict(NX.all_pairs_shortest_path(digraph))
     def hasPath(node1, node2):
         if node1 == node2:
             return False
         return (node1 in paths and node2 in paths[node1] and
                 len(paths[node1][node2]) > 0)
     for x in digraph.nodes():
         for y in digraph.nodes():
             for z in digraph.nodes():
                 if (x != z and digraph.has_edge(x, z) and hasPath(x, y) and
                     hasPath(y, z)):
                     digraph.remove_edge(x, z)
开发者ID:benedictpaten,项目名称:cactus,代码行数:13,代码来源:schedule.py

示例14: all_pairs_shortest_path

 def all_pairs_shortest_path(cls,topology):
     location_paths = {}
     switch_paths = nx.all_pairs_shortest_path(topology)
     for s1, paths in switch_paths.items():
         location_paths[s1] = {}
         for s2, path in paths.items():
             location_paths[s1][s2] = []
             cur = s1
             for nxt in path + [s2]:
                 if cur != nxt:
                     link = topology[cur][nxt]
                     loc = Location(cur,link[cur])
                     location_paths[s1][s2].append(loc)
                 cur = nxt
     return location_paths
开发者ID:YimengZhao,项目名称:netassay,代码行数:15,代码来源:network.py

示例15: main

def main(list_of_filenames):
	"""
	the function that runs all the experiments
	"""
	list_of_param_graphs = read_graphs_params(list_of_filenames[0])
	list_of_coeffs = read_coeff(list_of_filenames[1])

	list_of_setP = []
	list_of_path_dict = []
	list_of_uncovered_edges = []
	list_of_graph_iDs = []

	for param_graphs in list_of_param_graphs: # getting each graph parameters
		list_of_graph_iDs.append(param_graphs[0])
		H = nx.fast_gnp_random_graph(param_graphs[1], param_graphs[2], param_graphs[3], False)
		randSeed = param_graphs[6]

		for (u,v) in H.edges():
			random.seed(randSeed)
			rInt = random.randint(param_graphs[4], param_graphs[5])
			H[u][v]['w'] = rInt
			randSeed += 1

		set_of_paths_P = nx.all_pairs_shortest_path(H)
		setP = gc.remove_duplicate_paths(set_of_paths_P, nx.number_of_nodes(H))
		list_of_setP.append(setP)
		path_dict = create_path_dict(setP)
		list_of_path_dict.append(path_dict)
		uncovered_edges = gc.get_all_edges_from_SOP(setP)
		list_of_uncovered_edges.append(uncovered_edges)


	out_file = ''.join(["./data/csv/",list_of_filenames[2],".csv"])
	file = open(os.path.expanduser(out_file), 'at')
	writer = csv.writer(file)
	for idx in xrange(len(list_of_setP)): #loop through the graphs
		graph_iD = list_of_graph_iDs[idx]
		writer.writerow(('Graph Params: ', list_of_param_graphs[idx]))
		for coeffs in list_of_coeffs:
			for iteration in xrange(1): # change the input to xrange() depending on the number of iterations on a single experiment
				t_path_dict = list_of_path_dict[idx]
				t_uncovered_edges = set(list_of_uncovered_edges[idx])
				t_setP = list_of_setP[idx]
				six_tuple = gf.func_wrapper_greedy_algo(t_path_dict, t_uncovered_edges, coeffs[0])
				writer.writerow((graph_iD, coeffs[0], six_tuple[0], len(t_setP), six_tuple[1], 
					six_tuple[2], six_tuple[3], six_tuple[4], six_tuple[5]))
	file.close()
开发者ID:slin17,项目名称:Summer_Research_2016,代码行数:47,代码来源:summer_research_main.py


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