forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depth_First_Search.c
82 lines (66 loc) · 1.42 KB
/
Depth_First_Search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
//Structure for each vertex in graph
struct node
{
int data;
struct node* next;
};
//Function to add node at the end of given root
struct node* insert(struct node *root,int d)
{
struct node *newPtr = (struct node*)malloc(sizeof(struct node));
newPtr->data = d;
newPtr->next = NULL;
root->next = newPtr;
return newPtr;
}
//Function to perform DFS
void explore(int node,int vis[],struct node start[])
{
printf("%d ",node);
vis[node]=1;
struct node *ptr = start[node].next;
for(;ptr!=NULL;ptr=ptr->next)
{
if(vis[ptr->data]==0)
{
explore(ptr->data,vis,start);
}
}
}
int main()
{
int no_of_vertices = 7,i,vis[8];
struct node start[8];
for(i=1;i<=no_of_vertices;i++)
{
start[i].data=i;
start[i].next=NULL;
vis[i]=0;
}
/*
Creates the following graph :
1
/ \
2 3
/ \ / \
4 5 6 7
*/
struct node *cur;
cur = insert(&start[1],2);
insert(cur,3);
cur = insert(&start[2],4);
insert(cur,5);
cur = insert(&start[3],6);
insert(cur,7);
//The loop takes care of non connected graph also
printf("DFS of the graph : \n");
for(i=1;i<=7;i++)
{
if(vis[i]==0)
explore(i,vis,start);
}
return 0;
// Output : 1 2 4 5 3 6 7
}