-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS_adj_lis.c
More file actions
108 lines (65 loc) · 1.29 KB
/
Copy pathBFS_adj_lis.c
File metadata and controls
108 lines (65 loc) · 1.29 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
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int vertex;
struct Node* next;
}Node;
struct Node* createNode(int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
};
int queue[100];
int front=0;
int rear = 0;
struct Node* adj[100];
int visited[100];
void enqueue(int value){
queue[rear++] = value;
}
int dequeue(){
return queue[front++];
}
int isEmpty(){
if(front == rear){
return 1;
}else{
return 0;
}
}
void bfs(int v){
visited[v] = 1;
enqueue(v);
while(!isEmpty()){
int vertex = dequeue();
printf("%d ",vertex);
Node* temp = adj[vertex];
while(temp != NULL){
int neigh = temp->vertex;
if(!visited[neigh]){
visited[neigh] = 1;
enqueue(neigh);
}
temp = temp->next;
}
}
}
int main(){
int vertices;
int edges;
printf("Enter the number of vertices\n");
scanf("%d",&vertices);
printf("Enter the number of edges\n");
scanf("%d",&edges);
printf("Enter the edges pair u v\n");
for(int i=0;i<edges;i++){
int u,v;
scanf("%d %d",&u,&v);
Node* node = createNode(v);
node->next = adj[u];
adj[u] = node;
}
bfs(0);
return 0;
}