What Is a Graph Data Structure Python?


Heather Bennett

A graph data structure is a way of representing relationships between objects. It consists of a set of nodes, also known as vertices, and a set of edges that connect these nodes. In Python, there are various ways to implement a graph data structure, each with its own advantages and use cases.

Adjacency Matrix

One common way to represent a graph in Python is by using an adjacency matrix. This is a two-dimensional array where each row and column represents a node in the graph. The value at the intersection of row i and column j indicates whether there is an edge between node i and node j.

To create an adjacency matrix in Python, you can use a nested list or a NumPy array. Here’s an example:

import numpy as np

# Create an empty 3x3 adjacency matrix
adj_matrix = np.zeros((3, 3))

# Add edges between nodes
adj_matrix[0][1] = 1
adj_matrix[1][2] = 1
adj_matrix[2][0] = 1


This code will output:

[[0. 1. 0.] [0. 

1.] [1.]] 

Adjacency List

Another way to represent a graph in Python is by using an adjacency list. This is essentially a dictionary where each key represents a node, and the corresponding value is a list of nodes that are directly connected to it.

To create an adjacency list in Python, you can use the built-in defaultdict class from the collections module:

from collections import defaultdict

# Create an empty adjacency list
adj_list = defaultdict(list)

# Add edges between nodes

{0: [1], 1: [2], 2: [0]}

Breadth-First Search (BFS)

Once you have a graph represented in Python, you can perform various operations on it. One common operation is to traverse the graph using breadth-first search (BFS). BFS explores all the nodes at the current depth before moving on to the next depth level.

To implement BFS in Python, you can use a queue data structure to keep track of the nodes to visit. Here’s an example:

from collections import deque

def bfs(graph, start):
    visited = set()
    queue = deque([start])

    while queue:
        node = queue.popleft()

        if node not in visited:

            for neighbor in graph[node]:

# Example usage
graph = {0: [1, 2], 1: [3, 4], 2: [5], 3: [], 4: [], 5: []}
bfs(graph, 0)


In conclusion, a graph data structure is a powerful tool for representing relationships between objects. In Python, you can implement a graph using an adjacency matrix or an adjacency list.

Additionally, you can perform operations like BFS to traverse the graph and explore its nodes. Understanding and implementing graph data structures is essential in many areas of computer science, such as network analysis, social network analysis, and pathfinding algorithms.

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

Privacy Policy