-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBellmanFordAlg.c
98 lines (76 loc) · 2.33 KB
/
TestBellmanFordAlg.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
//
// Algoritmos e Estruturas de Dados --- 2024/2025
//
// Joaquim Madeira, Joao Manuel Rodrigues - Dec 2024
//
// Testing the Bellman-Ford algorithm
//
#include <assert.h>
#include "Graph.h"
#include "GraphBellmanFordAlg.h"
int main(void) {
// What kind of graph is dig01?
Graph* dig01 = GraphCreate(6, 1, 0);
GraphAddEdge(dig01, 1, 2);
GraphAddEdge(dig01, 1, 4);
GraphAddEdge(dig01, 3, 4);
printf("The graph:\n");
// Displaying in DOT format
GraphDisplayDOT(dig01);
printf("\n");
GraphCheckInvariants(dig01);
// Bellman-Ford Algorithm
// Consider each vertex as a start vertex
for (unsigned int i = 0; i < 6; i++) {
GraphBellmanFordAlg* BF_result = GraphBellmanFordAlgExecute(dig01, i);
printf("The shortest path tree rooted at %u\n", i);
GraphBellmanFordAlgDisplayDOT(BF_result);
printf("\n");
GraphBellmanFordAlgDestroy(&BF_result);
}
// What kind of graph is g01?
Graph* g01 = GraphCreate(6, 0, 0);
GraphAddEdge(g01, 0, 5);
GraphAddEdge(g01, 2, 4);
GraphAddEdge(g01, 2, 3);
GraphAddEdge(g01, 1, 2);
GraphAddEdge(g01, 0, 1);
GraphAddEdge(g01, 3, 4);
GraphAddEdge(g01, 3, 5);
GraphAddEdge(g01, 0, 2);
printf("The graph:\n");
// Displaying in DOT format
GraphDisplayDOT(g01);
printf("\n");
GraphCheckInvariants(g01);
// Bellman-Ford Algorithm
// Consider each vertex as a start vertex
for (unsigned int i = 0; i < 6; i++) {
GraphBellmanFordAlg* BF_result = GraphBellmanFordAlgExecute(g01, i);
printf("The shortest path tree rooted at %u\n", i);
GraphBellmanFordAlgDisplayDOT(BF_result);
printf("\n");
GraphBellmanFordAlgDestroy(&BF_result);
}
// Reading a directed graph from file
FILE* file = fopen("Graphs/DG_2.txt", "r");
Graph* dig03 = GraphFromFile(file);
fclose(file);
// Displaying in DOT format
GraphDisplayDOT(dig03);
printf("\n");
GraphCheckInvariants(dig03);
// Bellman-Ford Algorithm
// Consider each vertex as a start vertex
for (unsigned int i = 0; i < GraphGetNumVertices(dig03); i++) {
GraphBellmanFordAlg* BF_result = GraphBellmanFordAlgExecute(dig03, i);
printf("The shortest path tree rooted at %u\n", i);
GraphBellmanFordAlgDisplayDOT(BF_result);
printf("\n");
GraphBellmanFordAlgDestroy(&BF_result);
}
GraphDestroy(&g01);
GraphDestroy(&dig01);
GraphDestroy(&dig03);
return 0;
}