-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
109 lines (94 loc) · 2.83 KB
/
utils.c
File metadata and controls
109 lines (94 loc) · 2.83 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
/*
utils.c
Implementations for helper functions to do with reading and writing.
Skeleton written by Grady Fitzpatrick for COMP20007 Assignment 1 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "graph.h"
#include "utils.h"
struct graphProblem {
int numServers;
int numConnections;
int outageCount;
int *outageSIDs;
struct graph *graph;
};
struct graphProblem *readProblem(FILE *outageFile, FILE *networkFile){
int i;
int startServer;
int endServer;
/* Allocate space for problem specification */
struct graphProblem *problem = (struct graphProblem *)
malloc(sizeof(struct graphProblem));
assert(problem);
/* First line comprises number of servers and number of connections. */
assert(fscanf(networkFile, "%d %d", &(problem->numServers),
&(problem->numConnections)) == 2);
/* Build graph number of houses + 1 because of datacentre. */
problem->graph = newGraph(problem->numServers + 1);
/* Add all edges to graph. */
for(i = 0; i < problem->numConnections; i++){
assert(fscanf(networkFile, "%d %d", &startServer, &endServer) == 2);
addEdge(problem->graph, startServer, endServer);
}
/* Read outage information. */
assert(fscanf(outageFile, "%d", &(problem->outageCount)) == 1);
problem->outageSIDs = (int *) realloc(NULL, sizeof(int) *
problem->outageCount);
/* Allow 0 outages for tasks 2, 3 and 7. */
if(problem->outageCount > 0){
assert(problem->outageSIDs);
}
for(i = 0; i < problem->outageCount; i++){
assert(fscanf(outageFile, "%d", &((problem->outageSIDs)[i])) == 1);
}
return problem;
}
struct solution *findSolution(struct graphProblem *problem,
enum problemPart part){
return graphSolve(problem->graph, part, problem->numServers,
problem->outageCount, problem->outageSIDs);
}
void freeProblem(struct graphProblem *problem){
/* No need to free if no data allocated. */
if(! problem){
return;
}
if(problem->outageSIDs){
free(problem->outageSIDs);
}
freeGraph(problem->graph);
free(problem);
}
void freeSolution(struct solution *solution){
/* No need to free if no data allocated. */
if(! solution){
return;
}
if(solution->largestSubnetSIDs){
free(solution->largestSubnetSIDs);
}
if(solution->postOutageDiameterSIDs){
free(solution->postOutageDiameterSIDs);
}
if(solution->criticalServerSIDs){
free(solution->criticalServerSIDs);
}
free(solution);
}
void initaliseSolution(struct solution *solution){
if(! solution){
return;
}
solution->connectedSubnets = 0;
solution->largestSubnet = 0;
solution->largestSubnetSIDs = NULL;
solution->postOutageDiameter = 0;
/* If a path exists, this should be one larger than the diameter. */
solution->postOutageDiameterCount = 0;
solution->postOutageDiameterSIDs = NULL;
solution->criticalServerCount = 0;
solution->criticalServerSIDs = NULL;
}