PyGraph#
- class PyGraph(multigraph=True, attrs=None, *, node_count_hint=None, edge_count_hint=None)#
Bases:
objectA class for creating undirected graphs
The PyGraph class is used to create an undirected graph. It can be a multigraph (have multiple edges between nodes). Each node and edge (although rarely used for edges) is indexed by an integer id. These ids are stable for the lifetime of the graph object and on node or edge deletions you can have holes in the list of indices for the graph. Node indices will be reused on additions after removal. For example:
import rustworkx as rx graph = rx.PyGraph() graph.add_nodes_from(list(range(5))) graph.add_nodes_from(list(range(2))) graph.remove_node(2) print("After deletion:", graph.node_indices()) res_manual = graph.add_node(None) print("After adding a new node:", graph.node_indices())
After deletion: NodeIndices[0, 1, 3, 4, 5, 6] After adding a new node: NodeIndices[0, 1, 2, 3, 4, 5, 6]
Additionally, each node and edge contains an arbitrary Python object as a weight/data payload. You can use the index for access to the data payload as in the following example:
import rustworkx as rx graph = rx.PyGraph() data_payload = "An arbitrary Python object" node_index = graph.add_node(data_payload) print("Node Index: %s" % node_index) print(graph[node_index])
Node Index: 0 An arbitrary Python object
The PyGraph implements the Python mapping protocol for nodes so in addition to access you can also update the data payload with:
import rustworkx as rx graph = rx.PyGraph() data_payload = "An arbitrary Python object" node_index = graph.add_node(data_payload) graph[node_index] = "New Payload" print("Node Index: %s" % node_index) print(graph[node_index])
Node Index: 0 New Payload
By default a
PyGraphis a multigraph (meaning there can be parallel edges between nodes) however this can be disabled by setting themultigraphkwarg toFalsewhen calling thePyGraphconstructor. For example:import rustworkx as rx graph = rx.PyGraph(multigraph=False)
This can only be set at
PyGraphinitialization and not adjusted after creation. Whenmultigraphis set toFalseif a method call is made that would add a parallel edge it will instead update the existing edge’s weight/data payload.Each
PyGraphobject has anattrsattribute which is used to contain additional attributes/metadata of the graph instance. By default this is set toNonebut can optionally be specified by using theattrskeyword argument when constructing a new graph:graph = rustworkx.PyGraph(attrs=dict(source_path='/tmp/graph.csv'))
This attribute can be set to any Python object. Additionally, you can access and modify this attribute after creating an object. For example:
source_path = graph.attrs graph.attrs = {'new_path': '/tmp/new.csv', 'old_path': source_path}
The maximum number of nodes and edges allowed on a
PyGraphobject is \(2^{32} - 1\) (4,294,967,294) each. Attempting to add more nodes or edges than this will result in an exception being raised.- Parameters:
multigraph (bool) – When this is set to
Falsethe created PyGraph object will not be a multigraph. WhenFalseif a method call is made that would add parallel edges the weight/weight from that method call will be used to update the existing edge in place.attrs (Any) – An optional attributes payload to assign to the
attrsattribute. This can be any Python object. If it is not specifiedattrswill be set toNone.node_count_hint (int) – An optional hint that will allocate enough capacity to store this many nodes before needing to grow. This does not prepopulate any nodes with data, it is only a potential performance optimization if the complete size of the graph is known in advance.
edge_count_hint (int) – An optional hint that will allocate enough capacity to store this many edges before needing to grow. This does not prepopulate any edges with data, it is only a potential performance optimization if the complete size of the graph is known in advance.
Methods
Add an edge between 2 nodes.
Add new edges to the graph.
Add new edges to the graph without python data.
Add a new node to the graph.
Add new nodes to the graph.
Get the index and data for the neighbors of a node.
Clears all nodes and edges
Clears all edges, leaves nodes intact
Add another PyGraph object into this PyGraph
Substitute a set of nodes with a single new node.
Return a shallow copy of the graph
Get the degree for a node
Get an edge index map
Return a list of all edge indices.
Return a list of indices of all edges between specified nodes
Get edge list
Return a new PyGraph object for an edge induced subgraph of this graph
Return a list of all edge data.
Extend graph from an edge list
Extend graph from a weighted edge list
Filters a graph's edges by some criteria conditioned on a edge's data payload and returns those edges' indices.
Filters a graph's nodes by some criteria conditioned on a node's data payload and returns those nodes' indices.
Find node within this graph given a specific weight
Create a new
PyGraphobject from an adjacency matrix with matrix elements of typefloatCreate a new
PyGraphobject from an adjacency matrix with matrix elements of typecomplexReturn the edge data for all the edges between 2 nodes.
Return the edge data for the edge between 2 nodes.
Return the edge data for the edge by its given index
Return the edge endpoints for the edge by its given index
Return the node data for a given node index
Check if there is any undirected edge between
node_aandnode_b.Check if the node exists in the graph.
Detect if the graph has parallel edges or not
Return the list of edge indices incident to a provided node.
Get the endpoint indices and edge data for all edges of a node.
Return the index map of edges incident to a provided node
Return the list of edge indices incident to a provided node
Get the neighbors of a node.
Return a list of all node indices.
Return a list of all node indices.
Return a list of all node data.
Return the number of edges in the graph
Return the number of nodes in the graph
Return the list of edge indices incident to a provided node.
Get the endpoint indices and edge data for all edges of a node.
Read an edge list file and create a new PyGraph object from the contents
Remove an edge between 2 nodes.
Remove an edge identified by the provided index
Remove edges from the graph.
Remove a node from the graph.
Remove nodes from the graph.
Return a new PyGraph object for a subgraph of this graph.
Return a new PyGraph object for a subgraph of this graph and a NodeMap object that maps the nodes of the subgraph to the nodes of the original graph.
substitute_node_with_subgraph(self, node, other, edge_map_fn, /, node_filter=None, edge_weight_map=None
Generate a new
PyDiGraphobject from this graphGenerate a dot file from the graph
Update an edge's weight/payload in place
Update an edge's weight/data payload in place by the edge index
Get edge list with weights
Write an edge list file from the PyGraph object
Attributes
- attrs#
- multigraph#
Whether the graph is a multigraph (allows multiple edges between nodes) or not
If set to
Falsemultiple edges between nodes are not allowed and calls that would add a parallel edge will instead update the existing edge