# include # include # include # include "subset_sum_swap.h" int main ( ); void subset_sum_swap_tests ( ); void subset_sum_swap_try ( int n, int w[], int t ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: subset_sum_swap_test() tests subset_sum_swap(). Licensing: This code is distributed under the MIT license. Modified: 14 June 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "subset_sum_swap_test():\n" ); printf ( " C version\n" ); printf ( " Test subset_sum_swap().\n" ); subset_sum_swap_tests ( ); /* Terminate. */ printf ( "\n" ); printf ( "subset_sum_swap_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return EXIT_SUCCESS; } /******************************************************************************/ void subset_sum_swap_tests ( ) /******************************************************************************/ /* Purpose: subset_sum_swap_tests() tests subset_sum_swap(). Licensing: This code is distributed under the MIT license. Modified: 14 June 2024 Author: John Burkardt */ { int n; int t; static int w01[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w02[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w03[8] = { 15, 22, 14, 26, 32, 9, 16, 8 }; static int w04[10] = { 267, 493, 869, 961, 1000, 1153, 1246, 1598, 1766, 1922 }; static int w05[21] = {518533, 1037066, 2074132, 1648264, 796528, 1593056, 686112, 1372224, 244448, 488896, 977792, 1955584, 1411168, 322336, 644672, 1289344, 78688, 157376, 314752, 629504, 1259008}; static int w06[10] = { 41, 34, 21, 20, 8, 7, 7, 4, 3, 3 }; static int w07[9] = { 81, 80, 43, 40, 30, 26, 12, 11, 9 }; static int w08[6] = { 1, 2, 4, 8, 16, 32 }; static int w09[10] = { 25, 27, 3, 12, 6, 15, 9, 30, 21, 19 }; n = 8; t = 53; subset_sum_swap_try ( n, w01, t ); n = 8; t = 53; subset_sum_swap_try ( n, w02, t ); n = 8; t = 53; subset_sum_swap_try ( n, w03, t ); n = 10; t = 5842; subset_sum_swap_try ( n, w04, t ); n = 21; t = 2463098; subset_sum_swap_try ( n, w05, t ); n = 10; t = 50; subset_sum_swap_try ( n, w06, t ); n = 9; t = 100; subset_sum_swap_try ( n, w07, t ); n = 6; t = 22; subset_sum_swap_try ( n, w08, t ); n = 10; t = 50; subset_sum_swap_try ( n, w09, t ); return; } /******************************************************************************/ void subset_sum_swap_try ( int n, int w[], int t ) /******************************************************************************/ /* Purpose: subset_sum_swap_try() tries the swap code for a given subset_sum problem. Licensing: This code is distributed under the MIT license. Modified: 14 June 2024 Author: John Burkardt */ { int i; int *index; int sum_achieved; printf ( "\n" ); printf ( " Target value: %d\n", t ); printf ( "\n" ); printf ( " Available weights:\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %d\n", w[i] ); } index = ( int * ) malloc ( n * sizeof ( int ) ); sum_achieved = subset_sum_swap ( n, w, t, index ); printf ( "\n" ); printf ( " Selected weights:\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { if ( index[i] == 1 ) { printf ( " %d\n", w[i] ); } } printf ( "\n" ); printf ( " The target was %d\n", t ); printf ( " The achieved sum is %d\n", sum_achieved ); printf ( " The defect is %d\n", t - sum_achieved ); free ( index ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }