~ubuntu-branches/ubuntu/saucy/python-networkx/saucy

« back to all changes in this revision

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