To add an edge to a networkx digraph, you can use the add_edge()
method. Here's an example:
main.py257 chars18 lines
Output:
main.py17 chars2 lines
In the example above, we first create a new digraph G
, and then add two nodes to it using the add_node()
method. We then add an edge from node 1 to node 2 using the add_edge()
method. Finally, we add another edge from node 2 to node 1.
To check the edges of a graph, you can use the edges()
method, which returns a list of all edges in the graph.
Note that add_edge()
will automatically add any missing nodes to the graph. So in this example, we didn't need to add node 2 again before creating the edge from 2 to 1.
gistlibby LogSnag