Print Adjacency List for a Directed Graph
An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices which the particular vertex has an edge to.
Problem: Given the adjacency list and number of vertices and edges of a graph, the task is to represent the adjacency list for a directed graph.
Examples:
Input: V = 3, edges[][]= {{0, 1}, {1, 2} {2, 0}}
Output: 0 -> 1
1 -> 2
2 -> 0
Explanation:
The output represents the adjacency list for the given graph.
Input: V = 4, edges[][] = {{0, 1}, {1, 2}, {1, 3}, {2, 3}, {3, 0}}
Output: 0 -> 1
1 -> 2 3
2 -> 3
3 -> 0
Explanation:
The output represents the adjacency list for the given graph.
Approach (using STL): The main idea is to represent the graph as an array of vectors such that every vector represents the adjacency list of a single vertex. Using STL, the code becomes simpler and easier to understand.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector< int > adj[], int u, int v)
{
adj[u].push_back(v);
}
void adjacencylist(vector< int > adj[], int V)
{
for ( int i = 0; i < V; i++) {
cout << i << "->" ;
for ( int & x : adj[i]) {
cout << x << " " ;
}
cout << endl;
}
}
void initGraph( int V, int edges[3][2], int noOfEdges)
{
vector< int > adj[V];
for ( int i = 0; i < noOfEdges; i++) {
addEdge(adj, edges[i][0], edges[i][1]);
}
adjacencylist(adj, V);
}
int main()
{
int V = 3;
int edges[3][2] = { { 0, 1 }, { 1, 2 }, { 2, 0 } };
int noOfEdges = 3;
initGraph(V, edges, noOfEdges);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void addEdge(Vector<Integer> adj[], int u, int v)
{
adj[u].add(v);
}
static void adjacencylist(Vector<Integer> adj[], int V)
{
for ( int i = 0 ; i < V; i++) {
System.out.print(i+ "->" );
for ( int x : adj[i]) {
System.out.print(x+ " " );
}
System.out.println();
}
}
static void initGraph( int V, int edges[][], int noOfEdges)
{
@SuppressWarnings ( "unchecked" )
Vector<Integer> adj[] = new Vector[ 3 ];
for ( int i = 0 ;i<adj.length;i++) {
adj[i] = new Vector<>();
}
for ( int i = 0 ; i < noOfEdges; i++) {
addEdge(adj, edges[i][ 0 ], edges[i][ 1 ]);
}
adjacencylist(adj, V);
}
public static void main(String[] args)
{
int V = 3 ;
int edges[][] = { { 0 , 1 }, { 1 , 2 }, { 2 , 0 } };
int noOfEdges = 3 ;
initGraph(V, edges, noOfEdges);
}
}
|
Python3
def addEdge(adj, u, v):
adj[u].append(v)
def adjacencylist(adj, V):
for i in range ( 0 , V):
print (i, "->" , end = "")
for x in adj[i]:
print (x , " " , end = "")
print ()
def initGraph(V, edges, noOfEdges):
adj = [ 0 ] * 3
for i in range ( 0 , len (adj)):
adj[i] = []
for i in range ( 0 , noOfEdges) :
addEdge(adj, edges[i][ 0 ], edges[i][ 1 ])
adjacencylist(adj, V)
V = 3
edges = [[ 0 , 1 ], [ 1 , 2 ], [ 2 , 0 ]]
noOfEdges = 3 ;
initGraph(V, edges, noOfEdges)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void addEdge(List< int > []adj, int u, int v)
{
adj[u].Add(v);
}
static void adjacencylist(List< int > []adj, int V)
{
for ( int i = 0; i < V; i++) {
Console.Write(i+ "->" );
foreach ( int x in adj[i]) {
Console.Write(x+ " " );
}
Console.WriteLine();
}
}
static void initGraph( int V, int [,]edges, int noOfEdges)
{
List< int > []adj = new List< int >[3];
for ( int i = 0; i < adj.Length; i++) {
adj[i] = new List< int >();
}
for ( int i = 0; i < noOfEdges; i++) {
addEdge(adj, edges[i,0], edges[i,1]);
}
adjacencylist(adj, V);
}
public static void Main(String[] args)
{
int V = 3;
int [,]edges = { { 0, 1 }, { 1, 2 }, { 2, 0 } };
int noOfEdges = 3;
initGraph(V, edges, noOfEdges);
}
}
|
Javascript
<script>
function addEdge( adj , u , v) {
adj[u].push(v);
}
function adjacencylist(adj , V) {
for ( var i = 0; i < V; i++) {
document.write(i + "->" );
for ( var x of adj[i]) {
document.write(x + " " );
}
document.write( "<br/>" );
}
}
function initGraph(V , edges , noOfEdges) {
var adj = Array(3);
for ( var i = 0; i < adj.length; i++) {
adj[i] = Array();
}
for (i = 0; i < noOfEdges; i++) {
addEdge(adj, edges[i][0], edges[i][1]);
}
adjacencylist(adj, V);
}
var V = 3;
var edges = [ [ 0, 1 ], [ 1, 2 ], [ 2, 0 ]];
var noOfEdges = 3;
initGraph(V, edges, noOfEdges);
</script>
|
Time Complexity: O(V+E), where V = no. of vertices and E = no. of edges.
Auxiliary Space: For avg case, it’s O(V+E). But in the worst case, it’s O(V^2) when each vertex is connected to all the other vertices.
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...