liveBook · Manning

Adjacency list

As explained in Grokking Data Structures

An adjacency list is a graph representation that associates each source vertex with a collection of its outgoing edges or destination vertices.

An adjacency list represents a graph by grouping edges according to their source vertex. A dictionary associates each vertex with the collection of its outgoing edges or destination vertices. section 13.9

For a vertex v, its adjacency list contains every edge whose source is v. The collection can be a linked list, Python list, set, or another container that supports searching and traversal. section 13.9

A practical Python implementation can organize the representation like this:

  • Store vertices in a Python dictionary keyed by unique vertex identifiers. section 13.9
  • Give each internal Vertex object its own adjacency list for outgoing edges. section 13.9
  • Store destination vertices directly when edges are unweighted and unlabeled; otherwise store Edge objects or pairs and tuples such as (source, destination, weight). section 13.9
  • Search before insertion so duplicate edges are rejected, and remove incoming references when deleting a vertex. section 13.9

A small class structure

A Vertex can keep a singly linked list and expose operations such as has_edge_to and add_edge_to. An outer Graph can initialize an empty dictionary, retrieve vertices by key, insert vertices and edges, and delete vertices while removing references to them. section 13.9

class Vertex:
    def __init__(self, key):
        self.id = key
        self._adj_list = SinglyLinkedList()
    def has_edge_to(self, destination_vertex):
        return self._adj_list._search(destination_vertex) is not None
    def add_edge_to(self, destination_vertex):
        if self.has_edge_to(destination_vertex):
            raise ValueError(f'Edge already exists: {self} -> { destination_vertex}')
        self._adj_list.insert_in_front(destination_vertex)

Inspect the compact Vertex implementation that delegates edge searching and insertion to a singly linked list. section 13.9

Adjacency lists are one of the two common graph representations; adjacency matrices are used mainly in niche contexts. They support graph traversal and searching, including breadth-first search and depth-first search. section 13.8 section 13.16

How Grokking Data Structures covers it

How deep it goes
Defines the representation and sketches Python classes, edge operations, and deletion, but does not show the complete implementation.
Examples
Uses Python dictionaries, Vertex and Graph classes, singly linked lists, and destination vertices as stored edge values.
What it assumes
Assumes basic graph terminology and linked-list operations while presenting the adjacency-list organization directly.
What you can do after
Build it from scratch using a Python dictionary, Vertex objects, and linked-list, list, or set adjacency containers.
How it connects
Connects graphs with vertices, edges, linked lists, adjacency matrices, breadth-first search, and depth-first search.

Also explained in

Compare how 5 books explain adjacency list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.