How to Represent a Graph Data Structure in Python
Python representation of graph.
Graph is an important data structure studied in Computer Science. Graph theory is an equally important topic in both mathematics and Computer Science.
Representing a graph in a program means finding a way to store the graph in a computer’s memory. Which is a prerequisite to working with graphs in Computer Science.
In this article, I will discuss how to represent a graph using Python.
Table of Contents:· The Basics of Graph
· Two Types of Graph Representation
· Adjacency Matrix Representation
· Adjacency List Representation
∘ Adjacency list of a directed graph:
∘ Adjacency list of an undirected graph:
∘ Adjacency list of a graph with path costs:
· The Graph Class
· Take Input From File
· Conclusion
· Helpful ResourcesThe Basics of Graph
A graph is a non-linear data structure that consists of a set of nodes and edges. Nodes are also referred to as vertices. An edge is a path that connects two nodes.
If we consider the following graph:

Here we have 5 nodes and 7 edges. The set of nodes/vertices, V = {A, B, C, D, E}, and the set of edges, E = {(A, B), (A, C), (B, D), (B, E), (C, D), (D, A), (D, E)}.
The set of edges indicates two neighbor nodes. (A, B) means there is a path between node A and node B. The arrow means we can go from node A to node B and not the other way. This kind of graph is called a directed graph.
The other kind of graph is an undirected graph. See the graph below:

This is the same graph only without the arrows. Here edge(A, B) means we can go from A to B as well as from B to A.

An undirected graph can also be referred to as a bidirectional graph. Because no arrow actually means arrows on both sides.
The cost of the paths can also be given in a graph. We can think of it as the cost of traveling from one city to another city. Cities are the nodes, the road that connects the cities is the edge, and we have the cost of travel.
This kind of graph is called a weighted graph. The cost of an edge is also called weight. See the graph below:

Here the set of edges, E = {(A, B, 5), (A, C, 9), (B, D, 2), (B, E, 7), (C, D, 3), (D, A, 1), (D, E, 13)}. (A, B, 5) means A and B are neighbors and the weight/cost of the edge is 5.
A graph without the costs actually represents a constant cost. That means the cost of all edges in the graph is the same. It is called an unweighted graph. Usually, we don’t need to specify the constant cost when programming an unweighted graph.
From the above discussion, we have seen four basic types of graphs:
- Undirected graph
- Directed graph
- Unweighted graph
- Weighted graph
Now let’s see how to program a graph in Python.
Two Types of Graph Representation
Two commonly used methods to represent a graph in a program are:
Adjacency Matrix Representation
In this method, we represent a graph as a square matrix. If there is a path between two nodes, the value of their corresponding cell is 1, otherwise, the value is 0.

In the case of weighted graphs, instead of 1, we put the cost of the edge in the corresponding cell.

In Python, we can represent graphs like this using a two-dimensional array. And a two-dimensional array can be achieved in Python by creating a list of lists.
The indices of the list will represent the nodes. So if we want to create a graph having 5 nodes, we will represent the nodes from 0 to 4.
So, we need to convert the above graph like this:

Let’s create this graph in Python:









