~ubuntu-branches/ubuntu/wily/python-networkx/wily

« back to all changes in this revision

Viewing changes to networkx/tests/xbase_PseudoGraph.txt

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-03-02 01:06:32 UTC
  • mfrom: (1.2.1 upstream) (3.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080302010632-1lp6qe1orf59jl8b
* debian/control:
   + Replace python-setuptools with python-pkg-resources in the
     “Recommends:” since pkg_resources is now available in this
     separate package, thanks Matthias Klose (Closes: #468721).
* debian/copyright:
   + Use “© $years $name” instead of invalid “$name, $years” and
     “(C) $years, $name”, thanks to lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
PseudoGraph
2
 
=============
3
 
 
4
 
Unit tests for PseudoGraph version of XGraph in xgraph.py
5
 
---------------------------------------------------------
6
 
 
7
 
 
8
 
In addition to the usual suspects 
9
 
(the simple graphs P1, P2, P3, K1, K2, null, etc.)
10
 
we perform unit tests with the following three PseudoGraphs:
11
 
 
12
 
  - G: PseudoGraph with nodes A,B,C,...
13
 
       named "test",
14
 
       first grown and the deleted
15
 
 
16
 
 
17
 
 
18
 
  - H: copy of G with extra integer nodes from P3 and K3
19
 
 
20
 
  - K: famous Konigsberg graph (from Euler) with nodes: A,B,C,D 
21
 
       named 'Konigsberg'
22
 
      
23
 
       .. class:: doctest-block
24
 
       .. image:: base_PseudoGraph_K.png
25
 
 
26
 
  - Km: Konigsberg graph with named, multiple bridges
27
 
        (m->multiedges=True)
28
 
 
29
 
  - Kms: Km + 2 self-loops at node D
30
 
         (ms->multiedges=True and selfloops=True)
31
 
 
32
 
  - L: PseudoGraph over two nodes 1 and 2 with 3 self-loops on 1
33
 
       and 4 parallel edges between 1 and 2.
34
 
 
35
 
>>> from pprint import *
36
 
 
37
 
>>> from networkx import *
38
 
>>> from networkx.isomorph import graph_could_be_isomorphic
39
 
>>> is_isomorphic=graph_could_be_isomorphic
40
 
>>> from networkx.operators import convert_node_labels_to_integers as cnlti
41
 
 
42
 
Some small graphs
43
 
-----------------
44
 
 
45
 
>>> null=null_graph()
46
 
>>> P1=cnlti(path_graph(1),first_label=1)
47
 
>>> P3=cnlti(path_graph(3),first_label=1)
48
 
>>> P10=cnlti(path_graph(10),first_label=1)
49
 
>>> K1=cnlti(complete_graph(1),first_label=1)
50
 
>>> K3=cnlti(complete_graph(3),first_label=1)
51
 
>>> K5=cnlti(complete_graph(5),first_label=1)
52
 
 
53
 
Name
54
 
----
55
 
 
56
 
>>> G = XGraph(name="test", multiedges=True, selfloops=True)
57
 
>>> print G    # test of __str__
58
 
test
59
 
>>> print G.name
60
 
test
61
 
 
62
 
>>> H=XGraph(multiedges=True, selfloops=True)
63
 
>>> print H.name
64
 
No Name
65
 
 
66
 
The first PseudoGraph
67
 
(cf. http://mathworld.wolfram.com/KoenigsbergBridgeProblem.html)
68
 
 
69
 
>>> K=XGraph(name="Konigsberg", multiedges=True, loops=True) 
70
 
>>> sorted(K) # test empty K.__iter__
71
 
[]
72
 
>>> 'Euler' in K # test __contains__
73
 
False
74
 
>>> len(K) # test K.__len__
75
 
0
76
 
>>> K.__str__() # name
77
 
'Konigsberg'
78
 
 
79
 
 
80
 
Nodes
81
 
-----
82
 
 
83
 
>>> G.add_node('A')
84
 
>>> G.has_node('A')
85
 
True
86
 
>>> G.delete_node('A')
87
 
>>> G.has_node('A')
88
 
False
89
 
>>> G.add_nodes_from(list("ABCDEFGHIJKL"))
90
 
>>> G.has_node("L")
91
 
True
92
 
>>> G.delete_nodes_from(['H','I','J','K','L'])
93
 
>>> G.add_nodes_from([1,2,3,4])
94
 
>>> sorted(G.nodes())
95
 
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
96
 
>>> sorted(G)   # test __iter__
97
 
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
98
 
>>> 'A' in G    # test __contains__
99
 
True
100
 
>>> len(G)      # test __len__
101
 
11
102
 
 
103
 
>>> G.clear()     # test node portion of clear()
104
 
>>> G.nodes()
105
 
[]
106
 
 
107
 
>>> G=XGraph(multiedges=True)
108
 
>>> G.add_edge(1,2,'a')
109
 
>>> G.add_edge(1,2,'b')
110
 
>>> G.add_edge(1,2,'c')
111
 
>>> G.delete_node(1)
112
 
>>> G.nodes()
113
 
[2]
114
 
>>> G.edges()
115
 
[]
116
 
 
117
 
 
118
 
>>> G=XGraph(multiedges=True)
119
 
>>> G.add_edge(1,2,'a')
120
 
>>> G.add_edge(1,2,'b')
121
 
>>> G.add_edge(1,2,'c')
122
 
>>> G.delete_nodes_from([1])
123
 
>>> G.nodes()
124
 
[2]
125
 
>>> G.edges()
126
 
[]
127
 
 
128
 
 
129
 
>>> G=XDiGraph(multiedges=True)
130
 
>>> G.add_edge(1,2,20)
131
 
>>> G.add_edge(1,2,20)
132
 
>>> G.add_edge(1,2,20)
133
 
>>> G.add_edge(2,3,20)
134
 
>>> G.add_edge(2,3,20)
135
 
>>> G.add_edge(2,4,20)
136
 
>>> G.add_edge(2,1,20)
137
 
>>> G.delete_nodes_from([1])
138
 
>>> G.nodes()
139
 
[2, 3, 4]
140
 
>>> G.edges()
141
 
[(2, 3, 20), (2, 3, 20), (2, 4, 20)]
142
 
 
143
 
 
144
 
Test add_node and delete_node acting for various nbunch
145
 
 
146
 
>>> G = XGraph(name="test", multiedges=True, selfloops=True)
147
 
 
148
 
>>> G.add_node('m')
149
 
>>> G.has_node('m')
150
 
True
151
 
>>> G.add_node('m')   # no complaints
152
 
>>> G.delete_node('j') # NetworkXError
153
 
Traceback (most recent call last):
154
 
...
155
 
NetworkXError: node j not in graph
156
 
>>> G.delete_node('m')
157
 
>>> G.nodes()
158
 
[]
159
 
 
160
 
nbunch is a list.
161
 
 
162
 
>>> G.add_nodes_from(list("ABCD")) 
163
 
>>> G.add_nodes_from(P3) # add nbunch of nodes (nbunch=Graph)
164
 
>>> sorted(G.nodes())
165
 
[1, 2, 3, 'A', 'B', 'C', 'D']
166
 
>>> G.delete_nodes_from(P3) # delete nbunch of nodes (nbunch=Graph)
167
 
>>> sorted(G.nodes())
168
 
['A', 'B', 'C', 'D']
169
 
 
170
 
nbunch is a set
171
 
 
172
 
>>> nbunch=set("ABCDEFGHIJKL")
173
 
>>> G.add_nodes_from(nbunch)
174
 
>>> G.has_node("L")
175
 
True
176
 
 
177
 
nbunch is a dict with nodes as keys
178
 
 
179
 
>>> nbunch={'I':"foo",'J':2,'K':True,'L':"spam"}
180
 
>>> G.delete_nodes_from(nbunch)
181
 
>>> sorted(G.nodes())
182
 
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
183
 
 
184
 
nbunch is an iterator
185
 
 
186
 
G.clear()
187
 
>>> n_iter=P3.nodes_iter()
188
 
>>> G.add_nodes_from(n_iter)
189
 
>>> sorted(G.nodes())
190
 
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
191
 
>>> n_iter=P3.nodes_iter() # rebuild same iterator
192
 
>>> G.delete_nodes_from(n_iter) # delete nbunch of nodes (nbunch=iterator)
193
 
>>> sorted(G.nodes())
194
 
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
195
 
 
196
 
nbunch is a graph
197
 
 
198
 
>>> nbunch=K3
199
 
>>> G.add_nodes_from(nbunch)
200
 
>>> sorted(G.nodes())
201
 
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
202
 
 
203
 
 
204
 
Edges
205
 
-----
206
 
 
207
 
>>> G.add_edge('A')
208
 
Traceback (most recent call last):
209
 
...
210
 
ValueError: need more than 1 value to unpack
211
 
 
212
 
>>> G.add_edge('A','B')     # testing add_edge()
213
 
>>> G.add_edge('A','B') # should fail silently
214
 
>>> G.has_edge('A','B')     # testing has_edge()
215
 
True
216
 
>>> G.has_edge('A','C')
217
 
False
218
 
>>> G.has_edge( ('A','B') )
219
 
True
220
 
>>> G.has_edge('B','A') # G is undirected
221
 
True
222
 
>>> G.has_neighbor('A','C')  # same as has_edge
223
 
False
224
 
>>> G.has_neighbor('A','B')  
225
 
True
226
 
 
227
 
>>> G.add_edge('A','C')  # test directedness
228
 
>>> G.add_edge('C','A')  # There should now be two undirected edges
229
 
>>> G.get_edge('A','C')
230
 
[None, None]
231
 
>>> G.delete_edge('C','A') # delete one of the two edges added 
232
 
>>> G.get_edge('A','C') 
233
 
[None]
234
 
>>> G.delete_edge('C','A') # delete second
235
 
>>> G.has_edge('A','C') # G is undirected
236
 
False
237
 
>>> G.has_edge('C','A') 
238
 
False
239
 
 
240
 
 
241
 
>>> G.add_edge('A','C')  # test directedness
242
 
>>> G.add_edge('C','A')  # There should now be two undirected edges
243
 
>>> G.get_edge('A','C')
244
 
[None, None]
245
 
>>> G.delete_multiedge('C','A') # delete all edges
246
 
>>> G.has_edge('A','C') # G is undirected
247
 
False
248
 
>>> G.has_edge('C','A') 
249
 
False
250
 
 
251
 
 
252
 
>>> G.add_edge('A','A') # test self loops
253
 
>>> G.has_edge('A','A')
254
 
True
255
 
 
256
 
>>> G.add_edge('A','Z') # should add the node silently
257
 
>>> G.has_node('Z')
258
 
True
259
 
 
260
 
 
261
 
>>> G.add_edges_from([('B','C')])   # test add_edges_from()
262
 
>>> G.has_edge('B','C')
263
 
True
264
 
>>> G.has_edge('C','B')  # undirected
265
 
True
266
 
>>> G.add_edges_from([('D','F'),('B','D')])
267
 
>>> G.has_edge('D','F')
268
 
True
269
 
>>> G.has_edge('B','D')
270
 
True
271
 
>>> G.has_edge('D','B')  # undirected
272
 
True
273
 
>>> G.add_edges_from([tuple('HI'),tuple('DD'),tuple('IJ'),tuple('JK')])
274
 
>>> G.has_edge(('I','J'))
275
 
True
276
 
>>> G.has_edge(('D','D'))
277
 
True
278
 
>>> G.has_edge(('J','K'))
279
 
True
280
 
>>> G.has_edge(('K','J'))  # undirected
281
 
True
282
 
 
283
 
>>> G.add_path(list('ACDE'))      # test add_path() and add_cycle()
284
 
>>> G.has_edge('D','E')
285
 
True
286
 
>>> G.has_edge('E','C')
287
 
False
288
 
>>> G.add_cycle(list('MNOP'))
289
 
>>> G.has_edge('O','P')
290
 
True
291
 
>>> G.has_edge('P','M')
292
 
True
293
 
>>> G.delete_node('P')    # tests delete_node()'s handling of edges.
294
 
>>> G.has_edge('P','M')
295
 
False
296
 
 
297
 
 
298
 
 
299
 
>>> G.delete_edge('M')       # test delete_edge()
300
 
Traceback (most recent call last):
301
 
...
302
 
ValueError: need more than 1 value to unpack
303
 
>>> G.delete_edge('D','D')  # test self loops
304
 
>>> G.has_edge('D','D')
305
 
False
306
 
>>> G.add_edge('N','M')  
307
 
>>> G.get_edge('M','N')
308
 
[None, None]
309
 
>>> G.delete_multiedge('M','N')  # delete all parallel edges 
310
 
>>> G.has_edge('M','N')
311
 
False
312
 
>>> G.has_edge('N','M')  # undirected
313
 
False
314
 
>>> G.delete_edges_from([list('HI'),list('DF'),tuple('KK'),tuple('JK')]) # self loop fails silently
315
 
>>> G.has_edge('H','I')
316
 
False
317
 
>>> G.has_edge('J','K')
318
 
False
319
 
>>> G.delete_edges_from([list('IJ'),list('KK'),list('JK')])
320
 
>>> G.has_edge('I','J')
321
 
False
322
 
>>> G.delete_nodes_from(list('ZEFHIMNO'))
323
 
>>> sorted(G.nodes())
324
 
[1, 2, 3, 'A', 'B', 'C', 'D', 'G', 'J', 'K']
325
 
>>> G.delete_nodes_from([1,2,3])
326
 
>>> sorted(G.nodes())
327
 
['A', 'B', 'C', 'D', 'G', 'J', 'K']
328
 
 
329
 
pprint(sorted(G.edges()))
330
 
 
331
 
>>> pprint(sorted(G.edges()))
332
 
[('A', 'A', None),
333
 
 ('A', 'B', None),
334
 
 ('A', 'B', None),
335
 
 ('A', 'C', None),
336
 
 ('B', 'D', None),
337
 
 ('C', 'B', None),
338
 
 ('C', 'D', None)]
339
 
 
340
 
Test G.edges(nbunch) with various forms of nbunch
341
 
 
342
 
node not in nbunch should be quietly ignored
343
 
 
344
 
>>> sorted(G.edges(6))    # non-iterable non-node
345
 
Traceback (most recent call last):
346
 
...
347
 
NetworkXError: nbunch is not a node or a sequence of nodes.
348
 
 
349
 
>>> sorted(G.edges('Z'))  # iterable non-node
350
 
[]
351
 
 
352
 
nbunch can be an empty list
353
 
 
354
 
>>> sorted(G.edges([])) 
355
 
[]
356
 
 
357
 
nbunch can be a list
358
 
 
359
 
>>> sorted(G.edges(['A','B']))
360
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
361
 
 
362
 
nbunch can be a set
363
 
 
364
 
>>> sorted(G.edges(set(['A','B'])))
365
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
366
 
 
367
 
nbunch can be a graph
368
 
 
369
 
>>> G1=Graph()
370
 
>>> G1.add_nodes_from('AB')
371
 
>>> sorted(G.edges(G1)) # nbunch is a graph
372
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
373
 
 
374
 
nbunch can be a dict with nodes as keys
375
 
 
376
 
>>> ndict={'A': "thing1", 'B': "thing2"}
377
 
>>> sorted(G.edges(ndict))
378
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
379
 
 
380
 
nbunch can be a single node
381
 
 
382
 
>>> sorted(G.edges('A'))
383
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None)]
384
 
 
385
 
 
386
 
Test G.edges_iter(nbunch) with various forms of nbunch
387
 
 
388
 
node not in nbunch should be quietly ignored
389
 
 
390
 
>>> sorted(G.edges_iter('Z'))
391
 
[]
392
 
 
393
 
nbunch can be an empty list
394
 
 
395
 
>>> sorted(G.edges_iter([])) 
396
 
[]
397
 
 
398
 
nbunch can be a list
399
 
 
400
 
>>> sorted(G.edges_iter(['A','B']))
401
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
402
 
 
403
 
nbunch can be a set
404
 
 
405
 
>>> sorted(G.edges_iter(set(['A','B'])))
406
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
407
 
 
408
 
nbunch can be a graph
409
 
 
410
 
>>> G1=Graph()
411
 
>>> G1.add_nodes_from(['A','B'])
412
 
>>> sorted(G.edges_iter(G1)) # nbunch is a graph
413
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
414
 
 
415
 
nbunch can be a dict with nodes as keys
416
 
 
417
 
>>> ndict={'A': "thing1", 'B': "thing2"}
418
 
>>> sorted(G.edges_iter(ndict))
419
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
420
 
 
421
 
nbunch can be a single node
422
 
 
423
 
>>> sorted(G.edges_iter('A'))
424
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None)]
425
 
 
426
 
>>> sorted(G.edges_iter())
427
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
428
 
 
429
 
nbunch can be nothing (whole graph)
430
 
 
431
 
>>> sorted(G.edges_iter())
432
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
433
 
>>> sorted(G.nodes_iter())
434
 
['A', 'B', 'C', 'D', 'G', 'J', 'K']
435
 
 
436
 
At this stage G should be the following PseudoGraph:
437
 
 
438
 
.. class:: doctest-block
439
 
.. image:: base_PseudoGraph_G.png
440
 
 
441
 
 
442
 
*Now make H*
443
 
 
444
 
>>> H=G.copy()
445
 
>>> H.delete_nodes_from( ['G', 'J', 'K'])
446
 
>>> sorted(H.edges())  # test copy
447
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
448
 
 
449
 
Now grow and shrink H using various forms of nbunch
450
 
 
451
 
>>> H.add_nodes_from(P3) # add nbunch of nodes (nbunch=Graph)
452
 
>>> sorted(H.nodes())
453
 
[1, 2, 3, 'A', 'B', 'C', 'D']
454
 
>>> H.delete_nodes_from(P3) # delete nbunch of nodes (nbunch=Graph)
455
 
>>> sorted(H.nodes())
456
 
['A', 'B', 'C', 'D']
457
 
 
458
 
>>> nbunch=P3.nodes_iter()
459
 
>>> H.add_nodes_from(nbunch) # add nbunch of nodes (nbunch=iterator)
460
 
>>> sorted(H.nodes())
461
 
[1, 2, 3, 'A', 'B', 'C', 'D']
462
 
>>> nbunch=P3.nodes_iter() # rebuild same iterator
463
 
>>> H.delete_nodes_from(nbunch) # delete nbunch of nodes (nbunch=iterator)
464
 
>>> sorted(H.nodes())
465
 
['A', 'B', 'C', 'D']
466
 
 
467
 
>>> nbunch=P3.degree(with_labels=True) # create dict with keys to add to H
468
 
>>> H.add_nodes_from(nbunch) # add nbunch of nodes (nbunch=dict)
469
 
>>> sorted(H.nodes())
470
 
[1, 2, 3, 'A', 'B', 'C', 'D']
471
 
>>> H.delete_nodes_from(nbunch) # delete nbunch of nodes (nbunch=dict)
472
 
>>> sorted(H.nodes())
473
 
['A', 'B', 'C', 'D']
474
 
 
475
 
 
476
 
Build Konigsberg graph using only add_edge
477
 
 
478
 
>>> K.add_edges_from([("A","B"),("A","B")])
479
 
>>> K.add_edges_from([("A","C"),("A","C")])
480
 
>>> K.add_edges_from([("A","D"),("C","D"),("B","D")])
481
 
 
482
 
 
483
 
>>> sorted(K) # test K.__iter__
484
 
['A', 'B', 'C', 'D']
485
 
>>> 'A' in K # test contains
486
 
True
487
 
 
488
 
Build Km, Konigsberg graph with named edges
489
 
 
490
 
>>> Km=XGraph(name="Konigsberg",multiedges=True)
491
 
>>> Km.add_edges_from([("A","B","Honey"),("A","B","Blacksmith's")])
492
 
>>> Km.add_edges_from([("A","C","Green"),("A","C","Connecting")])
493
 
>>> Km.add_edges_from([("A","D","Merchant's"),("C","D","High"),("B","D","Wooden")])
494
 
 
495
 
Build Kms = Km + 2 self-loops at node D
496
 
 
497
 
>>> Kms=XGraph(name="Konigsberg",multiedges=True,selfloops=True)
498
 
>>> Kms.add_edges_from(Km.edges())
499
 
>>> Kms.add_edge(["D","D","D self-loop 1"])
500
 
>>> Kms.add_edge(["D","D","D self-loop 2"])
501
 
 
502
 
 
503
 
Build graph L: 3 self-loops + 4-skein
504
 
 
505
 
>>> L=XGraph(multiedges=True, selfloops=True)
506
 
>>> L.add_edges_from([(1,1),(1,1),(1,1)]) # 3 self-loops at node 1
507
 
>>> L.add_edges_from([(1,2),(1,2),(1,2),(1,2)]) # 4 parallel edges
508
 
>>> sorted(L.edges())
509
 
[(1, 1, None), (1, 1, None), (1, 1, None), (1, 2, None), (1, 2, None), (1, 2, None), (1, 2, None)]
510
 
 
511
 
>>> sorted(L.edges([1,2]))
512
 
[(1, 1, None), (1, 1, None), (1, 1, None), (1, 2, None), (1, 2, None), (1, 2, None), (1, 2, None)]
513
 
 
514
 
 
515
 
 
516
 
Extracting objects imbedded into edges.
517
 
---------------------------------------
518
 
 
519
 
>>> H.clear()
520
 
>>> H.add_edge(1,2,"hi")
521
 
>>> H.get_edge(1,2)
522
 
['hi']
523
 
>>> H.edges(1)
524
 
[(1, 2, 'hi')]
525
 
>>> sorted(H.edges_iter(1))
526
 
[(1, 2, 'hi')]
527
 
>>> sorted(H.nodes())
528
 
[1, 2]
529
 
>>> H.add_edges_from([(1,2,"there"),(1,3,"oof"),(1,1,"loop")])
530
 
>>> sorted(H.edges(1))
531
 
[(1, 1, 'loop'), (1, 2, 'hi'), (1, 2, 'there'), (1, 3, 'oof')]
532
 
>>> sorted(H.edges(2))
533
 
[(2, 1, 'hi'), (2, 1, 'there')]
534
 
>>> sorted(H.edges())
535
 
[(1, 1, 'loop'), (1, 2, 'hi'), (1, 2, 'there'), (1, 3, 'oof')]
536
 
>>> sorted(H.edges_iter(1))
537
 
[(1, 1, 'loop'), (1, 2, 'hi'), (1, 2, 'there'), (1, 3, 'oof')]
538
 
>>> sorted(H.edges_iter(2))
539
 
[(2, 1, 'hi'), (2, 1, 'there')]
540
 
>>> sorted(H.edges_iter())
541
 
[(1, 1, 'loop'), (1, 2, 'hi'), (1, 2, 'there'), (1, 3, 'oof')]
542
 
>>> H.delete_edge(1,1,'ooops')   # should fail silently
543
 
>>> H.delete_edge(1,1,'loop')   
544
 
>>> sorted(H.edges(1))
545
 
[(1, 2, 'hi'), (1, 2, 'there'), (1, 3, 'oof')]
546
 
>>> H.delete_multiedge(1,2) # should delete both edges between 1 and 2 
547
 
>>> sorted(H.edges(1))
548
 
[(1, 3, 'oof')]
549
 
>>> H.add_edges_from([(4,5),(4,6),(3,4)])
550
 
>>> sorted(H.edges(4))
551
 
[(4, 3, None), (4, 5, None), (4, 6, None)]
552
 
>>> H.add_edge(5,6)
553
 
>>> sorted(H.edges(6))
554
 
[(6, 4, None), (6, 5, None)]
555
 
>>> H.add_edges_from([(4,5),(1,1)])
556
 
>>> sorted(H.edges(4))
557
 
[(4, 3, None), (4, 5, None), (4, 5, None), (4, 6, None)]
558
 
>>> sorted(H.edges(1))
559
 
[(1, 1, None), (1, 3, 'oof')]
560
 
>>> sorted(H.nodes())
561
 
[1, 2, 3, 4, 5, 6]
562
 
>>> H.delete_nodes_from([1,2,3,4,5,6])
563
 
>>> sorted(H.edges(1))
564
 
Traceback (most recent call last):
565
 
...
566
 
NetworkXError: nbunch is not a node or a sequence of nodes.
567
 
 
568
 
 
569
 
 
570
 
Properties
571
 
----------
572
 
 
573
 
degree of single node must return single int
574
 
 
575
 
>>> G.degree('A')
576
 
5
577
 
 
578
 
degree of single node in iterable container must return list
579
 
 
580
 
>>> G.degree(['A'])
581
 
[5]
582
 
 
583
 
with_labels=True always return a dict with nodes as keys
584
 
 
585
 
>>> G.degree('A',with_labels=True)
586
 
{'A': 5}
587
 
 
588
 
>>> G.degree(['A','B'])
589
 
[5, 4]
590
 
>>> G.degree(['A','B'],with_labels=True)
591
 
{'A': 5, 'B': 4}
592
 
 
593
 
>>> sorted(G.degree())
594
 
[0, 0, 0, 2, 3, 4, 5]
595
 
>>> sorted(list(G.degree_iter()))
596
 
[0, 0, 0, 2, 3, 4, 5]
597
 
 
598
 
 
599
 
>>> G.order()
600
 
7
601
 
>>> G.size()
602
 
7
603
 
 
604
 
*Back in Konigsberg*
605
 
 
606
 
>>> number_of_nodes(K)
607
 
4
608
 
>>> number_of_edges(K)
609
 
7
610
 
 
611
 
 
612
 
 
613
 
Operations
614
 
-----------
615
 
 
616
 
copy
617
 
---- 
618
 
 
619
 
>>> H=G.copy()       # copy
620
 
>>> H.adj==G.adj
621
 
True
622
 
>>> H.name==G.name
623
 
True
624
 
>>> H==G
625
 
False
626
 
 
627
 
>>> Gsub=G.subgraph(['A','B','D'])     # subgraph
628
 
>>> sorted(Gsub.nodes())
629
 
['A', 'B', 'D']
630
 
>>> sorted(Gsub.edges())
631
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('B', 'D', None)]
632
 
 
633
 
  # to_directed
634
 
>>> Gdir=G.to_directed()
635
 
>>> Gdirsub=Gdir.subgraph(['A','B','D'])
636
 
>>> sorted(Gsub.nodes())
637
 
['A', 'B', 'D']
638
 
>>> sorted(Gsub.edges())
639
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('B', 'D', None)]
640
 
 
641
 
*Back in Konigsberg*
642
 
 
643
 
>>> Ksub=K.subgraph(['A','B'])
644
 
>>> sorted( Ksub.edges() )
645
 
[('A', 'B', None), ('A', 'B', None)]
646
 
 
647
 
Neighbors
648
 
---------
649
 
 
650
 
>>> sorted(G['A'])   # test __getitem__
651
 
['A', 'B', 'B', 'C']
652
 
>>> sorted(G.neighbors('A'))
653
 
['A', 'B', 'B', 'C']
654
 
>>> sorted(G.neighbors_iter('A'))
655
 
['A', 'B', 'B', 'C']
656
 
 
657
 
 
658
 
Functional interface
659
 
--------------------
660
 
 
661
 
>>> sorted(nodes(G))
662
 
['A', 'B', 'C', 'D', 'G', 'J', 'K']
663
 
>>> sorted(edges(G))
664
 
[('A', 'A', None), ('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
665
 
 
666
 
>>> sorted(degree(G))
667
 
[0, 0, 0, 2, 3, 4, 5]
668
 
>>> sorted(neighbors(G,'A'))
669
 
['A', 'B', 'B', 'C']
670
 
>>> number_of_nodes(G)
671
 
7
672
 
>>> number_of_edges(G)
673
 
7
674
 
 
675
 
 
676
 
*Back in Konigsberg*
677
 
 
678
 
>>> sorted(nodes(K))
679
 
['A', 'B', 'C', 'D']
680
 
>>> pprint(sorted(edges(K)))
681
 
[('A', 'B', None),
682
 
 ('A', 'B', None),
683
 
 ('A', 'C', None),
684
 
 ('A', 'C', None),
685
 
 ('A', 'D', None),
686
 
 ('B', 'D', None),
687
 
 ('C', 'D', None)]
688
 
 
689
 
>>> pprint(sorted(edges(Km)))
690
 
[('A', 'B', "Blacksmith's"),
691
 
 ('A', 'B', 'Honey'),
692
 
 ('A', 'C', 'Connecting'),
693
 
 ('A', 'C', 'Green'),
694
 
 ('A', 'D', "Merchant's"),
695
 
 ('B', 'D', 'Wooden'),
696
 
 ('C', 'D', 'High')]
697
 
 
698
 
>>> pprint(sorted(edges(Kms)))
699
 
[('A', 'B', "Blacksmith's"),
700
 
 ('A', 'B', 'Honey'),
701
 
 ('A', 'C', 'Connecting'),
702
 
 ('A', 'C', 'Green'),
703
 
 ('A', 'D', "Merchant's"),
704
 
 ('B', 'D', 'Wooden'),
705
 
 ('C', 'D', 'High'),
706
 
 ('D', 'D', 'D self-loop 1'),
707
 
 ('D', 'D', 'D self-loop 2')]
708
 
 
709
 
 
710
 
Iterators
711
 
---------
712
 
 
713
 
>>> sorted(G.nodes_iter())
714
 
['A', 'B', 'C', 'D', 'G', 'J', 'K']
715
 
>>> pprint(sorted(G.edges_iter()))
716
 
[('A', 'A', None),
717
 
 ('A', 'B', None),
718
 
 ('A', 'B', None),
719
 
 ('A', 'C', None),
720
 
 ('B', 'D', None),
721
 
 ('C', 'B', None),
722
 
 ('C', 'D', None)]
723
 
 
724
 
>>> sorted(G.degree())
725
 
[0, 0, 0, 2, 3, 4, 5]
726
 
 
727
 
>>> sorted(G.degree_iter())
728
 
[0, 0, 0, 2, 3, 4, 5]
729
 
 
730
 
>>> sorted(G.neighbors_iter('A'))
731
 
['A', 'B', 'B', 'C']
732
 
 
733
 
 
734
 
*Back in Konigsberg*
735
 
 
736
 
>>> sorted(K.nodes_iter())
737
 
['A', 'B', 'C', 'D']
738
 
>>> sorted(K.edges_iter())
739
 
[('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('A', 'C', None), ('A', 'D', None), ('B', 'D', None), ('C', 'D', None)]
740
 
 
741
 
>>> sorted(K.degree_iter())
742
 
[3, 3, 3, 5]
743
 
 
744
 
>>> sorted(K.neighbors_iter('A'))
745
 
['B', 'B', 'C', 'C', 'D']
746
 
 
747
 
>>> sorted(K.neighbors_iter('B'))
748
 
['A', 'A', 'D']
749
 
 
750
 
>>> sorted(K.neighbors_iter('C'))
751
 
['A', 'A', 'D']
752
 
 
753
 
>>> sorted(K.neighbors_iter('D'))
754
 
['A', 'B', 'C']
755
 
 
756
 
>>> sorted(K.neighbors_iter('X'))
757
 
Traceback (most recent call last):
758
 
...
759
 
NetworkXError: node X not in graph
760
 
 
761
 
 
762
 
ban_selfloops, clear
763
 
--------------------
764
 
 
765
 
>>> G.ban_selfloops()
766
 
>>> sorted(G.edges())
767
 
[('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
768
 
>>> G.clear()
769
 
>>> number_of_nodes(G)
770
 
0
771
 
 
772
 
*Back in Konigsberg*
773
 
 
774
 
>>> K.ban_selfloops() # no selfloops, so should be unchanged
775
 
>>> sorted(K.edges())
776
 
[('A', 'B', None), ('A', 'B', None), ('A', 'C', None), ('A', 'C', None), ('A', 'D', None), ('B', 'D', None), ('C', 'D', None)]
777
 
>>> K.clear()
778
 
>>> number_of_nodes(K)
779
 
0