The subset sum problem (SSP) is a decision problem in computer science. In its most general formulation, there is a multiset of integers and a target-sum , and the question is to decide whether any subset of the integers sum to precisely .[1] The problem is known to be NP-complete. Moreover, some restricted variants of it are NP-complete too, for example:[1]
The variant in which all inputs are positive.
The variant in which inputs may be positive or negative, and . For example, given the set , the answer is yes because the subset sums to zero.
The variant in which all inputs are positive, and the target sum is exactly half the sum of all inputs, i.e., . This special case of SSP is known as the partition problem.
SSP can also be regarded as an optimization problem: find a subset whose sum is at most T, and subject to that, as close as possible to T. It is NP-hard, but there are several algorithms that can solve it reasonably quickly in practice.
n - the number of input integers. If n is a small fixed number, then an exhaustive search for the solution is practical.
L - the precision of the problem, stated as the number of binary place values that it takes to state the problem. If L is a small fixed number, then there are dynamic programming algorithms that can solve it exactly.
As both n and L grow large, SSP is NP-hard. The complexity of the best known algorithms is exponential in the smaller of the two parameters n and L. The problem is NP-hard even when all input integers are positive (and the target-sum T is a part of the input). This can be proved by a direct reduction from 3SAT.[2] It can also be proved by reduction from 3-dimensional matching (3DM):[3]
We are given an instance of 3DM, where the vertex sets are W, X, Y. Each set has n vertices. There are m edges, where each edge contains exactly one vertex from each of W, X, Y. Denote L := ceiling(log2(m+1)), so that L is larger than the number of bits required to represent the number of edges.
We construct an instance of SSP with m positive integers. The integers are described by their binary representation. Each input integer can be represented by 3nL bits, divided into 3n zones of L bits. Each zone corresponds to a vertex.
For each edge (w,x,y) in the 3DM instance, there is an integer in the SSP instance, in which exactly three bits are "1": the least-significant bits in the zones of the vertices w, x, and y. For example, if n=10 and L=3, and W=(0,...,9), X=(10,...,19), Y=(20,...,29), then the edge (0, 10, 20) is represented by the number (20+230+260).
The target sum T in the SSP instance is set to an integer with "1" in the least-significant bit of every zone, that is, (20+21+...+23n-1).
If the 3DM instance has a perfect matching, then summing the corresponding integers in the SSP instance yields exactly T.
Conversely, if the SSP instance has a subset with sum exactly T, then, since the zones are sufficiently large so that there are no "carries" from one zone to the next, the sum must correspond to a perfect matching in the 3DM instance.
The following variants are also known to be NP-hard:
The input integers can be both positive and negative, and the target-sum T = 0. This can be proved by reduction from the variant with positive integers. Denote that variant by SubsetSumPositive and the current variant by SubsetSumZero. Given an instance (S, T) of SubsetSumPositive, construct an instance of SubsetSumZero by adding a single element with value −T. Given a solution to the SubsetSumPositive instance, adding the −T yields a solution to the SubsetSumZero instance. Conversely, given a solution to the SubsetSumZero instance, it must contain the −T (since all integers in S are positive), so to get a sum of zero, it must also contain a subset of S with a sum of +T, which is a solution of the SubsetSumPositive instance.
The input integers are positive, and T = sum(S)/2. This can also be proved by reduction from the general variant; see partition problem.
The analogous counting problem #SSP, which asks to enumerate the number of subsets summing to the target, is #P-complete.[4]
Exponential time algorithms
There are several ways to solve SSP in time exponential in n.[5]
Inclusion–exclusion
The most naïve algorithm would be to cycle through all subsets of n numbers and, for every one of them, check if the subset sums to the right number. The running time is of order , since there are subsets and, to check each subset, we need to sum at most n elements.
The algorithm can be implemented by depth-first search of a binary tree: each level in the tree corresponds to an input number; the left branch corresponds to excluding the number from the set, and the right branch corresponds to including the number (hence the name Inclusion-Exclusion). The memory required is . The run-time can be improved by several heuristics:[5]
Process the input numbers in descending order.
If the integers included in a given node exceed the sum of the best subset found so far, the node is pruned.
If the integers included in a given node, plus all remaining integers, are less than the sum of the best subset found so far, the node is pruned.
Horowitz and Sahni
In 1974, Horowitz and Sahni[6] published a faster exponential-time algorithm, which runs in time , but requires much more space - . The algorithm splits arbitrarily the n elements into two sets of each. For each of these two sets, it stores a list of the sums of all possible subsets of its elements. Each of these two lists is then sorted. Using even the fastest comparison sorting algorithm, Mergesort for this step would take time . However, given a sorted list of sums for elements, the list can be expanded to two sorted lists with the introduction of a ()th element, and these two sorted lists can be merged in time . Thus, each list can be generated in sorted form in time . Given the two sorted lists, the algorithm can check if an element of the first array and an element of the second array sum up to T in time . To do that, the algorithm passes through the first array in decreasing order (starting at the largest element) and the second array in increasing order (starting at the smallest element). Whenever the sum of the current element in the first array and the current element in the second array is more than T, the algorithm moves to the next element in the first array. If it is less than T, the algorithm moves to the next element in the second array. If two elements that sum to T are found, it stops. (The sub-problem for two elements sum is known as two-sum.[7])
Schroeppel and Shamir
In 1981, Schroeppel and Shamir presented an algorithm[8] based on Horowitz and Sanhi, that requires similar runtime - , much less space - . Rather than generating and storing all subsets of n/2 elements in advance, they partition the elements into 4 sets of n/4 elements each, and generate subsets of n/2 element pairs dynamically using a min heap, which yields the above time and space complexities since this can be done in and space given 4 lists of length k.
Due to space requirements, the HS algorithm is practical for up to about 50 integers, and the SS algorithm is practical for up to 100 integers.[5]
Howgrave-Graham and Joux
In 2010, Howgrave-Graham and Joux[9] presented a probabilistic algorithm that runs faster than all previous ones - in time using space . It solves only the decision problem, cannot prove there is no solution for a given sum, and does not return the subset sum closest to T.
The techniques of Howgrave-Graham and Joux were subsequently extended[10] bringing the time complexity to . A more recent generalization[11] lowered the time complexity to .
Pseudo-polynomial time dynamic programming solutions
We define a state as a pair (i, s) of integers. This state represents the fact that
"there is a nonempty subset of which sums to s."
Each state (i, s) has two next states:
(i+1, s), implying that is not included in the subset;
(i+1, s+), implying that is included in the subset.
Starting from the initial state (0, 0), it is possible to use any graph search algorithm (e.g. BFS) to search the state (N, T). If the state is found, then by backtracking we can find a subset with a sum of exactly T.
The run-time of this algorithm is at most linear in the number of states. The number of states is at most N times the number of different possible sums. Let A be the sum of the negative values and B the sum of the positive values; the number of different possible sums is at most B-A, so the total runtime is in . For example, if all input values are positive and bounded by some constant C, then B is at most N C, so the time required is .
This solution does not count as polynomial time in complexity theory because is not polynomial in the size of the problem, which is the number of bits used to represent it. This algorithm is polynomial in the values of A and B, which are exponential in their numbers of bits. However, Subset Sum encoded in unary is in P, since then the size of the encoding is linear in B-A. Hence, Subset Sum is only weakly NP-Complete.
For the case that each is positive and bounded by a fixed constant C, in 1999, Pisinger found a linear time algorithm having time complexity (note that this is for the version of the problem where the target sum is not necessarily zero, as otherwise the problem would be trivial).[12] In 2015, Koiliaris and Xu found a deterministic algorithm for the subset sum problem where T is the sum we need to find.[13] In 2017, Bringmann found a randomized time algorithm.[14]
In 2014, Curtis and Sanches found a simple recursion highly scalable in SIMD machines having time and space, where p is the number of processing elements, and is the lowest integer.[15] This is the best theoretical parallel complexity known so far.
A comparison of practical results and the solution of hard instances of the SSP is discussed by Curtis and Sanches.[16]
Suppose all inputs are positive. An approximation algorithm to SSP aims to find a subset of S with a sum of at most T and at least r times the optimal sum, where r is a number in (0,1) called the approximation ratio.
Simple 1/2-approximation
The following very simple algorithm has an approximation ratio of 1/2:[17]
Order the inputs by descending value;
Put the next-largest input into the subset, as long as it fits there.
When this algorithm terminates, either all inputs are in the subset (which is obviously optimal), or there is an input that does not fit. The first such input is smaller than all previous inputs that are in the subset and the sum of inputs in the subset is more than T/2 otherwise the input also is less than T/2 and it would fit in the set. Such a sum greater than T/2 is obviously more than OPT/2.
Fully-polynomial time approximation scheme
The following algorithm attains, for every , an approximation ratio of . Its run time is polynomial in n and . Recall that n is the number of inputs and T is the upper bound to the subset sum.
initialize a list L to contain one element 0.
for eachi from 1 to ndo
let Ui be a list containing all elements y in L, and all sums xi + y for all y in L.
sort Ui in ascending order
make L empty
let y be the smallest element of Ui
add y to Lfor each element z of Ui in increasing order do// Trim the list by eliminating numbers close to one another// and throw out elements greater than the target sum T.ify + ε T/n < z ≤ Ttheny = z
add z to Lreturn the largest element in L.
Note that without the trimming step (the inner "for each" loop), the list L would contain the sums of all subsets of inputs. The trimming step does two things:
It ensures that all sums remaining in L are below T, so they are feasible solutions to the subset-sum problem.
It ensures that the list L is "sparse", that is, the difference between each two consecutive partial-sums is at least .
These properties together guarantee that the list L contains no more than elements; therefore the run-time is polynomial in .
When the algorithm ends, if the optimal sum is in L, then it is returned and we are done. Otherwise, it must have been removed in a previous trimming step. Each trimming step introduces an additive error of at most , so n steps together introduce an error of at most . Therefore, the returned solution is at least which is at least .
The above algorithm provides an exact solution to SSP in the case that the input numbers are small (and non-negative). If any sum of the numbers can be specified with at most P bits, then solving the problem approximately with is equivalent to solving it exactly. Then, the polynomial time algorithm for approximate subset sum becomes an exact algorithm with running time polynomial in n and (i.e., exponential in P).
Kellerer, Mansini, Pferschy and Speranza[18] and Kellerer, Pferschy and Pisinger[19] present other FPTASes for subset sum.
See also
Knapsack problem – Problem in combinatorial optimization - a generalization of SSP in which each input item has both a value and a weight. The goal is to maximize the value subject to a bound on the total weight.
Multiple subset sum problem – Mathematical optimization problemPages displaying short descriptions of redirect targets - a generalization of SSP in which one should choose several subsets.
Merkle–Hellman knapsack cryptosystem – one of the earliest public key cryptosystems invented by Ralph Merkle and Martin Hellman in 1978. The ideas behind it are simpler than those involving RSA, and it has been brokenPages displaying wikidata descriptions as a fallback
^Howgrave-Graham, Nick; Joux, Antoine (2010). "New Generic Algorithms for Hard Knapsacks". In Gilbert, Henri (ed.). Advances in Cryptology – EUROCRYPT 2010. Lecture Notes in Computer Science. Vol. 6110. Berlin, Heidelberg: Springer. pp. 235–256. doi:10.1007/978-3-642-13190-5_12. ISBN978-3-642-13190-5.
^Becker, Anja; Coron, Jean-Sébastien; Joux, Antoine (2011). "Improved Generic Algorithms for Hard Knapsacks". In Patterson, Kenneth (ed.). Advances in Cryptology – EUROCRYPT 2011. Lecture Notes in Computer Science. Vol. 6632. Berlin, Heidelberg: Springer. pp. 364–385. doi:10.1007/978-3-642-20465-4_21. ISBN978-3-642-20465-4.
^Bonnetain, Xavier; Bricout, Rémi; Schrottenloher, André; Shen, Yixin (2020). "Improved Classical and Quantum Algorithms for Subset-Sum". In Moriai, Shiho; Wang, Huaxiong (eds.). Advances in Cryptology - ASIACRYPT 2020. Lecture Notes in Computer Science. Vol. 12492. Berlin, Heidelberg: Springer. pp. 633–666. doi:10.1007/978-3-030-64834-3_22. ISBN978-3-030-64833-6.
^Pisinger, David (1999). "Linear time algorithms for knapsack problems with bounded weights". Journal of Algorithms. 33 (1): 1–14. doi:10.1006/jagm.1999.1034. MR1712690.
^Koiliaris, Konstantinos; Xu, Chao (2015-07-08). "A Faster Pseudopolynomial Time Algorithm for Subset Sum". arXiv:1507.02318 [cs.DS].
^Bringmann, Karl (2017). "A near-linear pseudopolynomial time algorithm for subset sum". In Klein, Philip N. (ed.). Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA 2017). SIAM. pp. 1073–1084. arXiv:1610.04712. doi:10.1137/1.9781611974782.69.
^Curtis, V. V.; Sanches, C. A. A. (January 2016). "An efficient solution to the subset-sum problem on GPU: An efficient solution to the subset-sum problem on GPU". Concurrency and Computation: Practice and Experience. 28 (1): 95–113. doi:10.1002/cpe.3636. S2CID20927927.
^Curtis, V. V.; Sanches, C. A. A. (July 2017). "A low-space algorithm for the subset-sum problem on GPU". Computers & Operations Research. 83: 120–124. doi:10.1016/j.cor.2017.02.006.
Si ce bandeau n'est plus pertinent, retirez-le. Cliquez ici pour en savoir plus. Cet article ne s'appuie pas, ou pas assez, sur des sources secondaires ou tertiaires (mars 2020). Pour améliorer la vérifiabilité de l'article ainsi que son intérêt encyclopédique, il est nécessaire, quand des sources primaires sont citées, de les associer à des analyses faites par des sources secondaires. 1973 1982 Convention constitutionnelle d'Irlande du Nord 1er mai 1975 UPP – Harry West V...
Perang Utsmaniyah-Venesia IVBagian dari Perang Utsmaniyah-VenesiaPertempuran LepantoTanggal27 Juni 1570 – 7 Maret 1573LokasiSiprus, Laut Ionia dan AegeaHasil Kemenangan di pihak UtsmaniyahPerubahanwilayah Siprus di bawah pemerintahan UtsmaniyahPihak terlibat Liga Suci, termasuk: Republik Venesia Kerajaan Spanyol Kerajaan Napoli Negara Gereja Republik Genova lainnya Kesultanan UtsmaniyahTokoh dan pemimpin Marco Antonio Bragadin Alvise Martinengo Sebastiano Venie...
Untuk kegunaan lain, lihat Muna dan Muna. Kabupaten MunaKabupaten LambangMotto: Sowite (Demi Tanah Ku)PetaKabupaten MunaPetaTampilkan peta SulawesiKabupaten MunaKabupaten Muna (Indonesia)Tampilkan peta IndonesiaKoordinat: 4°48′S 122°36′E / 4.8°S 122.6°E / -4.8; 122.6Negara IndonesiaProvinsiSulawesi TenggaraTanggal berdiri-Dasar hukumUU No. 29 Tahun 1959Hari jadi4 Juli 1959Ibu kotaKota RahaJumlah satuan pemerintahan Daftar Kecamatan: 22 kecamatanKelura...
Graf Philippe François Maurice de Rivet d’Albignac, auch Graf von Ried(e) (* 15. Februar 1775 auf Schloss Triadou in Peyreleau (Département Aveyron, Frankreich); † 20. Januar 1824 in Pont-Saint-Esprit) war ein französischer General. Inhaltsverzeichnis 1 Leben 1.1 Ancien Regime und Revolutionäres Frankreich 1.2 Kaiserreich Frankreich 1.3 Königreich Westphalen 1.4 Kaiserreich Frankreich 1.5 Herrschaft der Hundert Tage 1.6 Zweite Restauration 2 Ehrungen; Tod 3 Literatur 4 Weblinks 5 Ein...
Magpie Kucica Eurasia Klasifikasi ilmiah Kerajaan: Animalia Filum: Kordata Kelas: Aves Ordo: Passeriformes Famili: Corvidae Genera Urocissa Cissa Cyanopica Kucica sedang bertengger, Swedia 2016 Kucica adalah burung dari keluarga Corvidae (gagak). Kucica Eurasia hitam-putih banyak dianggap sebagai salah satu hewan paling cerdas di dunia[1][2][3] dan salah satu dari sedikit spesies non-mamalia yang dapat mengenali dirinya sendiri dalam uji cermin.[4] Selain anggo...
Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada Februari 2023. YinzhiPangeran Zhi dari Peringkat KeduaTenure1698–1708PenerusHongfangInformasi pribadiKelahiranAisin Gioro Yinti(愛新覺羅 胤禔)12 Maret 1672Kematian7 Januari 1735 (usia 62)WangsaAisin GioroNama lengkapAisin Gioro Yunti(愛新覺羅 允禔)Ayah...
1939 film by Sidney Lanfield The Hound of the BaskervillesTheatrical release posterDirected bySidney LanfieldScreenplay byErnest PascalBased onThe Hound of the Baskervilles1902 novelby Arthur Conan DoyleProduced byGene MarkeyDarryl F. ZanuckStarring Richard Greene Basil Rathbone Wendy Barrie Nigel Bruce CinematographyPeverell MarleyEdited byRobert SimpsonMusic byDavid ButtolphCharles MaxwellCyril J. MockridgeDavid RaksinDistributed by20th Century FoxRelease date March 31, 1939 ...
State highway in Maryland, US Maryland Route 4Maryland Route 4 highlighted in redRoute informationMaintained by MDSHALength64.85 mi[1] (104.37 km)Existed1927–presentTouristroutes Star-Spangled Banner Scenic BywayMajor junctionsSouth end MD 5 in LeonardtownMajor intersections MD 235 in California MD 2 in Solomons MD 231 in Prince Frederick MD 2 in Sunderland MD 260 at Lyons Creek US 301 in Upper Marlboro MD 223 in Melwood Suitland Parkway in Melwood I-95 ...
Khagan of the Mongols Delbeg答里巴ᠳᠡᠯᠪᠡKhagan of the MongolsKhagan of the Northern Yuan dynastyReign1412–1415Coronation1411PredecessorBunyashiriSuccessorOyiradaiBorn1395Mongolian PlateauDied1415 (aged 19–20)Mongolian PlateauNamesDelbegHouseBorjiginDynastyNorthern Yuan Delbeg (Mongolian script:ᠳᠡᠯᠪᠡ Mongolian: Дэлбэг; Chinese: 答里巴), (1395–1415) was a khagan of the Northern Yuan dynasty, reigning from 1412 to 1415. Delbeg was installed by th...
2000 studio album by Glenn SpearmanFree WorldsStudio album by Glenn SpearmanReleased2000Recorded1994–1995StudioSan Francisco, California; Oakland, CaliforniaGenreFree jazzLength1:14:13LabelBlack Saint120207-2ProducerDon PaulGlenn Spearman chronology Blues for Falasha(1999) Free Worlds(2000) Free Worlds is a posthumously released album by saxophonist Glenn Spearman. It was recorded during 1994 and 1995 in San Francisco and Oakland, California, and was released in 2000 by the Black Sa...
Czech luger Luboš JíraPersonal informationNationalityCzechBorn (1968-08-30) 30 August 1968 (age 55)SportSportLuge Luboš Jíra (born 30 August 1968) is a Czech luger. He competed in the men's singles and doubles events at the 1988 Winter Olympics.[1] His son competed in the luge at the 2010 Winter Olympics.[1] References ^ a b Evans, Hilary; Gjerde, Arild; Heijmans, Jeroen; Mallon, Bill; et al. Luboš Jíra Olympic Results. Olympics at Sports-Reference.com. Sports ...
Old Palace School Croydon Palace, in Croydon, now part of south London, was the summer residence of the Archbishop of Canterbury for over 500 years. Regular visitors included Henry III and Queen Elizabeth I. Now known as Old Palace, the buildings are still in use as the Old Palace School, an independent girls' school of the Whitgift Foundation. It has been a grade I listed building since 1951.[1] History Engraving of Croydon Palace circa 1785, from The Antiquities of England and Wales...
Building in Bristol, EnglandKingsley HallLocation within BristolGeneral informationArchitectural styleGeorgianTown or cityBristolCountryEnglandCoordinates51°27′21″N 2°34′53″W / 51.455919°N 2.581369°W / 51.455919; -2.581369Completed1706 Kingsley Hall (grid reference ST596731) is at 59 Old Market Street in Old Market, Bristol. The hall was built as a private house in 1706 and restored in the late 19th century for use as a political club and office premises. I...
Indian actor (born 1977) This article contains wording that promotes the subject in a subjective manner without imparting real information. Please remove or replace such wording and instead of making proclamations about a subject's importance, use facts and attribution to demonstrate that importance. (February 2011) (Learn how and when to remove this template message) Kopil BoraKopil Bora records his voice for the Axomiya language version of TeachAids at Auditek Digital Recording Studio in Gu...
Municipio de Scott Municipio Municipio de ScottUbicación en el condado de Stevens en Minnesota Ubicación de Minnesota en EE. UU.Coordenadas 45°32′58″N 96°03′57″O / 45.549444444444, -96.065833333333Entidad Municipio • País Estados Unidos • Estado Minnesota • Condado StevensSuperficie • Total 91.98 km² • Tierra 87.15 km² • Agua (5.26 %) 4.84 km²Altitud • Media 344 m s. n. m.Población&...