Skip to content

Commit 2243e90

Browse files
New Commit
1 parent e28ca29 commit 2243e90

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

bfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void BFS(int startNode, int nodes, int adjMatrix[MAX][MAX]){
2323

2424
visited[startNode] = 1;
2525
queue[rear++] = startNode;
26-
printf("BFS Traversal starting from from node %d: ",startNode);
26+
printf("BFS Traversal starting from from node %d: ",startNode+1);
2727

2828
while(front!=rear){
2929
int current = queue[front++];

bfs.exe

0 Bytes
Binary file not shown.

dfs.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<stdio.h>
2+
#define MAX 100
3+
4+
void addEdge(int u, int v, int adjMatrix[MAX][MAX]){
5+
adjMatrix[u][v] = 1;
6+
adjMatrix[v][u] = 1;
7+
}
8+
9+
void displayMatrix(int nodes, int adjMatrix[MAX][MAX]){
10+
int i,j;
11+
printf("Adjacency Matrix:\n");
12+
for(i=0;i<nodes;i++){
13+
for(j=0;j<nodes;j++){
14+
printf("%d ",adjMatrix[i][j]);
15+
}
16+
printf("\n");
17+
}
18+
}
19+
20+
void DFS(int startNode, int nodes, int adjMatrix[MAX][MAX]){
21+
int visited[MAX] = {0};
22+
int stack[MAX], top = -1, i;
23+
24+
stack[++top] = startNode;
25+
visited[startNode] = 1;
26+
27+
printf("DFS Traversal starting from from node %d: ",startNode+1);
28+
29+
while(top!=-1){
30+
int current = stack[top--];
31+
printf("%d ",current+1);
32+
33+
for(i=0;i<nodes;i++){
34+
if(adjMatrix[current][i]==1 && visited[i]==0){
35+
visited[i] = 1;
36+
stack[++top] = i;
37+
}
38+
}
39+
}
40+
}
41+
42+
int main(){
43+
int i, nodes, edges, u, v;
44+
static int adjMatrix[MAX][MAX];
45+
46+
printf("Enter number of nodes in the graph: ");
47+
scanf("%d", &nodes);
48+
49+
printf("Enter number of edges in the graph: ");
50+
scanf("%d", &edges);
51+
52+
printf("Enter the edges (format: from_node to_node):\n");
53+
for(i=0;i<edges;i++){
54+
scanf("%d %d",&u, &v);
55+
addEdge(u-1, v-1, adjMatrix);
56+
}
57+
58+
displayMatrix(nodes, adjMatrix);
59+
60+
printf("Enter the starting node for DFS traversal: ");
61+
scanf("%d", &u);
62+
63+
DFS(u-1, nodes, adjMatrix);
64+
65+
return 0;
66+
}

dfs.exe

41.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)