# Adjacency list representation of graph

In Programming language graph is represented in a two ways. Such as Adjacency list Adjacency matrix. In this post are mentioning example of Adjacency list of Directed and Undirected graph.

## Directed Graph Adjacency list

Here given code implementation process.

``````//Adjacency list representation of
//Directed graph
#include<stdio.h>
#include<stdlib.h>

struct AjlistNode{
int vId;//Vertices id
struct AjlistNode*next;
};

struct Graph{
int data; //node key value
struct AjlistNode*next;
};
void setData(struct Graph *);
int size=5;

//set node key value
void setData(struct Graph*node){
if(node!=NULL && size>0){
int index=0;
for(index;index<size;index++){
//set vertic node data
node[index].data=index;//set node key
//Initial no AjlistNode
//set NULL Value
node[index].next=NULL;
}
}else{
printf("Vertic Node is Empty");
}
}
//Add Edge from Two given Nodes
void addEdge(struct Graph*node, int V ,int E){
//add edge form V to E
//V and E is Node location
if(V<size && E <size){
//first create Adjacency node
struct AjlistNode *newEdge=(struct AjlistNode*)malloc(sizeof(struct AjlistNode));
if(newEdge!=NULL){
newEdge->next=node[V].next;
newEdge->vId=E;
node[V].next=newEdge;
}else{
printf("\n Memory overflow");
}
}else{
//not valid Vertices
printf("Invalid Node Vertices %d  %d", V,E);
}
}
//Display Adjacency list of vertex
void printGraph(struct Graph*node){
if(node!=NULL){
struct AjlistNode *temp=NULL;
for(int index=0;index<size;index++){
printf("\n Adjacency list of vertex %d  :",index);
temp=node[index].next;
while(temp!=NULL){
//temp->vId is graph node vertices
//in this case temp->vId is same as
//node[temp->vId].data

printf("  %d",node[temp->vId].data);
temp=temp->next;
}
}
}else{
printf("Empty Graph");
}
}
int main(){
struct Graph*node=NULL;
node=(struct Graph*)malloc(sizeof(struct Graph)*size);

if(node==NULL){
printf("\n Memory overflow");
}else{
//First set node keys
setData(node);
//Connected two node with Edges

printGraph(node);
}
return 0;
}``````

#### Output

`````` Adjacency list of vertex 0  :  1
Adjacency list of vertex 1  :  4  2
Adjacency list of vertex 2  :  3  0
Adjacency list of vertex 3  :
Adjacency list of vertex 4  :  3``````
``````//C++ Graph represented by Adjacency List
//In Directed Graph
#include<iostream>
using namespace std;
struct AjlistNode{
int vId;//Vertices id
struct AjlistNode*next;
};
struct Vertices{
int data; //node key value
struct AjlistNode*next;
};
class Graph{
Vertices *node;
int size;//number of
public:
Graph(int);
void setData();
void printGraph();
};
Graph::Graph(int size){
this->size=size;
//set number of nodes
node=new Vertices[size];
}
//set node key value
void Graph:: setData(){
if(node!=NULL){
int index=0;
for(index;index<size;index++){
//set vertic node data
node[index].data=index;//set node key
//Initial no AjlistNode
//set NULL Value
node[index].next=NULL;
}
}else{
cout<<"Vertic Node is Empty"<<endl;
}
}
//Add Edge from Two given Nodes
void Graph ::addEdge(int V ,int E){
//add edge form V to E
//V and E is Node location
if(V<size && E <size){
//first create Adjacency node
AjlistNode *newEdge=new AjlistNode;
if(newEdge!=NULL){
newEdge->next=node[V].next;
newEdge->vId=E;
node[V].next=newEdge;
}
}else{
//not valid Vertices
cout<<"Invalid Node Vertices "<< V<<" "<<E;
}
}
//Display Adjacency list of vertex
void Graph::printGraph(){
if(node!=NULL){
AjlistNode *temp=NULL;
for(int index=0;index<size;index++){
cout<<"\n Adjacency list of vertex "<<index<<" :";
temp=node[index].next;
while(temp!=NULL){
//temp->vId is graph node vertices
//in this case temp->vId is same as
//node[temp->vId].data

cout<<" "<<node[temp->vId].data;
temp=temp->next;
}
}
}else{
cout<<"Empty Graph"<<endl;
}
}
int main(){
//Create Object
Graph g(5);
//First set node keys
g.setData();
//Connected two node with Edges
g.printGraph();
return 0;
}``````

#### Output

`````` Adjacency list of vertex 0 : 1
Adjacency list of vertex 1 : 4 2
Adjacency list of vertex 2 : 3 0
Adjacency list of vertex 3 :
Adjacency list of vertex 4 : 3``````
``````//Java Graph represented by Adjacency List
public class MyGraph{

static class AjlistNode{
int id;//Vertices node key
AjlistNode next;
}
static class Vertices{
int data;
AjlistNode next;
}

//number of Vertices
static int size;
Vertices node[];

MyGraph(int size){
//set value
this.size=size;
node=new Vertices[size];
}

//set initial node value
public void setData(){
if(node==null){
System.out.println("\nEmpty Graph");
}else{
for(int index=0;index<size;index++){
// avoid java.lang.NullPointerException
node[index]=new Vertices();
node[index].next=null;
}
}
}

public void addEdge(int S,int E){
if(S<size &&E<size &&node!=null){
AjlistNode newEdge=new AjlistNode();
newEdge.id=E;//end node
newEdge.next=node[S].next;
node[S].next=newEdge;
}else{
System.out.println("\nEmpty Graph");
}
}

public void printGraph(){

if(size>0&&node!=null){
for(int index=0;index<size;index++){
System.out.print("\nAdjacency list of vertex "+index+" :");
AjlistNode temp=node[index].next;
while(temp!=null){
System.out.print(" "+node[temp.id].data);
temp=temp.next;
}
}
}
}

public static void main(String[] args) {
int totalNode=5;

MyGraph g=new MyGraph(totalNode);
g.setData();
//Connected two node with Edges
g.printGraph();

}
}``````

#### Output

``````Adjacency list of vertex 0 : 1
Adjacency list of vertex 1 : 4 2
Adjacency list of vertex 2 : 3 0
Adjacency list of vertex 3 :
Adjacency list of vertex 4 : 3``````
``````#Python implement graph using Adjacency List
#In Directed Graph
def __init__(self,data):
self.id=data
self.next=None

class Vertices:
def __init__(self,data):
self.data=data
self.next=None

class Graph:
"""Constructor for Graph"""
def __init__(self, size):
self.size=size
self.node=[];

def setData(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
self.node.append(Vertices(index))
index+=1

#S and E is two nodes
if(self.size>S and self.size>E):
new_edge.next=self.node[S].next
self.node[S].next=new_edge
else:
print("Invalid nodes")

def printGraph(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
print("\nAdjacency list of vertex {0} :".format(index),end=" ")
temp=self.node[index].next
while temp!=None:
print("  {0}".format(temp.id),end=" ")
temp=temp.next
index+=1

def main():
g=Graph(5)
g.setData();
#Connected two node with Edges
g.printGraph();

if __name__=="__main__":
main()``````

#### Output

``````Adjacency list of vertex 0 :   1
Adjacency list of vertex 1 :   4   2
Adjacency list of vertex 2 :   3   0
Adjacency list of vertex 3 :
Adjacency list of vertex 4 :   3``````
``````//C# Graph represented by Adjacency List
//In Directed Graph
using System;
class AjlistNode{
public int key;
public AjlistNode next;
public AjlistNode(int key){
this.key=key;
this.next=null;
}
}
class Node{
public int data;
public AjlistNode next;
public Node(int data){
this.data=data;
this.next=null;

}
}
class MyGraph{
//empty array
public Node[]node= new Node[] {};
public int size;
public MyGraph(int size){
this.size=size;
node=new Node[size];
}
public void setData(){
if(size>0 && this.node!=null){
for(int index=0;index<size;index++){
node[index]=new Node(index);
}

}
}
public void addEdge(int start,int end){
if(this.size>start && this.size>end){
AjlistNode newEdge=new AjlistNode(end);
newEdge.next=node[start].next;
node[start].next=newEdge;
}
}
public void printGraph(){
if(size>0 && node.Length>0 && node!=null){
for(int index=0;index<size;index++){
Console.Write("\nAdjacency list of vertex {0} :",index);
AjlistNode temp=node[index].next;
while(temp!=null){
Console.Write(" "+node[temp.key].data);
temp=temp.next;
}
}
}
}

}
class Program{

static void Main(string[] args){
//create object
MyGraph g=new MyGraph(5);
g.setData();
//Connected two node with Edges
g.printGraph();
}
}
``````

#### Output

``````Adjacency list of vertex 0 : 1
Adjacency list of vertex 1 : 4 2
Adjacency list of vertex 2 : 3 0
Adjacency list of vertex 3 :
Adjacency list of vertex 4 : 3
``````
``````<?php
/*
* PHP Program Adjacency List in Directed Graph
*/

class AjlistNode{
public \$key;
public \$next;
function __construct(\$key){
\$this->key=\$key;
\$this->next=NULL;
}
}
class Node{
public \$data;
public \$next;
function __construct(\$data){
\$this->data=\$data;
\$this->next=NULL;

}
}
class MyGraph{

public \$node;
public \$size;
function __construct(\$size){
\$this->size=\$size;
\$this->node=[];  //empty array
}
public function setData(){
if(\$this->size>0){
for(\$index=0;\$index<\$this->size;\$index++){
\$this->node[\$index]=new Node(\$index);
}

}
}
if(\$this->size>\$start && \$this->size>\$end){
\$newEdge=new AjlistNode(\$end);
\$newEdge->next=\$this->node[\$start]->next;
\$this->node[\$start]->next=\$newEdge;
}
}
public function printGraph(){
if(\$this->size>0 && count(\$this->node)>0 && \$this->node!=NULL){
for(\$index=0;\$index<\$this->size;\$index++){
echo ("<br>Adjacency list of vertex ".\$index." : ");
\$temp=\$this->node[\$index]->next;
while(\$temp!=NULL){
echo (" ".\$this->node[\$temp->key]->data);
\$temp=\$temp->next;
}
}
}
}

}

function main(){
//create object
\$g=new MyGraph(5);
\$g->setData();

//Connected two node with Edges
\$g->printGraph();

}
main();
?>``````

#### Output

``````Adjacency list of vertex 0 : 1
Adjacency list of vertex 1 : 4 2
Adjacency list of vertex 2 : 3 0
Adjacency list of vertex 3 :
Adjacency list of vertex 4 : 3``````

## Un-Directed Graph Adjacency list

``````//Adjacency list representation of
//Un-Directed graph
#include<stdio.h>
#include<stdlib.h>

struct AjlistNode{
int vId;//Vertices id
struct AjlistNode*next;
};

struct Graph{
int data; //node key value
struct AjlistNode*next;
};

int size=5; //number of nodes

//set node key value
void setData(struct Graph*node){
if(node!=NULL && size>0){
int index=0;
for(index;index<size;index++){
//set vertic node data
node[index].data=index;//set node key
//Initial no AjlistNode
//set NULL Value
node[index].next=NULL;
}
}else{
printf("Vertic Node is Empty");
}
}
//Add Edge from Two given Nodes
void addEdge(struct Graph*node, int V ,int E){
//add edge form V to E
//V and E is Node location
if(V<size && E <size){
//first create Adjacency node
struct AjlistNode *newEdge=(struct AjlistNode*)malloc(sizeof(struct AjlistNode));
if(newEdge!=NULL){

newEdge->next=node[V].next;
newEdge->vId=E;
node[V].next=newEdge;
}else{
printf("\n Memory overflow");
}

//Second node age
newEdge=(struct AjlistNode*)malloc(sizeof(struct AjlistNode));
if(newEdge!=NULL){
newEdge->next=node[E].next;
newEdge->vId=V;
node[E].next=newEdge;
}else{
printf("\n Memory overflow");
}

}else{
//not valid Vertices
printf("Invalid Node Vertices %d  %d", V,E);
}
}
//Display Adjacency list of vertex
void printGraph(struct Graph*node){
if(node!=NULL){
struct AjlistNode *temp=NULL;
for(int index=0;index<size;index++){
printf("\n Adjacency list of vertex %d  :",index);
temp=node[index].next;
while(temp!=NULL){
//temp->vId is graph node vertices
//in this case temp->vId is same as
//node[temp->vId].data

printf("  %d",node[temp->vId].data);
temp=temp->next;
}
}
}else{
printf("Empty Graph");
}
}
int main(){
struct Graph*node=NULL;
node=(struct Graph*)malloc(sizeof(struct Graph)*size);

if(node==NULL){
printf("\n Memory overflow");
}else{
//First set node keys
setData(node);
//Connected two node with Edges in undirected graph

printGraph(node);
}
return 0;
}``````

#### Output

`````` Adjacency list of vertex 0  :  4  2  1
Adjacency list of vertex 1  :  4  2  0
Adjacency list of vertex 2  :  3  1  0
Adjacency list of vertex 3  :  4  2
Adjacency list of vertex 4  :  3  1  0``````
``````//C++ Graph represented by Adjacency List
//Un-Directed Graph
#include<iostream>
using namespace std;
struct AjlistNode{
int vId;//Vertices id
struct AjlistNode*next;
};
struct Vertices{
int data; //node key value
struct AjlistNode*next;
};
class Graph{
Vertices *node;
int size;//number of
public:
Graph(int);
void setData();
void printGraph();
};
Graph::Graph(int size){
this->size=size;
//set number of nodes
node=new Vertices[size];
}
//set node key value
void Graph:: setData(){
if(node!=NULL){
int index=0;
for(index;index<size;index++){
//set vertic node data
node[index].data=index;//set node key
//Initial no AjlistNode
//set NULL Value
node[index].next=NULL;
}
}else{
cout<<"Vertic Node is Empty"<<endl;
}
}
//Add Edge from Two given Nodes
void Graph ::addEdge(int V ,int E){
//add edge form V to E
//V and E is Node location
if(V<size && E <size){
AjlistNode *newEdge=new AjlistNode;
if(newEdge!=NULL){
newEdge->next=node[V].next;
newEdge->vId=E;
node[V].next=newEdge;
}
newEdge=new AjlistNode;
if(newEdge!=NULL){
newEdge->next=node[E].next;
newEdge->vId=V;
node[E].next=newEdge;
}

}else{
//not valid Vertices
cout<<"Invalid Node Vertices "<< V<<" "<<E;
}
}
//Display Adjacency list of vertex
void Graph::printGraph(){
if(node!=NULL){
AjlistNode *temp=NULL;
for(int index=0;index<size;index++){
cout<<"\n Adjacency list of vertex "<<index<<" :";
temp=node[index].next;
while(temp!=NULL){
//temp->vId is graph node vertices
//in this case temp->vId is same as
//node[temp->vId].data

cout<<" "<<node[temp->vId].data;
temp=temp->next;
}
}
}else{
cout<<"Empty Graph"<<endl;
}
}
int main(){
//Create Object
Graph g(5);
//First set node keys
g.setData();
//Connected two node with Edges

g.printGraph();
return 0;
}``````

#### Output

`````` Adjacency list of vertex 0 : 4 2 1
Adjacency list of vertex 1 : 4 2 0
Adjacency list of vertex 2 : 3 1 0
Adjacency list of vertex 3 : 4 2
Adjacency list of vertex 4 : 3 1 0``````
``````//Java Graph represented by Adjacency List
//Undirected graph
public class MyGraph{

static class AjlistNode{
int id;//Vertices node key
AjlistNode next;
}
static class Vertices{
int data;
AjlistNode next;
}

//number of Vertices
static int size;
Vertices node[];

MyGraph(int size){
//set value
this.size=size;
node=new Vertices[size];
}

//set initial node value
public void setData(){
if(node==null){
System.out.println("\nEmpty Graph");
}else{
for(int index=0;index<size;index++){
// avoid java.lang.NullPointerException
node[index]=new Vertices();
node[index].next=null;
}
}
}

public void addEdge(int S,int E){
if(S<size &&E<size &&node!=null){
AjlistNode newEdge=new AjlistNode();
newEdge.id=E;//end node
newEdge.next=node[S].next;
node[S].next=newEdge;

newEdge=new AjlistNode();
newEdge.id=S;//end node
newEdge.next=node[E].next;
node[E].next=newEdge;
}else{
System.out.println("\nEmpty Graph");
}
}

public void printGraph(){

if(size>0&&node!=null){
for(int index=0;index<size;index++){
System.out.print("\nAdjacency list of vertex "+index+" :");
AjlistNode temp=node[index].next;
while(temp!=null){
System.out.print(" "+node[temp.id].data);
temp=temp.next;
}
}
}
}

public static void main(String[] args) {
int totalNode=5;

MyGraph g=new MyGraph(totalNode);
g.setData();
//Connected two node with Edges

g.printGraph();

}
}``````

#### Output

``````Adjacency list of vertex 0 : 4 2 1
Adjacency list of vertex 1 : 4 2 0
Adjacency list of vertex 2 : 3 1 0
Adjacency list of vertex 3 : 4 2
Adjacency list of vertex 4 : 3 1 0``````
``````#Python implement graph using Adjacency List
#UnDirected Graph
def __init__(self,data):
self.id=data
self.next=None

class Vertices:
def __init__(self,data):
self.data=data
self.next=None

class Graph:
"""Constructor for Graph"""
def __init__(self, size):
self.size=size
self.node=[];

def setData(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
self.node.append(Vertices(index))
index+=1

#S and E is two nodes
if(self.size>S and self.size>E):
#first
new_edge.next=self.node[S].next
self.node[S].next=new_edge
#second
new_edge.next=self.node[E].next
self.node[E].next=new_edge
else:
print("Invalid nodes")

def printGraph(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
print("\nAdjacency list of vertex {0} :".format(index),end=" ")
temp=self.node[index].next
while temp!=None:
print("  {0}".format(temp.id),end=" ")
temp=temp.next
index+=1

def main():
g=Graph(5)
g.setData();
#Connected two node with Edges

g.printGraph();

if __name__=="__main__":
main()``````

#### Output

``````Adjacency list of vertex 0 :   4   2   1
Adjacency list of vertex 1 :   4   2   0
Adjacency list of vertex 2 :   3   1   0
Adjacency list of vertex 3 :   4   2
Adjacency list of vertex 4 :   3   1   0``````
``````//C# Graph represented by Adjacency List
//Un-Directed Graph
using System;
class AjlistNode{
public int key;
public AjlistNode next;
public AjlistNode(int key){
this.key=key;
this.next=null;
}
}
class Node{
public int data;
public AjlistNode next;
public Node(int data){
this.data=data;
this.next=null;

}
}
class MyGraph{
//empty array
public Node[]node= new Node[] {};
public int size;
public MyGraph(int size){
this.size=size;
node=new Node[size];
}
public void setData(){
if(size>0 && this.node!=null){
for(int index=0;index<size;index++){
node[index]=new Node(index);
}

}
}
public void addEdge(int start,int end){
if(this.size>start && this.size>end){
AjlistNode newEdge=new AjlistNode(end);
newEdge.next=node[start].next;
node[start].next=newEdge;

newEdge=new AjlistNode(start);
newEdge.next=node[end].next;
node[end].next=newEdge;
}
}
public void printGraph(){
if(size>0 && node.Length>0 && node!=null){
for(int index=0;index<size;index++){
Console.Write("\nAdjacency list of vertex {0} :",index);
AjlistNode temp=node[index].next;
while(temp!=null){
Console.Write(" "+node[temp.key].data);
temp=temp.next;
}
}
}
}

}
class Program{

static void Main(string[] args){
//create object
MyGraph g=new MyGraph(5);
g.setData();
//Connected two node with Edges

g.printGraph();
}
}``````

#### Output

``````Adjacency list of vertex 0 : 4 2 1
Adjacency list of vertex 1 : 4 2 0
Adjacency list of vertex 2 : 3 1 0
Adjacency list of vertex 3 : 4 2
Adjacency list of vertex 4 : 3 1 0
``````
``````<?php
/*
* PHP Program Adjacency List in unDirected Graph
*/

class AjlistNode{
public \$key;
public \$next;
function __construct(\$key){
\$this->key=\$key;
\$this->next=NULL;
}
}
class Node{
public \$data;
public \$next;
function __construct(\$data){
\$this->data=\$data;
\$this->next=NULL;

}
}
class MyGraph{

public \$node;
public \$size;
function __construct(\$size){
\$this->size=\$size;
\$this->node=[];  //empty array
}
public function setData(){
if(\$this->size>0){
for(\$index=0;\$index<\$this->size;\$index++){
\$this->node[\$index]=new Node(\$index);
}

}
}
if(\$this->size>\$start && \$this->size>\$end){
//first
\$newEdge=new AjlistNode(\$end);
\$newEdge->next=\$this->node[\$start]->next;
\$this->node[\$start]->next=\$newEdge;

//second
\$newEdge=new AjlistNode(\$start);
\$newEdge->next=\$this->node[\$end]->next;
\$this->node[\$end]->next=\$newEdge;
}
}
public function printGraph(){
if(\$this->size>0 && count(\$this->node)>0 && \$this->node!=NULL){
for(\$index=0;\$index<\$this->size;\$index++){
echo ("<br>Adjacency list of vertex ".\$index." : ");
\$temp=\$this->node[\$index]->next;
while(\$temp!=NULL){
echo (" ".\$this->node[\$temp->key]->data);
\$temp=\$temp->next;
}
}
}
}

}

function main(){
//create object
\$g=new MyGraph(5);
\$g->setData();

//Connected two node with Edges

\$g->printGraph();

}
main();
?>``````

#### Output

``````Adjacency list of vertex 0 : 4 2 1
Adjacency list of vertex 1 : 4 2 0
Adjacency list of vertex 2 : 3 1 0
Adjacency list of vertex 3 : 4 2
Adjacency list of vertex 4 : 3 1 0``````

Note that in both example first use an array which are contain actual node values. Array is useful to get any node quickly in existing array. When graph nodes are not predefined or you are remove existing graph node then array are not suitable here.

Because after create array, In most of programming language are not allowing to resize the array size such as add or delete existing node. In this case you'll can use linked list to storing the value of actual graph node.

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.