File: api_1.0.rst

package info (click to toggle)
python-networkx 1.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,052 kB
  • ctags: 3,986
  • sloc: python: 52,132; makefile: 176
file content (300 lines) | stat: -rw-r--r-- 8,364 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
*********************************
Version 1.0 notes and API changes
*********************************

We have made some significant API changes, detailed below, to add
functionality and clarity.  This page reflects changes from
networkx-0.99 to networkx-1.0.  For changes from earlier versions to
networkx-0.99 see :doc:`Version 0.99 API changes <api_0.99>`.

Version 1.0 requires Python 2.4 or greater.

Please send comments and questions to the networkx-discuss mailing list:
http://groups.google.com/group/networkx-discuss .


Version numbering
=================

In the future we will use a more standard release numbering system
with major.minor[build] labels where major and minor are numbers and
[build] is a label such as "dev1379" to indicate a development version
or "rc1" to indicate a release candidate.

We plan on sticking closer to a time-based release schedule with smaller
incremental changes released on a roughly quarterly basis.  The graph
classes API will remain fixed, unless we determine there are serious
bugs or other defects in the existing classes, until networkx-2.0 is
released at some time in the future.

Changes in base classes
=======================

The most significant changes in are in the graph classes.  All of the
graph classes now allow optional graph, node, and edge attributes.  Those
attributes are stored internally in the graph classes as dictionaries
and can be accessed simply like Python dictionaries in most cases.

Graph attributes
----------------
Each graph keeps a dictionary of key=value attributes
in the member G.graph.  These attributes can be accessed
directly using G.graph or added at instantiation using 
keyword arguments.

>>> G=nx.Graph(region='Africa')
>>> G.graph['color']='green'
>>> G.graph
{'color': 'green', 'region': 'Africa'}


Node attributes
---------------
Each node has a corresponding dictionary of attributes.
Adding attributes to nodes is optional.

Add node attributes using add_node(), add_nodes_from() or G.node

>>> G.add_node(1, time='5pm')
>>> G.add_nodes_from([3], time='2pm')
>>> G.node[1]
{'time': '5pm'}
>>> G.node[1]['room'] = 714
>>> G.nodes(data=True)
[(1, {'room': 714, 'time': '5pm'}), (3, {'time': '2pm'})]


Edge attributes
---------------
Each edge has a corresponding dictionary of attributes.
The default edge data is now an empty dictionary of attributes   
and adding attributes to edges is optional.

A common use case is to add a weight attribute to an edge:

>>> G.add_edge(1,2,weight=3.14159)

Add edge attributes using add_edge(), add_edges_from(), subscript
notation, or G.edge.

>>> G.add_edge(1, 2, weight=4.7 )
>>> G.add_edges_from([(3,4),(4,5)], color='red')
>>> G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])
>>> G[1][2]['weight'] = 4.7
>>> G.edge[1][2]['weight'] = 4



Methods changed
---------------

Graph(), DiGraph(), MultiGraph(), MultiDiGraph()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   Now takes optional keyword=value attributes on initialization.

   >>> G=nx.Graph(year='2009',city='New York')

add_node()
^^^^^^^^^^
   Now takes optional keyword=value attributes or a dictionary of attributes.

   >>> G.add_node(1,room=714)


add_nodes_from()
^^^^^^^^^^^^^^^^	
   Now takes optional keyword=value attributes or a dictionary of 
   attributes applied to all affected nodes.

   >>> G.add_nodes_from([1,2],time='2pm')  # all nodes have same attribute 

add_edge()
^^^^^^^^^^
   Now takes optional keyword=value attributes or a dictionary of attributes.

   >>> G.add_edge(1, 2, weight=4.7 )

add_edges_from()
^^^^^^^^^^^^^^^^	
   Now takes optional keyword=value attributes or a dictionary of 
   attributes applied to all affected edges.

   >>> G.add_edges_from([(3,4),(4,5)], color='red')
   >>> G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])


nodes() and nodes_iter()
^^^^^^^^^^^^^^^^^^^^^^^^
   New keyword data=True|False keyword determines whether to return
   two-tuples (n,dict) (True) with node attribution dictionary

   >>> G=nx.Graph([(1,2),(3,4)])
   >>> G.nodes(data=True)
   [(1, {}), (2, {}), (3, {}), (4, {})]

copy()
^^^^^^
   Now returns a deep copy of the graph (copies all underlying
   data and attributes for nodes and edges).  Use the class
   initializer to make a shallow copy:

   >>> G=nx.Graph()
   >>> G_shallow=nx.Graph(G) # shallow copy
   >>> G_deep=G.copy() # deep copy

to_directed(), to_undirected()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   Now returns a deep copy of the graph (copies all underlying
   data and attributes for nodes and edges).  Use the class
   initializer to make a shallow copy:

   >>> G=nx.Graph()
   >>> D_shallow=nx.DiGraph(G) # shallow copy
   >>> D_deep=G.to_directed() # deep copy

subgraph()
^^^^^^^^^^

   With copy=True now returns a deep copy of the graph 
   (copies all underlying data and attributes for nodes and edges).

   >>> G=nx.Graph()
   >>> # note: copy keyword deprecated in networkx>1.0
   >>> # H=G.subgraph([],copy=True) # deep copy of all data
   




add_cycle(), add_path(), add_star()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   Now take optional keyword=value attributes or a dictionary of 
   attributes which are applied to all edges affected by the method.

   >>> G=nx.Graph()
   >>> G.add_path([0,1,2,3],width=3.2)


Methods removed
---------------

delete_node()
^^^^^^^^^^^^^
   The preferred name is now remove_node().        


delete_nodes_from()
^^^^^^^^^^^^^^^^^^^
   No longer raises an exception on an attempt to delete a node not in
   the graph.  The preferred name is now remove_nodes_from().


delete_edge()
^^^^^^^^^^^^^
   Now raises an exception on an attempt to delete an edge not in the graph.
   The preferred name is now remove_edge().


delete_edges_from()
^^^^^^^^^^^^^^^^^^^
   The preferred name is now remove_edges_from().

has_neighbor():

   Use has_edge()  

get_edge()
^^^^^^^^^^
   Renamed to get_edge_data().	Returns the edge attribute dictionary.

   The fastest way to get edge data for edge (u,v) is to use G[u][v]
   instead of G.get_edge_data(u,v)


Members removed
---------------

directed, multigraph, weighted
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Use methods G.is_directed() and G.is_multigraph().
    All graphs are weighted graphs now if they have numeric
    values in the 'weight' edge attribute.


Methods added
-------------

add_weighted edges_from()
^^^^^^^^^^^^^^^^^^^^^^^^^ 
   Convenience method to add weighted edges to graph using a list of
   3-tuples (u,v,weight).

get_edge_data()
^^^^^^^^^^^^^^^
   Renamed from get_edge().	

   The fastest way to get edge data for edge (u,v) is to use G[u][v]
   instead of G.get_edge_data(u,v)

is_directed()
^^^^^^^^^^^^^
    replaces member G.directed

is_multigraph()
^^^^^^^^^^^^^^^
    replaces member G.multigraph



Classes Removed
---------------

LabeledGraph, LabeledDiGraph
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    These classes have been folded into the regular classes.

UbiGraph
^^^^^^^^
    Removed as the ubigraph platform is no longer being supported.


Additional functions/generators
===============================

ego_graph, stochastic_graph, PageRank algorithm, HITS algorithm, 
GraphML writer, freeze, is_frozen, A* algorithm, 
directed scale-free generator, random clustered graph.


Converting your existing code to networkx-1.0
=============================================

Weighted edges
--------------

Edge information is now stored in an attribution dictionary
so all edge data must be given a key to identify it.  

There is currently only one standard/reserved key, 'weight', which is
used by algorithms and functions that use weighted edges.  The
associated value should be numeric.  All other keys are available for
users to assign as needed.

>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=3.1415) # add the edge 1-2 with a weight
>>> G[1][2]['weight']=2.3 # set the weight to 2.3

Similarly, for direct access the edge data, use 
the key of the edge data to retrieve it.

>>> w = G[1][2]['weight']

All NetworkX algorithms that require/use weighted edges now use the
'weight' edge attribute.  If you have existing algorithms that assumed
the edge data was numeric, you should replace G[u][v] and
G.get_edge(u,v) with G[u][v]['weight'].

An idiom for getting a weight for graphs with or without an assigned
weight key is

>>> w= G[1][2].get('weight',1)  # set w to 1 if there is no 'weight' key