-
Notifications
You must be signed in to change notification settings - Fork 1
/
largura2.c
157 lines (132 loc) · 2.88 KB
/
largura2.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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct lista_adj no;
typedef struct grafo grafo;
typedef struct fila fila;
#define tammax 300
struct fila
{
int items[tammax];
int comeco;
int final;
};
struct lista_adj
{
int vertice;
no* prox;
};
struct grafo
{
int numv;
no** adj;
int *visitado;
};
fila* createQueue() {
fila* q = malloc(sizeof(fila));
q->comeco = -1;
q->final = -1;
return q;
}
int isEmpty(fila* q) {
if(q->final == -1)
return 1;
else
return 0;
}
void enqueue(fila* q, int value){
if(q->final == tammax-1)
printf("\nQueue is Full!!");
else {
if(q->comeco == -1)
q->comeco = 0;
q->final++;
q->items[q->final] = value;
}
}
int dequeue(fila* q){
int item;
if(isEmpty(q)){
printf("Queue is empty");
item = -1;
}
else{
item = q->items[q->comeco];
q->comeco++;
if(q->comeco > q->final){
printf("Resetting queue");
q->comeco = q->final = -1;
}
}
return item;
}
void printQueue(fila *q) {
int i = q->comeco;
if(isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for(i = q->comeco; i < q->final + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
no* createNode(int v)
{
no* newNode = malloc(sizeof(no));
newNode->vertice = v;
newNode->prox = NULL;
return newNode;
}
grafo* createGraph(int vertices)
{
grafo* graph = malloc(sizeof(grafo));
graph->numv = vertices;
graph->adj = malloc(vertices * sizeof(no*));
graph->visitado = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adj[i] = NULL;
graph->visitado[i] = 0;
}
return graph;
}
void addEdge(grafo* graph, int src, int dest)
{
// Add edge from src to dest
no* newNode = createNode(dest);
newNode->prox = graph->adj[src];
graph->adj[src] = newNode;
}
void bfs(grafo* graph, int inicio) {
struct queue* q = createQueue();
graph->visitado[inicio] = 1;
enqueue(q, inicio);
while(!isEmpty(q)){
printQueue(q);
int currentVertex = dequeue(q);
printf("visitado %d\n", currentVertex);
no* temp = graph->adj[currentVertex];
while(temp) {
int adjVertex = temp->vertice;
if(graph->visitado[adjVertex] == 0){
graph->visitado[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->prox;
}
}
}
int main()
{
grafo* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}