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 7873793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:37:01+00:00 2026-06-03T02:37:01+00:00

Is there a better way to compute Cartesian product. Since Cartesian product is a

  • 0

Is there a better way to compute Cartesian product. Since Cartesian product is a special case that differs on each case. I think, I need to explain what I need to achieve and why I end up doing Cartesian product. Please help me if Cartesian product is the only solution for my problem. If so, how to improve the performance?

Background:

We are trying to help customers to buy products cheaper.

Let say customer ordered 5 products (prod1, prod2, prod3, prod4, prod5).

Each ordered product has been offered by different vendors.

Representation Format 1:

  • Vendor 1 – offers prod1, prod2, prod4
  • vendor 2 – offers prod1, prod5
  • vendor 3 – offers prod1, prod2, prod5
  • vendor 4 – offers prod1
  • vendor 5 – offers prod2
  • vendor 6 – offers prod3, prod4

In other words

Representation Format 2:

  • Prod 1 – offered by vendor1, vendor2, vendor3, vendor4
  • Prod 2 – offered by vendor5, vendor3, vendor1
  • prod 3 – offered by vendor6
  • prod 4 – offered by vendor1, vendor6
  • prod 5 – offered by vendor3, vendor2

Now to choose the best vendor based on the price. We can sort the products by price and take the first one.

In that case we choose

  • prod 1 from vendor 1
  • prod 2 from vendor 5
  • prod 3 from vendor 6
  • prod 4 from vendor 1
  • prod 5 from vendor 3

Complexity:

Since we chose 4 unique vendors, we need to pay 4 shipping prices.

Also each vendor has a minimum purchase order. If we don’t meet it, then we end up paying that charge as well.

In order to choose the best combination of products, we have to do Cartesian product of offered products to compute the total price.

total price computation algorithm:

foreach unique vendor 
if (sum (product price offered by specific vendor * quantity) < minimum purchase order limit specified by specific vendor)
totalprice += sum (product price * quantity) + minimum purchase charge + shipping price
else
totalprice += sum (product price * quantity) + shipping price
end foreach

In our case

  • {vendor1, vendor2, vendor3, vendor4}
  • {vendor1, vendor3, vendor5}
  • {vendor6}
  • {vendor1, vendor6}
  • {vendor2, vendor3}

4 * 3 * 1 * 2 * 2 = 48 combination needs to be computed to find the best combination.

  • {vendor1,vendor1, vendor6, vendor1, vendor2} = totalprice1,
  • {vendor1, vendor3, vendor6, vendor1, vendor2} = totalprice2,
    • *
  • {vendor4, vendor5, vendor6, vendor6, vendor3} = totalprice48

Now sort the computed total price to find the best combination.

Actual problem:

If the customer orders more than 15 products, and assume, each product has been offered by 8 unique vendors, then we end up computing 8^15=35184372088832 combinations, which takes more than couple of hours. If the customer orders more than 20 products then it takes more than couple of days.

Is there a solution to approach this problem in a different angle?

  • 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-03T02:37:02+00:00Added an answer on June 3, 2026 at 2:37 am

    Your problem can get even more complex. A simple example:

       Product   1     2     3
    Vendor 1    10    20    40
    Vendor 2    20    10    40
    --------------------------
    needed cnt 100   100    25
    

    You need 100 El. of P1, 100 of P2, and 25 of P3.

    P1 can be purchased for 1000 at V1, P2 for 1000 at V2, and P3 for 1000 at V1 or V3.

    Now shipping would be free, if you purchase for 1500, but cost you 200 at each vendor else.

    So if you order everything at V1, you would pay 4000:

    1000+2000+1000+0 (shipping) = or for the same sum   
    2000+1000+1000+0 at V2, or splitted
    
    1000+0+0+200  = 1200 at V1 plus 
    0+1000+1000+0 = 2000 at V2, 
    

    which sums up to 3200 and could be found by your method.

    But you could split the purchase of product 3 this way:

    1000+0+500+0 = 1500 at V1 plus 
    0+1000+500+0 = 1500 at V2 
    

    which only sums up to 3000 and would not be found by your method.

    Afaik, there is established research in such topics, and the keywords are matrices and system of equations.

    You can describe your problem as

    f(c11, p11) + f(c22, p12) + f(c13, p13) = c1 => dc1
    f(c21, p21) + f(c22, p22) + f(c23, p23) = c2 => dc2
    ...
    f(c31, p31) + f(c32, p32) + f(c13, p33) = c3 => dc3 
    

    where cij is the count of product j at vendor i and pij is the price of product j at vendor i, but f(c11,p11) is not just count*price, but a function of count and price, since there might be a quantity discount. The right side is the purchase total for vendor i.

    This is without purchase discount, which has to be modeled on top. If the discount on shipping is only depending on the total costs, it can be modeled just from ci => dci.

    You would try to minimize sum (dc1+dc2+…+dcm).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there better way to delete a parameter from a query string in a
Is there better way to enumerate all photos on the device than this one?
Is there a better way to generate HTML email in C# (for sending via
Is there a better way than using globals to get interesting values from a
Is there a better way to control the space between certain block elements.. i
Is there a better way to wait for queued threads before execute another process?
Is there a better way than this to splice an array into another array
Is there a better way than examine them pixel by pixel?
Is there a better way to do this jquery code? Currently I have to
Is there a better way to shell sort using C#? // array of integers

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.