What Are Graphs?

Learn about graphs and how to deal with them in Python.

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.

Press + to interact
A road graph
A road graph

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 VVand our set of edges is defined byEE, we define a graph as G=(V,E)G = (V,E), with EE always containing elements in the form (v1,v2),vV(v_1, v_2), v \in V.

Having relationships between entities represented by edges allows us to represent different kinds of situations with the same general structure defined by (V,E)(V,E). For example, we can have persons as nodes and friendship relationships between them as edges, and that will also make a graph.

Graphs can get large when we keep adding nodes and edges, such as the following:

Press + to interact
A bigger graph
A bigger graph

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:

Press + to interact
# First, we need to import the library
import networkx as nx
# Now, we will create a graph
G = nx.Graph()
# Each tuple inside this list represents an edge
example_edges = [
(0, 1),
(0, 3),
(2, 3),
(3, 5),
(1, 4)
]
# Here, we are adding those edges to the graph we created
G.add_edges_from(example_edges)
# We can now see the nodes created
print('Nodes: ', G.nodes())
# and also the edges
print('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

Press + to interact
import networkx as nx
example_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:

Press + to interact
A square graph
A square graph

Try to implement it below using the NetworkX library:

Press + to interact
import networkx as nx
# Put your edges inside this list
exercise_1 = [
]
# Now create your graph
print('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.

Press + to interact
A social network
A social network

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.

Press + to interact
import networkx as nx
online = [
]
# Now create your graph
print('Nodes: ', G.nodes())
print('Edges: ', G.edges())