-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_traversal.c
167 lines (153 loc) · 2.63 KB
/
BFS_traversal.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
struct node* newNode1(int data)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void enqueue(int x)
{
struct node *temp;
temp=newNode1(x);
if (rear==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
}
int dequeue()
{
struct node *temp;
temp=front;
if (front==NULL)
{
printf("Queue is empty\n");
front=rear=NULL;
}
else
{
front=front->next;
return temp->data;
}
}
void addedge(struct node* a[],int u,int v){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=v;
temp->next=a[u];
a[u]=temp;
struct node* temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=u;
temp1->next=a[v];
a[v]=temp1;
}
struct QNode
{
int key;
struct QNode *next;
};
struct Queue
{
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue *q, int k)
{
struct QNode *temp = newNode(k);
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
struct QNode *deQueue(struct Queue *q)
{
if (q->front == NULL)
return NULL;
struct QNode *temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
return temp;
}
int isempty(struct Queue *q){
if (q->front==NULL)
{
return 1;
}
return 0;
}
void BFS(struct node* a[],int s){
int color[10];
//0 for unvisited;
//1 for visited;
//-1 for deleted from queue
for (int i = 0; i <10; ++i)
{
color[i]=0;
}
color[s]=1;
struct Queue *q=createQueue();
enQueue(q,s);
while(!(isempty(q))){
struct QNode* u=deQueue(q);
printf("%d ",u->key);
struct node* ttt;
ttt=a[u->key];
while(ttt!=NULL){
if (color[ttt->data]==0)
{
enQueue(q,ttt->data);
color[ttt->data]=1;
}
ttt=ttt->next;
}
color[u->key]=-1;
}
}
int main()
{
struct node* a[10];
for (int i = 0; i < 10; ++i)
{
a[i]=NULL;
}
addedge(a,0,1);
addedge(a,0,2);
addedge(a,1,2);
addedge(a,2,0);
addedge(a,2,3);
addedge(a,3,3);
BFS(a,2);
//struct node* temp2=(struct node*)malloc(sizeof(struct node));
// temp2=a[0];
// while(temp2!=NULL){
// printf("%d ",temp2->data);
// temp2=temp2->next;
// }
// puts("");
return 0;
}