Network Data Representations
Learn how to represent complex networks with edge lists, adjacency matrices, and adjacency lists.
We'll cover the following...
Representing complex networks
In this lesson, we’re going to explore different ways we can represent a complex network and how we can implement them in the NetworkX library. The importance of knowing these different types of representations is to know when to use each to be more productive and waste less computational resources. In some cases, some algorithms are applied more easily to one form over another, so converting between these forms will be very useful for our practice as a complex network analyst.
For every method, we’re going to see how to represent the following network:
Edge lists
The first way of representing complex networks is the edge list.
In an edge list, we have a list of tuples that defines which edges exist in our complex network.
edge_list = [(node_1, node_2),(node_2, node_3),(node_3, node_4)]
To make the reference network for this lesson, we can easily do the following:
import networkx as nxedge_list = [(0, 1),(0, 3),(2, 3),(3, 5),(1, 4)]G = nx.Graph()G.add_edges_from(edge_list)print('Nodes: ', G.nodes())print('Edges: ', G.edges())
By using the edge list, we can’t represent a case where we have a node without any edge connected to it, which we call a singleton. The data itself will never contain it because there’s no edge to represent. One way we can overcome this in NetworkX is to define the list of nodes separately:
import networkx as nxedge_list = [(0, 1),(0, 3),(2, 3),(3, 5),(1, 4)]nodes_list = [0, 1, 2, 3, 4, 5, 13]G = nx.Graph()# We add the nodes separately from the edgesG.add_nodes_from(nodes_list)G.add_edges_from(edge_list)print('Nodes: ', G.nodes())print('Edges: ', G.edges())
Line 16: It adds the list of nodes to the graph and the output of the
G.nodes()
method returns the new node, even if it has no edge.
Exercise
Look at the following network: