easygraph.classes.directed_multigraph module#
- class easygraph.classes.directed_multigraph.MultiDiGraph(incoming_graph_data=None, multigraph_input=None, **attr)[source]#
Bases:
MultiGraph
,DiGraph
- add_edge(u_for_edge, v_for_edge, key=None, **attr)[source]#
Add an edge between u and v.
The nodes u and v will be automatically added if they are not already in the graph.
Edge attributes can be specified with keywords or by directly accessing the edge’s attribute dictionary. See examples below.
- Parameters:
u_for_edge (nodes) – Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.
v_for_edge (nodes) – Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.
key (hashable identifier, optional (default=lowest unused integer)) – Used to distinguish multiedges between a pair of nodes.
attr (keyword arguments, optional) – Edge data (or labels or objects) can be assigned using keyword arguments.
- Return type:
The edge key assigned to the edge.
See also
add_edges_from
add a collection of edges
Notes
To replace/update edge data, use the optional key argument to identify a unique edge. Otherwise a new edge will be created.
EasyGraph algorithms designed for weighted graphs cannot use multigraphs directly because it is not clear how to handle multiedge weights. Convert to Graph using edge attribute ‘weight’ to enable weighted graph algorithms.
Default keys are generated using the method new_edge_key(). This method can be overridden by subclassing the base class and providing a custom new_edge_key() method.
Examples
The following all add the edge e=(1, 2) to graph G:
>>> G = eg.MultiDiGraph() >>> e = (1, 2) >>> key = G.add_edge(1, 2) # explicit two-node form >>> G.add_edge(*e) # single edge as tuple of two nodes 1 >>> G.add_edges_from([(1, 2)]) # add edges from iterable container [2]
Associate data to edges using keywords:
>>> key = G.add_edge(1, 2, weight=3) >>> key = G.add_edge(1, 2, key=0, weight=4) # update data for key=0 >>> key = G.add_edge(1, 3, weight=7, capacity=15, length=342.7)
For non-string attribute keys, use subscript notation.
>>> ekey = G.add_edge(1, 2) >>> G[1][2][0].update({0: 5}) >>> G.edges[1, 2, 0].update({0: 5})
>>> >>>
- property degree#
Returns the weighted degree of each node, i.e. sum of out/in degree.
- Parameters:
weight (string, optional (default : 'weight')) – Weight key of the original weighted graph.
- Returns:
degree – Each node’s (key) weighted in degree (value). For directed graph, it returns the sum of out degree and in degree.
- Return type:
dict
Notes
If the graph is not weighted, all the weights will be regarded as 1.
See also
Examples
>>> G.degree() >>> G.degree(weight='weight')
or you can customize the weight key
>>> G.degree(weight='weight_1')
- edge_key_dict_factory#
alias of
dict
- property edges#
Return an edge list
- property in_degree#
Returns the weighted in degree of each node.
- Parameters:
weight (string, optional (default : 'weight')) – Weight key of the original weighted graph.
- Returns:
in_degree – Each node’s (key) weighted in degree (value).
- Return type:
dict
Notes
If the graph is not weighted, all the weights will be regarded as 1.
See also
Examples
>>> G.in_degree(weight='weight')
- property in_edges#
- property out_degree#
Returns the weighted out degree of each node.
- Parameters:
weight (string, optional (default : 'weight')) – Weight key of the original weighted graph.
- Returns:
out_degree – Each node’s (key) weighted out degree (value).
- Return type:
dict
Notes
If the graph is not weighted, all the weights will be regarded as 1.
Examples
>>> G.out_degree(weight='weight')
- property out_edges#
Return an edge list
- remove_edge(u, v, key=None)[source]#
Remove an edge between u and v.
- Parameters:
u (nodes) – Remove an edge between nodes u and v.
v (nodes) – Remove an edge between nodes u and v.
key (hashable identifier, optional (default=None)) – Used to distinguish multiple edges between a pair of nodes. If None remove a single (arbitrary) edge between u and v.
- Raises:
EasyGraphError – If there is not an edge between u and v, or if there is no edge with the specified key.
See also
remove_edges_from
remove a collection of edges
Examples
>>> G = eg.MultiDiGraph() >>> G.add_edges_from([(1, 2), (1, 2), (1, 2)]) # key_list returned [0, 1, 2] >>> G.remove_edge(1, 2) # remove a single (arbitrary) edge
For edges with keys
>>> G = eg.MultiDiGraph() >>> G.add_edge(1, 2, key="first") 'first' >>> G.add_edge(1, 2, key="second") 'second' >>> G.remove_edge(1, 2, key="second")
- reverse(copy=True)[source]#
Returns the reverse of the graph.
The reverse is a graph with the same nodes and edges but with the directions of the edges reversed.
- Parameters:
copy (bool optional (default=True)) – If True, return a new DiGraph holding the reversed edges. If False, the reverse graph is created using a view of the original graph.
- to_undirected(reciprocal=False)[source]#
Returns an undirected representation of the multidigraph.
- Parameters:
reciprocal (bool (optional)) – If True only keep edges that appear in both directions in the original digraph.
- Returns:
G – An undirected graph with the same name and nodes and with edge (u, v, data) if either (u, v, data) or (v, u, data) is in the digraph. If both edges exist in digraph and their edge data is different, only one edge is created with an arbitrary choice of which edge data to use. You must check and correct for this manually if desired.
- Return type:
See also
MultiGraph
,add_edge
,add_edges_from
Notes
This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.
This is in contrast to the similar D=MultiDiGraph(G) which returns a shallow copy of the data.
See the Python copy module for more information on shallow and deep copies, https://docs.python.org/3/library/copy.html.
Warning: If you have subclassed MultiDiGraph to use dict-like objects in the data structure, those changes do not transfer to the MultiGraph created by this method.
Examples
>>> G = eg.path_graph(2) # or MultiGraph, etc >>> H = G.to_directed() >>> list(H.edges) [(0, 1), (1, 0)] >>> G2 = H.to_undirected() >>> list(G2.edges) [(0, 1)]