A graph or a network is a diagram representing a system of connections or interrelations among two or more things. It can also be defined as a collection of nodes or vertices along with identified pairs of nodes called edges.
In NetworkX, nodes can be any hashable object like a text string, an image, etc.
We will start by creating an empty graph i.e., a graph with no nodes or edges.
The below code imports the networkx module and creates a Graph() object which is by definition an empty graph.
import networkx as nx
G = nx.Graph()
Note that Python's None object should not be used as a node as it is used to determine whether optional function arguments have been assigned in many functions and will thus result in unexpected behavior.
There are several ways to grow a graph. Some of the most common ones are to add edges and/or nodes.
Growing graph by adding nodes
We can add nodes in several different ways.
Adding one node at a time: We can use the add_node() method to add a single node to our graph. The below code shows how to create a graph and add nodes to it one by one.
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
The graph from the above code looks like this
Tip: Save the code in a cell of a Jupyter notebook and run the cell after adding the line 'nx.draw(G)' at the end to view the graph.
Adding nodes through Python lists: We can add several nodes simultaneously using lists or any other iterable container like strings, tuples using the add_nodes_from() method. The below code shows how to create a graph and add nodes using a list.
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
The graph from the above code looks like this
Adding node attributes with nodes: We can add node attributes while adding nodes if our nodes given to the add_nodes_from() method are tuples of the form '(node, node_attribute_dictionary)'. The below code shows how to create a graph and add nodes along with their attributes.
import networkx as nx
G = nx.Graph()
G.add_nodes_from([(1, {'label': 1}), (2, {'label': 2}), (3, {'label': 3})])
The graph from the above code looks like this
Adding nodes from one graph to another: We can also use the add_nodes_from() method to add nodes from one graph to another. The below code shows how to add nodes of one graph to another.
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
H = nx.Graph()
H.add_nodes_from(G)
As expected the graph looks like this
Growing graph by adding edges
We can add edges similar to the way we added nodes.
Adding one edge at a time: We can use the add_edge() method to add a single node to our graph. The below code shows how to create a graph and add edges to it one by one.
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2)
G.add_edge(3, 4)
G.add_edge(2, 4)
The graph from the above code looks like this
Adding edges through Python lists: We can add several nodes simultaneously using lists using the add_edges_from() method. The below code shows how to create a graph and add edges using a list.
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1, 2), (3, 4), (2, 3)])
The graph from the above code looks like this
As in the case of nodes we can add edge attributes also by keeping our edge tuples of the form '(node1, node2, edge_attribute_dictionary)'.
Comments