00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "mincov_int.h"
00011
00012 static sm_matrix *build_intersection_matrix();
00013
00014
00015 #if 0
00016
00017
00018
00019 static int
00020 verify_indep_set(A, indep)
00021 sm_matrix *A;
00022 sm_row *indep;
00023 {
00024 register sm_row *prow, *prow1;
00025 register sm_element *p, *p1;
00026
00027 for(p = indep->first_col; p != 0; p = p->next_col) {
00028 prow = sm_get_row(A, p->col_num);
00029 for(p1 = p->next_col; p1 != 0; p1 = p1->next_col) {
00030 prow1 = sm_get_row(A, p1->col_num);
00031 if (sm_row_intersects(prow, prow1)) {
00032 return 0;
00033 }
00034 }
00035 }
00036 return 1;
00037 }
00038 #endif
00039
00040 solution_t *
00041 sm_maximal_independent_set(A, weight)
00042 sm_matrix *A;
00043 int *weight;
00044 {
00045 register sm_row *best_row, *prow;
00046 register sm_element *p;
00047 int least_weight;
00048 sm_row *save;
00049 sm_matrix *B;
00050 solution_t *indep;
00051
00052 indep = solution_alloc();
00053 B = build_intersection_matrix(A);
00054
00055 while (B->nrows > 0) {
00056
00057 best_row = B->first_row;
00058 for(prow = B->first_row->next_row; prow != 0; prow = prow->next_row) {
00059 if (prow->length < best_row->length) {
00060 best_row = prow;
00061 }
00062 }
00063
00064
00065 if (weight == NIL(int)) {
00066 least_weight = 1;
00067 } else {
00068 prow = sm_get_row(A, best_row->row_num);
00069 least_weight = weight[prow->first_col->col_num];
00070 for(p = prow->first_col->next_col; p != 0; p = p->next_col) {
00071 if (weight[p->col_num] < least_weight) {
00072 least_weight = weight[p->col_num];
00073 }
00074 }
00075 }
00076 indep->cost += least_weight;
00077 (void) sm_row_insert(indep->row, best_row->row_num);
00078
00079
00080 save = sm_row_dup(best_row);
00081 for(p = save->first_col; p != 0; p = p->next_col) {
00082 sm_delrow(B, p->col_num);
00083 sm_delcol(B, p->col_num);
00084 }
00085 sm_row_free(save);
00086 }
00087
00088 sm_free(B);
00089
00090
00091
00092
00093
00094
00095 return indep;
00096 }
00097
00098 static sm_matrix *
00099 build_intersection_matrix(A)
00100 sm_matrix *A;
00101 {
00102 register sm_row *prow, *prow1;
00103 register sm_element *p, *p1;
00104 register sm_col *pcol;
00105 sm_matrix *B;
00106
00107
00108 B = sm_alloc();
00109 for(prow = A->first_row; prow != 0; prow = prow->next_row) {
00110
00111
00112 for(p = prow->first_col; p != 0; p = p->next_col) {
00113 pcol = sm_get_col(A, p->col_num);
00114 for(p1 = pcol->first_row; p1 != 0; p1 = p1->next_row) {
00115 prow1 = sm_get_row(A, p1->row_num);
00116 prow1->flag = 0;
00117 }
00118 }
00119
00120
00121 for(p = prow->first_col; p != 0; p = p->next_col) {
00122 pcol = sm_get_col(A, p->col_num);
00123 for(p1 = pcol->first_row; p1 != 0; p1 = p1->next_row) {
00124 prow1 = sm_get_row(A, p1->row_num);
00125 if (! prow1->flag) {
00126 prow1->flag = 1;
00127 (void) sm_insert(B, prow->row_num, prow1->row_num);
00128 }
00129 }
00130 }
00131 }
00132
00133 return B;
00134 }