#include<stdio.h>
#include<assert.h>
#include<conio.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
/*graph is undirected and not weighted*/
#define maxVertices 100
void Dfs(int graph[][maxVertices],int *size,int presentVertex,int *visited)
{
printf("Now visiting vertex %d\n",presentVertex);
visited[presentVertex]=1;
/* Iterate through all the vertices connected to the presentVertex and perform dfs on those
vertices if they are not visited before */
int iter;
for(iter=0;iter<size[presentVertex];iter++)
{
if(!visited[graph[presentVertex][iter]])
Dfs(graph,size,graph[presentVertex][iter],visited);
}
return;
}
/* Input Format: Graph is directed and unweighted. First two integers must be number of vertces and edges
which must be followed by pairs of vertices which has an edge between them. */
int main()
{
int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVertices]={0};
int vertices,edges,iter;
/* vertices represent number of vertices and edges represent number of edges in the graph. */
scanf("%d%d",&vertices,&edges);
int vertex1,vertex2;
for(iter=0;iter<edges;iter++)
{
scanf("%d%d",&vertex1,&vertex2);
assert(vertex1>=0 && vertex1<vertices);
assert(vertex2>=0 && vertex2<vertices);
graph[vertex1][size[vertex1]++] = vertex2;
}
int presentVertex;
for(presentVertex=0;presentVertex<vertices;presentVertex++)
{
if(!visited[presentVertex])
{
Dfs(graph,size,presentVertex,visited);
}
}
getche();
return 0;
}
--------------------------------------PYTHON IMPLEMENTATION---------------------------------------------
def dfs(graph,p_vertex,visited):
print("Visiting vertex {0}".format(p_vertex))
visited[p_vertex]=1
#print(len(graph[p_vertex]))
for itr in range(0,len(graph[p_vertex])):
if(visited[graph[p_vertex][itr]]==0):
#print("sent to visit vertex {0}".format(graph[p_vertex][itr]))
dfs(graph,graph[p_vertex][itr],visited)
return
vertices=int(input("Enter number of vertices::"))
edges=int(input("Enter the number of edges::"))
graph=[]
visited=[0]*vertices
#create the vertices
for i in range(0,vertices):
v=[]
graph.append(v)
#take input
for i in range(0,edges):
v1,v2=input().split()
v1,v2=int(v1),int(v2)
assert(v1>=0 and v1<vertices)
assert(v2>=0 and v2<vertices)
graph[v1].append(v2)
print(graph)
for p_vertex in range(0,vertices):
if(visited[p_vertex]==0):
dfs(graph,p_vertex,visited)
#include<assert.h>
#include<conio.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
/*graph is undirected and not weighted*/
#define maxVertices 100
void Dfs(int graph[][maxVertices],int *size,int presentVertex,int *visited)
{
printf("Now visiting vertex %d\n",presentVertex);
visited[presentVertex]=1;
/* Iterate through all the vertices connected to the presentVertex and perform dfs on those
vertices if they are not visited before */
int iter;
for(iter=0;iter<size[presentVertex];iter++)
{
if(!visited[graph[presentVertex][iter]])
Dfs(graph,size,graph[presentVertex][iter],visited);
}
return;
}
/* Input Format: Graph is directed and unweighted. First two integers must be number of vertces and edges
which must be followed by pairs of vertices which has an edge between them. */
int main()
{
int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVertices]={0};
int vertices,edges,iter;
/* vertices represent number of vertices and edges represent number of edges in the graph. */
scanf("%d%d",&vertices,&edges);
int vertex1,vertex2;
for(iter=0;iter<edges;iter++)
{
scanf("%d%d",&vertex1,&vertex2);
assert(vertex1>=0 && vertex1<vertices);
assert(vertex2>=0 && vertex2<vertices);
graph[vertex1][size[vertex1]++] = vertex2;
}
int presentVertex;
for(presentVertex=0;presentVertex<vertices;presentVertex++)
{
if(!visited[presentVertex])
{
Dfs(graph,size,presentVertex,visited);
}
}
getche();
return 0;
}
--------------------------------------PYTHON IMPLEMENTATION---------------------------------------------
def dfs(graph,p_vertex,visited):
print("Visiting vertex {0}".format(p_vertex))
visited[p_vertex]=1
#print(len(graph[p_vertex]))
for itr in range(0,len(graph[p_vertex])):
if(visited[graph[p_vertex][itr]]==0):
#print("sent to visit vertex {0}".format(graph[p_vertex][itr]))
dfs(graph,graph[p_vertex][itr],visited)
return
vertices=int(input("Enter number of vertices::"))
edges=int(input("Enter the number of edges::"))
graph=[]
visited=[0]*vertices
#create the vertices
for i in range(0,vertices):
v=[]
graph.append(v)
#take input
for i in range(0,edges):
v1,v2=input().split()
v1,v2=int(v1),int(v2)
assert(v1>=0 and v1<vertices)
assert(v2>=0 and v2<vertices)
graph[v1].append(v2)
print(graph)
for p_vertex in range(0,vertices):
if(visited[p_vertex]==0):
dfs(graph,p_vertex,visited)