-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsl 11 graph.c
More file actions
127 lines (88 loc) · 1.82 KB
/
dsl 11 graph.c
File metadata and controls
127 lines (88 loc) · 1.82 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Develop a Program in C for the following operations on Graph(G)
of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a
digraph using DFS/BFS method
*/
#include<stdio.h>
#include<stdlib.h>
int n,i,j,a[25][25],visited[20], source,f = 0, r = -1, queue[20];
void DFS(int v)
{
visited[v]=1;
printf("%d ",v);
for(i=0;i<n;i++)
{
if(a[v][i]==1 && visited[i]==0)
DFS(i);
}
}
void BFS(int v)
{
int u;
queue[++r] = v;
visited[v] = 1;
printf("%d ", v); // Print the starting node
while (f <= r)
{
u = queue[f++];
for (i = 0; i < n; i++)
{
if (a[u][i] == 1 && visited[i] == 0)
{
queue[++r] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}
int main()
{
printf("\nEnter the number of vertex..\n ");
scanf("%d",&n);
printf("\nEnter Adjecency matrix..\n ");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
printf("\nEnter source..\n ");
scanf("%d",&source);
for (i = 0; i < n; i++)
visited[i] = 0;
printf("\nDFS:");
DFS(source);
for (i = 0; i < n; i++)
visited[i] = 0;
printf("\nBFS:");
BFS(source);
return 0;
}
/*
Enter the number of vertex..
6
Enter Adjecency matrix..
0 1 1 0 0 1
1 0 0 0 1 0
1 0 0 0 1 0
0 0 0 0 0 1
1 0 1 0 0 0
1 0 0 1 0 0
Enter source..
3
DFS:3 5 0 1 4 2
BFS:3 5 0 1 2 4
Enter the number of vertex..
4
Enter Adjecency matrix..
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
Enter source..
0
DFS:0 1 2 3
BFS:0 1 2 3
*/