# Depth First Traversal for a Graph

Here given code implementation process.

``````//C Program to find DFS in 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; //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){
struct AjlistNode *newEdge=(struct AjlistNode*)malloc(sizeof(struct AjlistNode));
if(newEdge!=NULL){

newEdge->next=NULL;
newEdge->vId=E;

struct AjlistNode *temp=node[V].next;

if(temp==NULL){
node[V].next=newEdge;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newEdge;
}
}else{
printf("\n Memory overflow");
}
}else{
//not valid Vertices
printf("Invalid Node Vertices %d  %d", V,E);
}
}
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");
}
}

void dfs(int point,int *visit,struct Graph*node){

if(visit[point]==1){
return;
}else{
visit[point]=1;
printf("  %d",point);
struct AjlistNode *temp=node[point].next;
while(temp!=NULL){
dfs(temp->vId,visit,node);
temp=temp->next;
}
}
}
int main(){

size=6;
struct Graph*node=NULL;

int *visit=(int*)calloc(size,sizeof(int));

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);
printf(" \n DFS :");
dfs(4,visit,node);
}
return 0;
}
``````

#### Output

`````` Adjacency list of vertex 0  :  1  5
Adjacency list of vertex 1  :  1
Adjacency list of vertex 2  :  1
Adjacency list of vertex 3  :  0  3
Adjacency list of vertex 4  :  2  3
Adjacency list of vertex 5  :  1
DFS :  4  2  1  3  0  5``````
``````//C++ DFS for a 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 *visit;
int size;//number of
public:
Graph(int);
void setData();
void printGraph();
void dfs(int);
};
Graph::Graph(int size){
this->size=size;
//set number of nodes
node=new Vertices[size];

visit=new int[size];

for(int index=0;index<size;index++){
visit[index]=0;
}
}
//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=NULL;
newEdge->vId=E;

AjlistNode *temp=node[V].next;

if(temp==NULL){
node[V].next=newEdge;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newEdge;
}
}
}else{
//not valid Vertices
cout<<"Invalid Node Vertices "<< V<<" "<<E;
}
}
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;
}
}
void Graph::dfs(int point){

if(visit[point]==1){
return;
}else{
visit[point]=1;
cout<<" "<<point;
AjlistNode *temp=node[point].next;
while(temp!=NULL){
dfs(temp->vId);
temp=temp->next;
}
}
}
int main(){
//Create Object
Graph g(6);
//First set node keys
g.setData();

//Connected two node with Edges
g.printGraph();
cout<<"\n DFS of node 4 : ";
g.dfs(4); //start node 4
return 0;
}``````

#### Output

`````` Adjacency list of vertex 0 : 1 5
Adjacency list of vertex 1 : 1
Adjacency list of vertex 2 : 1
Adjacency list of vertex 3 : 0 3
Adjacency list of vertex 4 : 2 3
Adjacency list of vertex 5 : 1
DDFS of node 4 :  4 2 1 3 0 5``````
``````//Java DFS of directed 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;
public int visit[];
public Vertices node[];

MyGraph(int size){
//set value
this.size=size;
node=new Vertices[size];
visit=new int[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;
}
}
}

if(S<size &&E<size &&node!=null){
AjlistNode newEdge=new AjlistNode();
newEdge.id=E;//end node
newEdge.next=null;
if(node[S].next==null){
node[S].next=newEdge;
}else{
AjlistNode temp=node[S].next;
while(temp.next!=null){
temp=temp.next;
}
temp.next=newEdge;
}

}else{
System.out.println("\nEmpty Graph");
}
}
public void dfs(int point){

if(visit[point]==1){
return;
}else{
visit[point]=1;
System.out.print("  "+point);

AjlistNode temp=node[point].next;
while(temp!=null){
dfs(temp.id);
temp=temp.next;
}
}
}

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=6;

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

g.printGraph();
System.out.print("\nDFS : ");
g.dfs(4);

}
}``````

#### Output

``````Adjacency list of vertex 0 : 1 5
Adjacency list of vertex 1 : 1
Adjacency list of vertex 2 : 1
Adjacency list of vertex 3 : 0 3
Adjacency list of vertex 4 : 2 3
Adjacency list of vertex 5 : 1
DFS :   4  2  1  3  0  5``````
``````#Python DFS 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=[]
self.visit=[0]*size

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):
if(self.node[S].next==None):
self.node[S].next=new_edge
else:
temp=self.node[S].next
while(temp.next!=None):
temp=temp.next
temp.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 dfs(self, point):
if(self.visit[point]==1):
return
else:
self.visit[point]=1
print(point,end=" ")

temp=self.node[point].next
while(temp!=None):
self.dfs(temp.id)
temp=temp.next

def main():
g=Graph(6)
g.setData()
#Connected two node with Edges
g.printGraph()
print("\nDFS :",end=" ")

g.dfs(4)

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

#### Output

``````Adjacency list of vertex 0 :   1   5
Adjacency list of vertex 1 :   1
Adjacency list of vertex 2 :   1
Adjacency list of vertex 3 :   0   3
Adjacency list of vertex 4 :   2   3
Adjacency list of vertex 5 :   1
DFS : 4 2 1 3 0 5 ``````
``````//C# DFS 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 int[]visit= new int[] {};
public MyGraph(int size){
this.size=size;
node=new Node[size];
visit=new int[size];
}
public void setData(){
if(size>0 && this.node!=null){
for(int index=0;index<size;index++){
node[index]=new Node(index);
}

}
}
if(this.size>start && this.size>end){
AjlistNode newEdge=new AjlistNode(end);

if(node[start].next==null){
node[start].next=newEdge;
}else{
AjlistNode temp=node[start].next;
while(temp.next!=null){
temp=temp.next;
}
temp.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;
}
}
}
}
public void dfs(int point){

if(visit[point]==1){
return;
}else{
visit[point]=1;
Console.Write(" {0}",point);

AjlistNode temp=node[point].next;
while(temp!=null){
dfs(temp.key);
temp=temp.next;
}
}
}

}
class Program{

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

g.printGraph();
Console.Write("\nDFS : ");
g.dfs(4);
}
}``````

#### Output

``````Adjacency list of vertex 0 : 1 5
Adjacency list of vertex 1 : 1
Adjacency list of vertex 2 : 1
Adjacency list of vertex 3 : 0 3
Adjacency list of vertex 4 : 2 3
Adjacency list of vertex 5 : 1
DFS :  4 2 1 3 0 5
``````
``````<?php
/*
* PHP Program DFS 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;
public \$visit;
function __construct(\$size){
\$this->size=\$size;
\$this->node=[];  //empty array

\$this->visit=array_fill(0, \$size, 0);
}
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);
if(\$this->node[\$start]->next==NULL){
\$this->node[\$start]->next=\$newEdge;
}else{

\$temp=\$this->node[\$start]->next;
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
\$temp->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;
}
}
}
}
public function dfs(\$point){

if(\$this->visit[\$point]==1){
return;
}else{
\$this->visit[\$point]=1;
echo ("  ".\$point);

\$temp=\$this->node[\$point]->next;
while(\$temp!=NULL){
\$this->dfs(\$temp->key);
\$temp=\$temp->next;
}
}
}

}

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

//Connected two node with Edges
\$g->printGraph();
echo "<br>DFS : ";
\$g->dfs(4);

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

#### Output

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

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.