Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Algorithm to find all possible combinations

Algorithm to find all possible combinations

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 4 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DavidM29
    wrote on last edited by
    #1

    Hello,

    I'm need to find every combinations following this explanation :

    I have 4 lists :
    1st list : (A,B)
    2nd list (C,D)
    3rd list (E,F)
    4th list (G,H)

    The result should be :

    Only 1 from the first list
    Only 1 from the second list
    None to all from the third
    Only one from the fourth list

    Examples :
    A + C+ G
    A +C + E,F + H
    B+ D + F + G

    How can I find all those combinations ?
    Not sure to be very clear here so if I'm not please ask me.

    aha_1980A JonBJ 2 Replies Last reply
    0
    • D DavidM29

      Hello,

      I'm need to find every combinations following this explanation :

      I have 4 lists :
      1st list : (A,B)
      2nd list (C,D)
      3rd list (E,F)
      4th list (G,H)

      The result should be :

      Only 1 from the first list
      Only 1 from the second list
      None to all from the third
      Only one from the fourth list

      Examples :
      A + C+ G
      A +C + E,F + H
      B+ D + F + G

      How can I find all those combinations ?
      Not sure to be very clear here so if I'm not please ask me.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @DavidM29,

      A very cheap (and probably slow, but that should not matter for such small lists) solution would be to have some nested for loops.

      For the first, second and forth list you take one element per iteration.

      Unfortuantely, for the third list you will have to take all possible combinations, which could be done by creating a temporary list C' (-; E; F; E, F) first. (don't know if you need F, E also?)

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • D Offline
        D Offline
        DavidM29
        wrote on last edited by
        #3

        @aha_1980
        Thank you for your reply I'll do that. I don't need the F,E.

        1 Reply Last reply
        0
        • D DavidM29

          Hello,

          I'm need to find every combinations following this explanation :

          I have 4 lists :
          1st list : (A,B)
          2nd list (C,D)
          3rd list (E,F)
          4th list (G,H)

          The result should be :

          Only 1 from the first list
          Only 1 from the second list
          None to all from the third
          Only one from the fourth list

          Examples :
          A + C+ G
          A +C + E,F + H
          B+ D + F + G

          How can I find all those combinations ?
          Not sure to be very clear here so if I'm not please ask me.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @DavidM29
          And make sure you end up with 32 combinations, exactly as per @aha_1980's algorithm :-;

          BTW, my old favorite Prolog language would be better than C++ for this :) No loops!

          aha_1980A 1 Reply Last reply
          3
          • JonBJ JonB

            @DavidM29
            And make sure you end up with 32 combinations, exactly as per @aha_1980's algorithm :-;

            BTW, my old favorite Prolog language would be better than C++ for this :) No loops!

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB

            BTW, my old favorite Prolog language would be better than C++ for this :) No loops!

            I don't doubt that! However, even in C++ might be better approaches - my solution is a very simple one.

            I'm sure deep in the STL or an external library there is an algorithm for such problems - but that's just outside my normal work area.

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              https://en.wikipedia.org/wiki/Constraint_programming and https://en.wikipedia.org/wiki/Gecode?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved