easygraph.classes.operation module#
- easygraph.classes.operation.add_path(G_to_add_to, nodes_for_path, **attr)[source]#
Add a path to the Graph G_to_add_to.
- Parameters:
G_to_add_to (graph) – A EasyGraph graph
nodes_for_path (iterable container) – A container of nodes. A path will be constructed from the nodes (in order) and added to the graph.
attr (keyword arguments, optional (default= no attributes)) – Attributes to add to every edge in path.
See also
add_star
,add_cycle
Examples
>>> G = eg.Graph() >>> eg.add_path(G, [0, 1, 2, 3]) >>> eg.add_path(G, [10, 11, 12], weight=7)
- easygraph.classes.operation.density(G)[source]#
Returns the density of a graph.
The density for undirected graphs is
\[d = \frac{2m}{n(n-1)},\]and for directed graphs is
\[d = \frac{m}{n(n-1)},\]where n is the number of nodes and m is the number of edges in G.
Notes
The density is 0 for a graph without edges and 1 for a complete graph. The density of multigraphs can be higher than 1.
Self loops are counted in the total number of edges so graphs with self loops can have density higher than 1.
- easygraph.classes.operation.number_of_selfloops(G)[source]#
Returns the number of selfloop edges.
A selfloop edge has the same node at both ends.
- Returns:
nloops – The number of selfloops.
- Return type:
int
See also
nodes_with_selfloops
,selfloop_edges
Examples
>>> G = eg.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc >>> G.add_edge(1, 1) >>> G.add_edge(1, 2) >>> eg.number_of_selfloops(G) 1
- easygraph.classes.operation.selfloop_edges(G, data=False, keys=False, default=None)[source]#
Returns an iterator over selfloop edges.
A selfloop edge has the same node at both ends.
- Parameters:
G (graph) – A EasyGraph graph.
data (string or bool, optional (default=False)) – Return selfloop edges as two tuples (u, v) (data=False) or three-tuples (u, v, datadict) (data=True) or three-tuples (u, v, datavalue) (data=’attrname’)
keys (bool, optional (default=False)) – If True, return edge keys with each edge.
default (value, optional (default=None)) – Value used for edges that don’t have the requested attribute. Only relevant if data is not True or False.
- Returns:
edgeiter – An iterator over all selfloop edges.
- Return type:
iterator over edge tuples
See also
nodes_with_selfloops
,number_of_selfloops
Examples
>>> G = eg.MultiGraph() # or Graph, DiGraph, MultiDiGraph, etc >>> ekey = G.add_edge(1, 1) >>> ekey = G.add_edge(1, 2) >>> list(eg.selfloop_edges(G)) [(1, 1)] >>> list(eg.selfloop_edges(G, data=True)) [(1, 1, {})] >>> list(eg.selfloop_edges(G, keys=True)) [(1, 1, 0)] >>> list(eg.selfloop_edges(G, keys=True, data=True)) [(1, 1, 0, {})]
- easygraph.classes.operation.set_edge_attributes(G, values, name=None)[source]#
Sets edge attributes from a given value or dictionary of values.
Warning
The call order of arguments values and name switched between v1.x & v2.x.
- Parameters:
G (EasyGraph Graph) –
values (scalar value, dict-like) –
What the edge attribute should be set to. If values is not a dictionary, then it is treated as a single attribute value that is then applied to every edge in G. This means that if you provide a mutable object, like a list, updates to that object will be reflected in the edge attribute for each edge. The attribute name will be name.
If values is a dict or a dict of dict, it should be keyed by edge tuple to either an attribute value or a dict of attribute key/value pairs used to update the edge’s attributes. For multigraphs, the edge tuples must be of the form
(u, v, key)
, where u and v are nodes and key is the edge key. For non-multigraphs, the keys must be tuples of the form(u, v)
.name (string (optional, default=None)) – Name of the edge attribute to set if values is a scalar.
Examples
After computing some property of the edges of a graph, you may want to assign a edge attribute to store the value of that property for each edge:
>>> G = eg.path_graph(3) >>> bb = eg.edge_betweenness_centrality(G, normalized=False) >>> eg.set_edge_attributes(G, bb, "betweenness") >>> G.edges[1, 2]["betweenness"] 2.0
If you provide a list as the second argument, updates to the list will be reflected in the edge attribute for each edge:
>>> labels = [] >>> eg.set_edge_attributes(G, labels, "labels") >>> labels.append("foo") >>> G.edges[0, 1]["labels"] ['foo'] >>> G.edges[1, 2]["labels"] ['foo']
If you provide a dictionary of dictionaries as the second argument, the entire dictionary will be used to update edge attributes:
>>> G = eg.path_graph(3) >>> attrs = {(0, 1): {"attr1": 20, "attr2": "nothing"}, (1, 2): {"attr2": 3}} >>> eg.set_edge_attributes(G, attrs) >>> G[0][1]["attr1"] 20 >>> G[0][1]["attr2"] 'nothing' >>> G[1][2]["attr2"] 3
Note that if the dict contains edges that are not in G, they are silently ignored:
>>> G = eg.Graph([(0, 1)]) >>> eg.set_edge_attributes(G, {(1, 2): {"weight": 2.0}}) >>> (1, 2) in G.edges() False
- easygraph.classes.operation.set_node_attributes(G, values, name=None)[source]#
Sets node attributes from a given value or dictionary of values.
Warning
The call order of arguments values and name switched between v1.x & v2.x.
- Parameters:
G (EasyGraph Graph) –
values (scalar value, dict-like) –
What the node attribute should be set to. If values is not a dictionary, then it is treated as a single attribute value that is then applied to every node in G. This means that if you provide a mutable object, like a list, updates to that object will be reflected in the node attribute for every node. The attribute name will be name.
If values is a dict or a dict of dict, it should be keyed by node to either an attribute value or a dict of attribute key/value pairs used to update the node’s attributes.
name (string (optional, default=None)) – Name of the node attribute to set if values is a scalar.
Examples
After computing some property of the nodes of a graph, you may want to assign a node attribute to store the value of that property for each node:
>>> G = eg.path_graph(3) >>> bb = eg.betweenness_centrality(G) >>> isinstance(bb, dict) True >>> eg.set_node_attributes(G, bb, "betweenness") >>> G.nodes[1]["betweenness"] 1.0
If you provide a list as the second argument, updates to the list will be reflected in the node attribute for each node:
>>> G = eg.path_graph(3) >>> labels = [] >>> eg.set_node_attributes(G, labels, "labels") >>> labels.append("foo") >>> G.nodes[0]["labels"] ['foo'] >>> G.nodes[1]["labels"] ['foo'] >>> G.nodes[2]["labels"] ['foo']
If you provide a dictionary of dictionaries as the second argument, the outer dictionary is assumed to be keyed by node to an inner dictionary of node attributes for that node:
>>> G = eg.path_graph(3) >>> attrs = {0: {"attr1": 20, "attr2": "nothing"}, 1: {"attr2": 3}} >>> eg.set_node_attributes(G, attrs) >>> G.nodes[0]["attr1"] 20 >>> G.nodes[0]["attr2"] 'nothing' >>> G.nodes[1]["attr2"] 3 >>> G.nodes[2] {}
Note that if the dictionary contains nodes that are not in G, the values are silently ignored:
>>> G = eg.Graph() >>> G.add_node(0) >>> eg.set_node_attributes(G, {0: "red", 1: "blue"}, name="color") >>> G.nodes[0]["color"] 'red' >>> 1 in G.nodes False