Adjacency list and graph
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Graphs are a fundamental data structure in computer science used to model relationships between pairs of entities. A graph is composed of a set of nodes, also called vertices, and a collection of edges that connect pairs of nodes. Graphs are ubiquitous in a wide range of applications, such as computer networks, social networks, biological networks, transportation systems, and many more.
There are various ways to represent graphs in data structures, each with its trade-offs in terms of time complexity, space complexity, and ease of operations. One popular method of representing a graph is through an adjacency list. In this article, we will delve into the technical details of adjacency lists and explore graphs, their properties, and applications.
Graphs and Their Properties
Basic Terminology
- Vertex (Node): A fundamental part of a graph. It can contain data and can be connected to other vertices.
- Edge: A connection between two vertices in the graph. Edges can be directed (one-way) or undirected (two-way).
- Degree: The number of edges incident to a vertex. In directed graphs, we have
in-degree(edges coming in) andout-degree(edges going out). - Path: A sequence of vertices connected by edges.
- Cycle: A path that starts and ends at the same vertex without repeating any edges or vertices (except the initial and final vertex).
Types of Graphs
- Directed Graph (Digraph): A graph where edges have directions indicating what vertex can be reached from another.
- Undirected Graph: A graph where edges do not have any direction.
- Weighted Graph: A graph where every edge is assigned a weight.
- Unweighted Graph: A graph where all edges have the same weight or no specific weight.
- Connected Graph: An undirected graph where there is a path between every pair of vertices.
- Disconnected Graph: A graph in which at least two vertices lack a path between them.
Adjacency List
Definition
An adjacency list is a collection of lists or arrays used to represent a graph, where each list corresponds to a vertex and contains the list of its neighboring vertices. This structure is memory-efficient, especially for sparse graphs (graphs with relatively few edges).
Technical Explanation
In an adjacency list, each vertex maintains a list of all adjacent vertices. This can be implemented using various data structures, such as:
- Linked Lists: Each vertex is associated with a linked list that contains all its adjacent vertices.
- Arrays of Linked Lists: Uses a fixed-size array where each index correlates to a vertex, pointing to a linked list of other vertices.
- Hash Maps (Dictionaries): Utilize a mapping from vertex identifiers to lists of neighbors for dynamic graph structures.
Here is a pseudocode example illustrating an adjacency list for an undirected graph:
- Space Efficient: For sparse graphs, where
|E|(number of edges) is much less than|V|^2(square of number of vertices), adjacency lists use less space than adjacency matrices. - Edge List Traversal: It is straightforward to iterate over the neighbors of a vertex.
- Edge Existence: Checking if an edge exists between two vertices is slower compared to adjacency matrices (O(V) instead of O(1)).
- Complex Edge Operations: Operations that require knowing about all edges in the graph may be slower compared to matrix representation.
- Network Design: Used to model and analyze computer networks, containing nodes as computers and links as edges.
- Social Networks: Modeling relationships between individuals or entities.
- Routing Algorithms: Algorithms like Dijkstra’s and Bellman-Ford use graphs to find the shortest path between nodes.
- Biological Systems: Used in genomics and systems biology to model cellular networks and protein interactions.
- Recommendation Systems: Graph-based models enhancing recommendation engines by capturing relationships and preferences between users and items.

