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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| #include <stdio.h> #include <stdlib.h> #include <sys/time.h> // for gettimeofday() #include <time.h> // for time()
#define MAX_SEQ_LEN 1000 typedef struct _SubseqSum { int value; int start; int end; } SubseqSum;
double current_timestamp(); int max(int num0, int num1); void init_random_sequence(int seq[], int len); void print_sequence(int seq[], int len); int sum_subseq(int seq[], int start, int end);
SubseqSum max_subseq_sum_force(int seq[], int seq_len); SubseqSum max_subseq_sum_force_adv(int seq[], int seq_len); SubseqSum max_subseq_sum_divide(int seq[], int left, int right); SubseqSum max_subseq_sum_divide_for_center(int seq[], int center, int left, int right); SubseqSum max_subseq_sum_dynamic_programming(int seq[], int seq_len);
int main(int argc, char *argv[]) { int seq_len = 50; int seq[MAX_SEQ_LEN]; init_random_sequence(seq, seq_len); SubseqSum max_sum; double start_time, end_time;
print_sequence(seq, seq_len); printf("\n以上序列的最大子段和为:\n");
start_time = current_timestamp(); max_sum = max_subseq_sum_force(seq, seq_len); end_time = current_timestamp(); printf("- 穷举算法 : %d (求和区间: [%d, %d] +=>> %d), 耗时: %f毫秒\n" , max_sum.value , max_sum.start, max_sum.end , sum_subseq(seq, max_sum.start, max_sum.end) , end_time - start_time);
start_time = current_timestamp(); max_sum = max_subseq_sum_force_adv(seq, seq_len); end_time = current_timestamp(); printf("- 穷举算法(改进版): %d (求和区间: [%d, %d] +=>> %d), 耗时: %f毫秒\n" , max_sum.value , max_sum.start, max_sum.end , sum_subseq(seq, max_sum.start, max_sum.end) , end_time - start_time);
start_time = current_timestamp(); max_sum = max_subseq_sum_divide(seq, 0, seq_len - 1); end_time = current_timestamp(); printf("- 分治算法 : %d (求和区间: [%d, %d] +=>> %d), 耗时: %f毫秒\n" , max_sum.value , max_sum.start, max_sum.end , sum_subseq(seq, max_sum.start, max_sum.end) , end_time - start_time);
start_time = current_timestamp(); max_sum = max_subseq_sum_dynamic_programming(seq, seq_len); end_time = current_timestamp(); printf("- 动态规划算法 : %d (求和区间: [%d, %d] +=>> %d), 耗时: %f毫秒\n" , max_sum.value , max_sum.start, max_sum.end , sum_subseq(seq, max_sum.start, max_sum.end) , end_time - start_time);
return 0; }
SubseqSum max_subseq_sum_force(int seq[], int seq_len) { SubseqSum max_sum = { .value = 0, .start = 0, .end = 0 };
int max_sum_value = 0; for (int i = 0; i < seq_len; i++) { for (int j = i; j < seq_len; j++) { int sum_value = 0; for (int k = i; k <= j; k++) { sum_value += seq[k]; } if (sum_value > max_sum_value) { max_sum_value = sum_value;
max_sum.value = max_sum_value; max_sum.start = i; max_sum.end = j; } } } return max_sum; }
SubseqSum max_subseq_sum_force_adv(int seq[], int seq_len) { SubseqSum max_sum = { .value = 0, .start = 0, .end = 0 };
int max_sum_value = 0; for (int i = 0; i < seq_len; i++) { int sum_value = 0; for (int j = i; j < seq_len; j++) { sum_value += seq[j];
if (sum_value > max_sum_value) { max_sum_value = sum_value;
max_sum.value = max_sum_value; max_sum.start = i; max_sum.end = j; } } } return max_sum; }
SubseqSum max_subseq_sum_divide(int seq[], int left, int right) { if (left == right) { return (SubseqSum) { .value = max(0, seq[left]), .start = left, .end = left }; }
int center = (left + right) / 2; SubseqSum left_max_sum = max_subseq_sum_divide(seq, left, center); SubseqSum right_max_sum = max_subseq_sum_divide(seq, center + 1, right); SubseqSum center_max_sum = max_subseq_sum_divide_for_center(seq, center, left, right);
SubseqSum max_sum = center_max_sum; if(max_sum.value < left_max_sum.value) { max_sum = left_max_sum; } if(max_sum.value < right_max_sum.value) { max_sum = right_max_sum; } return max_sum; }
SubseqSum max_subseq_sum_divide_for_center(int seq[], int center, int left, int right) { SubseqSum max_sum = { .value = 0, .start = center, .end = center };
int left_sum_value = 0; int left_max_sum_value = 0; for (int i = center; i >= left; i--) { left_sum_value += seq[i];
if (left_sum_value > left_max_sum_value) { left_max_sum_value = left_sum_value; max_sum.start = i; } } int right_sum_value = 0; int right_max_sum_value = 0; for(int j = center + 1; j <= right; j++) { right_sum_value += seq[j];
if(right_sum_value > right_max_sum_value) { right_max_sum_value = right_sum_value; max_sum.end = j; } }
int max_sum_value = left_max_sum_value + right_max_sum_value;
max_sum.value = max_sum_value; if (left_max_sum_value == 0 && right_max_sum_value > 0) { max_sum.start = center + 1; } if (right_max_sum_value == 0 && left_max_sum_value > 0) { max_sum.end = center; } if (left_max_sum_value == 0 && right_max_sum_value == 0) { max_sum.start = max_sum.end = center; }
return max_sum; }
SubseqSum max_subseq_sum_dynamic_programming(int seq[], int seq_len) { int seq_sum[MAX_SEQ_LEN] = { seq[0] }; SubseqSum max_sum = { .value = max(0, seq_sum[0]), .start = 0, .end = 0 };
int max_sum_value = 0; int expected_sum_start = 0; for(int j = 1; j < seq_len; j++) { if (seq_sum[j - 1] > 0) { seq_sum[j] = seq_sum[j - 1] + seq[j]; } else { seq_sum[j] = seq[j]; expected_sum_start = j; }
if (seq_sum[j] > max_sum_value) { max_sum_value = seq_sum[j];
max_sum.value = max_sum_value; max_sum.start = expected_sum_start; max_sum.end = j; } } return max_sum; }
int max(int num0, int num1) { return num0 > num1 ? num0 : num1; }
double current_timestamp() { struct timeval te; gettimeofday(&te, NULL);
double msec = te.tv_sec * 1000.0 + (te.tv_usec / 1000.0); return msec; }
void init_random_sequence(int seq[], int len) { srand(time(0));
for (int i = 0; i < len; i++) { seq[i] = (int) (rand() * 1.0 / RAND_MAX * 100) * (rand() % 2 == 0 ? 1 : -1); } }
void print_sequence(int seq[], int len) { int columns = 10; for (int i = 0; i < len; i++) { printf("%3d: %3d, ", i, seq[i]);
if ((i + 1) % columns == 0 && i < len - 1) { printf("\n"); } } }
int sum_subseq(int seq[], int start, int end) { int sum = 0; for (int i = (start < end ? start : end); i <= (end > start ? end : start); i++) { sum += seq[i]; } return sum; }
|