-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_AdjMatrix.c
293 lines (256 loc) · 6.66 KB
/
Graph_AdjMatrix.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
// An adjacency matrix implementation of a graph ADT.
// This file needs to be modified for lab06
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Graph.h"
//The preorder array. Each index stores the order the vertex was visited.
int *pre = NULL;
int cnt;
//The spanning tree array. Each index stores the predecessor vertex in the search
int *st = NULL;
typedef struct graphRep GraphRep;
//You will need to modify this for task 1 to store information
//about country/capital/population data.
struct graphRep {
int nV; // #vertices
int nE; // #edges
int **adj; // matrix of 0 or 1
char **countries; //array of countries
char **capitals; //array of capitals
long *population; //array of population
};
// local checking function
static int validV(Graph g, Vertex v) {
return (v >= 0 && v < g->nV);
}
// Create an edge from v to w
// Does allow self loops
Edge mkEdge(Vertex v, Vertex w) {
assert(v >= 0 && w >= 0);
Edge e = {v,w};
return e;
}
//Initialise a new graph
Graph newGraph(int nV) {
assert(nV >= 0);
int i, j;
Graph g = malloc(sizeof(GraphRep));
assert(g != NULL);
g->adj = malloc(nV * sizeof(int *));
assert(g->adj != NULL);
for (i = 0; i < nV; i++) {
g->adj[i] = malloc(nV * sizeof(int));
assert(g->adj[i] != NULL);
for (j = 0; j < nV; j++) {
g->adj[i][j] = 0;
}
}
g->nV = nV;
g->nE = 0;
g->countries = malloc(sizeof(char *) *nV);
assert(g->countries != NULL);
g->capitals = malloc(sizeof(char *) *nV);
assert(g->capitals != NULL);
g->population = malloc(sizeof(long) *nV);
assert(g->population != NULL);
return g;
}
// returns true if there is an edge from v1 to v2
int isAdjacent(Graph g, Vertex v1, Vertex v2){
assert(g != NULL);
return(g->adj[v1][v2]);
}
//Insert an edge into a graph
void insertE(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (!g->adj[e.v][e.w]){
g->adj[e.v][e.w] = 1;
g->adj[e.w][e.v] = 1;
g->nE++;
}
}
// remove an edge from a graph
void removeE(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v)&& validV(g,e.w));
if (g->adj[e.v][e.w]){
g->adj[e.v][e.w] = 0;
g->adj[e.w][e.v] = 0;
g->nE--;
}
}
//Display the graph
void showGraph(Graph g) {
assert(g != NULL);
printf("V=%d, E=%d\n", g->nV, g->nE);
int i, j;
for (i = 0; i < g->nV; i++) {
int nshown = 0;
for (j = 0; j < g->nV; j++) {
if (g->adj[i][j] != 0) {
printf("%d-%d ",i,j);
nshown++;
}
}
if (nshown > 0) {
printf("\n");
}
}
}
// get all the edges from the graph and put them in the array es
// return the number of edges
int edges(Edge es[], int nE, Graph g) {
assert(g != NULL && es != NULL && nE > 0);
int i, j, n = 0;
for (i = 0; i < g->nV; i++) {
for (j = i+1; j < g->nV; j++) {
if (g->adj[i][j] != 0) {
assert(n < nE);
es[n++] = mkEdge(i,j);
}
}
}
return n;
}
// returns the maximum number of vertices in the graph
int numV(Graph g){
assert(g != NULL);
return g->nV;
}
// Returns the number of edges in the graph
int numE(Graph g){
assert(g != NULL);
return g->nE;
}
// Searching Functions
// Assume we start with cnt = 0 and pre[v] = -1 and
// st[v] = -1 for all v
// Assume we start with Edge(v,v) if v is our start
// vertex
void dfsR(Graph g, Edge e){
int t, w = e.w;
pre[w] = cnt++;
st[w] = e.v;
for(t=0; t < g->nV; t++) {
if(g->adj[w][t] != 0) { // if t is adjacent
if (pre[t] == -1){ // if t is not visited yet
dfsR(g, mkEdge(w, t));
}
}
}
}
void dfSearch(Graph g){
assert(g != NULL);
int v=0; cnt = 0;
pre = malloc(sizeof(int)*g->nV);
st = malloc(sizeof(int)*g->nV);
for(v=0;v<g->nV;v++) {
pre[v] = -1;
st[v] = -1;
}
for(v=0;v < g->nV; v++){
if(pre[v] == -1){
dfsR(g,mkEdge(v,v));
}
}
}
void printPre(Graph g){
int i=0;
printf("Showing traversal order\n");
for(i=0;i< g->nV;i++){
printf("%d %d\n",i,pre[i]);
}
}
void destroyGraph(Graph g){
int i;
assert(g != NULL);
assert(g->adj != NULL);
for (i = 0; i < g->nV; i++) {
assert(g->adj[i] != NULL);
free(g->adj[i]);
}
free(g->adj);
if(pre != NULL){
free(pre);
}
if(st != NULL){
free(st);
}
for (i = 0; i < g->nV; i++) {
assert(g->countries[i] != NULL);
free(g->countries[i]);
}
free(g->countries);
for (i = 0; i < g->nV; i++) {
assert(g->capitals[i] != NULL);
free(g->capitals[i]);
}
free(g->capitals);
free(g->population);
free(g);
}
//IMPLEMENT THESE FUNCTIONS
void showGraphLabels(Graph g){
assert(g != NULL);
printf("V=%d, E=%d\n", g->nV, g->nE);
int i, j;
for (i = 0; i < g->nV; i++) {
int nshown = 0;
for (j = 0; j < g->nV; j++) {
if (g->adj[i][j] != 0) {
printf("%s-%s ",g->countries[i],g->countries[j]);
nshown++;
}
}
if (nshown > 0) {
printf("\n");
}
}
}
void showData(Graph g){
int i = 0;
for(i = 0; i < g->nV; i++){
showVertexData(g,i);
}
}
void showVertexData(Graph g,Vertex v){
printf("%d: %s %s %ld\n",v,g->countries[v],g->capitals[v],g->population[v]);
}
void dfSearch2(Graph g, Vertex * dfsOrder){
int v;
Vertex *visited = malloc(sizeof(int) *g->nV);
st = malloc(sizeof(int) *g->nV);
for(v = 0; v < g->nV; v++){
visited[v] = -1;
st[v] = -1;
dfsOrder[v] = -1;
}
for(v = 0; v < g->nV; v++){
if(dfsOrder[v] == -1){
dfsR2(g,mkEdge(v,v),dfsOrder);
}
}
free(visited);
}
void dfsR2(Graph g, Edge e, Vertex * dfsOrder){
int i;
dfsOrder[cnt++] = e.w;
Vertex *visited = malloc(sizeof(int) *g->nV);
visited[e.w] = 1;
st[e.w] = e.v;
for(i = 0; i < g->nV; i++){
if((g->adj[e.w][i] != 0) && (visited[i] == -1)){
dfsR2(g,mkEdge(e.w,i),dfsOrder);
}
}
free(visited);
}
char *getVertexLabel(Graph g, Vertex v){
return g->countries[v];
}
void setData(Graph g, Vertex v, char *country, char *capital, long population){
g->countries[v] = strdup(country);
g->capitals[v] = strdup(capital);
g->population[v] = population;
}