im getting one more edge in networkx digraph in python

To add an edge to a networkx digraph, you can use the add_edge() method. Here's an example:

main.py
import networkx as nx

# create a new digraph
G = nx.DiGraph()

# add two nodes
G.add_node(1)
G.add_node(2)

# add an edge from node 1 to node 2
G.add_edge(1, 2)

# add another edge from node 2 to node 1
G.add_edge(2, 1)

# check the edges
print(G.edges())
257 chars
18 lines

Output:

main.py
[(1, 2), (2, 1)]
17 chars
2 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.

related categories

gistlibby LogSnag