Introduction

Greetings! Welcome to the next stage of our journey in the "Mastering Graphs in Python" course! Up to this point, we've explored graph structures and adjacency matrices in great detail, uncovering the mechanics behind these critical data structures. In today's session, we'll delve into another essential graph representation: the adjacency list.

Consider your friends list on a social networking site like Facebook; this can be viewed as a classic example of an adjacency list. Each person on Facebook has a list of connections (or friends), and you can discover mutual connections by examining the overlap in your friends lists. That's precisely how adjacency lists function!

The adjacency list representation is generally more space-efficient for storing sparse graphs compared to adjacency matrices. We'll begin by theoretically understanding adjacency lists and then illustrate how to implement them in Python. We'll then learn how to perform basic operations. To put theory into practice, we'll simulate a real scenario: building a social network graph using an adjacency list. So, let's get started!

Understanding Adjacency Lists in Graph Theory

Before we dive into the implementation, let's familiarize ourselves with the concept of adjacency lists. An adjacency list simplifies a graph into its most essential and straightforward form. It's similar to creating a contacts list on your phone, where you have a compendium of everyone you can call. Likewise, in a graph, every node keeps a list, akin to a contacts list, of the nodes it's connected to.

Let's further refine our understanding with a simple example:

Suppose we have four interconnected cities shown below.

Here, cities are our vertices, and roads connecting them are our edges. The adjacency list for this graph would appear as follows:

San Diego: San Francisco, Los Angeles, Las Vegas
San Francisco: San Diego, Los Angeles
Los Angeles: San Diego, San Francisco, Las Vegas
Las Vegas: San Diego, Los Angeles

This adjacency list informs us, for instance, that San Diego is connected to San Francisco, Los Angeles, and Las Vegas - much like a city roadmap!

Creating an Adjacency List for a Graph in Python

When it comes to Python, the built-in dictionaries and lists are invaluable for representing adjacency lists.

In an adjacency list representation, dictionaries function exceptionally well. The keys represent the nodes of the graph, and the corresponding values are lists containing the adjacent nodes.

You can translate the city roadmap mentioned above into a Python dictionary as follows:

Python
roadmap = {
    'San Diego': ['San Francisco', 'Los Angeles', 'Las Vegas'],
    'San Francisco': ['San Diego', 'Los Angeles'],
    'Los Angeles': ['San Diego', 'San Francisco', 'Las Vegas'],
    'Las Vegas': ['San Diego', 'Los Angeles']
}

This adjacency representation is highly efficient for sparse graphs wherein the number of edges is much less than the square of the number of vertices.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal