#include
#include
int graph[10][10];
int visited[10];
void intialize_graph(int no_v)
{
int i,j;
for(i=1;i
for(i=1;i<=no_v;i++) { if(i!=start_v && visited[i]==0 && graph[start_v][i]==1) { DFS(i,no_v); } } } void main() { int no_v,no_e; int s_v,e_v,start_v; int i,j; clrscr(); printf("\n Enter the number of vertices in the graph:-"); scanf("%d",&no_v); printf("\n Enter the number of edges in the graph:-"); scanf("%d",&no_e); printf("\n Enter the adjacent vertices of each graph"); for(i=1;i<=no_e;i++) { printf("\n Edges: %d-->start & end vertices:",i);
scanf("%d %d",&s_v,&e_v);
graph[s_v][e_v]=1;
graph[e_v][s_v]=1;
}
print_graph(no_v);
printf("\n\nDepth first Search:\nEnter the starting vertex for searching:-");
scanf("%d",start_v);
printf("Depth First Search Tree:-\n");
DFS(start_v,no_v);
getch();
}
/*
Output
Enter the number of vertices in the graph:-4
Enter the number of edges in the graph:-4
Enter the adjacent vertices of each graph
Edges: 1-->start & end vertices:1 2
Edges: 2-->start & end vertices:2 3
Edges: 3-->start & end vertices:3 4
Edges: 4-->start & end vertices:4 1
The Graph Matrix Representation:
1 2 3 4
1 0 1 0 1
2 1 0 1 0
3 0 1 0 1
4 1 0 1 0
Depth first Search:
Enter the starting vertex for searching:-4
Depth First Search Tree:-
64-->
*/
No comments:
Post a Comment