PyGraph¶
- class PyGraph(multigraph=True, /)¶
Bases:
object
A 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 retworkx graph = retworkx.PyGraph() graph.add_nodes_from(list(range(5))) graph.add_nodes_from(list(range(2))) graph.remove_node(2) print("After deletion:", graph.node_indexes()) res_manual = graph.add_node(None) print("After adding a new node:", graph.node_indexes())
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 retworkx graph = retworkx.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 PyDiGraph implements the Python mapping protocol for nodes so in addition to access you can also update the data payload with:
import retworkx graph = retworkx.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
PyGraph
is a multigraph (meaning there can be parallel edges between nodes) however this can be disabled by setting themultigraph
kwarg toFalse
when calling thePyGraph
constructor. For example:import retworkx graph = retworkx.PyGraph(multigraph=False)
This can only be set at
PyGraph
initialization and not adjusted after creation. Whenmultigraph
is set toFalse
if a method call is made that would add a parallel edge it will instead update the existing edge’s weight/data payload.- Parameters
multigraph (bool) – When this is set to
False
the created PyGraph object will not be a multigraph. WhenFalse
if a method call is made that would add parallel edges the the weight/weight from that method call will be used to update the existing edge in place.
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.
Add another PyGraph object into this PyGraph
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.
Get edge list
Return a new PyGraph object for an edge induced subgrapph of this graph
Return a list of all edge data.
Extend graph from an edge list
Extend graph from a weighted edge list
Find node within this graph given a specific weight
Create a new
PyGraph
object from an adjacency matrixReturn the edge data for all the edges between 2 nodes.
Return the edge data for the edge between 2 nodes.
Return the node data for a given node index
Return True if there is an edge between node_a to node_b.
Detect if the graph has parallel edges or not
Get the neighbors of a node.
Return a list of all node indexes.
Return a list of all node data.
Return the number of edges in the graph
Return the number of nodes in the graph
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
Generate a new
PyDiGraph
object 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
- multigraph¶
Whether the graph is a multigraph (allows multiple edges between nodes) or not
If set to
False
multiple edges between nodes are not allowed and calls that would add a parallel edge will instead update the existing edge