-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphAllPairsShortestDistances.c
133 lines (104 loc) · 3.5 KB
/
GraphAllPairsShortestDistances.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
//
// Algoritmos e Estruturas de Dados --- 2024/2025
//
// Joaquim Madeira - Dec 2024
//
// GraphAllPairsShortestDistances
//
// Student Name :
// Student Number :
// Student Name :
// Student Number :
/*** COMPLETE THE GraphAllPairsShortestDistancesExecute FUNCTION ***/
#include "GraphAllPairsShortestDistances.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
#include "GraphBellmanFordAlg.h"
struct _GraphAllPairsShortestDistances {
int** distance; // The 2D matrix storing the all-pairs shortest distances
// It is stored as an array of pointers to 1D rows
// Idea: an INDEFINITE distance value is stored as -1
Graph* graph;
};
// Allocate memory and initialize the distance matrix
// Compute the distances between vertices by running the Bellman-Ford algorithm
GraphAllPairsShortestDistances* GraphAllPairsShortestDistancesExecute(
Graph* g) {
assert(g != NULL);
// COMPLETE THE CODE
unsigned int numVertices = GraphGetNumVertices(g);
// Allocate memory for the result structure
GraphAllPairsShortestDistances* result = (GraphAllPairsShortestDistances*)malloc(sizeof(struct _GraphAllPairsShortestDistances));
assert(result != NULL);
result->graph = g;
// Allocate memory for the distance matrix
result->distance = (int**)malloc(numVertices * sizeof(int*));
assert(result->distance != NULL);
for (unsigned int i = 0; i < numVertices; i++) {
result->distance[i] = (int*)malloc(numVertices * sizeof(int));
assert(result->distance[i] != NULL);
for (unsigned int j = 0; j < numVertices; j++) {
result->distance[i][j] = -1;
}
}
// Compute the shortest paths for each vertex
for (unsigned int v = 0; v < numVertices; v++) {
GraphBellmanFordAlg* bfResult = GraphBellmanFordAlgExecute(g, v);
if (bfResult == NULL) {
for (unsigned int i = 0; i < numVertices; i++) {
free(result->distance[i]);
}
free(result->distance);
free(result);
return NULL;
}
// Store the distances in the distance matrix
for (unsigned int w = 0; w < numVertices; w++) {
if (GraphBellmanFordAlgReached(bfResult, w)) {
result->distance[v][w] = GraphBellmanFordAlgDistance(bfResult, w);
}
}
GraphBellmanFordAlgDestroy(&bfResult);
}
return result;
}
void GraphAllPairsShortestDistancesDestroy(GraphAllPairsShortestDistances** p) {
assert(*p != NULL);
GraphAllPairsShortestDistances* aux = *p;
unsigned int numVertices = GraphGetNumVertices(aux->graph);
for (unsigned int i = 0; i < numVertices; i++) {
free(aux->distance[i]);
}
free(aux->distance);
free(*p);
*p = NULL;
}
// Getting the result
int GraphGetDistanceVW(const GraphAllPairsShortestDistances* p, unsigned int v,
unsigned int w) {
assert(p != NULL);
assert(v < GraphGetNumVertices(p->graph));
assert(w < GraphGetNumVertices(p->graph));
return p->distance[v][w];
}
// DISPLAYING on the console
void GraphAllPairsShortestDistancesPrint(
const GraphAllPairsShortestDistances* p) {
assert(p != NULL);
unsigned int numVertices = GraphGetNumVertices(p->graph);
printf("Graph distance matrix - %u vertices\n", numVertices);
for (unsigned int i = 0; i < numVertices; i++) {
for (unsigned int j = 0; j < numVertices; j++) {
int distanceIJ = p->distance[i][j];
if (distanceIJ == -1) {
// INFINITY - j was not reached from i
printf(" INF");
} else {
printf(" %3d", distanceIJ);
}
}
printf("\n");
}
}