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.
Description
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:
 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.
Pseudocode
class Graph
Initialize class variables:
vertices
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

Initialize a
Graph
class with a constructor that accepts the number of vertices. Inside the constructor, create an emptyadgeList
vector with as many elements as there are vertices. 
Implement the
addEdge(u, v)
function to add an edge from vertexu
to vertexv
. Before adding the edge, make sure that bothu
andv
are valid vertex indices. 
Implement the
printGraph()
function to iterate through each vertex and its correspondingadgeList
. Print the vertex's index and then iterate through theadgeList
, printing the neighbors.
Code Solution

1) Implement adjacency list in directed graph using vector in java
2) Implement adjacency list in directed graph using vector in c++
3) Implement adjacency list in directed graph using list in c#
4) Implement adjacency list in directed graph using array in php
5) Implement adjacency list in directed graph using list in python
6) Implement adjacency list in directed graph in ruby
7) Implement adjacency list in directed graph using array in node js
8) Implement adjacency list in directed graph using array in typescript
9) Implement adjacency list in directed graph using list in vb.net
10) Implement adjacency list in directed graph using ArrayBuffer in scala
11) Implement adjacency list in directed graph using array in swift
12) Implement adjacency list in directed graph using MutableList in kotlin
13) Efficiently implement adjacency list in directed graph in golang
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