-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.c
More file actions
54 lines (46 loc) · 761 Bytes
/
DFS.c
File metadata and controls
54 lines (46 loc) · 761 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#define s 20
//adjacency list
struct graph_list
{
int V;
int E;
int *adj;
};
struct list
{
int v;
struct list *next;
};
void DFS(struct graph_list *G, int u, int visited[])
{
printf("%d\n", u);
visited[u] = 1;
for(int v=0; v<G->V; v++)
{
v = G->adj[u]->next;
if(!visited[v] && G->adj[u])
{
DFS(G,v,visited);
v = G->adj[u]->next;
}
}
}
int main()
{
struct graph_list *G = (struct graph_list *)malloc(sizeof(struct graph_list));
if(!G)
{
printf("Memory error\n");
exit(1);
}
scanf("%d %d", &G->V, &G->E);
G->adj = malloc(sizeof(struct list) * G->V);
int visited[G->V];
for(int i=0; i<G->V; i++)
visited[i] = 0;
for(int i=0; i<G->V; i++)
if(!visited[i])
DFS(G,i,visited);
}