C++ Program to Implement Adjacency List
An adjacency list is a way to represent a graph data structure in C++ using an array of linked lists. Each index of the array represents a vertex, and each element in its linked list represents the other vertices that form an edge with the vertex. In this article, we will learn how to implement an adjacency list in C++.
Implement Adjacency List in C++
One of the most efficient ways to represent a graph in C++ is through the adjacency list. Before we learn the approach for implementing the adjacency list for graphs let us understand how the adjacency list is represented in C++. Let’s assume there are n vertices in the graph So, create an array of list of size n as adjList[n].
- adjList[0] will have all the nodes which are connected to vertex 0, called as neighbour vertex 0.
- adjList[1] will have all the nodes which are connected to vertex 1 , and so on.
Representation of Undirected Graph to Adjacency List

Consider the above undirected graph with three nodes labeled 0, 1, and 2. Here, node 0 is connected to both nodes 1 and 2. We need to first create an array of lists of size 3 (because there are 3 vertices in the graph).
- Vertex 0: Neighbors are 1 and 2. Insert vertex 1 and 2 at index 0 of the array.
- Vertex 1: Neighbors are 2 and 0. Insert vertex 0 and 2 at index 1 of the array.
- Vertex 2: Neighbors are 1 and 0. Insert vertex 0 and 1 at index 2 of the array.
Representation of Directed Graph to Adjacency List

Consider the above directed graph with three nodes labeled 0, 1, and 2. We need to first create an array of lists of size 3 (because there are 3 vertices in the graph).
- Vertex 0: No neighbors. Leave index 0 of the array empty.
- Vertex 1: Neighbors are 0 and 2. Insert vertex 0 and 2 at index 1 of the array.
- Vertex 2: Neighbors are 0. Insert vertex 0 at index 2 of the array.
Algorithm to Implement Adjacency List
- Define the number of vertices V in the graph.
- Create an array of linked lists adjList of size V.
- Initialize each element of adjList to an empty list.
- Define a function addEdge(u, v) to add an edge between two vertices:
- Check if u and v are valid vertices (0 <= u, v < V).
- If valid, add v to the linked list of u.
- Also add u to the linked list of v as this is an undirected graph.
- Define a function printAdjList() to print the adjacency list.
- Iterate through the array using a loop and print the linked list for each vertex.
C++ Program to Implement Adjacency List
The following program illustrates how we can implement an adjacency list in C++.
// C++ Program to Implement Adjacency List
#include <iostream>
#include <vector>
using namespace std;
// Class representing a graph
class Graph {
private:
// Adjacency list to represent the graph
vector<vector<int> > adjList;
// Boolean flag to indicate if the graph is directed
bool isDirected;
public:
// Constructor to initialize the graph
// Parameters: vertices - number of vertices in the
// graph
// directed - flag to indicate if the graph is directed
// (default is false)
Graph(int vertices, bool directed = false)
{
// Resize the adjacency list to accommodate the
// vertices
adjList.resize(vertices);
// Set the directed flag
isDirected = directed;
}
// Function to add an edge to the graph
// Parameters: src - source vertex
// dest - destination vertex
void addEdge(int src, int dest)
{
// Add the destination to the adjacency list of the
// source
adjList[src].push_back(dest);
// If the graph is undirected
if (!isDirected) {
// Add the source to the adjacency list of the
// destination
adjList[dest].push_back(src);
}
}
// Function to print the adjacency list of the graph
void printGraph()
{
// Iterate through each vertex
for (int i = 0; i < adjList.size(); ++i) {
// Print the vertex
cout << i << ": ";
// Iterate through the adjacency list of the
// vertex
for (int j = 0; j < adjList[i].size(); ++j) {
// Print each adjacent vertex
cout << adjList[i][j] << " -> ";
}
// Indicate the end of the adjacency list
cout << "NULL" << endl;
}
}
};
int main()
{
// Number of vertices in the graph
int vertices = 3;
// Undirected Graph Example
Graph undirectedGraph(vertices);
undirectedGraph.addEdge(0, 1);
undirectedGraph.addEdge(0, 2);
undirectedGraph.addEdge(1, 2);
// Print the adjacency list of the undirected graph
cout << "Undirected Graph Adjacency List:" << endl;
undirectedGraph.printGraph();
cout << endl;
// Directed Graph Example
Graph directedGraph(vertices, true);
directedGraph.addEdge(1, 2);
directedGraph.addEdge(1, 0);
directedGraph.addEdge(2, 0);
// Print the adjacency list of the directed graph
cout << "Directed Graph Adjacency List:" << endl;
directedGraph.printGraph();
return 0;
}
Output
Undirected Graph Adjacency List: 0: 1 -> 2 -> NULL 1: 0 -> 2 -> NULL 2: 0 -> 1 -> NULL Directed Graph Adjacency List: 0: NULL 1: 2 -> 0 -> NULL 2: 0 -> NULL
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space: O(V + E)
Related Articles:
- Adjacency List meaning & definition in DSA
- Graph and its representations
- Print Adjacency List for a Directed Graph