Skip to content

Commit f7acea7

Browse files
committedMay 18, 2021
troublesome bfs comeon
1 parent 47a4e25 commit f7acea7

File tree

5 files changed

+786
-13
lines changed

5 files changed

+786
-13
lines changed
 

‎bfs.c

+240
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#define DEBUG if(1)
5+
#define MAX_SIZE 10000
6+
//https://thehuxley.com/problem/805
7+
8+
typedef struct adjacent_list //glorified node
9+
{
10+
int item;
11+
struct adjacent_list* next;
12+
}adj_list;
13+
typedef struct graph
14+
{
15+
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
16+
short visited[MAX_SIZE];
17+
}graph;
18+
typedef struct _queue
19+
{
20+
adj_list* head;
21+
adj_list* tail;
22+
}Queue;
23+
24+
graph* createGraph() //creates a graph with nullified entries and visited[] = {0}
25+
{
26+
graph* g = (graph*) malloc(sizeof(graph));
27+
for (int i = 0; i <= MAX_SIZE-1; i++)
28+
{
29+
g->vertices[i] = NULL;
30+
g->visited[i] = 0;
31+
}
32+
return g;
33+
}
34+
void bubbleSort(adj_list* list) //list = vertices[i];
35+
{
36+
if (list == NULL) return;
37+
adj_list* pos; //auxiliary node* that points to head
38+
adj_list* tmp = NULL;
39+
int flag = 0; //flag will switch to 1 while it's making swaps
40+
do
41+
{
42+
pos = list;//set the position to head.
43+
flag = 0;
44+
45+
while (pos->next != tmp)
46+
{
47+
if (pos->item > pos->next->item)
48+
{
49+
//DEBUG printf("%d > %d\n",pos->item , pos->next->item);
50+
int t = pos->item;
51+
pos->item = pos->next->item;
52+
pos->next->item = t;
53+
flag = 1;
54+
}
55+
pos = pos->next;
56+
}
57+
tmp = pos; //stores the last pos 'cuz it's already been sorted.
58+
} while (flag);
59+
60+
}
61+
adj_list* createAdjList(int origin) //creates a "node" with value
62+
{
63+
adj_list* adj = (adj_list*) malloc (sizeof(adj_list));
64+
adj->item = origin;
65+
adj->next = NULL;
66+
return adj;
67+
}
68+
void addEdge(graph* g, int origin, int destination) //adds edges for an directioned graph
69+
{
70+
adj_list *vertex = createAdjList(destination);
71+
vertex->next = g->vertices[origin];
72+
g->vertices[origin] = vertex;
73+
//DEBUG printf("Aresta que liga %d %d\n",origin,destination);
74+
}
75+
Queue* initQ() //initializes the queue, nullifying the head and tail
76+
{
77+
Queue* newQ = (Queue*) malloc(sizeof(Queue));
78+
newQ->head = NULL;
79+
newQ->tail = NULL;
80+
return newQ;
81+
}
82+
int isEmptyQ(Queue* q)
83+
{
84+
return(q->head == NULL);
85+
}
86+
void enQueue(Queue* q, int origin)
87+
{
88+
adj_list* toBeQueued = createAdjList(origin);
89+
if (isEmptyQ(q))
90+
{
91+
q->head = toBeQueued;
92+
q->tail = toBeQueued;
93+
}
94+
else
95+
{
96+
q->tail->next = toBeQueued;
97+
q->tail = toBeQueued;
98+
}
99+
}
100+
int deQueue(Queue* q)
101+
{
102+
int deQueued = q->head->item;
103+
adj_list* tmp = q->head->next;
104+
free(q->head);
105+
q->head = tmp;
106+
return deQueued;
107+
}
108+
void printGraph(graph* gr, int vertexNum)
109+
{
110+
for (int i = 0; i < vertexNum; i++)
111+
{
112+
adj_list* tmp = gr->vertices[i];
113+
printf("v[%d]: ",i);
114+
while (tmp)
115+
{
116+
printf("v(%d),",tmp->item);
117+
tmp= tmp->next;
118+
}
119+
printf("\n");
120+
}
121+
}
122+
void printQ(Queue* q)
123+
{
124+
adj_list* tmp = q->head;
125+
while (tmp != NULL)
126+
{
127+
printf("[%d]→",tmp->item);
128+
tmp = tmp->next;
129+
}
130+
printf("\n");
131+
132+
}
133+
void bfs(graph* graph,int origin, int destination, int count)
134+
{
135+
for (int i = 0; i < MAX_SIZE; i++)
136+
{
137+
graph->visited[i] = 0;
138+
}
139+
140+
adj_list* tmp = graph->vertices[origin];
141+
Queue* queue = initQ();
142+
int dequeued;
143+
graph->visited[origin] = 1;
144+
//DEBUG printf("\tgraph->visited[%d] = %d\n",origin,graph->visited[origin]);
145+
146+
enQueue(queue,origin);
147+
printf("Iniciando busca em largura a partir de %d\n",origin); //para a primeira entrada
148+
DEBUG printQ(queue);
149+
150+
int lasts[MAX_SIZE];
151+
int found = 0;
152+
while (!isEmptyQ(queue))
153+
{
154+
//DEBUG printf("Fila não está vazia ainda.\n");
155+
dequeued = deQueue(queue);
156+
tmp = graph->vertices[dequeued];
157+
while (tmp != NULL && !graph->visited[tmp->item])
158+
{
159+
//DEBUG printf("found == [%d], tmp = [%d]\n",found,tmp->item);
160+
//if(tmp->next != NULL) DEBUG printf("Visitando [%d], proximo = [%d]\n",tmp->item,tmp->next->item);
161+
printf("Iniciando busca em largura a partir de %d\n",tmp->item);
162+
if(found == 0){ lasts[count] = tmp->item; count++;}
163+
if(tmp->item == destination)
164+
{
165+
found = 1;
166+
//count++;
167+
//DEBUG printf("ENCONTRADO depois de %d passagens, ultimo foi [%d]\n",count,lasts[count-1]);
168+
//break;
169+
}
170+
if(!graph->visited[tmp->item])
171+
{
172+
// DEBUG printf("\tgraph->visited[%d] = %d\n",tmp->item,graph->visited[tmp->item]);
173+
graph->visited[tmp->item] = 1;
174+
enQueue(queue,tmp->item);
175+
DEBUG printQ(queue);
176+
}
177+
tmp = tmp->next;
178+
count++;
179+
180+
}
181+
}
182+
183+
//node_explanation(graph);
184+
185+
186+
if (found == 0) printf("Sem caminho entre %d e %d\n\n",origin,destination);
187+
else
188+
{
189+
printf("Caminho entre %d e %d: %d => ",origin,destination,origin);
190+
for (int i = 0; i < count; i++)
191+
{
192+
if (i == count-1)
193+
{
194+
printf("%d\n",lasts[i]);
195+
break;
196+
}
197+
printf("%d => ",lasts[i]);
198+
}
199+
}
200+
DEBUG printf("FIM DA BUSCA!\n");
201+
202+
}
203+
int main()
204+
{
205+
graph* graph = createGraph();
206+
int verticesNum, linkNum, testNum; //V A T
207+
scanf("%d",&verticesNum);
208+
scanf("%d",&linkNum);
209+
scanf("%d",&testNum);
210+
for (int i = 0; i < linkNum; i++)
211+
{
212+
int n1,n2;
213+
scanf("%d",&n1);
214+
scanf("%d",&n2);
215+
addEdge(graph,n1,n2);
216+
}
217+
218+
//printGraph(graph,verticesNum);
219+
for (int i = 0; i < verticesNum; i++)
220+
{
221+
//DEBUG printf("indo para [%d]\n",i);
222+
bubbleSort(graph->vertices[i]);
223+
}
224+
printGraph(graph,verticesNum);
225+
226+
227+
for (int i = 0; i < testNum; i++)
228+
{
229+
int n1,n2;
230+
scanf("%d",&n1); //origin
231+
scanf("%d",&n2); //destination
232+
printf("--------\n\nCaso de Teste #%d - BFS(%d)\n\n",i+1,n1);
233+
DEBUG printf("Alvo: [%d]\n",n2);
234+
bfs(graph,n1,n2,0);
235+
}
236+
237+
238+
return 0;
239+
}
240+
// como fazer visitas de ordem crescente?

0 commit comments

Comments
 (0)
Please sign in to comment.