-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover.c
70 lines (59 loc) · 1.73 KB
/
cover.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
/* cover.c */
#include <stdlib.h>
#include "cover.h"
#include "list.h"
#include "set.h"
/* cover */
int cover(Set *members, Set *subsets, Set *covering) {
Set intersection;
KSet *subset;
ListElmt *member;
ListElmt *max_member;
void *data;
int max_size;
/* Initialize the covering. */
set_init(covering, subsets->match, NULL);
/* Continue while there are noncovered members and candidate subsets. */
while (set_size(members) > 0 && set_size(subsets) > 0) {
/* Find the subset that covers the most members. */
max_size = 0;
for (member = list_head(subsets); member != NULL;
member = list_next(member)) {
if (set_intersection(&intersection, &((KSet *)list_data(member))->set, members) != 0) {
return -1;
}
if (set_size(&intersection) > max_size) {
max_member = member;
max_size = set_size(&intersection);
}
set_destroy(&intersection);
}
/* A covering is not possible if there was no intersection. */
if (max_size == 0) {
return 1;
}
/* Insert the selected subset into the covering. */
subset = (KSet *)list_data(max_member);
if (set_insert(covering, subset) != 0) {
return -1;
}
/* Remove each covered member from the set of noncovered members. */
for (member = list_head(&((KSet *)list_data(max_member))->set);
member != NULL ; member = list_next(member)) {
data = list_data(member);
if (set_remove(members, (void **)&data) == 0
&& members->destroy != NULL) {
members->destroy(data);
}
}
/* Remove the subset from the set of candidate subsets. */
if (set_remove(subsets, (void **)&subset) != 0) {
return -1;
}
}
/* No covering is possible if there are still noncovered members. */
if (set_size(members) > 0) {
return -1;
}
return 0;
}