NetworkX in python

268 views Asked by At

I want to define a weighted graph where node ids are the name of persons and links between the nodes are directed. Each edge has a label representing relationship and a trust value which is a real number.

Then, I need to find a path between a pair of users to calculate indirect trust between the two. I have written following code, but I am getting following error:

File "osn_graph.py", line 30, in trust=trust*float(G[list[i]][list[i+1]][trust]) KeyError: 1

#!/usr/bin/python  
import networkx as nx  
G = nx.DiGraph()  
f = open("osndata.txt","rb")  
while 1:  
    line = f.readline()  
    if not line: 
        break  
    src = line.rstrip('\n')  
    G.add_node(src)  
    line = f.readline()  
    dest = line.rstrip('\n')  
    G.add_node(dest)  
    line = f.readline()  
    t = int(line)*0.01  
    line = f.readline()  
    r = line.rstrip('\n')  
    G.add_edge(src, dest, weight=t, relation=r)  
f.close()  
try:  
    list = nx.dijkstra_path(G, "akash", "jitendra")  
    trust = 1  
    i = 0  
    for j in range(0, len(list) - 1):  
       trust = trust * float(G[list[j]][list[j + 1]]['weight'])  
        i += 1  
    print trust  
except nx.NetworkXNoPath:  
    print "There is no path between nodes"   
0

There are 0 answers