-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphClient.c
134 lines (115 loc) · 3.12 KB
/
graphClient.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
// graphClient.c
// A graph client that reads in and creates a graph from a file
// This needs to be modified to complete lab07
// Written by: Angela Finlayson
// Date: 17/01/09
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Graph.h"
#include "time.h"
// This means that st is a global varaible
// that was defined in a different file
extern int *st;
extern int cnt;
// THIS NEEDS TO BE COMPLETED FOR TASK 1
// Reads in data from the file with the given filename and creates a newGraph
// The file must be of the format
// numVertices
// v0 v1 v2 v3
// v1 v2 v4
Graph readGraph(char * filename) {
FILE *fp;
fp = fopen (filename, "r"); // open data file
assert (fp != NULL);
int city = 0;
int dest = 0;
char c = 0;
// First line of file has the number of vertices
int numV;
Graph g;
if(fscanf(fp, "%d", &numV) > 0){
g = NULL;
}
g = newGraph(numV);
// scan through file and insert edges into graph
int counter = 0;
while (counter < numV) {
if(fscanf(fp, "%d", &city) > 0){
counter++;
}
while (c != '\n') {
if(fscanf (fp, "%d", &dest) > 0){
insertE(g, mkEdge(city, dest));
c = getc(fp);
}
}
c = 0;
}
int v;
char name[100];
char capital[100];
long population;
while(fscanf(fp, "%d %s %s %ld", &v, name, capital, &population) == 4) {
// TASK 1: ACTUALLY DO SOMETHING WITH THIS DATA!
setData(g,v,name,capital,population);
}
fclose(fp);
return g;
}
// IMPLEMENT THIS FOR TASK 3
Edge getNextFlight(Graph g, int *dfsOrdered, int *nDiffCountries, Vertex currCountry){
int n = *nDiffCountries;
Vertex end = dfsOrdered[n];
if(isAdjacent(g,currCountry,end)){
*nDiffCountries = *nDiffCountries + 1;
st[end] = currCountry;
} else {
end = st[currCountry];
}
return mkEdge(currCountry,end);
}
void getFlights(Graph g, int * dfsOrdered){
int nDiffCountries = 1;
int nFlights = 0;
int currCountry = 0;
while (nDiffCountries < numV(g)){
Edge nextFlight = getNextFlight(g, dfsOrdered, &nDiffCountries, currCountry);
nFlights++;
printf("Flight %d %s %s\n", nFlights, getVertexLabel(g,nextFlight.v), getVertexLabel(g,nextFlight.w));
currCountry = nextFlight.w;
}
}
int main(int argc, char * argv[]){
if (argc < 2){
printf("Incorrect usage: must enter filename\n");
exit (1);
}
Graph g = readGraph(argv[1]);
showGraph(g);
printf("\n");
// TASK 1
printf("TASK 1\n");
showGraphLabels(g);
printf("\n");
showData(g);
printf("\n");
// TASK 2
printf("TASK 2\n");
int *dfsOrdered = malloc(numV(g) * sizeof(Vertex));
dfSearch2(g, dfsOrdered);
int i;
for(i=0;i< cnt;i++){
Vertex v = dfsOrdered[i];
showVertexData(g,v);
}
printf("\n");
// TASK 3
printf("TASK 3\n");
printf("\nFlying all over the world\n");
//Uncomment out this line when you get to task 3
getFlights(g,dfsOrdered);
free(dfsOrdered);
destroyGraph(g);
return 0;
}