Posted on by Kalkicode
Code Graph

Efficiently implementing adjacency list in directed graph

In computer science, graphs are a fundamental data structure used to represent relationships between different entities. A directed graph, also known as a digraph, is a type of graph where edges have a specific direction from one node to another. An adjacency list is a common way to represent a graph, where each node is associated with a list of its neighboring nodes.

Problem Statement

The problem at hand involves efficiently implementing an adjacency list for a directed graph. This means creating a data structure that stores the relationships between nodes in a way that allows for quick access to neighboring nodes.


Consider a scenario where you want to represent a network of connections, such as a social media platform where users are connected to each other. This network can be modeled as a directed graph, where users are nodes and connections between users are directed edges.

For instance, let's take a simplified social network with 10 users. The following connections exist:

Adjacency list representation of directed graph
  • User 0 is connected to users 1, 2, and 7.
  • User 1 is connected to users 5 and 7.
  • User 2 is connected to users 3 and 7.
  • User 3 is connected to users 4 and 8.
  • User 4 is connected to user 9.
  • User 5 is connected to users 6 and 8.
  • User 6 is connected to users 8 and 9.
  • User 7 is connected to user 8.

Solution Idea

To efficiently implement an adjacency list for this directed graph, we use a vector of vectors. In Java, the Vector class is used to create dynamic arrays, similar to lists in Python. The outer vector represents the nodes, and each inner vector represents the neighbors of a node.


class Graph
    Initialize class variables:
        adgeList (a vector of vectors)
    Initialize constructor:
        Accept the number of vertices
        Initialize adgeList with empty vectors
    Function addEdge(u, v):
        Check if u and v are valid vertices
        Add v to the adgeList of u
    Function printGraph():
        Iterate through vertices:
            Print vertex
            Iterate through adgeList of vertex:
                Print neighbors

Algorithm Explanation

  1. Initialize a Graph class with a constructor that accepts the number of vertices. Inside the constructor, create an empty adgeList vector with as many elements as there are vertices.

  2. Implement the addEdge(u, v) function to add an edge from vertex u to vertex v. Before adding the edge, make sure that both u and v are valid vertex indices.

  3. Implement the printGraph() function to iterate through each vertex and its corresponding adgeList. Print the vertex's index and then iterate through the adgeList, printing the neighbors.

Code Solution

Time Complexity

  • Creating the graph and adding edges both have a time complexity of O(1), as they involve simple operations.
  • Printing the graph takes O(V + E) time, where V is the number of vertices and E is the total number of edges. This is because each vertex and its neighboring edges are visited once.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment