本文整理汇总了Python中networkx.single_source_dijkstra_path函数的典型用法代码示例。如果您正苦于以下问题:Python single_source_dijkstra_path函数的具体用法?Python single_source_dijkstra_path怎么用?Python single_source_dijkstra_path使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了single_source_dijkstra_path函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dijkstra
def test_dijkstra(self):
(D, P) = nx.single_source_dijkstra(self.XG, 's')
validate_path(self.XG, 's', 'v', 9, P['v'])
assert_equal(D['v'], 9)
validate_path(
self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v'])
assert_equal(dict(
nx.single_source_dijkstra_path_length(self.XG, 's'))['v'], 9)
validate_path(
self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v'])
validate_path(
self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v'])
GG = self.XG.to_undirected()
# make sure we get lower weight
# to_undirected might choose either edge with weight 2 or weight 3
GG['u']['x']['weight'] = 2
(D, P) = nx.single_source_dijkstra(GG, 's')
validate_path(GG, 's', 'v', 8, P['v'])
assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge
validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v'))
assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8)
validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3))
validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3))
assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15)
validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2))
assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4)
validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2))
validate_path(
self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]['v'])
validate_path(
self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v'])
validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v'))
assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2)
# NetworkXError: node s not reachable from moon
assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon')
assert_raises(
nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon')
validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3))
validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4))
assert_equal(
nx.single_source_dijkstra(self.cycle, 0, 0), ({0: 0}, {0: [0]}))
示例2: hirarcy
def hirarcy(self, c, files_dict):
edges = []
interfaces = 'select path, superClass from classes'
files_Names = [x.split(".")[-1] for x in files_dict]
pathNames = {}
for row in c.execute(interfaces):
nameClass = (row[0]).split(".")[-1]
pathNames[nameClass] = row[0]
nameSuper = (row[1]).split(".")[-1]
if (nameClass in files_Names):
sup = 'root'
if (nameSuper in files_Names):
sup = nameSuper
edges.append((sup, nameClass))
g = networkx.DiGraph()
g.add_node('root')
g.add_edges_from(edges)
degs = g.out_degree()
degsIN = g.in_degree()
succ = dict(networkx.bfs_successors(g, 'root'))
for s in succ:
succ[s] = len(succ[s])
paths = networkx.single_source_dijkstra_path(g, 'root')
depth = {}
for n in g.nodes():
depth[n] = 2
if (n in paths):
depth[n] = len(paths[n])
self.addFromDict(files_dict,degs,pathNames)
self.addFromDict(files_dict,degsIN,pathNames)
self.addFromDict(files_dict,succ,pathNames)
self.addFromDict(files_dict,depth,pathNames)
return files_Names,
示例3: test_dijkstra
def test_dijkstra(self):
(D,P)= nx.single_source_dijkstra(self.XG,'s')
assert_equal(P['v'], ['s', 'x', 'u', 'v'])
assert_equal(D['v'],9)
assert_equal(nx.single_source_dijkstra_path(self.XG,'s')['v'],
['s', 'x', 'u', 'v'])
assert_equal(nx.single_source_dijkstra_path_length(self.XG,'s')['v'],9)
assert_equal(nx.single_source_dijkstra(self.XG,'s')[1]['v'],
['s', 'x', 'u', 'v'])
assert_equal(nx.single_source_dijkstra_path(self.MXG,'s')['v'],
['s', 'x', 'u', 'v'])
GG=self.XG.to_undirected()
# make sure we get lower weight
# to_undirected might choose either edge with weight 2 or weight 3
GG['u']['x']['weight']=2
(D,P)= nx.single_source_dijkstra(GG,'s')
assert_equal(P['v'] , ['s', 'x', 'u', 'v'])
assert_equal(D['v'],8) # uses lower weight of 2 on u<->x edge
assert_equal(nx.dijkstra_path(GG,'s','v'), ['s', 'x', 'u', 'v'])
assert_equal(nx.dijkstra_path_length(GG,'s','v'),8)
assert_equal(nx.dijkstra_path(self.XG2,1,3), [1, 4, 5, 6, 3])
assert_equal(nx.dijkstra_path(self.XG3,0,3), [0, 1, 2, 3])
assert_equal(nx.dijkstra_path_length(self.XG3,0,3),15)
assert_equal(nx.dijkstra_path(self.XG4,0,2), [0, 1, 2])
assert_equal(nx.dijkstra_path_length(self.XG4,0,2), 4)
assert_equal(nx.dijkstra_path(self.MXG4,0,2), [0, 1, 2])
assert_equal(nx.single_source_dijkstra(self.G,'s','v')[1]['v'],
['s', 'u', 'v'])
assert_equal(nx.single_source_dijkstra(self.G,'s')[1]['v'],
['s', 'u', 'v'])
assert_equal(nx.dijkstra_path(self.G,'s','v'), ['s', 'u', 'v'])
assert_equal(nx.dijkstra_path_length(self.G,'s','v'), 2)
# NetworkXError: node s not reachable from moon
assert_raises(nx.NetworkXNoPath,nx.dijkstra_path,self.G,'s','moon')
assert_raises(nx.NetworkXNoPath,nx.dijkstra_path_length,self.G,'s','moon')
assert_equal(nx.dijkstra_path(self.cycle,0,3),[0, 1, 2, 3])
assert_equal(nx.dijkstra_path(self.cycle,0,4), [0, 6, 5, 4])
assert_equal(nx.single_source_dijkstra(self.cycle,0,0),({0:0}, {0:[0]}) )
示例4: compute_shortest_paths
def compute_shortest_paths(self, ydim):
start = time.time()
self.source = self.N.nodes()[argmin(self.P[self.N.nodes(), ydim])]
self.shortest_paths = nx.single_source_dijkstra_path(self.N,
self.source)
self.max_path_len = max(nx.single_source_dijkstra_path_length(self.N,
self.source).values())
print 'G compute time: %f seconds' % (time.time() - start)
示例5: test_dijkstra
def test_dijkstra(self):
(D,P)= nx.single_source_dijkstra(self.XG,'s')
assert_equal(P['v'], ['s', 'x', 'u', 'v'])
assert_equal(D['v'],9)
assert_equal(nx.single_source_dijkstra_path(self.XG,'s')['v'],
['s', 'x', 'u', 'v'])
assert_equal(nx.single_source_dijkstra_path_length(self.XG,'s')['v'],9)
assert_equal(nx.single_source_dijkstra(self.XG,'s')[1]['v'],
['s', 'x', 'u', 'v'])
assert_equal(nx.single_source_dijkstra_path(self.MXG,'s')['v'],
['s', 'x', 'u', 'v'])
GG=self.XG.to_undirected()
(D,P)= nx.single_source_dijkstra(GG,'s')
assert_equal(P['v'] , ['s', 'x', 'u', 'v'])
assert_equal(D['v'],8) # uses lower weight of 2 on u<->x edge
assert_equal(nx.dijkstra_path(GG,'s','v'), ['s', 'x', 'u', 'v'])
assert_equal(nx.dijkstra_path_length(GG,'s','v'),8)
assert_equal(nx.dijkstra_path(self.XG2,1,3), [1, 4, 5, 6, 3])
assert_equal(nx.dijkstra_path(self.XG3,0,3), [0, 1, 2, 3])
assert_equal(nx.dijkstra_path_length(self.XG3,0,3),15)
assert_equal(nx.dijkstra_path(self.XG4,0,2), [0, 1, 2])
assert_equal(nx.dijkstra_path_length(self.XG4,0,2), 4)
assert_equal(nx.dijkstra_path(self.MXG4,0,2), [0, 1, 2])
assert_equal(nx.single_source_dijkstra(self.G,'s','v')[1]['v'],
['s', 'u', 'v'])
assert_equal(nx.single_source_dijkstra(self.G,'s')[1]['v'],
['s', 'u', 'v'])
assert_equal(nx.dijkstra_path(self.G,'s','v'), ['s', 'u', 'v'])
assert_equal(nx.dijkstra_path_length(self.G,'s','v'), 2)
# NetworkXError: node s not reachable from moon
assert_raises(nx.NetworkXNoPath,nx.dijkstra_path,self.G,'s','moon')
assert_raises(nx.NetworkXNoPath,nx.dijkstra_path_length,self.G,'s','moon')
assert_equal(nx.dijkstra_path(self.cycle,0,3),[0, 1, 2, 3])
assert_equal(nx.dijkstra_path(self.cycle,0,4), [0, 6, 5, 4])
assert_equal(nx.single_source_dijkstra(self.cycle,0,0),(0, [0]) )
示例6: test_bidirectional_dijkstra
def test_bidirectional_dijkstra(self):
assert_equal(nx.bidirectional_dijkstra(self.XG, "s", "v"), (9, ["s", "x", "u", "v"]))
assert_equal(nx.bidirectional_dijkstra(self.G, "s", "v"), (2, ["s", "x", "v"]))
assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 3), (3, [0, 1, 2, 3]))
assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 4), (3, [0, 6, 5, 4]))
assert_equal(nx.bidirectional_dijkstra(self.XG3, 0, 3), (15, [0, 1, 2, 3]))
assert_equal(nx.bidirectional_dijkstra(self.XG4, 0, 2), (4, [0, 1, 2]))
# need more tests here
assert_equal(nx.dijkstra_path(self.XG, "s", "v"), nx.single_source_dijkstra_path(self.XG, "s")["v"])
示例7: __configure_routing
def __configure_routing(self):
for n in self.graph:
self.routing[n] = single_source_dijkstra_path(self.graph, n)
self.ipdestlpm = PyTricia()
for n,d in self.graph.nodes_iter(data=True):
dlist = d.get('ipdests','').split()
for destipstr in dlist:
ipnet = ipaddr.IPNetwork(destipstr)
xnode = {}
self.ipdestlpm[str(ipnet)] = xnode
if 'dests' in xnode:
xnode['dests'].append(n)
else:
xnode['net'] = ipnet
xnode['dests'] = [ n ]
# install static forwarding table entries to each node
# FIXME: there's a problematic bit of code here that triggers
# pytricia-related (iterator) core dump
for nodename,nodeobj in self.nodes.iteritems():
if isinstance(nodeobj, Router):
for prefix in self.ipdestlpm.keys():
lpmnode = self.ipdestlpm.get(prefix)
if nodename not in lpmnode['dests']:
routes = self.routing[nodename]
for d in lpmnode['dests']:
try:
path = routes[d]
except KeyError:
self.logger.warn("No route from {} to {}".format(nodename, d))
continue
nexthop = path[1]
nodeobj.addForwardingEntry(prefix, nexthop)
self.owdhash = {}
for a in self.graph:
for b in self.graph:
key = a + ':' + b
rlist = [ a ]
while rlist[-1] != b:
nh = self.nexthop(rlist[-1], b)
if not nh:
self.logger.debug('No route from %s to %s (in owd; ignoring)' % (a,b))
return None
rlist.append(nh)
owd = 0.0
for i in xrange(len(rlist)-1):
owd += self.delay(rlist[i],rlist[i+1])
self.owdhash[key] = owd
示例8: test_single_source_shortest_path
def test_single_source_shortest_path(self):
p=nx.shortest_path(self.cycle,0)
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_shortest_path(self.cycle,0))
p=nx.shortest_path(self.grid,1)
validate_grid_path(4, 4, 1, 12, p[12])
# now with weights
p=nx.shortest_path(self.cycle,0,weight='weight')
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0))
p=nx.shortest_path(self.grid,1,weight='weight')
validate_grid_path(4, 4, 1, 12, p[12])
示例9: test_single_source_shortest_path
def test_single_source_shortest_path(self):
p=nx.shortest_path(self.cycle,0)
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_shortest_path(self.cycle,0))
p=nx.shortest_path(self.grid,1)
assert_equal(p[12],[1, 2, 3, 4, 8, 12])
# now with weights
p=nx.shortest_path(self.cycle,0,weighted=True)
assert_equal(p[3],[0,1,2,3])
assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0))
p=nx.shortest_path(self.grid,1,weighted=True)
assert_equal(p[12],[1, 2, 3, 4, 8, 12])
示例10: test_SPF
def test_SPF():
edgeLen = 50
G = tNetworkX.createGridTopo(edgeLen=edgeLen)
# print "G: "+str(G.edges(data=True))
s = time.clock()
s1 = time.time()
path = nx.single_source_dijkstra_path(G,'0000.0000.0000')
e = time.clock()
e1 = time.time()
print "time.clock() consume time: "+str(e-s)
print "time.time() consume time: "+str(e1-s1)
pass
示例11: allpairs
def allpairs(graph_file=None,wt_attr=None):
"""
Print the shortest path for all nodes, using
the attribute named <b>wt_attr</b> as the weighting
function.
"""
if graph_file is None and wt_attr is None:
parser = argparse.ArgumentParser()
parser.add_argument("-w", help="Attribute to use for shortest path weight",
metavar="<weight attribute>")
parser.add_argument("graph_file",help="Modelnet Graph File")
args = parser.parse_args()
graph_file = args.graph_file
wt_attr = args.w
gr = load_graph(graph_file)
print '<?xml version="1.0" encoding="ISO-8859-1"?>'
print '<allpairs>'
numdone = 0
sys.stderr.write("Routing Node %s" % str(numdone))
for src in gr.nodes():
if gr.node[src]['vn'] == -1:
continue
if wt_attr:
sp = nx.single_source_dijkstra_path(gr,src,wt_attr)
else:
sp = nx.single_source_shortest_path(gr,src)
for dst in sp:
if gr.node[dst]['vn'] == -1:
continue
if dst == src:
continue
path = sp[dst]
hops = [gr[x][y]['int_idx'] for x,y in pairwise(path)]
print ('<path int_vndst="%d" int_vnsrc="%d" hops="%s"/>'
% (gr.node[dst]['vn'],
gr.node[src]['vn'],
" ".join(map(str,hops))))
sys.stderr.write('\b'*len(str(numdone)))
sys.stderr.write("%d" % int(numdone+1))
numdone += 1
print '</allpairs>'
示例12: test_dijkstra
def test_dijkstra(self):
(D, P) = nx.single_source_dijkstra(self.XG, "s")
assert_equal(P["v"], ["s", "x", "u", "v"])
assert_equal(D["v"], 9)
assert_equal(nx.single_source_dijkstra_path(self.XG, "s")["v"], ["s", "x", "u", "v"])
assert_equal(nx.single_source_dijkstra_path_length(self.XG, "s")["v"], 9)
assert_equal(nx.single_source_dijkstra(self.XG, "s")[1]["v"], ["s", "x", "u", "v"])
assert_equal(nx.single_source_dijkstra_path(self.MXG, "s")["v"], ["s", "x", "u", "v"])
GG = self.XG.to_undirected()
(D, P) = nx.single_source_dijkstra(GG, "s")
assert_equal(P["v"], ["s", "x", "u", "v"])
assert_equal(D["v"], 8) # uses lower weight of 2 on u<->x edge
assert_equal(nx.dijkstra_path(GG, "s", "v"), ["s", "x", "u", "v"])
assert_equal(nx.dijkstra_path_length(GG, "s", "v"), 8)
assert_equal(nx.dijkstra_path(self.XG2, 1, 3), [1, 4, 5, 6, 3])
assert_equal(nx.dijkstra_path(self.XG3, 0, 3), [0, 1, 2, 3])
assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15)
assert_equal(nx.dijkstra_path(self.XG4, 0, 2), [0, 1, 2])
assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4)
assert_equal(nx.dijkstra_path(self.MXG4, 0, 2), [0, 1, 2])
assert_equal(nx.single_source_dijkstra(self.G, "s", "v")[1]["v"], ["s", "u", "v"])
assert_equal(nx.single_source_dijkstra(self.G, "s")[1]["v"], ["s", "u", "v"])
assert_equal(nx.dijkstra_path(self.G, "s", "v"), ["s", "u", "v"])
assert_equal(nx.dijkstra_path_length(self.G, "s", "v"), 2)
# NetworkXError: node s not reachable from moon
assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, "s", "moon")
assert_raises(nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, "s", "moon")
assert_equal(nx.dijkstra_path(self.cycle, 0, 3), [0, 1, 2, 3])
assert_equal(nx.dijkstra_path(self.cycle, 0, 4), [0, 6, 5, 4])
示例13: test_bidirectional_dijkstra
def test_bidirectional_dijkstra(self):
assert_equal(nx.bidirectional_dijkstra(self.XG, "s", "v"), (9, ["s", "x", "u", "v"]))
(dist, path) = nx.bidirectional_dijkstra(self.G, "s", "v")
assert_equal(dist, 2)
# skip this test, correct path could also be ['s','u','v']
# assert_equal(nx.bidirectional_dijkstra(self.G,'s','v'),
# (2, ['s', 'x', 'v']))
assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 3), (3, [0, 1, 2, 3]))
assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 4), (3, [0, 6, 5, 4]))
assert_equal(nx.bidirectional_dijkstra(self.XG3, 0, 3), (15, [0, 1, 2, 3]))
assert_equal(nx.bidirectional_dijkstra(self.XG4, 0, 2), (4, [0, 1, 2]))
# need more tests here
assert_equal(nx.dijkstra_path(self.XG, "s", "v"), nx.single_source_dijkstra_path(self.XG, "s")["v"])
示例14: test_dijkstra_vs_networkx_single_source_all_lenghts_and_paths
def test_dijkstra_vs_networkx_single_source_all_lenghts_and_paths():
""" Test Dijkstra: Rion's implementation vs Networkx implementation > Single Source """
# NX Version
G = nx.from_edgelist(edgelist_james)
nx.set_edge_attributes(G, 'weight', edgelist_james)
nx_lenghts = nx.single_source_dijkstra_path_length(G, source='s', weight='weight')
nx_paths = nx.single_source_dijkstra_path(G, source='s', weight='weight')
# My Version
d = Dijkstra()
d.from_edgelist(edgelist_james, directed=False)
dc_lenghts, dc_paths = d.single_source_shortest_paths('s', kind='metric')
assert (nx_lenghts == dc_lenghts)
assert (nx_paths == dc_paths)
示例15: clip
def clip(self, d):
self.G = nx.single_source_dijkstra_path(self.N, self.source)
g = nx.Graph()
P = set()
print 'geodesic clipping..'
for path in pbar(self.G.values()):
if len(path) < 2: continue
curr_dist = 0
for p in range(1, len(path)): # from source (level 0) to i
curr_dist += self.N[path[p-1]][path[p]]['weight']
if curr_dist <= d:
g.add_edge(path[p-1], path[p])
P.add(harray(self.P[path[p-1]]))
P.add(harray(self.P[path[p]]))
else:
break
return vstack(P)