What Are Graphs?
Learn about graphs and how to deal with them in Python.
We'll cover the following
A graph is a structure that represents sets of objects and how they’re related to each other. This is a generic definition capable of representing lots of different sets and phenomena.
One example of a graph is a set of cities connected by roads. Consider the image below: each circle represents a city, and each line represents a road that connects these two cities. With that graph, we can see, for example, how to get from one city to another and by which cities we must pass during our trip.
Let’s give the circles and lines names. The circles will be called nodes, and the lines will be called edges. We can simply define these as:
Node: Any entity we’re interested in analyzing. Another name used is vertex.
Edge: A representation of a relationship or interaction between two nodes.
So, given that our set of nodes is defined by
Having relationships between entities represented by edges allows us to represent different kinds of situations with the same general structure defined by
Graphs can get large when we keep adding nodes and edges, such as the following:
And sometimes way bigger, in a way we can’t even draw, with millions of nodes and hundreds of millions of edges, such as the World Wide Web or the Facebook social network. This is why we need complex network analysis. Sometimes, we can’t visually inspect a graph, so we need a set of tools to query it and understand how it works.
Graphs in Python
Let’s learn how to deal with graphs using Python. For that, we’ll mainly use the NetworkX library, one of the most famous libraries for network (or graph) analysis. Here’s a quick introduction to the library:
# First, we need to import the libraryimport networkx as nx# Now, we will create a graphG = nx.Graph()# Each tuple inside this list represents an edgeexample_edges = [(0, 1),(0, 3),(2, 3),(3, 5),(1, 4)]# Here, we are adding those edges to the graph we createdG.add_edges_from(example_edges)# We can now see the nodes createdprint('Nodes: ', G.nodes())# and also the edgesprint('Edges: ', G.edges())
Line 2: We import the NetworkX library.
Line 5: We create an instance of the
Graph
object to hold our edges and nodes.Lines 8–14: The
example_edges
list contains the definition of our edges. Each tuple inside this list defines a pair of nodes that should be connected. This representation is enough to define the graph because NetworkX infers the nodes from the edges we create.Line 17: We add the edges we created to our graph.
Line 20: We print the list of the nodes of the graph.
Line 23: We print the list of the edges of the graph.
As we can see, we can easily define the edges we want to create and then add them to the network. We also can create edges with strings as nodes, not just integers
import networkx as nxexample_edges = [('X', 'Y'),('Y', 'Z')]G = nx.Graph()G.add_edges_from(example_edges)print('Nodes: ', G.nodes())print('Edges: ', G.edges())
Exercise
Now, let’s try an exercise. Look at the following graph:
Try to implement it below using the NetworkX library:
import networkx as nx# Put your edges inside this listexercise_1 = []# Now create your graphprint('Nodes: ', G.nodes())print('Edges: ', G.edges())
Examples of real-world graphs
One classical example of a graph is a social network. We can represent a social network as a graph where each node is a person, and each edge is a friendship relationship between two persons.
Graphs aren’t just used to represent social connections: we can use graphs to represent how airports are connected, with edges being created if there’s a flight from one airport to the other. We can also use graphs to model how medicines interact with each other, how politicians agree or disagree, and so on.
Exercise
Try to create the graph for the social network we saw above. Remember that we can define the nodes inside the edges with strings. Be careful with these names, and keep the first letter in uppercase.
import networkx as nxonline = []# Now create your graphprint('Nodes: ', G.nodes())print('Edges: ', G.edges())