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++
usingnamespacestd;// Function to return adjacency list of undirected graphvector<vector<int>>printGraph(intV,vector<pair<int,int>>&edges){// Initialize adjacency list with V empty listsvector<vector<int>>adj(V);// Traverse all edgesfor(autoedge:edges){intu=edge.first;intv=edge.second;// Add edge to adjacency list (undirected)adj[u].push_back(v);adj[v].push_back(u);}returnadj;}intmain(){intV=4;vector<pair<int,int>>edges={{0,3},{0,2},{2,1}};vector<vector<int>>adj=printGraph(V,edges);// Print adjacency listfor(inti=0;i<V;i++){cout<<i<<": ";for(intx:adj[i])cout<<x<<" ";cout<<endl;}return0;}
Java
importjava.util.ArrayList;importjava.util.List;classGFG{// Function to return adjacency list of undirected graphpublicList<List<Integer>>printGraph(intV,int[][]edges){// Initialize adjacency listList<List<Integer>>adj=newArrayList<>();for(inti=0;i<V;i++)adj.add(newArrayList<>());// Traverse all edgesfor(inti=0;i<edges.length;i++){intu=edges[i][0];intv=edges[i][1];// Add edge to adjacency list (undirected)adj.get(u).add(v);adj.get(v).add(u);}returnadj;}publicstaticvoidmain(String[]args){GFGobj=newGFG();intV=4;int[][]edges={{0,3},{0,2},{2,1}};List<List<Integer>>adj=obj.printGraph(V,edges);// Print adjacency listfor(inti=0;i<V;i++){System.out.print(i+": ");for(intx:adj.get(i))System.out.print(x+" ");System.out.println();}}}
Python
fromtypingimportList# Function to create adjacency list of undirected graphdefprintGraph(V:int,edges:List[List[int]])->List[List[int]]:# Initialize adjacency listadj=[[]for_inrange(V)]# Traverse all edgesfornode1,node2inedges:# Add edge to adjacency list (undirected)adj[node1].append(node2)adj[node2].append(node1)returnadjif__name__=="__main__":V=4edges=[[0,3],[0,2],[2,1]]adj=printGraph(V,edges)# Print adjacency listforiinrange(V):print(i,":",*adj[i])
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// Function to return adjacency list of undirected graphpublicList<List<int>>printGraph(intV,int[,]edges){// Initialize adjacency listList<List<int>>adj=newList<List<int>>();for(inti=0;i<V;i++)adj.Add(newList<int>());// Traverse all edgesfor(inti=0;i<edges.GetLength(0);i++){intu=edges[i,0];intv=edges[i,1];// Add edge to adjacency list (undirected)adj[u].Add(v);adj[v].Add(u);}returnadj;}publicstaticvoidMain(string[]args){GFGobj=newGFG();intV=4;int[,]edges={{0,3},{0,2},{2,1}};List<List<int>>adj=obj.printGraph(V,edges);// Print adjacency listfor(inti=0;i<V;i++){Console.Write(i+": ");foreach(intxinadj[i])Console.Write(x+" ");Console.WriteLine();}}}
JavaScript
// Function to create adjacency list of undirected graphfunctionprintGraph(V,edges){// Initialize adjacency listconstgraph=newArray(V).fill().map(()=>[]);// Traverse all edgesfor(constedgeofedges){const[src,dest]=edge;graph[src].push(dest);graph[dest].push(src);}returngraph;}// Driver codeconstV=4;constedges=[[0,3],[0,2],[2,1]];constadj=printGraph(V,edges);// Print adjacency listfor(leti=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)