How Do You Create a Graph in Data Structure in Python?


Heather Bennett

Creating a Graph in Data Structure in Python

In this tutorial, we will explore how to create a graph in Python using the data structure concept. A graph is a collection of nodes or vertices connected by edges.

It is widely used in various applications such as network analysis, social media algorithms, and recommendation systems.

Understanding Graphs

Before we dive into the implementation details, let’s understand the basic concepts of graphs. A graph consists of two main components: vertices (also known as nodes) and edges.

  • Vertices: Vertices are the entities or objects that are interconnected in a graph. For example, if we consider a social network, each person can be seen as a vertex.
  • Edges: Edges represent the connections between vertices.

    They can be directional (one-way) or undirected (two-way). Continuing with our social network example, an edge can represent a friendship between two individuals.

Implementing Graphs in Python

In Python, we can implement graphs using various approaches such as adjacency matrix and adjacency list. Here, we will focus on using an adjacency list to represent our graph.

Adjacency List Representation

The adjacency list representation uses a dictionary to store the vertices and their corresponding adjacent vertices. Each vertex is mapped to a list of its neighboring vertices.

Let’s see how we can create a graph using an adjacency list in Python:

class Graph:
    def __init__(self):
        self.graph = {}

    def add_vertex(self, vertex):
        self.graph[vertex] = []

    def add_edge(self, vertex1, vertex2):

In the above code snippet, we define a class called “Graph” with two methods: “add_vertex” and “add_edge”. The “add_vertex” method adds a new vertex to the graph by initializing an empty list for its adjacent vertices.

The “add_edge” method connects two vertices by adding each other to their respective adjacency lists.

Using the Graph Class

Now that we have implemented our graph class, let’s create an instance and add some vertices and edges to it:

# Create a new graph
graph = Graph()

# Add vertices

# Add edges
graph.add_edge('A', 'B')
graph.add_edge('B', 'C')
graph.add_edge('C', 'D')

In the above code, we create a new instance of the “Graph” class and add four vertices (‘A’, ‘B’, ‘C’, and ‘D’) using the “add_vertex” method. We then connect these vertices using the “add_edge” method.


Congratulations! You have learned how to create a graph in Python using an adjacency list representation.

Graphs are powerful data structures that can be used in various applications. Feel free to explore more advanced operations on graphs such as traversals and shortest path algorithms.

Remember, understanding data structures like graphs is essential for any programmer or data scientist. So keep practicing and experimenting with different implementations to strengthen your skills.

Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy