|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright © 2019-2020 Ralf Weber |
| 5 | +# |
| 6 | +# This file is part of MetaboBlend. |
| 7 | +# |
| 8 | +# MetaboBlend is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# MetaboBlend is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with MetaboBlend. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +# |
| 21 | + |
| 22 | +import numpy |
| 23 | + |
| 24 | + |
| 25 | +def find_path(mass_list, sum_matrix, n, mass, max_subset_length, path=[]): |
| 26 | + """ |
| 27 | + Recursive solution for backtracking through the dynamic programming boolean matrix. All possible subsets are found |
| 28 | +
|
| 29 | + :param mass_list: A list of masses from which to identify subsets. |
| 30 | +
|
| 31 | + :param mass: The target mass of the sum of the substructures. |
| 32 | +
|
| 33 | + :param sum_matrix: The dynamic programming boolean matrix. |
| 34 | +
|
| 35 | + :param n: The size of mass_list. |
| 36 | +
|
| 37 | + :param max_subset_length: The maximum length of subsets to return. Allows the recursive backtracking algorithm to |
| 38 | + terminate early in many cases, significantly improving runtime. |
| 39 | +
|
| 40 | + :param path: List for keeping track of the current subset. |
| 41 | +
|
| 42 | + :return: Generates of lists containing the masses of valid subsets. |
| 43 | + """ |
| 44 | + |
| 45 | + # base case - the path has generated a correct solution |
| 46 | + if mass == 0: |
| 47 | + yield sorted(path) |
| 48 | + return |
| 49 | + |
| 50 | + # stop running when we overshoot the mass |
| 51 | + elif mass < 0: |
| 52 | + return |
| 53 | + |
| 54 | + # can we sum up to the target value using the remaining masses? recursive call |
| 55 | + elif sum_matrix[n][mass]: |
| 56 | + yield from find_path(mass_list, sum_matrix, n - 1, mass, max_subset_length, path) |
| 57 | + |
| 58 | + if len(path) < max_subset_length: |
| 59 | + path.append(mass_list[n-1]) |
| 60 | + |
| 61 | + yield from find_path(mass_list, sum_matrix, n - 1, mass - mass_list[n - 1], max_subset_length, path) |
| 62 | + path.pop() |
| 63 | + |
| 64 | + |
| 65 | +def subset_sum(mass_list, mass, max_subset_length=3): |
| 66 | + """ |
| 67 | + Dynamic programming implementation of subset sum. Note that, whilst this algorithm is pseudo-polynomial, the |
| 68 | + backtracking algorithm for obtaining all possible subsets has exponential complexity and so remains unsuitable |
| 69 | + for large input values. This does, however, tend to perform a lot better than non-sum_matrix implementations, as |
| 70 | + we're no longer doing sums multiple times and we've cut down the operations performed during the exponential portion |
| 71 | + of the method. |
| 72 | +
|
| 73 | + :param mass_list: A list of masses from which to identify subsets. |
| 74 | +
|
| 75 | + :param mass: The target mass of the sum of the substructures. |
| 76 | +
|
| 77 | + :param max_subset_length: The maximum length of subsets to return. Allows the recursive backtracking algorithm to |
| 78 | + terminate early in many cases, significantly improving runtime. |
| 79 | +
|
| 80 | + :return: Generates of lists containing the masses of valid subsets. |
| 81 | + """ |
| 82 | + |
| 83 | + n = len(mass_list) |
| 84 | + |
| 85 | + # initialise dynamic programming array |
| 86 | + sum_matrix = numpy.ndarray([n + 1, mass + 1], bool) |
| 87 | + |
| 88 | + # subsets can always equal 0 |
| 89 | + for i in range(n+1): |
| 90 | + sum_matrix[i][0] = True |
| 91 | + |
| 92 | + # empty subsets do not have non-zero sums |
| 93 | + for i in range(mass): |
| 94 | + sum_matrix[0][i + 1] = False |
| 95 | + |
| 96 | + # fill in the remaining boolean matrix |
| 97 | + for i in range(n): |
| 98 | + for j in range(mass+1): |
| 99 | + if j >= mass_list[i]: |
| 100 | + sum_matrix[i + 1][j] = sum_matrix[i][j] or sum_matrix[i][j - mass_list[i]] |
| 101 | + else: |
| 102 | + sum_matrix[i + 1][j] = sum_matrix[i][j] |
| 103 | + |
| 104 | + # backtrack through the matrix recursively to obtain all solutions |
| 105 | + return find_path(mass_list, sum_matrix, n, mass, max_subset_length) |
0 commit comments