-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.c
228 lines (196 loc) · 5.2 KB
/
Graph.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
/*
Graph.c
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Graph.h"
struct graphRep {
int V;
int E;
int **adj; //an array of adjacency matrix
char ** vLabels;
char * types;
};
static int GRAPHvalidEdge(Graph g, Edge e);
static int GRAPHvalidVertex(Graph g, Vertex v);
// Create an edge from v to w
Edge mkEdge(Vertex v, Vertex w, int weight) {
assert(v >= 0 && w >= 0);
Edge e = {v,w,weight};
return e;
}
//Initialise a new graph
Graph newGraph(int nV) {
//printf("number of nV is %d\n",nV);
int i,j,v = 0;
// creating the graph
Graph G;
G = malloc(sizeof(struct graphRep));
assert(G != NULL);
G->V = nV; //set number of verticies
G->E = 0; //set number of edges
G->adj = malloc(nV * sizeof(int *));
assert(G->adj != NULL);
//making the adjacency matrix
for (v = 0; v < nV; v++){
G->adj[v] = malloc(nV * sizeof(int));
assert(G->adj[v] != NULL);
for(j = 0; j < nV; j++){
G->adj[v][j] = 0;
}
}
G->types = malloc(nV *sizeof(char *));
assert(G->types != NULL);
G->vLabels = malloc(nV * sizeof(char *));
assert(G->vLabels != NULL);
for(i = 0; i < G->V; i++){
G->vLabels[i] = NULL;
}
return G;
}
static int GRAPHvalidEdge(Graph g, Edge e){
assert(g != NULL);
return (e.v >= 0 && e.v < g->V &&
e.w >= 0 && e.w < g->V );
}
static int GRAPHvalidVertex(Graph g, Vertex v){
assert(g != NULL);
return (v >= 0 && v < g->V);
}
//Insert an edge into a graph
//the edge must not be inserted if
// the vertices are not valid
// it would create a self loop
// there already exists an edge between the vertices
void insertE(Graph g, Edge e) {
if(GRAPHvalidEdge(g,e) && e.v != e.w){
g->adj[e.v][e.w] = e.weight;
g->adj[e.w][e.v] = e.weight;
g->E++;
} else {
printf("Edge not valid ignoring ");
//GRAPHEdgePrint(e);
printf("\n");
} //COMPLETE THIS
}
//returns 1 if there is an edge from v to w
//returns 0 otherwise
int isAdjacent(Graph g, Vertex v, Vertex w){
//COMPLETE THIS
assert(g != NULL && GRAPHvalidVertex(g,v) && GRAPHvalidVertex(g,w));
int adjacent = 0;
if(g->adj[v][w] != 0){
adjacent = 1;
}
return adjacent;
}
//returns the number of adjacent vertices
//and fills the adj array with the adjacent vertices
int adjacentVertices(Graph g, Vertex v, Vertex adj[]){
//COMPLETE THIS
int counter, i = 0;
for(i = 0,counter = 0; i < g->V; i++){
if(g->adj[v][i] != 0){
adj[counter] = i;
counter++;
}
}
return counter;
}
//returns the number of incident edges
//and fills the edges with incident edges
int incidentEdges(Graph g, Vertex v, Edge edges[]){
//COMPLETE THIS
int counter = 0;
int i = 0;
for(i = 0;i < g->V; i++){
if(g->adj[v][i] > 0){
edges[counter] = mkEdge(v,i,g->adj[v][i]);
counter++;
}
}
return counter;
}
char * getVertexLabel(Graph g, Vertex v){
//COMPLETE THIS
return g->vLabels[v];
}
void destroyGraph(Graph g){
//COMPLETE THIS
assert(g != NULL);
int v = 0;
for(v = 0; v < g->V;v++){
free(g->adj[v]);
free(g->vLabels[v]);
}
free(g->adj);
free(g->vLabels);
free(g->types);
free(g);
}
//return the number of vertices in the graph
int numV(Graph g){
//COMPLETE THIS
return g->V;
}
//return the number of edges in the graph
int numE(Graph g){
//COMPLETE THIS
return g->E;
}
// returns the number of edges and fills the array of edges.
// nE is the max size of the es array
// The edges in the edges function should be in ascending order and not
// contain duplicates.
int edges(Edge es[], int nE, Graph g){
//COMPLETE THIS
assert(g != NULL && es != NULL && nE > 0);
int i,j,n = 0;
for(i = 0; i < g->V; i++){
for(j = 0; j < g->V; j++){
if(g->adj[i][j] != 0){
assert(n < nE);
es[n++] = mkEdge(i,j,g->adj[i][j]);
}
}
}
return n;
}
//Display the graph
void show(Graph g) {
assert(g != NULL);
printf("V=%d, E=%d\n", numV(g), numE(g));
int i, j;
for (i = 0; i < numV(g); i++) {
int nshown = 0;
for (j = 0; j < numV(g); j++) {
if (isAdjacent(g,i,j)) {
printf("%d-%d : %d ",i,j,g->adj[i][j]);
nshown++;
}
}
if (nshown > 0){
printf("\n");
}
}
}
//my functions
void cityInfo(Graph g, Vertex v, char *cityName, char informant){
if( v >= 0 && v < g->V){
g->vLabels[v] = strdup(cityName);
g->types[v] = informant;
}
//printf("ho\n");
}
int informant(Graph g, Vertex v){
int i = 0;
if(g->types[v] == 'i'){
i = 1;
}
return i;
}
int edgeWeight(Graph g, Vertex v, Vertex w){
return g->adj[v][w];
}