Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:43:40+00:00 2026-06-01T10:43:40+00:00

I wrote an algorithm to find a solution to the subset sum problem in

  • 0

I wrote an algorithm to find a solution to the subset sum problem in Haskell. The signature is

subsetSum :: (Ord a, Num a) => [a] -> a -> Maybe [a]

QuickCheck seems to be a good fit to test that. For example I here is one of the properties that I could check:

prop_sumEqualsS l s = case subsetSum l s of
                        Just solution -> (sum solution) == s
                        Nothing       -> True

The problem is that the algorithm is quite computationally intensive and running 100 tests with big input lists takes ages to run.

I tried with QuickCheck 1 and it did run quickly, but the data sets used for testing were very small. After moving to QuickCheck 2 it seems to be the opposite problem. There is a manual for QC but it seems to be old (there is no date information) and I don’t know how much still applies to QC2. A Tutorial is available on the Haskell Wiki but there is not much details, just a few words on instantiating Arbitrary.

So I have two questions:

  • What changes in QuickCheck 2 make it become so much slower than QuickCheck?
  • What is the best way to control data sets creation to make them useful for a given test?

Edit: To be more specific, I would like to test my solution with a list size from 0 to 100, containing integers between -10000 and 10000.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:43:42+00:00Added an answer on June 1, 2026 at 10:43 am

    One thing that QuickCheck 2 introduced that could be a source of
    inefficiency is the shrink function. If a property fails, then
    the shrink function is called which gives candidates on smaller
    test values, and they are shrunk until they cannot give a
    smaller value for which the property fails. But this only
    happens when properties fail.

    The changes for the arbitrary instance for lists has not changed
    much between version 1
    and version 2.
    The difference is in wording, version 1 uses vector, and version 2 uses
    a list comprehension, but then vector is implemented with exactly such a list comprehension
    of sequenced calls to arbitrary.

    Both implementations used the size parameter. In QuickCheck 1, the size of
    a generated value is by
    default div n 2 + 3, where n is the number of the test.
    QuickCheck 2 uses another approach, where the maximum size
    and the number of tests is configured. The test sizes will be distributed
    in that range, growing linearly in the number of tests (see computeSize' in quickCheckWithResult).
    This means, with the default setting of 100 tests and maximum size, the maximum size
    from QuickCheck 1 would be (div 100 2 + 3) = 53, and with QuickCheck 2
    it would simply be 100.

    As subset sum is NP-complete you probably have an exponential algorithm 😉
    Then the difference in running time between a list of length 50 and 100
    can of course be enormous. Understandably you want small lists to test with. You have two
    choices: make a new data type (preferably with newtype) or make
    a stand-alone generator and use forAll. By using newtype you can
    also specify how values should be shrunk. This is an example implementation using the newtype approach:

    newtype SmallIntList = SmallIntList [Int] deriving (Eq,Show)
    
    instance Arbitrary SmallIntList where
      arbitrary = sized $ \s -> do
                     n <- choose (0,s `min` 50)
                     xs <- vectorOf n (choose (-10000,10000))
                     return (SmallIntList xs)
      shrink (SmallIntList xs) = map SmallIntList (shrink xs)
    

    This Arbitrary instance does not make lists longer than 50 elements.
    The elements do not use the size parameter, so they are always in
    the range [-10000,10000], so there is some room for improvement.
    The shrink function simply uses the available shrinks for lists and
    Ints.

    To use this instance with your property, just use a SmallIntList as
    an argument:

    prop_sumEqualsS (SmallIntList l) s = case subsetSum l s of
                                             ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write an algorithm that find the path in DAG with single
I wrote bresenham algorithm for 0<Angular coefficient<1 I don't know much about graphics in
I wrote a sorting algorithm in python. It returns a Python Dictionary object. How
I wrote an in-place permutation algorithm [an exercise in TAOCP3], where the inner loop
Let's say I wrote a program containing an algorithm that had an exponential runtime
I once wrote a Tetris AI that played Tetris quite well. The algorithm I
I wrote the following algorithm for finding all possible permutations of n unique alphabets.
In QuickGraph - is there algorithm for find all parents (up to root vertex's)
i wrote a function to find the position of the next column of an
What's the most efficient algorithm to find the rectangle with the largest area which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.