forked from MirceaOvidiu/CleanCoding-GIT-PA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.c
More file actions
220 lines (161 loc) · 4.02 KB
/
Copy pathexample2.c
File metadata and controls
220 lines (161 loc) · 4.02 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
#include <stdio.h>
#include <stdlib.h>
/* Structura pentru nod */
typedef struct Node
{
int data;
struct Node *next;
} Node;
/* Structura pentru graf */
typedef struct Graph
{
int vertices;
int *visited;
Node **adjacencyLists;
} Graph;
/* Creeaza nod */
Node *createNode(int value)
{
Node *newNode = malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
/* Creeaza graf */
Graph *createGraph(int vertices)
{
Graph *graph = malloc(sizeof(Graph));
graph->vertices = vertices;
graph->adjacencyLists = malloc(vertices * sizeof(Node *));
graph->visited = malloc(vertices * sizeof(int));
for (int i = 0; i < vertices; i++)
{
graph->adjacencyLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
/* Adauga muchie */
void addEdge(Graph *graph, int source, int destination)
{
Node *newNode = createNode(destination);
newNode->next = graph->adjacencyLists[source];
graph->adjacencyLists[source] = newNode;
newNode = createNode(source);
newNode->next = graph->adjacencyLists[destination];
graph->adjacencyLists[destination] = newNode;
}
/* Introdu muchii */
void insertEdges(Graph *graph, int numberOfEdges)
{
int source;
int destination;
printf("Introdu muchiile:\n");
for (int i = 0; i < numberOfEdges; i++)
{
scanf("%d %d", &source, &destination);
addEdge(graph, source, destination);
}
}
/* Verifica daca coada e goala */
int isEmpty(Node *queue)
{
return queue == NULL;
}
/* Adauga in coada */
void enqueue(Node **queue, int value)
{
Node *newNode = createNode(value);
if (*queue == NULL)
{
*queue = newNode;
}
else
{
Node *temp = *queue;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
/* Scoate din coada */
int dequeue(Node **queue)
{
Node *temp = *queue;
int value = temp->data;
*queue = (*queue)->next;
free(temp);
return value;
}
/* Reset visited */
void resetVisited(Graph *graph)
{
for (int i = 0; i < graph->vertices; i++)
{
graph->visited[i] = 0;
}
}
/* DFS */
void DFS(Graph *graph, int vertex)
{
graph->visited[vertex] = 1;
printf("%d ", vertex);
Node *temp = graph->adjacencyLists[vertex];
while (temp != NULL)
{
int connectedVertex = temp->data;
if (graph->visited[connectedVertex] == 0)
{
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
/* BFS */
void BFS(Graph *graph, int startVertex)
{
Node *queue = NULL;
graph->visited[startVertex] = 1;
enqueue(&queue, startVertex);
while (!isEmpty(queue))
{
int currentVertex = dequeue(&queue);
printf("%d ", currentVertex);
Node *temp = graph->adjacencyLists[currentVertex];
while (temp != NULL)
{
int adjacentVertex = temp->data;
if (graph->visited[adjacentVertex] == 0)
{
graph->visited[adjacentVertex] = 1;
enqueue(&queue, adjacentVertex);
}
temp = temp->next;
}
}
}
int main()
{
int numberOfVertices;
int numberOfEdges;
int startVertex;
printf("Introdu numarul de noduri: ");
scanf("%d", &numberOfVertices);
printf("Introdu numarul de muchii: ");
scanf("%d", &numberOfEdges);
Graph *graph = createGraph(numberOfVertices);
insertEdges(graph, numberOfEdges);
printf("Start DFS: ");
scanf("%d", &startVertex);
printf("Parcurgere DFS:\n");
DFS(graph, startVertex);
resetVisited(graph);
printf("\n");
printf("Start BFS: ");
scanf("%d", &startVertex);
printf("Parcurgere BFS:\n");
BFS(graph, startVertex);
return 0;
}