networkx--基础(转)

1 # Filename: youxiangtu.py
 2 
 3 # NetworkX
 4 import networkx as nx
 5 
 6 # establish a null undirected graphy
 7 G = nx.Graph()  # directed graphy: G = nx.DiGraph()
 8 # add node1
 9 G.add_node(1)
10 # add edge2-3(also add node2 node3)
11 G.add_edge(2, 3)
12 # add edge3-2 
13 G.add_edge(3, 2) # undirected graphy, edge2-3 & edge3-2 is the same
14 
15 print G.nodes()  # output all nodes: [1, 2, 3]
16 print G.edges()  # output all edges: [(2, 3)]
17 print G.number_of_edges()  # output the count of edges: 1
18 
19 # weighted graphy
20 # add edge0-1, edge1-2, which weighted 3.0 & 7.5
21 G.add_weighted_edges_from([(0, 1, 3.0), (1, 2, 7.5)])
22 # output the weight of edge1-2: {'weght': 7.5}
23 print G.get_edge_data(1, 2)
24 
25 # calculate the shortest pathes for the graphy G
26 path = nx.all_pairs_shortest_path(G)
27 print path[0][2]  # output the shortest path between node0 & node2: [0,1, 2]

原文:http://www.cnblogs.com/forstudy/archive/2012/03/19/2406911.html

转载于:https://www.cnblogs.com/Maxnorm/p/6025595.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值