-
Notifications
You must be signed in to change notification settings - Fork 0
/
design4.c
399 lines (340 loc) · 11.7 KB
/
design4.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/********************************************************************************
* @author: Alan Chen
* @email: salianbooth@gmail.com
* @date: 2024/5/31 14:32
* @version: 1.0
* @description: 图的基本操作
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
#define MAX_VERTICES 100
#define INFINITY INT_MAX
typedef struct AdjListNode {
char dest;
int weight;
struct AdjListNode* next;
} AdjListNode;
typedef struct {
AdjListNode* head;
} AdjList;
typedef struct {
int V;
int E;
AdjList* array;
char* vertices; // Array to store vertex names
} Graph;
// Function to create a new adjacency list node
AdjListNode* newAdjListNode(char dest, int weight) {
AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode));
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}
// Function to create a graph of V vertices
Graph* createGraph(int V) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->V = V;
graph->array = (AdjList*)malloc(V * sizeof(AdjList));
graph->vertices = (char*)malloc(V * sizeof(char));
for (int i = 0; i < V; i++) {
graph->array[i].head = NULL;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(Graph* graph, char src, char dest, int weight) {
AdjListNode* newNode = newAdjListNode(dest, weight);
int srcIndex = strchr(graph->vertices, src) - graph->vertices;
newNode->next = graph->array[srcIndex].head;
graph->array[srcIndex].head = newNode;
}
// Function to calculate the in-degree and out-degree of each vertex
void calculateDegrees(Graph* graph, int inDegree[], int outDegree[]) {
for (int i = 0; i < graph->V; i++) {
outDegree[i] = 0;
inDegree[i] = 0;
}
for (int v = 0; v < graph->V; v++) {
AdjListNode* pCrawl = graph->array[v].head;
while (pCrawl) {
outDegree[v]++;
int destIndex = strchr(graph->vertices, pCrawl->dest) - graph->vertices;
inDegree[destIndex]++;
pCrawl = pCrawl->next;
}
}
}
// DFS traversal of the vertices reachable from v
void DFSUtil(Graph* graph, int v, bool visited[]) {
visited[v] = true;
printf("%c ", graph->vertices[v]);
AdjListNode* pCrawl = graph->array[v].head;
while (pCrawl) {
int adj = strchr(graph->vertices, pCrawl->dest) - graph->vertices;
if (!visited[adj]) {
DFSUtil(graph, adj, visited);
}
pCrawl = pCrawl->next;
}
}
// DFS traversal of the entire graph
void DFS(Graph* graph, char startVertex) {
bool* visited = (bool*)malloc(graph->V * sizeof(bool));
for (int i = 0; i < graph->V; i++) {
visited[i] = false;
}
int startIndex = strchr(graph->vertices, startVertex) - graph->vertices;
printf("DFS starting from vertex %c:\n", startVertex);
DFSUtil(graph, startIndex, visited);
printf("\n");
free(visited);
}
// Function to perform BFS traversal
void BFS(Graph* graph, char startVertex) {
bool* visited = (bool*)malloc(graph->V * sizeof(bool));
for (int i = 0; i < graph->V; i++) {
visited[i] = false;
}
int* queue = (int*)malloc(graph->V * sizeof(int));
int front = 0, rear = 0;
int startIndex = strchr(graph->vertices, startVertex) - graph->vertices;
visited[startIndex] = true;
queue[rear++] = startIndex;
printf("BFS starting from vertex %c:\n", startVertex);
while (front < rear) {
int currVertex = queue[front++];
printf("%c ", graph->vertices[currVertex]);
AdjListNode* pCrawl = graph->array[currVertex].head;
while (pCrawl) {
int adj = strchr(graph->vertices, pCrawl->dest) - graph->vertices;
if (!visited[adj]) {
visited[adj] = true;
queue[rear++] = adj;
}
pCrawl = pCrawl->next;
}
}
printf("\n");
free(visited);
free(queue);
}
// Function to delete edges connected to a specific vertex
void deleteEdges(Graph* graph, char vertex) {
int vertexIndex = strchr(graph->vertices, vertex) - graph->vertices;
// Delete outgoing edges
AdjListNode* current = graph->array[vertexIndex].head;
while (current) {
AdjListNode* temp = current;
current = current->next;
free(temp);
}
graph->array[vertexIndex].head = NULL;
// Delete incoming edges
for (int i = 0; i < graph->V; i++) {
AdjListNode* pCrawl = graph->array[i].head;
AdjListNode* prev = NULL;
while (pCrawl) {
if (pCrawl->dest == vertex) {
if (prev) {
prev->next = pCrawl->next;
} else {
graph->array[i].head = pCrawl->next;
}
free(pCrawl);
break;
}
prev = pCrawl;
pCrawl = pCrawl->next;
}
}
}
// Check if graph is connected using DFS
bool isConnected(Graph* graph, char startVertex) {
bool* visited = (bool*)malloc(graph->V * sizeof(bool));
for (int i = 0; i < graph->V; i++) {
visited[i] = false;
}
int startIndex = strchr(graph->vertices, startVertex) - graph->vertices;
DFSUtil(graph, startIndex, visited);
for (int i = 0; i < graph->V; i++) {
if (!visited[i]) {
free(visited);
return false;
}
}
free(visited);
return true;
}
// Convert adjacency list to adjacency matrix
void adjListToAdjMatrix(Graph* graph, int adjMatrix[][MAX_VERTICES]) {
for (int i = 0; i < graph->V; i++) {
for (int j = 0; j < graph->V; j++) {
adjMatrix[i][j] = 0;
}
}
for (int i = 0; i < graph->V; i++) {
AdjListNode* pCrawl = graph->array[i].head;
while (pCrawl) {
int destIndex = strchr(graph->vertices, pCrawl->dest) - graph->vertices;
adjMatrix[i][destIndex] = pCrawl->weight;
pCrawl = pCrawl->next;
}
}
}
typedef struct {
int adjvex;
int lowcost;
} Closedge;
Closedge closedge[MAX_VERTICES]; // Auxiliary array
int getVertexIndex(Graph* graph, char vertex) {
return strchr(graph->vertices, vertex) - graph->vertices;
}
void PrimMiniSpanTree(Graph* graph, char v0) {
int v0Index = getVertexIndex(graph, v0);
closedge[v0Index].lowcost = 0;
for (int i = 0; i < graph->V; i++) {
if (i != v0Index) {
closedge[i].adjvex = v0Index; // Initialize all vertices' adjacent vertices to v0
closedge[i].lowcost = INFINITY;
}
}
AdjListNode* pCrawl = graph->array[v0Index].head;
while (pCrawl) {
int adjIndex = getVertexIndex(graph, pCrawl->dest);
closedge[adjIndex].lowcost = pCrawl->weight;
pCrawl = pCrawl->next;
}
for (int i = 0; i < graph->V - 1; i++) { // Loop V-1 times since v0 is already included
int min = INFINITY;
int index = 0; // To store the index of the vertex with the minimum edge
for (int j = 0; j < graph->V; j++) {
if (closedge[j].lowcost != 0 && closedge[j].lowcost < min) {
min = closedge[j].lowcost;
index = j;
}
}
printf("(%c, %c) ", graph->vertices[closedge[index].adjvex], graph->vertices[index]); // Output the minimum edge
closedge[index].lowcost = 0; // Mark this vertex as included in MST
pCrawl = graph->array[index].head;
while (pCrawl) {
int adjIndex = getVertexIndex(graph, pCrawl->dest);
if (closedge[adjIndex].lowcost != 0 && pCrawl->weight < closedge[adjIndex].lowcost) {
closedge[adjIndex].lowcost = pCrawl->weight;
closedge[adjIndex].adjvex = index;
}
pCrawl = pCrawl->next;
}
}
}
int main() {
int V = 5;
int E = 6;
Graph* graph = createGraph(V);
graph->vertices[0] = 'A';
graph->vertices[1] = 'B';
graph->vertices[2] = 'C';
graph->vertices[3] = 'D';
graph->vertices[4] = 'E';
addEdge(graph, 'A', 'B', 3);
addEdge(graph, 'A', 'C', 2);
addEdge(graph, 'B', 'C', 1);
addEdge(graph, 'C', 'D', 4);
addEdge(graph, 'D', 'E', 6);
addEdge(graph, 'D', 'B', 9);
addEdge(graph, 'E', 'A', 7);
int inDegree[MAX_VERTICES], outDegree[MAX_VERTICES];
calculateDegrees(graph, inDegree, outDegree);
printf("Vertex\tIn-Degree\tOut-Degree\n");
for (int i = 0; i < V; i++) {
printf("%c\t%d\t\t%d\n", graph->vertices[i], inDegree[i], outDegree[i]);
}
DFS(graph, 'A');
BFS(graph, 'A');
/* char deleteVertex;
deleteEdges(graph, 'E');*/
if (isConnected(graph, 'A')) {
printf("The graph is connected.\n");
} else {
printf("The graph is not connected.\n");
}
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
adjListToAdjMatrix(graph, adjMatrix);
for(int i = 0;i < V;i++){
for(int j = 0;j < E ;j++){
printf("%d ",adjMatrix[i][j]);
}
printf("\n");
}
PrimMiniSpanTree(graph, 'A');
/* printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
Graph* graph = createGraph(V);
printf("Enter the vertices (single character):\n");
for (int i = 0; i < V; i++) {
printf("Vertex %d: ", i + 1);
scanf(" %c", &graph->vertices[i]);
}
printf("Enter the edges (src dest weight):\n");
for (int i = 0; i < E; i++) {
char src, dest;
int weight;
printf("Edge %d: ", i + 1);
scanf(" %c %c %d", &src, &dest, &weight);
addEdge(graph, src, dest, weight);
}*/
/* int inDegree[MAX_VERTICES], outDegree[MAX_VERTICES];
calculateDegrees(graph, inDegree, outDegree);
printf("Vertex\tIn-Degree\tOut-Degree\n");
for (int i = 0; i < V; i++) {
printf("%c\t%d\t\t%d\n", graph->vertices[i], inDegree[i], outDegree[i]);
}*/
/* char startVertex;
printf("Enter the starting vertex for DFS and BFS: ");
scanf(" %c", &startVertex);
DFS(graph, startVertex);
BFS(graph, startVertex);*/
/* char deleteVertex;
printf("Enter the vertex to delete edges: ");
scanf(" %c", &deleteVertex);
deleteEdges(graph, deleteVertex);
if (isConnected(graph, startVertex)) {
printf("The graph is connected.\n");
} else {
printf("The graph is not connected.\n");
}*/
/*
int vertexToDelete;
printf("Enter the vertex to delete its edges: ");
scanf("%d", &vertexToDelete);
deleteEdges(graph, vertexToDelete);
printf("DFS after deleting edges of vertex %d:\n", vertexToDelete);
DFS(graph, startVertex);
bool connected = isConnected(graph, startVertex);
if (connected) {
printf("The graph is connected.\n");
} else {
printf("The graph is not connected.\n");
}
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
adjListToAdjMatrix(graph, adjMatrix);
primMST(adjMatrix, V);
*/
free(graph->vertices);
for (int i = 0; i < V; i++) {
AdjListNode* current = graph->array[i].head;
while (current) {
AdjListNode* temp = current;
current = current->next;
free(temp);
}
}
free(graph->array);
free(graph);
return 0;
}