Warning
The documentation for retworkx has migrated to:
https://qiskit.org/documentation/retworkx
These docs will no longer be updated.
retworkx.PyDiGraph¶
- class PyDiGraph(check_cycle=False, multigraph=True, /)¶
A class for creating directed graphs
The PyDiGraph class is used to create a directed 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. 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.PyDiGraph() 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.PyDiGraph() 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
The PyDiGraph class has an option for real time cycle checking which can be used to ensure any edges added to the graph does not introduce a cycle. By default the real time cycle checking feature is disabled for performance, however you can enable it by setting the
check_cycle
attribute to True. For example:import retworkx dag = retworkx.PyDiGraph() dag.check_cycle = True
or at object creation:
import retworkx dag = retworkx.PyDiGraph(check_cycle=True)
With check_cycle set to true any calls to
PyDiGraph.add_edge()
will ensure that no cycles are added, ensuring that the PyDiGraph class truly represents a directed acyclic graph. Do note that this cycle checking onadd_edge()
,add_edges_from()
,add_edges_from_no_data()
,extend_from_edge_list()
, andextend_from_weighted_edge_list()
comes with a performance penalty that grows as the graph does. If you’re adding a node and edge at the same time leveragingPyDiGraph.add_child()
orPyDiGraph.add_parent()
will avoid this overhead.- __init__()¶
Initialize self. See help(type(self)) for accurate signature.
Methods
__init__
()Initialize self.
add_child
(parent, obj, edge, /)Add a new child node to the graph.
add_edge
(parent, child, edge, /)Add an edge between 2 nodes.
add_edges_from
(obj_list, /)Add new edges to the dag.
add_edges_from_no_data
(obj_list, /)Add new edges to the dag without python data.
add_node
(obj, /)Add a new node to the graph.
add_nodes_from
(obj_list, /)Add new nodes to the graph.
add_parent
(child, obj, edge, /)Add a new parent node to the dag.
adj
(node, /)Get the index and data for the neighbors of a node.
adj_direction
(node, direction, /)Get the index and data for either the parent or children of a node.
compose
(other, node_map, /[, node_map_func, …])Add another PyDiGraph object into this PyDiGraph
Get edge list
edges
()Return a list of all edge data.
extend_from_edge_list
(edge_list, /)Extend graph from an edge list
extend_from_weighted_edge_list
(edge_lsit, /)Extend graph from a weighted edge list
find_adjacent_node_by_edge
(node, predicate, /)Find a target node with a specific edge
Find node within this graph given a specific weight
from_adjacency_matrix
(matrix, /)Create a new
PyDiGraph
object from an adjacency matrixget_all_edge_data
(node_a, node_b, /)Return the edge data for all the edges between 2 nodes.
get_edge_data
(node_a, node_b, /)Return the edge data for an edge between 2 nodes.
get_node_data
(node, /)Return the node data for a given node index
has_edge
(node_a, node_b, /)Return True if there is an edge from node_a to node_b.
in_degree
(node, /)Get the degree of a node for inbound edges.
in_edges
(node, /)Get the index and edge data for all parents of a node.
insert_node_on_in_edges
(node, ref_node, /)Insert a node between a reference node and all its predecessor nodes
insert_node_on_in_edges_multiple
(node, …)Insert a node between a list of reference nodes and all their predecessors
insert_node_on_out_edges
(node, ref_node, /)Insert a node between a reference node and all its successor nodes
insert_node_on_out_edges_multiple
(node, …)Insert a node between a list of reference nodes and all their successors
Check if the graph is symmetric
merge_nodes
(u, /, v)Merge two nodes in the graph.
neighbors
(node, /)Get the neighbors (i.e.
Return a list of all node indexes.
nodes
()Return a list of all node data.
out_degree
(node, /)Get the degree of a node for outbound edges.
out_edges
(node, /)Get the index and edge data for all children of a node.
predecessor_indices
(node, /)Get the predecessor indices of a node.
predecessors
(node, /)Return a list of all the node predecessor data.
read_edge_list
(path, /[, comment, deliminator])Read an edge list file and create a new PyDiGraph object from the contents
remove_edge
(parent, child, /)Remove an edge between 2 nodes.
remove_edge_from_index
(edge, /)Remove an edge identified by the provided index
remove_edges_from
(index_list, /)Remove edges from the graph.
remove_node
(node, /)Remove a node from the graph.
remove_node_retain_edges
(node, /[, …])Remove a node from the graph and add edges from all predecessors to all successors
remove_nodes_from
(index_list, /)Remove nodes from the graph.
subgraph
(nodes, /)Return a new PyDiGraph object for a subgraph of this graph
successor_indices
(node, /)Get the successor indices of a node.
successors
(node, /)Return a list of all the node successor data.
to_dot
([node_attr, edge_attr, graph_attr, …])Generate a dot file from the graph
Generate a new PyGraph object from this graph
update_edge
(source, target, /, edge)Update an edge’s weight/payload inplace
update_edge_by_index
(source, target, /, edge)Update an edge’s weight/payload by the edge index
Get edge list with weights
Attributes
Whether cycle checking is enabled for the DiGraph/DAG.
Whether the graph is a multigraph (allows multiple edges between nodes) or not
- add_child(parent, obj, edge, /)¶
Add a new child node to the graph.
This will create a new node on the graph and add an edge from the parent to that new node.
- Parameters
parent (int) – The index for the parent node
obj – The python object to attach to the node
edge – The python object to attach to the edge
- Returns
The index of the newly created child node
- Return type
int
- add_edge(parent, child, edge, /)¶
Add an edge between 2 nodes.
Use add_child() or add_parent() to create a node with an edge at the same time as an edge for better performance. Using this method will enable adding duplicate edges between nodes if the
check_cycle
attribute is set toTrue
.- Parameters
parent (int) – Index of the parent node
child (int) – Index of the child node
edge – The object to set as the data for the edge. It can be any python object.
- Returns
The edge index of the created edge
- Return type
int
- Raises
When the new edge will create a cycle
- add_edges_from(obj_list, /)¶
Add new edges to the dag.
- Parameters
obj_list (list) – A list of tuples of the form
(parent, child, obj)
to attach to the graph.parent
andchild
are integer indexes describing where an edge should be added, and obj is the python object for the edge data.- Returns
A list of int indices of the newly created edges
- Return type
list
- add_edges_from_no_data(obj_list, /)¶
Add new edges to the dag without python data.
- Parameters
obj_list (list) – A list of tuples of the form
(parent, child)
to attach to the graph.parent
andchild
are integer indexes describing where an edge should be added. Unlikeadd_edges_from()
there is no data payload and when the edge is created None will be used.- Returns
A list of int indices of the newly created edges
- Return type
list
- add_node(obj, /)¶
Add a new node to the graph.
- Parameters
obj – The python object to attach to the node
- Returns
The index of the newly created node
- Return type
int
- add_nodes_from(obj_list, /)¶
Add new nodes to the graph.
- Parameters
obj_list (list) – A list of python objects to attach to the graph as new nodes
- Returns
A list of int indices of the newly created nodes
- Return type
- add_parent(child, obj, edge, /)¶
Add a new parent node to the dag.
This create a new node on the dag and add an edge to the child from that new node
- Parameters
child (int) – The index of the child node
obj – The python object to attach to the node
edge – The python object to attach to the edge
- Returns index
The index of the newly created parent node
- Return type
int
- adj(node, /)¶
Get the index and data for the neighbors of a node.
This will return a dictionary where the keys are the node indexes of the adjacent nodes (inbound or outbound) and the value is the edge dat objects between that adjacent node and the provided node. Note in the case of a multigraph only one edge will be used, not all of the edges between two node.
- Parameters
node (int) – The index of the node to get the neighbors
- Returns
A dictionary where the keys are node indexes and the value is the edge data object for all nodes that share an edge with the specified node.
- Return type
dict
- adj_direction(node, direction, /)¶
Get the index and data for either the parent or children of a node.
This will return a dictionary where the keys are the node indexes of the adjacent nodes (inbound or outbound as specified) and the value is the edge data objects for the edges between that adjacent node and the provided node. Note in the case of a multigraph only one edge one edge will be used, not all of the edges between two node.
- Parameters
node (int) – The index of the node to get the neighbors
direction (bool) – The direction to use for finding nodes, True means inbound edges and False means outbound edges.
- Returns
A dictionary where the keys are node indexes and the value is the edge data object for all nodes that share an edge with the specified node.
- Return type
dict
- check_cycle¶
Whether cycle checking is enabled for the DiGraph/DAG.
If set to
True
adding new edges that would introduce a cycle will raise aDAGWouldCycle
exception.
- compose(other, node_map, /, node_map_func=None, edge_map_func=None)¶
Add another PyDiGraph object into this PyDiGraph
- Parameters
other (PyDiGraph) – The other PyDiGraph object to add onto this graph.
node_map (dict) –
A dictionary mapping node indexes from this PyDiGraph object to node indexes in the other PyDiGraph object. The keys are a node index in this graph and the value is a tuple of the node index in the other graph to add an edge to and the weight of that edge. For example:
{ 1: (2, "weight"), 2: (4, "weight2") }
node_map_func – An optional python callable that will take in a single node weight/data object and return a new node weight/data object that will be used when adding an node from other onto this graph.
edge_map_func – An optional python callable that will take in a single edge weight/data object and return a new edge weight/data object that will be used when adding an edge from other onto this graph.
- Returns
new_node_ids: A dictionary mapping node index from the other PyDiGraph to the corresponding node index in this PyDAG after they’ve been combined
- Return type
dict
For example, start by building a graph:
import os import tempfile import pydot from PIL import Image import retworkx # Build first graph and visualize: graph = retworkx.PyDiGraph() node_a = graph.add_node('A') node_b = graph.add_child(node_a, 'B', 'A to B') node_c = graph.add_child(node_b, 'C', 'B to C') dot_str = graph.to_dot( lambda node: dict( color='black', fillcolor='lightblue', style='filled')) dot = pydot.graph_from_dot_data(dot_str)[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'graph.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image
Then build a second one:
# Build second graph and visualize: other_graph = retworkx.PyDiGraph() node_d = other_graph.add_node('D') other_graph.add_child(node_d, 'E', 'D to E') dot_str = other_graph.to_dot( lambda node: dict( color='black', fillcolor='lightblue', style='filled')) dot = pydot.graph_from_dot_data(dot_str)[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'other_graph.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image
Finally compose the
other_graph
ontograph
node_map = {node_b: (node_d, 'B to D')} graph.compose(other_graph, node_map) dot_str = graph.to_dot( lambda node: dict( color='black', fillcolor='lightblue', style='filled')) dot = pydot.graph_from_dot_data(dot_str)[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'combined_graph.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image
- edge_list()¶
Get edge list
Returns a list of tuples of the form
(source, target)
wheresource
andtarget
are the node indices.- Returns
An edge list with weights
- Return type
- edges()¶
Return a list of all edge data.
- Returns
A list of all the edge data objects in the graph
- Return type
list
- extend_from_edge_list(edge_list, /)¶
Extend graph from an edge list
This method differs from
add_edges_from_no_data()
in that it will add nodes if a node index is not present in the edge list.- Parameters
edge_list (list) – A list of tuples of the form
(source, target)
where source and target are integer node indices. If the node index is not present in the graph, nodes will be added (with a node weight ofNone
) to that index.
- extend_from_weighted_edge_list(edge_lsit, /)¶
Extend graph from a weighted edge list
This method differs from
add_edges_from()
in that it will add nodes if a node index is not present in the edge list.- Parameters
edge_list (list) – A list of tuples of the form
(source, target, weight)
where source and target are integer node indices. If the node index is not present in the graph nodes will be added (with a node weight ofNone
) to that index.
- find_adjacent_node_by_edge(node, predicate, /)¶
Find a target node with a specific edge
This method is used to find a target node that is a adjacent to a given node given an edge condition.
- Parameters
node (int) – The node to use as the source of the search
predicate (callable) – A python callable that will take a single parameter, the edge object, and will return a boolean if the edge matches or not
- Returns
The node object that has an edge to it from the provided node index which matches the provided condition
- find_node_by_weight()¶
Find node within this graph given a specific weight
This algorithm has a worst case of O(n) since it searches the node indices in order. If there is more than one node in the graph with the same weight only the first match (by node index) will be returned.
- Parameters
obj – The weight to look for in the graph.
- Returns
the index of the first node in the graph that is equal to the weight. If no match is found
None
will be returned.- Return type
int
- static from_adjacency_matrix(matrix, /)¶
Create a new
PyDiGraph
object from an adjacency matrixThis method can be used to construct a new
PyDiGraph
object from an input adjacency matrix. The node weights will be the index from the matrix. The edge weights will be a float value of the value from the matrix.
- get_all_edge_data(node_a, node_b, /)¶
Return the edge data for all the edges between 2 nodes.
- Parameters
node_a (int) – The index for the first node
node_b (int) – The index for the second node
- Returns
A list with all the data objects for the edges between nodes
- Return type
list
- Raises
NoEdgeBetweenNodes – When there is no edge between nodes
- get_edge_data(node_a, node_b, /)¶
Return the edge data for an edge between 2 nodes.
- Parameters
node_a (int) – The index for the first node
node_b (int) – The index for the second node
- Returns
The data object set for the edge
- Raises
NoEdgeBetweenNodes – When there is no edge between nodes
- get_node_data(node, /)¶
Return the node data for a given node index
- Parameters
node (int) – The index for the node
- Returns
The data object set for that node
- Raises
IndexError – when an invalid node index is provided
- has_edge(node_a, node_b, /)¶
Return True if there is an edge from node_a to node_b.
- Parameters
node_a (int) – The source node index to check for an edge
node_b (int) – The destination node index to check for an edge
- Returns
True if there is an edge false if there is no edge
- Return type
bool
- in_degree(node, /)¶
Get the degree of a node for inbound edges.
- Parameters
node (int) – The index of the node to find the inbound degree of
- Returns
The inbound degree for the specified node
- Return type
int
- in_edges(node, /)¶
Get the index and edge data for all parents of a node.
This will return a list of tuples with the parent index the node index and the edge data. This can be used to recreate add_edge() calls. :param int node: The index of the node to get the edges for
- Parameters
node (int) – The index of the node to get the edges for
- Returns
A list of tuples of the form:
(parent_index, node_index, edge_data)`
- Return type
- insert_node_on_in_edges(node, ref_node, /)¶
Insert a node between a reference node and all its predecessor nodes
This essentially iterates over all edges into the reference node specified in the
ref_node
parameter removes those edges and then adds 2 edges, one from the predecessor ofref_node
tonode
and the other fromnode
toref_node
. The edge payloads for the newly created edges are copied by reference from the original edge that gets removed.- Parameters
node (int) – The node index to insert between
ref_node (int) – The reference node index to insert
node
between
- insert_node_on_in_edges_multiple(node, ref_nodes, /)¶
Insert a node between a list of reference nodes and all their predecessors
This essentially iterates over all edges into the reference node specified in the
ref_nodes
parameter removes those edges and then adds 2 edges, one from the predecessor ofref_node
tonode
and the other fromnode
toref_node
. The edge payloads for the newly created edges are copied by reference from the original edge that gets removed.- Parameters
node (int) – The node index to insert between
ref_node (int) – The reference node index to insert
node
between
- insert_node_on_out_edges(node, ref_node, /)¶
Insert a node between a reference node and all its successor nodes
This essentially iterates over all edges out of the reference node specified in the
ref_node
parameter removes those edges and then adds 2 edges, one fromref_node
tonode
and the other fromnode
to the successor ofref_node
. The edge payloads for the newly created edges are copied by reference from the original edge that gets removed.- Parameters
node (int) – The node index to insert between
ref_node (int) – The reference node index to insert
node
between
- insert_node_on_out_edges_multiple(node, ref_nodes, /)¶
Insert a node between a list of reference nodes and all their successors
This essentially iterates over all edges out of the reference node specified in the
ref_node
parameter removes those edges and then adds 2 edges, one fromref_node
tonode
and the other fromnode
to the successor ofref_node
. The edge payloads for the newly created edges are copied by reference from the original edge that gets removed.- Parameters
node (int) – The node index to insert between
ref_nodes (int) – The list of node indices to insert
node
between
- is_symmetric()¶
Check if the graph is symmetric
- Returns
True if the graph is symmetric
- Return type
bool
- merge_nodes(u, /, v)¶
Merge two nodes in the graph.
If the nodes have equal weight objects then all the edges into and out of u will be added to v and u will be removed from the graph. If the nodes don’t have equal weight objects then no changes will be made and no error raised
- Parameters
u (int) – The source node that is going to be merged
v (int) – The target node that is going to be the new node
- 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
- neighbors(node, /)¶
Get the neighbors (i.e. successors) of a node.
This will return a list of neighbor node indices. This function is equivalent to
successor_indices()
.- Parameters
node (int) – The index of the node to get the neighbors of
- Returns
A list of the neighbor node indices
- Return type
- node_indexes()¶
Return a list of all node indexes.
- Returns
A list of all the node indexes in the graph
- Return type
- nodes()¶
Return a list of all node data.
- Returns
A list of all the node data objects in the graph
- Return type
list
- out_degree(node, /)¶
Get the degree of a node for outbound edges.
- Parameters
node (int) – The index of the node to find the outbound degree of
- Returns
The outbound degree for the specified node
- Return type
int
- out_edges(node, /)¶
Get the index and edge data for all children of a node.
This will return a list of tuples with the child index the node index and the edge data. This can be used to recreate add_edge() calls.
- Parameters
node (int) – The index of the node to get the edges for
- Returns out_edges
A list of tuples of the form:
`(node_index, child_index, edge_data)`
- Return type
- predecessor_indices(node, /)¶
Get the predecessor indices of a node.
This will return a list of the node indicies for the predecessors of a node
- Parameters
node (int) – The index of the node to get the predecessors of
- Returns
A list of the neighbor node indicies
- Return type
- predecessors(node, /)¶
Return a list of all the node predecessor data.
- Parameters
node (int) – The index for the node to get the predecessors for
- Returns
A list of the node data for all the parent neighbor nodes
- Return type
list
- static read_edge_list(path, /, comment=None, deliminator=None)¶
Read an edge list file and create a new PyDiGraph object from the contents
The expected format for the edge list file is a line seperated list of deliminated node ids. If there are more than 3 elements on a line the 3rd on will be treated as a string weight for the edge
- Parameters
path (str) – The path of the file to open
comment (str) – Optional character to use as a comment by default there are no comment characters
deliminator (str) – Optional character to use as a deliminator by default any whitespace will be used
For example:
import os import tempfile from PIL import Image import pydot import retworkx with tempfile.NamedTemporaryFile('wt') as fd: path = fd.name fd.write('0 1\n') fd.write('0 2\n') fd.write('0 3\n') fd.write('1 2\n') fd.write('2 3\n') fd.flush() graph = retworkx.PyDiGraph.read_edge_list(path) # Draw graph dot = pydot.graph_from_dot_data(graph.to_dot())[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'dag.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image
- remove_edge(parent, child, /)¶
Remove an edge between 2 nodes.
Note if there are multiple edges between the specified nodes only one will be removed.
- Parameters
parent (int) – The index for the parent node.
child (int) – The index of the child node.
- Raises
NoEdgeBetweenNodes – If there are no edges between the nodes specified
- remove_edge_from_index(edge, /)¶
Remove an edge identified by the provided index
- Parameters
edge (int) – The index of the edge to remove
- remove_edges_from(index_list, /)¶
Remove edges from the graph.
Note if there are multiple edges between the specified nodes only one will be removed.
- Parameters
index_list (list) – A list of node index pairs to remove from the graph
- remove_node(node, /)¶
Remove a node from the graph.
- Parameters
node (int) – The index of the node to remove. If the index is not present in the graph it will be ignored and this function will have no effect.
- remove_node_retain_edges(node, /, use_outgoing=None, condition=None)¶
Remove a node from the graph and add edges from all predecessors to all successors
By default the data/weight on edges into the removed node will be used for the retained edges.
- Parameters
node (int) – The index of the node to remove. If the index is not present in the graph it will be ingored and this function willl have no effect.
use_outgoing (bool) – If set to true the weight/data from the edge outgoing from
node
will be used in the retained edge instead of the default weight/data from the incoming edge.condition –
A callable that will be passed 2 edge weight/data objects, one from the incoming edge to
node
the other for the outgoing edge, and will return abool
on whether an edge should be retained. For example setting this kwarg to:lambda in_edge, out_edge: in_edge == out_edge
would only retain edges if the input edge to
node
had the same data payload as the outgoing edge.
- remove_nodes_from(index_list, /)¶
Remove nodes from the graph.
If a node index in the list is not present in the graph it will be ignored.
- Parameters
index_list (list) – A list of node indicies to remove from the the graph.
- subgraph(nodes, /)¶
Return a new PyDiGraph object for a subgraph of this graph
- Parameters
nodes (list) – A list of node indices to generate the subgraph from. If a node index is included that is not present in the graph it will silently be ignored.
- Returns
A new PyDiGraph object representing a subgraph of this graph. It is worth noting that node and edge weight/data payloads are passed by reference so if you update (not replace) an object used as the weight in graph or the subgraph it will also be updated in the other.
- Return type
- successor_indices(node, /)¶
Get the successor indices of a node.
This will return a list of the node indicies for the succesors of a node
- Parameters
node (int) – The index of the node to get the successors of
- Returns
A list of the neighbor node indicies
- Return type
- successors(node, /)¶
Return a list of all the node successor data.
- Parameters
node (int) – The index for the node to get the successors for
- Returns
A list of the node data for all the child neighbor nodes
- Return type
list
- to_dot(node_attr=None, edge_attr=None, graph_attr=None, filename=None)¶
Generate a dot file from the graph
- Parameters
node_attr – A callable that will take in a node data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be strings. If they’re not strings retworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)
edge_attr – A callable that will take in an edge data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be a string. If they’re not strings retworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)
graph_attr (dict) – An optional dictionary that specifies any graph attributes for the output dot file. The key and value of this dictionary must be a string. If they’re not strings retworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)
filename (str) – An optional path to write the dot file to if specified there is no return from the function
- Returns
A string with the dot file contents if filename is not specified.
- Return type
str
Using this method enables you to leverage graphviz to visualize a
retworkx.PyDiGraph
object. For example:import os import tempfile import pydot from PIL import Image import retworkx graph = retworkx.directed_gnp_random_graph(15, .25) dot_str = graph.to_dot( lambda node: dict( color='black', fillcolor='lightblue', style='filled')) dot = pydot.graph_from_dot_data(dot_str)[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'dag.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image
- to_undirected()¶
Generate a new PyGraph object from this graph
This will create a new
PyGraph
object from this graph. All edges in this graph will be created as undirected edges in the new graph object. Do note that the node and edge weights/data payloads will be passed by reference to the newPyGraph
object.- Returns
A new PyGraph object with an undirected edge for every directed edge in this graph
- Return type
- update_edge(source, target, /, edge)¶
Update an edge’s weight/payload inplace
If there are parallel edges in the graph only one edge will be updated. if you need to update a specific edge or need to ensure all parallel edges get updated you should use
update_edge_by_index()
instead.- Parameters
source (int) – The index for the first node
target (int) – The index for the second node
- Raises
NoEdgeBetweenNodes – When there is no edge between nodes
- update_edge_by_index(source, target, /, edge)¶
Update an edge’s weight/payload by the edge index
- Parameters
edge_index (int) – The index for the edge
edge (object) – The data payload/weight to update the edge with
- Raises
NoEdgeBetweenNodes – When there is no edge between nodes
- weighted_edge_list()¶
Get edge list with weights
Returns a list of tuples of the form
(source, target, weight)
wheresource
andtarget
are the node indices andweight
is the payload of the edge.- Returns
An edge list with weights
- Return type