-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.c
276 lines (264 loc) · 7.37 KB
/
bfs.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG if(0)
#define MAX_SIZE 10000
//https://thehuxley.com/problem/805
typedef struct adjacent_list //glorified node
{
int item;
struct adjacent_list* next;
}adj_list;
typedef struct graph
{
adj_list* vertices[MAX_SIZE]; //lista de nós, fala pra qual nó aponta o vertice do index, exemplo: v[1] = vertices que o 1 aponta para
short visited[MAX_SIZE];
}graph;
typedef struct _queue
{
adj_list* head;
adj_list* tail;
}Queue;
graph* createGraph() //creates a graph with nullified entries and visited[] = {0}
{
graph* g = (graph*) malloc(sizeof(graph));
for (int i = 0; i <= MAX_SIZE-1; i++)
{
g->vertices[i] = NULL;
g->visited[i] = 0;
}
return g;
}
void bubbleSort(adj_list* list) //list = vertices[i];
{
if (list == NULL) return;
adj_list* pos; //auxiliary node* that points to head
adj_list* tmp = NULL;
int flag = 0; //flag will switch to 1 while it's making swaps
do
{
pos = list;//set the position to head.
flag = 0;
while (pos->next != tmp)
{
if (pos->item > pos->next->item)
{
//DEBUG printf("%d > %d\n",pos->item , pos->next->item);
int t = pos->item;
pos->item = pos->next->item;
pos->next->item = t;
flag = 1;
}
pos = pos->next;
}
tmp = pos; //stores the last pos 'cuz it's already been sorted.
} while (flag);
}
adj_list* createAdjList(int origin) //creates a "node" with value
{
adj_list* adj = (adj_list*) malloc (sizeof(adj_list));
adj->item = origin;
adj->next = NULL;
return adj;
}
void addEdge(graph* g, int origin, int destination) //adds edges for an directioned graph
{
adj_list *vertex = createAdjList(destination);
vertex->next = g->vertices[origin];
g->vertices[origin] = vertex;
//DEBUG printf("Aresta que liga %d %d\n",origin,destination);
}
Queue* initQ() //initializes the queue, nullifying the head and tail
{
Queue* newQ = (Queue*) malloc(sizeof(Queue));
newQ->head = NULL;
newQ->tail = NULL;
return newQ;
}
int isEmptyQ(Queue* q)
{
return(q->head == NULL);
}
void enQueue(Queue* q, int origin)
{
adj_list* toBeQueued = createAdjList(origin);
if (isEmptyQ(q))
{
q->head = toBeQueued;
q->tail = toBeQueued;
}
else
{
q->tail->next = toBeQueued;
q->tail = toBeQueued;
}
}
int deQueue(Queue* q)
{
int deQueued = q->head->item;
adj_list* tmp = q->head->next;
free(q->head);
q->head = tmp;
return deQueued;
}
void printGraph(graph* gr, int vertexNum)
{
for (int i = 0; i < vertexNum; i++)
{
adj_list* tmp = gr->vertices[i];
printf("vertices[%d]: ",i);
while (tmp)
{
printf("v(%d), ",tmp->item);
tmp= tmp->next;
}
printf("\n");
}
}
void printQ(Queue* q, graph* g)
{
adj_list* tmp = q->head;
while (tmp != NULL)
{
printf("[%d]→",tmp->item);
tmp = tmp->next;
}
printf("\n");
}
void theWay(graph* g,int distToSource[], int lasts[],int origin, int destination, int pass)
{
int buffer[distToSource[destination]];
//printf("[[%d]]\n",distToSource[destination]);
int i = 0;
while (pass != origin)
{
DEBUG printf("ORIGEM: [%d]\t",origin);
DEBUG printf("ultimo de [%d] = [%d]\n",i,lasts[i]);
buffer[i] = lasts[pass];
pass = lasts[pass];
i++;
}
printf("Caminho entre %d e %d: ",origin,destination);
for(int i = distToSource[destination]-1 ; i>= 0 ;i--)
{
printf("%d => ",buffer[i]);
}
printf("%d\n",destination);
}
void bfs(graph* graph,int origin, int destination, int count)
{
for (int i = 0; i < MAX_SIZE; i++)
{
graph->visited[i] = 0;
}
int verticesNum = count; count = 0;
adj_list* tmp = graph->vertices[origin];
Queue* queue = initQ();
int dequeued;
graph->visited[origin] = 1;
enQueue(queue,origin);
printf("Iniciando busca em largura a partir de %d\n",origin); //para a primeira entrada
int distToSource[MAX_SIZE];
distToSource[origin] = 0;
int lasts[MAX_SIZE]; //vai guardar qual o pai do index... se 3 é pai de 2, lasts[2] = 3
int found = 0;
while (!isEmptyQ(queue))
{
dequeued = deQueue(queue); //dequeued é o numero que vai ser visitado,
tmp = graph->vertices[dequeued];
if(dequeued == destination) found = 1; //aqui vemos os vertices que ele tem adjacentes
while (tmp != NULL)
{
if(!graph->visited[tmp->item])
{
graph->visited[tmp->item] = 1;
enQueue(queue,tmp->item);
lasts[tmp->item] = dequeued;
distToSource[tmp->item] = distToSource[dequeued]+1;
DEBUG printQ(queue,graph);
DEBUG printf("ultimo de [%d] = [%d]\n",count,lasts[count]);
}
else
{
tmp = tmp->next;
DEBUG printf("FINIS ___ ultimo de [%d] = [%d]\n",count,lasts[count]);
continue;
}
printf("Iniciando busca em largura a partir de %d\n",tmp->item);
if(found == 0)
{
DEBUG printf("\t count++\n");
count++;
}
if(tmp->item == destination)
{
found = 1;
DEBUG printf("encontrado [%d]!\t", found);
//DEBUG printf("ENCONTRADO depois de %d passagens, ultimo foi [%d]\n",count,lasts[count-1]);
//break;
}
tmp = tmp->next;
//count++;
}
}
//node_explanation(graph);
DEBUG for (int i = 0; i < count; i++)
{
printf("LAST[%d] = [[%d]]\n",i,lasts[i]);
}
printf("\n");
for (int i = 0; i < verticesNum; i++)
{
switch (graph->visited[i])
{
case 0:
printf("%d | - | -\n", i);
break;
default:
if(distToSource[i] == 0) {printf("%d | %d | -\n", i,distToSource[i]); break;}
printf("%d | %d | %d\n", i,distToSource[i],lasts[i]);
break;
}
}
printf("\n");
if (found == 0) printf("Sem caminho entre %d e %d\n\n",origin,destination);
else
{
theWay(graph,distToSource,lasts,origin,destination,destination);
printf("\n");
}
DEBUG printf("FIM DA BUSCA!\n");
}
int main()
{
graph* graph = createGraph();
int verticesNum, linkNum, testNum; //V A T
scanf("%d",&verticesNum);
scanf("%d",&linkNum);
scanf("%d",&testNum);
for (int i = 0; i < linkNum; i++)
{
int n1,n2;
scanf("%d",&n1);
scanf("%d",&n2);
addEdge(graph,n1,n2);
}
for (int i = 0; i < verticesNum; i++)
{
//DEBUG printf("indo para [%d]\n",i);
bubbleSort(graph->vertices[i]);
}
DEBUG printGraph(graph,verticesNum);
for (int i = 0; i < testNum; i++)
{
int n1,n2;
scanf("%d",&n1); //origin
scanf("%d",&n2); //destination
printf("--------\n\nCaso de Teste #%d - BFS(%d)\n\n",i+1,n1);
DEBUG printf("Alvo: [%d]\n",n2);
bfs(graph,n1,n2,verticesNum);
}
printf("--------\n");
return 0;
}
// como fazer visitas de ordem crescente?