abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
indep.c
Go to the documentation of this file.
1 /*
2  * Revision Control Information
3  *
4  * $Source$
5  * $Author$
6  * $Revision$
7  * $Date$
8  *
9  */
10 #include "mincov_int.h"
11 
13 
14 
16 
17 
18 #if 0
19 /*
20  * verify that all rows in 'indep' are actually independent !
21  */
22 static int
23 verify_indep_set(A, indep)
24 sm_matrix *A;
25 sm_row *indep;
26 {
27  register sm_row *prow, *prow1;
28  register sm_element *p, *p1;
29 
30  for(p = indep->first_col; p != 0; p = p->next_col) {
31  prow = sm_get_row(A, p->col_num);
32  for(p1 = p->next_col; p1 != 0; p1 = p1->next_col) {
33  prow1 = sm_get_row(A, p1->col_num);
34  if (sm_row_intersects(prow, prow1)) {
35  return 0;
36  }
37  }
38  }
39  return 1;
40 }
41 #endif
42 
43 solution_t *
45 sm_matrix *A;
46 int *weight;
47 {
48  register sm_row *best_row, *prow;
49  register sm_element *p;
50  int least_weight;
51  sm_row *save;
52  sm_matrix *B;
53  solution_t *indep;
54 
55  indep = solution_alloc();
57 
58  while (B->nrows > 0) {
59  /* Find the row which is disjoint from a maximum number of rows */
60  best_row = B->first_row;
61  for(prow = B->first_row->next_row; prow != 0; prow = prow->next_row) {
62  if (prow->length < best_row->length) {
63  best_row = prow;
64  }
65  }
66 
67  /* Find which element in this row has least weight */
68  if (weight == NIL(int)) {
69  least_weight = 1;
70  } else {
71  prow = sm_get_row(A, best_row->row_num);
72  least_weight = weight[prow->first_col->col_num];
73  for(p = prow->first_col->next_col; p != 0; p = p->next_col) {
74  if (weight[p->col_num] < least_weight) {
75  least_weight = weight[p->col_num];
76  }
77  }
78  }
79  indep->cost += least_weight;
80  (void) sm_row_insert(indep->row, best_row->row_num);
81 
82  /* Discard the rows which intersect this row */
83  save = sm_row_dup(best_row);
84  for(p = save->first_col; p != 0; p = p->next_col) {
85  sm_delrow(B, p->col_num);
86  sm_delcol(B, p->col_num);
87  }
88  sm_row_free(save);
89  }
90 
91  sm_free(B);
92 
93 /*
94  if (! verify_indep_set(A, indep->row)) {
95  fail("sm_maximal_independent_set: row set is not independent");
96  }
97 */
98  return indep;
99 }
100 
101 static sm_matrix *
103 sm_matrix *A;
104 {
105  register sm_row *prow, *prow1;
106  register sm_element *p, *p1;
107  register sm_col *pcol;
108  sm_matrix *B;
109 
110  /* Build row-intersection matrix */
111  B = sm_alloc();
112  for(prow = A->first_row; prow != 0; prow = prow->next_row) {
113 
114  /* Clear flags on all rows we can reach from row 'prow' */
115  for(p = prow->first_col; p != 0; p = p->next_col) {
116  pcol = sm_get_col(A, p->col_num);
117  for(p1 = pcol->first_row; p1 != 0; p1 = p1->next_row) {
118  prow1 = sm_get_row(A, p1->row_num);
119  prow1->flag = 0;
120  }
121  }
122 
123  /* Now record which rows can be reached */
124  for(p = prow->first_col; p != 0; p = p->next_col) {
125  pcol = sm_get_col(A, p->col_num);
126  for(p1 = pcol->first_row; p1 != 0; p1 = p1->next_row) {
127  prow1 = sm_get_row(A, p1->row_num);
128  if (! prow1->flag) {
129  prow1->flag = 1;
130  (void) sm_insert(B, prow->row_num, prow1->row_num);
131  }
132  }
133  }
134  }
135 
136  return B;
137 }
139 
int length
Definition: sparse.h:46
sm_row * next_row
Definition: sparse.h:50
solution_t * sm_maximal_independent_set(sm_matrix *A, int *weight)
Definition: indep.c:44
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static ABC_NAMESPACE_IMPL_START sm_matrix * build_intersection_matrix()
solution_t * solution_alloc()
Definition: solution.c:17
sm_element * sm_row_insert(sm_row *prow, int col)
Definition: rows.c:100
#define NIL(type)
Definition: avl.h:25
sm_row * sm_row_dup(sm_row *prow)
Definition: rows.c:82
int row_num
Definition: sparse.h:45
void sm_delrow(sm_matrix *A, int i)
Definition: matrix.c:273
#define sm_get_row(A, rownum)
Definition: sparse.h:93
int flag
Definition: sparse.h:47
sm_element * first_row
Definition: sparse.h:63
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define sm_get_col(A, colnum)
Definition: sparse.h:89
void sm_free(sm_matrix *A)
Definition: matrix.c:60
sm_element * sm_insert(sm_matrix *A, int row, int col)
Definition: matrix.c:155
ABC_NAMESPACE_IMPL_START sm_matrix * sm_alloc()
Definition: matrix.c:31
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
sm_row * row
Definition: mincov_int.h:37
sm_row * first_row
Definition: sparse.h:79
typedefABC_NAMESPACE_HEADER_START struct sm_element_struct sm_element
Definition: sparse.h:21
void sm_row_free(sm_row *prow)
Definition: rows.c:53
void sm_delcol(sm_matrix *A, int i)
Definition: matrix.c:307
int sm_row_intersects(sm_row *p1, sm_row *p2)
Definition: rows.c:190
sm_element * first_col
Definition: sparse.h:48