DiGraph
=======
>>> from networkx import *
Unit tests for DiGraph Class
----------------------------
In addition to the usual suspects (P1, P2, P3, K1, K2, null, etc.)
we use: P1di, P2di, etc. where Xdi=to_directed(X)
G -- named "test"
-- grown and removed, with nodes A,B,C,...
H -- copy of G with extra int nodes from P3 and K3
>>> from networkx import convert_node_labels_to_integers as cnlti
Some small Graphs
-----------------
>>> null=null_graph()
>>> P1=cnlti(path_graph(1),first_label=1)
>>> P3=cnlti(path_graph(3),first_label=1)
>>> P10=cnlti(path_graph(10),first_label=1)
>>> K1=cnlti(complete_graph(1),first_label=1)
>>> K3=cnlti(complete_graph(3),first_label=1)
>>> K5=cnlti(complete_graph(5),first_label=1)
Some small digraphs
>>> nulldi=null.to_directed()
>>> P1di=P1.to_directed()
>>> P3di=P3.to_directed()
>>> P10di=P10.to_directed()
>>> K1di=K1.to_directed()
>>> K3di=K3.to_directed()
>>> K5di=K5.to_directed()
Name
----
>>> G = DiGraph(name="test")
>>> print G # test of __str__
test
>>> print G.name
test
>>> H= DiGraph()
>>> print H.name
<BLANKLINE>
>>> G2=DiGraph(data={1:[2],2:[1]}, name="test")
>>> print G2.edges()
[(1, 2), (2, 1)]
>>> print G2.name
test
Nodes
-----
>>> G.add_node('A')
>>> G.has_node('A')
True
>>> G.remove_node('A')
>>> G.has_node('A')
False
>>> G.add_nodes_from(list("ABCDEFGHIJKL"))
>>> G.has_node("L")
True
>>> G.remove_nodes_from(['H','I','J','K','L'])
>>> G.add_nodes_from([1,2,3,4])
>>> sorted(G.nodes(),key=str)
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> sorted(G,key=str) # test __iter__
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> 'A' in G # test __contains__
True
>>> len(G) # test __len__
11
>>> G.clear() # test node portion of clear()
>>> G.nodes()
[]
Test add_node and remove_node acting for various nbunch
>>> G.add_node('m')
>>> G.has_node('m')
True
>>> G.add_node('m') # no complaints
>>> G.remove_node('j') # NetworkXError
Traceback (most recent call last):
...
NetworkXError: The node j is not in the digraph.
>>> G.remove_node('m')
>>> G.nodes()
[]
nbunch is a list.
>>> G.add_nodes_from(list("ABCD"))
>>> G.add_nodes_from(P3) # add nbunch of nodes (nbunch=Graph)
>>> sorted(G.nodes(),key=str)
[1, 2, 3, 'A', 'B', 'C', 'D']
>>> G.remove_nodes_from(P3) # remove nbunch of nodes (nbunch=Graph)
>>> sorted(G.nodes(),key=str)
['A', 'B', 'C', 'D']
nbunch is a set
>>> nbunch=set("ABCDEFGHIJKL")
>>> G.add_nodes_from(nbunch)
>>> G.has_node("L")
True
nbunch is a dict with nodes as keys
>>> nbunch={'I':"foo",'J':2,'K':True,'L':"spam"}
>>> G.remove_nodes_from(nbunch)
>>> sorted(G.nodes())
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
nbunch is an iterator
>>> n_iter=P3.nodes_iter()
>>> G.add_nodes_from(n_iter)
>>> sorted(G.nodes())
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
>>> n_iter=P3.nodes_iter() # rebuild same iterator
>>> G.remove_nodes_from(n_iter) # remove nbunch of nodes (nbunch=iterator)
>>> sorted(G.nodes())
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
nbunch is a graph
>>> nbunch=K3
>>> G.add_nodes_from(nbunch)
>>> sorted(G.nodes(),key=str)
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
Edges
-----
>>> G.add_edge('A')
Traceback (most recent call last):
...
TypeError: add_edge() takes at least 3 arguments (2 given)
>>> G.add_edge('A','B') # testing add_edge()
>>> G.add_edge('A','B') # should fail silently
>>> G.has_edge('A','B') # testing has_edge()
True
>>> G.has_edge('A','C')
False
>>> G.has_edge( *('A','B') )
True
>>> G.has_edge('B','A') # G is directed, so B->A is not an edge
False
>>> G.add_edge('A','C') # test directedness
>>> G.add_edge('C','A')
>>> G.remove_edge('C','A')
>>> G.has_edge('A','C') # G is directed
True
>>> G.has_edge('C','A')
False
>>> G.add_edge('A','A') # test self loops
>>> G.has_edge('A','A')
True
>>> G.remove_edge('A','A')
>>> G.add_edge('X','X')
>>> G.has_node('X') # added node but not self loop
True
>>> G.remove_node('X')
>>> G.add_edge('A','Z') # should add the node silently
>>> G.has_node('Z')
True
>>> G.add_edges_from([('B','C')]) # test add_edges_from()
>>> G.has_edge('B','C')
True
>>> G.has_edge('C','B') # directed
False
>>> G.add_edges_from([('D','F'),('B','D')]) # test add_edges_from()
>>> G.has_edge('D','F')
True
>>> G.has_edge('B','D')
True
>>> G.has_edge('D','B') # directed
False
>>> G.add_edges_from([tuple('IJ'),list('KK'),tuple('JK')]) # after failing silently, should add 3rd edge
>>> G.has_edge(*('I','J'))
True
>>> G.has_edge(*('K','K'))
True
>>> G.has_edge(*('J','K'))
True
>>> G.has_edge(*('K','J')) # directed
False
>>> G.add_edges_from(zip(list('ACD'),list('CDE')))
>>> G.has_edge('D','E')
True
>>> G.has_edge('E','C')
False
>>> G.add_edges_from(zip(list('MNOP'),list('NOPM')))
>>> G.has_edge('O','P')
True
>>> G.has_edge('P','M')
True
>>> G.remove_node('P') # tests remove_node()'s handling of edges.
>>> G.has_edge('P','M')
False
>>> G.remove_edge('M') # test remove_edge()
Traceback (most recent call last):
...
TypeError: remove_edge() takes exactly 3 arguments (2 given)
>>> G.add_edge('N','M')
>>> G.has_edge('M','N')
True
>>> G.remove_edge('M','N')
>>> G.has_edge('M','N')
False
>>> G.has_edge('N','M') # directed
True
>>> G.remove_edges_from([list('HI'),list('DF'),tuple('KK'),tuple('JK')]) # self loop fails silently
>>> G.has_edge('H','I')
False
>>> G.has_edge('J','K')
False
>>> G.remove_nodes_from(set('ZEFHIMNO'))
>>> sorted(G.nodes(),key=str)
[1, 2, 3, 'A', 'B', 'C', 'D', 'G', 'J', 'K']
>>> G.remove_nodes_from([1,2,3])
>>> sorted(G.nodes())
['A', 'B', 'C', 'D', 'G', 'J', 'K']
>>> sorted(G.edges())
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
Test G.edges(nbunch) with various forms of nbunch
node not in nbunch should be quietly ignored
>>> sorted(G.edges(6)) # non-iterable non-node
Traceback (most recent call last):
...
NetworkXError: nbunch is not a node or a sequence of nodes.
>>> sorted(G.edges('Z')) # iterable non-node
[]
nbunch can be an empty list
>>> sorted(G.edges([]))
[]
nbunch can be a list
>>> sorted(G.edges(['A','B']))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a set
>>> sorted(G.edges(set(['A','B'])))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a graph
>>> G1=Graph()
>>> G1.add_nodes_from('AB')
>>> sorted(G.edges(G1))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a dict with nodes as keys
>>> ndict={'A': "thing1", 'B': "thing2"}
>>> sorted(G.edges(ndict))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a single node
>>> sorted(G.edges('A'))
[('A', 'B'), ('A', 'C')]
Test G.edges_iter(nbunch) with various forms of nbunch
node not in nbunch should be quietly ignored
>>> sorted(G.edges_iter('Z'))
[]
nbunch can be an empty list
>>> sorted(G.edges_iter([]))
[]
nbunch can be a list
>>> sorted(G.edges_iter(['A','B']))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a set
>>> sorted(G.edges_iter(set(['A','B'])))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a graph
>>> G1=Graph()
>>> G1.add_nodes_from(['A','B'])
>>> sorted(G.edges_iter(G1)) # nbunch is a graph
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a dict with nodes as keys
>>> ndict={'A': "thing1", 'B': "thing2"}
>>> sorted(G.edges_iter(ndict))
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
nbunch can be a single node
>>> sorted(G.edges_iter('A'))
[('A', 'B'), ('A', 'C')]
nbunch can be nothing (whole graph)
>>> sorted(G.edges_iter())
[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
>>> sorted(G.nodes_iter())
['A', 'B', 'C', 'D', 'G', 'J', 'K']
Properties
----------
degree of single node must return single int
>>> G.degree('A')
2
degree of single node in iterable container must return list
>>> G.degree(['A']).values()
[2]
>>> G.degree(['A'])
{'A': 2}
>>> sorted(G.degree(['A','B']).values())
[2, 3]
>>> G.degree(['A','B'])
{'A': 2, 'B': 3}
>>> sorted(G.in_degree().values())
[0, 0, 0, 0, 1, 2,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
networkx-1.2.tar.gz (336个子文件)
iso_r01_s80.A99 1KB
si2_b06_m200.A99 310B
si2_b06_m200.B99 2KB
iso_r01_s80.B99 1KB
chess_masters_WCC.pgn.bz2 98KB
chess_masters_WCC.pgn.bz2 98KB
setup.cfg 80B
lanl_routes.edgelist 19KB
hartford_drug.edgelist 3KB
words_dat.txt.gz 33KB
knuth_miles.txt.gz 20KB
knuth_miles.txt.gz 20KB
roget_dat.txt.gz 15KB
index.html 4KB
gallery.html 3KB
indexsidebar.html 1KB
layout.html 926B
MANIFEST.in 727B
Makefile 4KB
unix_email.mbox 2KB
unix_email.mbox 2KB
PKG-INFO 1KB
art1.png 65KB
atlas.py 209KB
graph.py 52KB
digraph.py 38KB
isomorphvf2.py 37KB
degree_seq.py 34KB
random_graphs.py 31KB
matching.py 31KB
multigraph.py 30KB
threshold.py 28KB
nx_pylab.py 27KB
convert.py 25KB
multidigraph.py 24KB
test_graph.py 19KB
test_betweenness_centrality.py 17KB
weighted.py 16KB
attrmatrix.py 16KB
layout.py 15KB
classic.py 15KB
docscrape.py 15KB
bipartite.py 14KB
nx_agraph.py 14KB
adjlist.py 14KB
clique.py 13KB
mixing.py 13KB
unweighted.py 13KB
operators.py 13KB
small.py 13KB
graphml.py 12KB
test_multidigraph.py 11KB
test_operators.py 10KB
benchmark.py 10KB
edgelist.py 10KB
gml.py 10KB
vf2weighted.py 10KB
directed.py 9KB
utils.py 9KB
pagerank_alg.py 9KB
test_betweenness_centrality_subset.py 9KB
hits_alg.py 9KB
docdiff.py 8KB
strongly_connected.py 8KB
betweenness.py 8KB
test_multigraph.py 8KB
test_digraph.py 8KB
maxflow.py 7KB
test_isomorphvf2.py 7KB
current_flow_betweenness_subset.py 7KB
function.py 7KB
test_mixing_attributes.py 7KB
nx_pydot.py 7KB
betweenness_subset.py 7KB
docscrape_sphinx.py 7KB
test_maxflow.py 7KB
generic.py 7KB
test_mixing_degree.py 7KB
current_flow_betweenness.py 7KB
test_weighted.py 6KB
load.py 6KB
iterated_dynamical_systems.py 6KB
cluster.py 6KB
chess_masters.py 5KB
chess_masters.py 5KB
test_function.py 5KB
test_graphml.py 5KB
make_examples_rst.py 5KB
pajek.py 5KB
conf.py 5KB
eigenvector.py 5KB
depth_first_search.py 5KB
isomorph.py 5KB
test_current_flow_betweenness_centrality_subset.py 5KB
test_unweighted.py 5KB
spectrum.py 5KB
test_generic.py 4KB
dag.py 4KB
sparsegraph6.py 4KB
connected.py 4KB
共 336 条
- 1
- 2
- 3
- 4
资源评论
程序员Chino的日记
- 粉丝: 3674
- 资源: 5万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功