Skip to content

Commit 1946b4f

Browse files
Merge pull request #654 from anshumankmr/master
Added solutions to BFS and DFS in C
2 parents b9efcbb + 64c9d26 commit 1946b4f

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed

BFS/C/bfs.c

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX 100
4+
int queue[MAX];
5+
int front,rear;
6+
struct Node
7+
{
8+
int vertex;
9+
struct Node* next;
10+
int size;
11+
};
12+
struct Node* initialize()
13+
{
14+
struct Node* node=NULL;
15+
node->size=0;
16+
return node;
17+
}
18+
struct Node* getNode()
19+
{
20+
return (struct Node*)malloc(sizeof(struct Node));
21+
}
22+
struct Node* insert_rear(struct Node* head, int data)
23+
{
24+
struct Node* temp=getNode();
25+
temp->next=NULL;
26+
temp->vertex=data;
27+
if (head==NULL)
28+
{
29+
//printf("%d",temp->vertex);
30+
return temp;
31+
}
32+
else
33+
{
34+
struct Node* ptr = head;
35+
while(ptr->next!=NULL)
36+
{
37+
ptr=ptr->next;
38+
}
39+
ptr->next=temp;
40+
head->size+=1;
41+
//printf("%d %d",ptr->vertex,ptr->next->vertex);
42+
return head;
43+
}
44+
}
45+
void printList(struct Node* head)
46+
{
47+
struct Node* temp = head;
48+
int count = 10;
49+
while(temp!=NULL)
50+
{
51+
printf("%d->", temp->vertex);
52+
53+
temp=temp->next;
54+
55+
}
56+
printf("\n");
57+
}
58+
int empty()
59+
{
60+
if (front==-1 && rear==-1)
61+
{
62+
return 1;
63+
}
64+
return 0;
65+
}
66+
int Front()
67+
{
68+
return queue[front];
69+
}
70+
void push(int add_item)//modify to fit the
71+
{
72+
73+
if (rear == MAX - 1)
74+
{
75+
//printf("Queue Overflow \n");
76+
}
77+
else
78+
{
79+
if (front == - 1)
80+
{/*If queue is initially empty */
81+
front = 0;
82+
}
83+
rear = rear + 1;
84+
queue[rear] = add_item;
85+
}
86+
}
87+
void pop()//
88+
{
89+
if (front == - 1 || front > rear)
90+
{
91+
//printf("Queue Underflow \n");
92+
return ;
93+
}
94+
else
95+
{
96+
printf("Element deleted from queue is : %d\n", queue[front]);
97+
front = front + 1;
98+
}
99+
}//basic functions all defined i suppose
100+
void dfs(struct Node* graph[], int vertices, int edges, int source)
101+
{
102+
int level[100]={0}; //To determine the level of each node
103+
int vis[100]={0}; //Mark the node if visited
104+
push(source);
105+
level[source] = 0 ; //Setting the level of the source node as 0
106+
vis[source] = 1;
107+
while(!empty())
108+
{
109+
int p = Front();
110+
pop();
111+
struct Node* temp=graph[p];
112+
while(temp!=NULL)
113+
{
114+
if(vis[temp->vertex] == 0)
115+
{
116+
//Setting the level of each node with an increment in the level of parent node
117+
level[ temp->vertex ] = level[p]+1;
118+
push(temp->vertex);
119+
vis[temp->vertex] = 1;
120+
}
121+
temp=temp->next;
122+
}
123+
if(empty())
124+
{
125+
break;
126+
}
127+
}
128+
129+
}
130+
int main()
131+
{
132+
int x,y,node,edges,vertices,source;
133+
printf("Enter the number of edges: ");
134+
scanf("%d",&edges);
135+
printf("Enter the number of vertices: ");
136+
scanf("%d",&vertices);
137+
struct Node* graph[vertices];
138+
for (int i=0;i<edges;i++)
139+
{
140+
graph[i]=NULL;
141+
}
142+
for (int i = 0 ;i<edges;i++)
143+
{
144+
scanf("%d %d",&x,&y);
145+
graph[x]= insert_rear(graph[x],y);
146+
//printf("\ngraph data %d ",graph[x]->vertex);
147+
printf("\n");
148+
}
149+
for (int i = 0;i <vertices;i++)
150+
{
151+
printf("The adjacency list for vertex number %d is ", i);
152+
printList(graph[i]);
153+
}
154+
front=-1;
155+
rear=-1;
156+
printf("Enter the source vertex\n");
157+
scanf("%d",&source);
158+
dfs(graph,vertices,edges,source);
159+
return 0;
160+
}

DFS/C/dfs.c

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX 100
4+
int queue[MAX];
5+
int front,rear;
6+
struct Node
7+
{
8+
int vertex;
9+
struct Node* next;
10+
int size;
11+
};
12+
struct Node* initialize()
13+
{
14+
struct Node* node=NULL;
15+
node->size=0;
16+
return node;
17+
}
18+
struct Node* getNode()
19+
{
20+
return (struct Node*)malloc(sizeof(struct Node));
21+
}
22+
struct Node* insert_rear(struct Node* head, int data)
23+
{
24+
struct Node* temp=getNode();
25+
temp->next=NULL;
26+
temp->vertex=data;
27+
if (head==NULL)
28+
{
29+
//printf("%d",temp->vertex);
30+
return temp;
31+
}
32+
else
33+
{
34+
struct Node* ptr = head;
35+
while(ptr->next!=NULL)
36+
{
37+
ptr=ptr->next;
38+
}
39+
ptr->next=temp;
40+
head->size+=1;
41+
//printf("%d %d",ptr->vertex,ptr->next->vertex);
42+
return head;
43+
}
44+
}
45+
void printList(struct Node* head)
46+
{
47+
struct Node* temp = head;
48+
int count = 10;
49+
while(temp!=NULL)
50+
{
51+
printf("%d->", temp->vertex);
52+
53+
temp=temp->next;
54+
55+
}
56+
printf("\n");
57+
}
58+
int empty()
59+
{
60+
if (front==-1 && rear==-1)
61+
{
62+
return 1;
63+
}
64+
return 0;
65+
}
66+
int Front()
67+
{
68+
return queue[front];
69+
}
70+
void push(int add_item)//modify to fit the
71+
{
72+
73+
if (rear == MAX - 1)
74+
{
75+
//printf("Queue Overflow \n");
76+
}
77+
else
78+
{
79+
if (front == - 1)
80+
{/*If queue is initially empty */
81+
front = 0;
82+
}
83+
rear = rear + 1;
84+
queue[rear] = add_item;
85+
}
86+
}
87+
void pop()//
88+
{
89+
if (front == - 1 || front > rear)
90+
{
91+
//printf("Queue Underflow \n");
92+
return ;
93+
}
94+
else
95+
{
96+
printf("Element deleted from queue is : %d\n", queue[front]);
97+
front = front + 1;
98+
}
99+
}//basic functions all defined i suppose
100+
void bfs(struct Node* graph[], int vertices, int edges, int source)
101+
{
102+
int level[100]={0}; //To determine the level of each node
103+
int vis[100]={0}; //Mark the node if visited
104+
push(source);
105+
level[source] = 0 ; //Setting the level of the source node as 0
106+
vis[source] = 1;
107+
while(!empty())
108+
{
109+
int p = Front();
110+
pop();
111+
struct Node* temp=graph[p];
112+
while(temp!=NULL)
113+
{
114+
if(vis[temp->vertex] == 0)
115+
{
116+
//Setting the level of each node with an increment in the level of parent node
117+
level[ temp->vertex ] = level[p]+1;
118+
push(temp->vertex);
119+
vis[temp->vertex] = 1;
120+
}
121+
temp=temp->next;
122+
}
123+
if(empty())
124+
{
125+
break;
126+
}
127+
}
128+
129+
}
130+
int main()
131+
{
132+
int x,y,node,edges,vertices,source;
133+
printf("Enter the number of edges: ");
134+
scanf("%d",&edges);
135+
printf("Enter the number of vertices: ");
136+
scanf("%d",&vertices);
137+
struct Node* graph[vertices];
138+
for (int i=0;i<edges;i++)
139+
{
140+
graph[i]=NULL;
141+
}
142+
for (int i = 0 ;i<edges;i++)
143+
{
144+
scanf("%d %d",&x,&y);
145+
graph[x]= insert_rear(graph[x],y);
146+
//printf("\ngraph data %d ",graph[x]->vertex);
147+
printf("\n");
148+
}
149+
for (int i = 0;i <vertices;i++)
150+
{
151+
printf("The adjacency list for vertex number %d is ", i);
152+
printList(graph[i]);
153+
}
154+
front=-1;
155+
rear=-1;
156+
printf("Enter the source vertex\n");
157+
scanf("%d",&source);
158+
bfs(graph,vertices,edges,source);
159+
return 0;
160+
}

0 commit comments

Comments
 (0)