Skip to content

Commit

Permalink
Add section to intro tutorial on graph element modification (#596)
Browse files Browse the repository at this point in the history
This commit adds a section to the introduction tutorial on graph element
modification. It's a basic feature of interacting with a graph to show
how you can update a node or edge in place on an existing graph object
however this was previously not mentioned in the introduction tutorial.
This commit corrects this oversight by explaining how this can be done.

(cherry picked from commit d02b544)
  • Loading branch information
mtreinish authored and mergify-bot committed Apr 27, 2022
1 parent 0bf06a8 commit 31c1334
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/source/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ subsequent additions. For example, building off the previous example if you ran

this new node is assigned index 2 again.

Modifying elements of a graph
=============================

The graph classes in retworkx also allow for in place mutation of the payloads
for elements in the graph. For nodes you can simply use the mapping protocol to
change the payload via it's node index. For example:

.. jupyter-execute::

last_index = graph.node_indices()[-1]
graph[last_index] = "New Payload"
print(graph[last_index])

You can update the payload of any node in the graph using this interface. For
edges you can leverage the :class:`~.PyGraph.update_edge` or
:class:`~.PyGraph.update_edge_by_index` methods to update an edge's payload
in place. For example:

.. jupyter-execute::

edge_index = graph.add_edge(0, 1, None)
graph.update_edge_by_index(edge_index, "New Edge Payload")
print(graph.get_edge_data_by_index(edge_index))

.. _data_payload:

What to use for node and edge data payload
Expand Down

0 comments on commit 31c1334

Please sign in to comment.