network add edges from in python

You can add edges to a graph in networkx using the .add_edge() method. Here's an example:

main.py
import networkx as nx

# Create an empty graph
g = nx.Graph()

# Add edges
g.add_edge(1, 2)
g.add_edge(1, 3)
g.add_edge(2, 3)
126 chars
10 lines

In this example, we first create an empty graph and then add three edges between nodes 1, 2, and 3.

If you want to add multiple edges at once, you can use the .add_edges_from() method. Here's how:

main.py
# Create a list of edges
edges = [(1, 2), (1, 3), (2, 3)]

# Add the edges to the graph
g.add_edges_from(edges)
112 chars
6 lines

This will add the same three edges as in the first example, but all at once.

related categories

gistlibby LogSnag