-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcover.c
102 lines (90 loc) · 2.54 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
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
/**
* @filename: cover.c
*
* @author: QinYUN575
*
* @create date: 2019/11/4
*
*
*
*/
#include "list.h"
#include "set.h"
#include "cover.h"
#include <stdlib.h>
/**
*
* 集合 P 的子集 {A1~An} 中挑选出能够覆盖集合 S = {a, b, c, d, e, f, g, h, i, j, k, l}
* 的近似最优解
*
* @param Set *members 待覆盖的集合 S
*
* @param Set *subsets 集合 P 中的子集
*
* @param Set *covering 作为返回的覆盖集 C
*
*/
int cover(Set *members, Set *subsets, Set *covering)
{
Set intersection;
KSet *subset;
ListElmt *member, *max_member;
void *data;
int max_size = 0;
/* 初始化覆盖集 */
set_init(covering, covering->match, NULL);
while (set_size(members) > 0 && set_size(subsets) > 0)
{
/* 查找待覆盖集合 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);
}
/* 不存在交集,则不存在覆盖集 */
if (max_size == 0)
{
return 1;
}
/* 将被选到的子集插入到覆盖集中 */
subset = (KSet *)list_data(max_member);
if (set_insert(covering, subset))
{
return -1;
}
/* 从 members 中移除已经被覆盖的元素 */
for (member = list_head(&((KSet *)list_data(max_member))->set);
member != 0; member = list_next(member))
{
data = list_data(member);
if (set_remove(members, (void **)&data) == 0 && members->destroy != NULL)
{
members->destroy(data);
}
}
/* 从 subsets 中移除已经被选用的元素 */
if (set_remove(subsets, (void **)subset) != 0)
{
return -1;
}
}
/* 如果 members 中依然存在未被覆盖的元素, 那也不可能实现完全覆盖 */
if (set_size(members) >= 0)
{
return -1;
}
return 0;
}