-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path19_graphbreadthfirst_v0.c
More file actions
237 lines (189 loc) · 5.07 KB
/
19_graphbreadthfirst_v0.c
File metadata and controls
237 lines (189 loc) · 5.07 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
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
/* Project 19 -- BREATH FIRST SEARCH ALGORITHM (BFS)
19_GRAPHBREATHFIRST_V0.C
------------------------
This is a college work for BREATH FIRST SEARCH ALGORITHM (BFS).
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two categories:
Visited
Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The algorithm works as follows:
Start by putting any one of the graph's vertices at the back of a queue.
Take the front item of the queue and add it to the visited list.
Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back of the queue.
Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover every vertex, we can also run the BFS algorithm on every node
************************************************************
Output:
Choose a vertex to start the search:
0
Vertex Adjacency List 0:
2 -> 1 ->
Vertex Adjacency List 1:
3 -> 0 ->
Vertex Adjacency List 2:
3 -> 0 ->
Vertex Adjacency List 3:
Pressione qualquer tecla para continuar. . .
(base) PS C:\Users\giljr\Desktop\vscode2> cd "c:\Users\giljr\Desktop\vscode2\" ; if ($?) { gcc 19_graphbreadthfirst_v0.c -o 19_graphbreadthfirst_v0 } ; if ($?) { .\19_graphbreadthfirst_v0 }
Choose a vertex to start the search:
0
Vertex Adjacency List 0:
2 -> 1 ->
Vertex Adjacency List 1:
3 -> 0 ->
Vertex Adjacency List 2:
3 -> 0 ->
Vertex Adjacency List 3:
5 -> 4 -> 2 -> 1 ->
Vertex Adjacency List 4:
3 ->
Vertex Adjacency List 5:
3 ->
Visited Graph Vertexes, starting at V0:
V0 V2 V3 V5 V4 V1
Pressione qualquer tecla para continuar. . .
************************************************************
Based on: https://www.programiz.com/dsa/graph-bfs
UNINTER - Course: Computer Engineering
Polytechnic School
Author: Gilberto Jr RU 3326662
Edited: J3
Date: Dez, 2021
*/
#include <stdio.h>
#include <stdlib.h>
#define VERTEX_NUMBER 6
typedef struct AdjacentNodesList
{
int vertex;
struct AdjacentNodesList *prox;
} AdjNodesList_t;
typedef struct Graph
{
int Totalvertexes;
struct AdjacentNodesList **ListaAdj;
} Graph_t;
struct STACK
{
int vertex;
struct STACK *prox;
} *top = NULL;
Graph_t *MakeGraph(int vertexes);
void AddVertex(Graph_t *Graph, int source, int destination);
void ShowGraph(Graph_t *Graph);
void BreadthFirstSearch(Graph_t *AdjNodes, int v, int visited[]);
void push(int y);
void pop();
int main()
{
int visited[VERTEX_NUMBER] = {0};
int source;
Graph_t *GraphTest = MakeGraph(VERTEX_NUMBER);
AddVertex(GraphTest, 0, 1);
AddVertex(GraphTest, 0, 2);
AddVertex(GraphTest, 1, 3);
AddVertex(GraphTest, 2, 3);
AddVertex(GraphTest, 3, 4);
AddVertex(GraphTest, 3, 5);
printf("Choose a vertex to start the search:\n");
scanf_s("%d", &source);
ShowGraph(GraphTest);
printf("\nVisited Graph Vertexes, starting at V%d:\n", source);
BreadthFirstSearch(GraphTest, source, visited);
printf("\n");
system("pause");
return 0;
}
Graph_t *MakeGraph(int Totalvertexes)
{
Graph_t *Graph = (Graph_t *)malloc(sizeof(Graph_t));
Graph->Totalvertexes = Totalvertexes;
Graph->ListaAdj = (AdjNodesList_t **)malloc(Totalvertexes * sizeof(AdjNodesList_t *));
for (int i = 0; i < Totalvertexes; i++)
Graph->ListaAdj[i] = NULL;
return Graph;
}
void AddVertex(Graph_t *Graph, int source, int destination)
{
AdjNodesList_t *NewItem = (AdjNodesList_t *)malloc(sizeof(AdjNodesList_t));
NewItem->vertex = destination;
NewItem->prox = NULL;
NewItem->prox = Graph->ListaAdj[source];
Graph->ListaAdj[source] = NewItem;
NewItem = (AdjNodesList_t *)malloc(sizeof(AdjNodesList_t));
NewItem->vertex = source;
NewItem->prox = NULL;
NewItem->prox = Graph->ListaAdj[destination];
Graph->ListaAdj[destination] = NewItem;
}
void ShowGraph(Graph_t *Graph)
{
int v;
for (v = 0; v < Graph->Totalvertexes; v++)
{
AdjNodesList_t *ScanItem = Graph->ListaAdj[v];
printf("\n Vertex Adjacency List %d:\n ", v);
while (ScanItem != NULL)
{
printf("%d -> ", ScanItem->vertex);
ScanItem = ScanItem->prox;
}
printf("\n");
}
}
void BreadthFirstSearch(Graph_t *AdjNodes, int v, int visited[])
{
int w;
AdjNodesList_t *ScanItem;
ScanItem = (AdjNodesList_t *)malloc(sizeof(AdjNodesList_t));
visited[v] = 1;
printf("V%d\t", v);
push(v);
ScanItem = AdjNodes->ListaAdj[v];
//if (ScanItem == NULL) {
// return;
//}
while (ScanItem != NULL)
{
w = ScanItem->vertex;
if (visited[w] != 1)
{
BreadthFirstSearch(AdjNodes, w, visited);
}
ScanItem = ScanItem->prox;
}
pop();
}
void push(int y)
{
struct STACK *NewItem;
NewItem = (struct STACK *)malloc(sizeof(struct STACK));
NewItem->vertex = y;
if (top == NULL)
{
top = NewItem;
top->prox = NULL;
}
else
{
NewItem->prox = top;
top = NewItem;
}
}
void pop()
{
struct STACK *NewItem;
NewItem = (struct STACK *)malloc(sizeof(struct STACK));
if (top == NULL)
{
//top = NewItem;
//top->prox = NULL;
}
else
{
NewItem = top;
top = top->prox;
free(NewItem);
}
}