easygraph.classes.directed_multigraph module#

class easygraph.classes.directed_multigraph.MultiDiGraph(incoming_graph_data=None, multigraph_input=None, **attr)[source]#

Bases: MultiGraph, DiGraph

Attributes:
A

Return the adjacency matrix \(\mathbf{A}\) of the sample graph with torch.sparse_coo_tensor format.

D_v

Return the diagonal matrix of vertex degree \(\mathbf{D}_v\) with torch.sparse_coo_tensor format.

D_v_neg_1_2

Return the normalized diagonal matrix of vertex degree \(\mathbf{D}_v^{-\frac{1}{2}}\) with torch.sparse_coo_tensor format.

L_GCN

Return the GCN Laplacian matrix \(\mathcal{L}_{GCN}\) of the graph with torch.sparse_coo_tensor format.

adj

Return the adjacency matrix

degree

Returns the weighted degree of each node, i.e.

e

Return the edge list, weight list and property list in the graph.

e_both_side

Return the list of edges including both directions.

edges

Return an edge list

in_degree

Returns the weighted in degree of each node.

in_edges
index2node

Assign an integer index for each node (start from 0)

name

String identifier of the graph.

ndata
node_index

Assign an integer index for each node (start from 0)

nodes

return [node for node in self._node]

out_degree

Returns the weighted out degree of each node.

out_edges

Return an edge list

pred

Return the pred of each node

Methods

add_edge(u_for_edge, v_for_edge[, key])

Add an edge between u and v.

add_edges(edges_for_adding[, edges_attr])

Add a list of edges.

add_edges_from(ebunch_to_add, **attr)

Add all the edges in ebunch_to_add.

add_edges_from_file(file[, weighted])

Added edges from file For example, txt files,

add_extra_selfloop()

Add extra selfloops to the graph.

add_node(node_for_adding, **node_attr)

Add one node

add_nodes(nodes_for_adding[, nodes_attr])

Add nodes with a list of nodes.

add_nodes_from(nodes_for_adding, **attr)

Add multiple nodes.

add_weighted_edge(u_of_edge, v_of_edge, weight)

Add a weighted edge

add_weighted_edges_from(ebunch_to_add[, weight])

Add weighted edges in ebunch_to_add with specified weight attr

adjlist_inner_dict_factory

alias of dict

adjlist_outer_dict_factory

alias of dict

all_neighbors(node)

Returns an iterator of a node's neighbors, including both successors and predecessors.

clone()

Clone the graph.

copy()

Returns a copy of the graph.

edge_attr_dict_factory

alias of dict

edge_key_dict_factory

alias of dict

ego_subgraph(center)

Returns an ego network graph of a node.

get_edge_data(u, v[, key, default])

Returns the attribute dictionary associated with edge (u, v).

gnn_data_dict_factory

alias of dict

graph_attr_dict_factory

alias of dict

has_edge(u, v[, key])

Returns True if the graph has an edge between nodes u and v.

has_node(node)

Returns whether a node exists

is_directed()

Returns True if graph is directed, False otherwise.

is_multigraph()

Returns True if graph is a multigraph, False otherwise.

nbr_v(v_idx)

Return a vertex list of the neighbors of the vertex v_idx.

nbunch_iter([nbunch])

Returns an iterator over nodes contained in nbunch that are also in the graph.

neighbors(node)

Returns an iterator of a node's neighbors (successors).

new_edge_key(u, v)

Returns an unused key for edges between nodes u and v.

node_attr_dict_factory

alias of dict

node_dict_factory

alias of dict

node_index_dict

alias of dict

nodes_subgraph(from_nodes)

Returns a subgraph of some nodes

number_of_edges([u, v])

Returns the number of edges between two nodes.

number_of_nodes()

Returns the number of nodes.

order()

Returns the number of nodes in the graph.

predecessors(node)

Returns an iterator of a node's neighbors (predecessors).

raw_selfloop_dict

alias of dict

remove_edge(u, v[, key])

Remove an edge between u and v.

remove_edges(edges_to_remove)

Remove a list of edges from your graph.

remove_edges_from(ebunch)

Remove all edges specified in ebunch.

remove_extra_selfloop()

Remove extra selfloops from the graph.

remove_node(node_to_remove)

Remove one node from your graph.

remove_nodes(nodes_to_remove)

Remove nodes from your graph.

remove_nodes_from(nodes)

Remove multiple nodes.

remove_selfloop()

Remove all selfloops from the graph.

reverse([copy])

Returns the reverse of the graph.

size([weight])

Returns the number of edges or total of all edge weights.

smoothing_with_GCN(X[, drop_rate])

Return the smoothed feature matrix with GCN Laplacian matrix \(\mathcal{L}_{GCN}\).

successors(node)

Returns an iterator of a node's neighbors (successors).

to_directed()

Returns a directed representation of the graph.

to_directed_class()

Returns the class to use for empty directed copies.

to_index_node_graph([begin_index])

Returns a deep copy of graph, with each node switched to its index.

to_undirected([reciprocal])

Returns an undirected representation of the multidigraph.

N_v

cpp

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

out_degree, in_degree

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

out_degree, degree

Examples

>>> G.in_degree(weight='weight')
property in_edges#
is_directed()[source]#

Returns True if graph is directed, False otherwise.

is_multigraph()[source]#

Returns True if graph is a multigraph, False otherwise.

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.

See also

in_degree, degree

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:

MultiGraph

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)]