Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / classes / ordered.py @ 5cef0f13

History | View | Annotate | Download (2.64 KB)

1
"""
2
Consistently ordered variants of the default base classes.
3
Note that if you are using Python 3.6+, you shouldn't need these classes
4
because the dicts in Python 3.6+ are ordered.
5
Note also that there are many differing expectations for the word "ordered"
6
and that these classes may not provide the order you expect.
7
The intent here is to give a consistent order not a particular order.
8

9
The Ordered (Di/Multi/MultiDi) Graphs give a consistent order for reporting of
10
nodes and edges.  The order of node reporting agrees with node adding, but for
11
edges, the order is not necessarily the order that the edges were added.
12

13
In general, you should use the default (i.e., unordered) graph classes.
14
However, there are times (e.g., when testing) when you may need the
15
order preserved.
16

17
Special care is required when using subgraphs of the Ordered classes.
18
The order of nodes in the subclass is not necessarily the same order
19
as the original class.  In general it is probably better to avoid using
20
subgraphs and replace with code similar to:
21

22
    # instead of SG = G.subgraph(ordered_nodes)
23
    SG=nx.OrderedGraph()
24
    SG.add_nodes_from(ordered_nodes)
25
    SG.add_edges_from((u, v) for (u, v) in G.edges() if u in SG if v in SG)
26

27
"""
28
from collections import OrderedDict
29

    
30
from .graph import Graph
31
from .multigraph import MultiGraph
32
from .digraph import DiGraph
33
from .multidigraph import MultiDiGraph
34

    
35
__all__ = []
36

    
37
__all__.extend([
38
    'OrderedGraph',
39
    'OrderedDiGraph',
40
    'OrderedMultiGraph',
41
    'OrderedMultiDiGraph',
42
])
43

    
44

    
45
class OrderedGraph(Graph):
46
    """Consistently ordered variant of :class:`~networkx.Graph`."""
47
    node_dict_factory = OrderedDict
48
    adjlist_outer_dict_factory = OrderedDict
49
    adjlist_inner_dict_factory = OrderedDict
50
    edge_attr_dict_factory = OrderedDict
51

    
52

    
53
class OrderedDiGraph(DiGraph):
54
    """Consistently ordered variant of :class:`~networkx.DiGraph`."""
55
    node_dict_factory = OrderedDict
56
    adjlist_outer_dict_factory = OrderedDict
57
    adjlist_inner_dict_factory = OrderedDict
58
    edge_attr_dict_factory = OrderedDict
59

    
60

    
61
class OrderedMultiGraph(MultiGraph):
62
    """Consistently ordered variant of :class:`~networkx.MultiGraph`."""
63
    node_dict_factory = OrderedDict
64
    adjlist_outer_dict_factory = OrderedDict
65
    adjlist_inner_dict_factory = OrderedDict
66
    edge_key_dict_factory = OrderedDict
67
    edge_attr_dict_factory = OrderedDict
68

    
69

    
70
class OrderedMultiDiGraph(MultiDiGraph):
71
    """Consistently ordered variant of :class:`~networkx.MultiDiGraph`."""
72
    node_dict_factory = OrderedDict
73
    adjlist_outer_dict_factory = OrderedDict
74
    adjlist_inner_dict_factory = OrderedDict
75
    edge_key_dict_factory = OrderedDict
76
    edge_attr_dict_factory = OrderedDict