码迷,mamicode.com
首页 > Web开发 > 详细

Networkx study notes(0)

时间:2019-11-18 12:41:14      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:dir   doc   result   tween   bec   table   access   replace   report   

 

  

Networkx Class

Type

Self-loops allowed

Parallel edges allowed

Graph

undirected

Yes

No

DiGraph

directed

Yes

No

MultiGraph

undirected

Yes

Yes

MultiDiGraph

directed

Yes

Yes

  

  There are two methods to grow the Graph :

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_node(1, features = 10)
G.add_node(1, features = 20)
G.add_node(2, features = ‘bobo‘)

nodes = [(3, {‘features‘ : ‘aoao‘}), (4, {‘features‘ : 9})]
G.add_nodes_from(nodes)

G.nodes[1][‘feature‘] = ‘nice‘
print(G.nodes)
print(G.nodes(data = True))
#result
#[1, 2, 3, 4]
#[(1, {‘features‘: ‘nice‘}), (2, {‘features‘: ‘bobo‘}), (3, {‘features‘: ‘aoao‘}), (4, {‘features‘:9})]

 

 

G.add_edge(0, 1, distance = 100)
G.add_edge(0, 1, distance = 200)

edges = [(1, 3, {‘distance‘: 142}), (2, 3, {‘distance‘: 19})]
G.add_edges_from(edges)

G.edges[0, 1][‘distance‘] = 999
G[0][1][‘distance‘] = 1999

print(G.edges)
print(G.edges(data = True))
#result
#[(1, 0), (1, 3), (2, 3)]
#[(1, 0, {‘distance‘: 1999}), (1, 3, {‘distance‘: 142}), (2, 3, {‘distance‘: 19})]

 

 

   the other useful methods to report something :(neighbors and adj are not useful almostly,because we could access the Graph index directly)

for n in G.neighbors(1):
    print(n)
for n in G[1]:
    print(n)
for nbr, datadict in G.adj[2].items():
    print(nbr, datadict)
for nbr, datadict in G[2].items():
    print(nbr, datadict)
print(G.has_node(0))
print(G.has_node(5))
print(G.has_edge(0,1))
print(G.has_edge(2,1))
#0
#3
#0
#3
#3 {‘distance‘: 19}
#3 {‘distance‘: 19}
#True
#False
#True
#False

 

 

   how to use the data of the attributes of nodes and edges :

for node_index, node_feature in G.nodes(data=True):
    print(node_index, node_feature)
for receiver, sender, features in G.edges(data=True):
    print(receiver, sender, features)
1 {‘features‘: ‘nice‘}
2 {‘features‘: ‘bobo‘}
3 {‘features‘: ‘aoao‘}
4 {‘features‘: 9}
0 {}
1 0 {‘distance‘: 1999}
1 3 {‘distance‘: 142}
2 3 {‘distance‘: 19}

 

 

  there are two methods to get a directed graph like G, they have the same nodes,name ,but with each edge (u, v, data) replaced by two directed edges (u, v, data) and (v, u, data), the difference between of them is that ‘.to_directed()‘ is a deep copy , nx.DiGraph  is a shallow copy         

diG = G.to_directed()
for receiver, sender, features in diG.edges(data=True):
    print(receiver, sender, features)
diG = nx.DiGraph(G)
1 0 {‘distance‘: 1999}
1 3 {‘distance‘: 142}
2 3 {‘distance‘: 19}
3 1 {‘distance‘: 142}
3 2 {‘distance‘: 19}
0 1 {‘distance‘: 1999}

 

 

Networkx study notes(0)

标签:dir   doc   result   tween   bec   table   access   replace   report   

原文地址:https://www.cnblogs.com/bobo-aoao/p/11881228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!