C++ Program to Implement Adjacency List



An adjacency list of graph is a collection of unordered lists, that represents a finite graph data structure using linked lists. Each list in the collection represents one of the vertex of the graph and it will store the adjacent vertices of that vertex. In this article we will learn how to implement adjacency list for a graph using C++ program. First of all, let's see an example of adjacency list.

Example of Adjacency List

To understand what is an adjacency list is, first we need to take a graph data structure for reference. The image below represent a simple undirected graph with 6 vertices and 8 edges.

Graph

In the above graph there are edges between 0-3, 0-4, 1-2, 1-5, 1-4, 2-3, 2-5, 3-5, and 4-5, which means in the adjacency list, the list 0 will store 3 and 4, list 1 will store 2, 4 and 5, and so on. The image below shows the exact adjacency list of given graph.

Adjacency List

The adjacency list of the above graph is shown below.

Adjacency List

Steps to Implement Adjacency List

The following are the steps/algorithm to implement adjacency list for a graph.

  • Step 1: Start
  • Step 2: Input the number of vertices V and number of edges E in the graph
  • Step 3: Create an array of lists (adjList[V]) to store adjacent vertices for each vertex
  • Step 4: For each edge, repeat the steps 5,6 and 7.
  • Step 5: Input the pair of vertices (u, v) that form an edge
  • Step 6: Add v to the adjacency list of u
  • Step 7: If the graph is undirected, also add u to the adjacency list of v
  • Step 8: Display the adjacency list for each vertex
  • Step 9: End

C++ Program to Implement Adjacency List

The following C++ program shows how to create an adjacency list for a graph. The program allows the user to input the number of vertices and edges, and then it displays the adjacency list.

#include <iostream>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    // Assumed values
    int V = 5; // 5 vertices numbered 0 to 4
    int E = 6; // 6 edges
    bool isDirected = false; // Change to true for directed graph

    cout << "Number of vertices: " << V << endl;
    cout << "Number of edges: " << E << endl;
    cout << "Is the graph directed? : " << (isDirected ? "Yes" : "No") << endl;

    // Create adjacency list
    vector<int> adjList[V];

    // Assumed edges
    int edges[6][2] = {
        {0, 1},
        {0, 2},
        {1, 2},
        {1, 3},
        {2, 4},
        {3, 4}
    };

    cout << "Assumed edges (from -> to):" << endl;
    for (int i = 0; i < E; i++) {
        cout << edges[i][0] << " -> " << edges[i][1] << endl;
        adjList[edges[i][0]].push_back(edges[i][1]);
        if (!isDirected) {
            adjList[edges[i][1]].push_back(edges[i][0]);
        }
    }

    // Display adjacency list
    cout << "\nAdjacency List:\n" << endl;
    for (int i = 0; i < V; i++) {
        cout << i << " -> ";
        for (int neighbor : adjList[i]) {
            cout << neighbor << " ";
        }
        cout << endl;
    }

    return 0;
}

The output of the above code will be:

Number of vertices: 5
Number of edges: 6
Is the graph directed? : No
Assumed edges (from -> to):
0 -> 1
0 -> 2
1 -> 2
1 -> 3
2 -> 4
3 -> 4
Adjacency List:
0 -> 1 2 
1 -> 0 2 3 
2 -> 0 1 4 
3 -> 1 4 
4 -> 2 3 

Complexity of Adjacency List Representation

This implementation takes O(V+2E) for undirected graph, and O(V+E) for directed graph. If the number of edges are increased, then the required space will also be increased.

Updated on: 2025-04-28T18:18:09+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements