-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.c
More file actions
273 lines (234 loc) · 5.45 KB
/
set.c
File metadata and controls
273 lines (234 loc) · 5.45 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
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
/*
** HashSet Module
** Stores a key value for every Vertex
** Implements constant Add and Lookup
** Implemented for Assignment 2 COMP20007 by Hinam Mehra(May14,2015)
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "set.h"
/*
** Creates an array of set for the number of school vertices
** Returns a Sets_array pointer
*/
Sets_array *create_sets_array(int num_schools){
Sets_array *sa = (Sets_array *)malloc(sizeof(Sets_array) * num_schools);
assert(sa);
sa->set = (HashSet **)malloc(sizeof(HashSet) * num_schools);
assert(sa->set);
sa->n = 0;
return sa;
}
/*
** Creates a NULL set of the source vertexId
** Allocates initial memory for set components
** Returns a pointer to the set
*/
HashSet *create_set(intu source_vertexId){
HashSet *s = (HashSet *)malloc(sizeof(HashSet));
assert(s);
s->source_vertexId = source_vertexId;
s->i = 0;
s->size = 1;
s->map_size = 1;
//s->cost = 0;
s->ids = (intu *)malloc(sizeof(intu) * s->size);
assert(s->ids);
s->hashmap = (HashItem *)malloc(sizeof(HashItem) * s->map_size);
assert(s->hashmap);
return s;
}
/*
** Adds a vertex to the Given Set
** Returns SUCCESS if successfully inserted
*/
int
add_vertex(HashSet *s,intu vertexId){
assert(s);
// No space in ids array
if(s->i == s->size){
s->size *= 2;
s->ids = (intu *)realloc(s->ids, sizeof(intu) * s->size);
assert(s->ids);
}
// increase size of hashmap which is indexed by vertexId's
if(vertexId > s->map_size){
s->map_size = vertexId + 1;
s->hashmap = (HashItem *)realloc(s->hashmap,sizeof(HashItem) * s->map_size);
assert(s->hashmap);
}
//set cannot contain duplicate elements
if(contains(s,vertexId)){
return FAIL;
}
s->hashmap[vertexId].vertexId = vertexId;
s->hashmap[vertexId].hashid = HASH;
s->ids[s->i] = vertexId;
s->i++;
return SUCCESS;
}
/*
** Adds a Set to sets array
** Returns SUCCESS when set is inserted
*/
int add_set(Sets_array *sa, HashSet *s){
assert(sa);
assert(s);
sa->set[sa->n] = s;
sa->n++;
return SUCCESS;
}
/*
** Checks if a vertexId is in a Set
** Return SUCCESS if it does exist
*/
int contains(HashSet *s,intu vertexId){
if(s->hashmap[vertexId].vertexId == vertexId && s->hashmap[vertexId].hasid == HASH){
return SUCCESS;
}
return FAIL;
}
/*
** Calculates the intersection of two sets
* Returns the intersection set
*/
HashSet *intersect(HashSet *s1, HashSet *s2){
HashSet *intersect = create_set(-1);
int c;
for(c = 0; c < s1->i; c++){
if(contains(s2,s1->ids[c])){
add_vertex(intersect,s1->ids[c]);
}
}
return intersect;
}
/*
** Calculates set difference i.e. s1 - s2
** Returns the set which has elements from s1 which are not in s2
*/
HashSet *set_difference(HashSet *s1, HashSet *s2){
HashSet *difference = create_set(-1);
int c;
for(c = 0; c < s1-> i; c++){
if(!(contains(s2,s1->ids[c]))){
add_vertex(difference,s1->ids[c]);
}
}
return difference;
}
/*
** Prints all the sets in a sets_array
*/
void print_sets_array(Sets_array* sa){
assert(sa);
int i;
for(i = 0 ; i < sa->n ; i++){
print_set(sa->set[i]);
}
}
/*
** Prints the set
*/
void print_set(HashSet *s){
assert(s);
printf("\n-----------------------\n");
printf("Source : %d\n", s->source_vertexId);
printf("n : %d\n", s->i);
printf("Vertex Id: \n");
int c;
for(c = 0; c < s->i; c++){
printf("%d ",s->ids[c]);
}
printf("\n");
printf("\n-----------------------\n");
}
/*
** removes a set from sets_array
** Returns SUCCESS if the removal is successfull
*/
int remove_set(Sets_array *sa, HashSet *s){
assert(sa);
assert(s);
int index = 0,i;
for(i = 0; i < sa->n; i++){
if(sa->set[i]->source_vertexId == s->source_vertexId){
index = i;
break;
}
}
for(i = index; i < sa->n-1; i++){
sa->set[i] = sa->set[i+1];
}
sa->n--;
return SUCCESS;
}
/*
** Frees all space allocated for sets_array and sets
*/
void destroy(Sets_array *sa){
if(sa == NULL){
return;
}
int i,j;
if(sa->n > 0){
for(i=0; i < sa->n; i++){
if(sa->set[i]->size > 0){
for(j = 0; j < sa->set[i]->size; j++){
free(sa->set[i]->ids);
free(sa->set[i]->hashmap);
}
}
free(sa->set[i]);
}
}
return;
}
/*
** Calculates the minimum number of sets required from sets_array subset
** that cover all the elements in HashSet
** Implements a greedy approach.
** Returns void, but prints out set indices
*/
void set_cover(HashSet *mainset, Sets_array *subset,int n_H){
HashSet *uncovered = mainset;
HashSet *chosen;
while(uncovered->i > 0){
// choose the set with maximum uncovered elements
chosen = max_uncovered(uncovered,subset);
if(chosen){
// prints the school vertex id from 0..S-1
printf("%d\n",chosen->source_vertexId - n_H);
remove_set(subset,chosen);
// subtract all elements from uncovered that were covered by chosen
uncovered = set_difference(uncovered,chosen);
}
//There is atleast one element in mainset that is not covered by all the elements
// in different sets of sets_array
else{
printf("No set cover\n");
}
}
}
/*
** Returns the index of the set from sets_array which has maximum
** number of uncovered elements
*/
HashSet *max_uncovered(HashSet *uncovered, Sets_array *subset){
int num_uncovered = 0;
HashSet *chosen;
HashSet *set_intersect;
int x;
for(x = 0; x < subset->n ; x++){
// calculates uncovered elements in a set
set_intersect = intersect(subset->set[x],uncovered);
if(set_intersect->i > num_uncovered){
num_uncovered = set_intersect->i;
chosen = subset->set[x];
}
}
if(num_uncovered > 0){
return chosen;
}
return FAIL;
}