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

« back to all changes in this revision

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