Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Path Networkx-如何获得显示节点id而不是标签的节点之间的最短路径长度_Path_Label_Nodes_Networkx_Shortest - Fatal编程技术网

Path Networkx-如何获得显示节点id而不是标签的节点之间的最短路径长度

Path Networkx-如何获得显示节点id而不是标签的节点之间的最短路径长度,path,label,nodes,networkx,shortest,Path,Label,Nodes,Networkx,Shortest,我不熟悉将NetworkX库与Python一起使用 假设我导入了一个Pajek格式的文件: import networkx as nx G=nx.read_pajek("pajek_network_file.net") G=nx.Graph(G) 我的文件的内容是(在Pajek中,节点称为“顶点”): 现在,我想计算网络中节点之间的所有最短路径长度,根据库文档,我正在使用这个函数 path = nx.all_pairs_shortest_path_length(G) 返回:length–由源

我不熟悉将NetworkX库与Python一起使用

假设我导入了一个Pajek格式的文件:

import networkx as nx
G=nx.read_pajek("pajek_network_file.net")
G=nx.Graph(G)
我的文件的内容是(在Pajek中,节点称为“顶点”):

现在,我想计算网络中节点之间的所有最短路径长度,根据库文档,我正在使用这个函数

path = nx.all_pairs_shortest_path_length(G)
返回:length–由源和目标键入的最短路径长度字典

我得到的回报是:

print path
{u'Author4': {u'Author4': 0, u'Author5': 1, u'Author6': 3, u'Author1': 4, u'Author2': 1, u'Author3': 2}, u'Author5': {u'Author4': 1, u'Author5': 0, u'Author6': 2, u'Author1': 3, u'Author2': 2, u'Author3': 1}, u'Author6': {u'Author4': 3, u'Author5': 2, u'Author6': 0, u'Author1': 1, u'Author2': 4, u'Author3': 1}, u'Author1': {u'Author4': 4, u'Author5': 3, u'Author6': 1, u'Author1': 0, u'Author2': 5, u'Author3': 2}, u'Author2': {u'Author4': 1, u'Author5': 2, u'Author6': 4, u'Author1': 5, u'Author2': 0, u'Author3': 3}, u'Author3': {u'Author4': 2, u'Author5': 1, u'Author6': 1, u'Author1': 2, u'Author2': 3, u'Author3': 0}}
正如你所看到的,这真的很难阅读,也很难在以后使用

理想情况下,我想要的是一份格式类似于以下内容的退货:

source_node_id, target_node_id, path_length
123, 456, 5
123, 789, 2
123, 111, 4
简而言之,我需要仅使用(或至少包括)节点ID获取返回,而不是仅显示节点标签。并且,为了得到一条直线上的每一对可能的线以及它们对应的最短路径

这在NetworkX中可能吗


函数参考:

像这样的东西怎么样

import networkx as nx                                                            
G=nx.read_pajek("pajek_network_file.net")                                        
G=nx.Graph(G)
# first get all the lengths      
path_lengths = nx.all_pairs_shortest_path_length(G)                              

# now iterate over all pairs of nodes      
for src in G.nodes():
    # look up the id as desired                           
    id_src = G.node[src].get('id')
    for dest in G.nodes():                                                       
        if src != dest: # ignore self-self paths
            id_dest =  G.node[dest].get('id')                                    
            l = path_lengths.get(src).get(dest)                                  
            print "{}, {}, {}".format(id_src, id_dest, l) 
这将产生一个输出

111, 222, 1
111, 333, 3
111, 123, 4
111, 456, 1
111, 789, 2
...
如果需要进行进一步处理(例如排序),则存储
l
值,而不仅仅是打印它们


(你可以使用类似于
G.nodes(),2)的方法更清晰地循环成对的节点。
但是如果你不熟悉的话,上面的方法会更明确一些。)

最后,我只需要计算整个网络的一个子集的最短路径(我的实际网络很大,有600K个节点和6M个边),因此,我编写了一个脚本,从CSV文件中读取源节点和目标节点对,存储到numpy数组,然后将它们作为参数传递到nx.shortest_path_length,并计算每对节点的长度,最后将结果保存到CSV文件中

代码如下,我发布它只是为了防止它对其他人有用:

print "Importing libraries..."

import networkx as nx
import csv
import numpy as np

#Import network in Pajek format .net
myG=nx.read_pajek("MyNetwork_0711_onlylabel.net")

print "Finished importing Network Pajek file"

#Simplify graph into networkx format
G=nx.Graph(myG)

print "Finished converting to Networkx format"

#Network info
print "Nodes found: ",G.number_of_nodes()
print "Edges found: ",G.number_of_edges()


#Reading file and storing to array
with open('paired_nodes.csv','rb') as csvfile:
    reader = csv.reader(csvfile, delimiter = ',', quoting=csv.QUOTE_MINIMAL)#, quotechar = '"')
    data = [data for data in reader]
paired_nodes = np.asarray(data)
paired_nodes.astype(int)

print "Finished reading paired nodes file"

#Add extra column in array to store shortest path value
paired_nodes = np.append(paired_nodes,np.zeros([len(paired_nodes),1],dtype=np.int),1)

print "Just appended new column to paired nodes array"

#Get shortest path for every pair of nodes

for index in range(len(paired_nodes)):
    try:
    shortest=nx.shortest_path_length(G,paired_nodes[index,0],paired_nodes[index,1])
        #print shortest
        paired_nodes[index,2] = shortest
    except nx.NetworkXNoPath:
        #print '99999'  #Value to print when no path is found
        paired_nodes[index,2] = 99999

print "Finished calculating shortest path for paired nodes"

#Store results to csv file      
f = open('shortest_path_results.csv','w')

for item in paired_nodes:
    f.write(','.join(map(str,item)))
    f.write('\n')
f.close()

print "Done writing file with results, bye!"

你能解释一下你是如何在networkx中生成图形G的吗。
Suda-t
123
有何关联?请尝试给出一个答案。我认为networkx可以满足您的需要,但问题在于您在何处输入网络。我刚刚编辑了这篇文章,其中包括一个更好的示例,以及我用于导入网络的更多详细信息。非常感谢您的帮助!这很有效,非常感谢!虽然我不需要计算整个网络的最短路径(我的实际网络很大,有600K个节点和6M个边),但我最终要做的是编写一个脚本,将源节点和目标节点作为参数传递给nx.shortest_path_length,并计算每对的长度。嗨-这段代码在networkx v2.x中不再有效。我相信一个修正是
path\u length=dict(nx.all\u pairs\u shortest\u path\u length(G))
print "Importing libraries..."

import networkx as nx
import csv
import numpy as np

#Import network in Pajek format .net
myG=nx.read_pajek("MyNetwork_0711_onlylabel.net")

print "Finished importing Network Pajek file"

#Simplify graph into networkx format
G=nx.Graph(myG)

print "Finished converting to Networkx format"

#Network info
print "Nodes found: ",G.number_of_nodes()
print "Edges found: ",G.number_of_edges()


#Reading file and storing to array
with open('paired_nodes.csv','rb') as csvfile:
    reader = csv.reader(csvfile, delimiter = ',', quoting=csv.QUOTE_MINIMAL)#, quotechar = '"')
    data = [data for data in reader]
paired_nodes = np.asarray(data)
paired_nodes.astype(int)

print "Finished reading paired nodes file"

#Add extra column in array to store shortest path value
paired_nodes = np.append(paired_nodes,np.zeros([len(paired_nodes),1],dtype=np.int),1)

print "Just appended new column to paired nodes array"

#Get shortest path for every pair of nodes

for index in range(len(paired_nodes)):
    try:
    shortest=nx.shortest_path_length(G,paired_nodes[index,0],paired_nodes[index,1])
        #print shortest
        paired_nodes[index,2] = shortest
    except nx.NetworkXNoPath:
        #print '99999'  #Value to print when no path is found
        paired_nodes[index,2] = 99999

print "Finished calculating shortest path for paired nodes"

#Store results to csv file      
f = open('shortest_path_results.csv','w')

for item in paired_nodes:
    f.write(','.join(map(str,item)))
    f.write('\n')
f.close()

print "Done writing file with results, bye!"