Adjacency List from Edges (original) (raw)
Last Updated : 5 Apr, 2026
Given an undirected graph with **V vertices (0-based indexing) and **edges, create an adjacency list of the graph.
**Examples:
**Input: V = 5, edges = [[0,1], [0,4], [4,1], [4,3], [1,3], [1,2], [3,2]]
**Output: [[1,4], [0,2,3,4], [1,3], [1,2,4], [0,1,3]]
**Explanation:
- Node 0 is connected to 1 and 4.
- Node 1 is connected to 0,2,3 and 4.
- Node 2 is connected to 1 and 3.
- Node 3 is connected to 1,2 and 4.
- Node 4 is connected to 0,1 and 3.
**Input: V = 4, edges = [[0,3], [0,2], [2,1]]
**Output: [[2,3], [2], [0,1], [0]]
**Explanation:
- Node 0 is connected to 2 and 3.
- Node 1 is only connected to 2.
- Node 2 is connected to 0 and 1.
- Node 3 is only connected to 0.
Consider the graph **V = 4, **edges = [[0,3], [0,2], [2,1]]. Here are the steps to build its adjacency list.

**Step 1: Initialize empty lists for each vertex: 0: [], 1: [], 2: [], 3: [], 4: []
**Step 2: For each edge [u, v], since this is undirected, we add v to u's list and u to v's list.
- Edge [0,3] - Add 3 to 0’s list and 0 to 3’s list: 0: [3], 1: [], 2: [], 3: [0]
- Edge [0,2] - Add 2 to 0’s list and 0 to 2’s list: 0: [3, 2], 1: [], 2: [0], 3: [0]
- Edge [2,1] - Add 1 to 2’s list and 2 to 1’s list: 0: [3, 2], 1: [2], 2: [0, 1], 3: [0]
**Step 3: Final adjacency list: 0: [3, 2], 1: [2], 2: [0, 1], 3: [0]. Each list represents the neighbors of that vertex.
C++ `
using namespace std;
// Function to return adjacency list of undirected graph vector<vector> printGraph(int V, vector<pair<int, int>> &edges) {
// Initialize adjacency list with V empty lists
vector<vector<int>> adj(V);
// Traverse all edges
for (auto edge : edges)
{
int u = edge.first;
int v = edge.second;
// Add edge to adjacency list (undirected)
adj[u].push_back(v);
adj[v].push_back(u);
}
return adj;}
int main() { int V = 4; vector<pair<int, int>> edges = { { 0, 3 }, { 0, 2 }, { 2, 1 } };
vector<vector<int>> adj = printGraph(V, edges);
// Print adjacency list
for (int i = 0; i < V; i++)
{
cout << i << ": ";
for (int x : adj[i])
cout << x << " ";
cout << endl;
}
return 0;}
Java
import java.util.ArrayList; import java.util.List;
class GFG {
// Function to return adjacency list of undirected graph
public List<List<Integer> > printGraph(int V,
int[][] edges)
{
// Initialize adjacency list
List<List<Integer> > adj = new ArrayList<>();
for (int i = 0; i < V; i++)
adj.add(new ArrayList<>());
// Traverse all edges
for (int i = 0; i < edges.length; i++) {
int u = edges[i][0];
int v = edges[i][1];
// Add edge to adjacency list (undirected)
adj.get(u).add(v);
adj.get(v).add(u);
}
return adj;
}
public static void main(String[] args)
{
GFG obj = new GFG();
int V = 4;
int[][] edges = { { 0, 3 }, { 0, 2 }, { 2, 1 } };
List<List<Integer> > adj = obj.printGraph(V, edges);
// Print adjacency list
for (int i = 0; i < V; i++) {
System.out.print(i + ": ");
for (int x : adj.get(i))
System.out.print(x + " ");
System.out.println();
}
}}
Python
from typing import List
Function to create adjacency list of undirected graph
def printGraph(V: int, edges: List[List[int]]) -> List[List[int]]:
# Initialize adjacency list
adj = [[] for _ in range(V)]
# Traverse all edges
for node1, node2 in edges:
# Add edge to adjacency list (undirected)
adj[node1].append(node2)
adj[node2].append(node1)
return adjif name == "main": V = 4 edges = [[0,3], [0,2], [2,1]] adj = printGraph(V, edges)
Print adjacency list
for i in range(V):
print(i, ":", *adj[i])C#
using System; using System.Collections.Generic;
class GFG {
// Function to return adjacency list of undirected graph
public List<List<int> > printGraph(int V, int[, ] edges)
{
// Initialize adjacency list
List<List<int> > adj = new List<List<int> >();
for (int i = 0; i < V; i++)
adj.Add(new List<int>());
// Traverse all edges
for (int i = 0; i < edges.GetLength(0); i++) {
int u = edges[i, 0];
int v = edges[i, 1];
// Add edge to adjacency list (undirected)
adj[u].Add(v);
adj[v].Add(u);
}
return adj;
}
public static void Main(string[] args)
{
GFG obj = new GFG();
int V = 4;
int[, ] edges= { { 0, 3 }, { 0, 2 }, { 2, 1 } };
List<List<int> > adj = obj.printGraph(V, edges);
// Print adjacency list
for (int i = 0; i < V; i++) {
Console.Write(i + ": ");
foreach(int x in adj[i]) Console.Write(x + " ");
Console.WriteLine();
}
}}
JavaScript
// Function to create adjacency list of undirected graph function printGraph(V, edges) {
// Initialize adjacency list
const graph = new Array(V).fill().map(() => []);
// Traverse all edges
for (const edge of edges) {
const [src, dest] = edge;
graph[src].push(dest);
graph[dest].push(src);
}
return graph;}
// Driver code const V = 4; const edges = [[0,3], [0,2], [2,1]]; const adj = printGraph(V, edges);
// Print adjacency list for (let i = 0; i < V; i++) { console.log(i, ":", ...adj[i]); }
`
Output
0: 3 2 1: 2 2: 0 1 3: 0
**Time Complexity: O(V+E), where V = number of vertices, E = number of edges.
**Auxiliary Space: O(V + E) (average case), O(V²) (worst case if fully connected)
