-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.c
More file actions
37 lines (31 loc) · 739 Bytes
/
dfs.c
File metadata and controls
37 lines (31 loc) · 739 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
// Check connectivity of a given graph using DFS method
#include <stdio.h>
#define MAX 100
void dfs(int graph[][MAX], int v, int visited[])
{
int i;
visited[v] = 1;
for (i = 0; i < MAX; i++)
{
if (graph[v][i] && !visited[i])
dfs(graph, i, visited);
}
}
int isConnected(int graph[][MAX], int n)
{
int visited[MAX] = {0};
dfs(graph, 0, visited);
for (int i = 0; i < n; i++)
if (!visited[i])
return 0;
return 1;
}
int main()
{
int graph[MAX][MAX] = {{0, 1, 1, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}, {0, 0, 1, 0}};
int n = 4;
if (isConnected(graph, n))
printf("\nThe graph is connected");
else
printf("\nThe graph is not connected");
}