algorithm - All Possible Combinations from non uniform table, with only one value from each column -


background

in grade 11 in school, required take 4 subjects , english list of predefined subjects. today, given grid helps make our combo, such can take 1 subject each column.

the grid

|    economics     |   maths  | psychology/politcal science |                     english                      |     geography    | | hindi/psychology |  history |          sociology          |                       art                        | elective english | |      maths       |  account |          commerce           |                    economics                     |      english     | |     english      |  physics |          chemistry          | biology/computer applications/mecahnical drawing |       maths      | 

the problem

i'm trying write program list out possible legal combinations table. rules are:

  • english must present
  • there must total of 5 subjects (english + 4)
  • no 2 subjects can same column
  • no subjects in final 5 may duplicate (note elective english , english separate subjects)

now pretty task if normal 4 5 matrix. however, [row 1, column 3], [row 2, column 1] , [row 4, column 4] contain more 1 subject, out of can still pick one. combination of cells can subdivided more combinations.

i'm having trouble coming algorithm this. i'm not asking free code, instead algorithm. i'm comfortable major languages (java, javascript, php etc), , pseudo codes , flowcharts work too.

this solution can, in theory, work infinite number of columns , infinite , varying number of rows within columns.

  1. split multi-subject rows separate rows while filling columns.
  2. put columns in array , order english-containing columns being first.
  3. iterate following while still have english in columns (we'll remove one-by-one):
    1. always take first column. contains english.
    2. choose english first element of combinations in iteration.
    3. construct combinations other columns recursively, taking care eliminate duplicates. doable, if pass around current combination.
    4. remove english column , sort columns again (based on whether have english or not).

this should combinations start english , eliminate duplicates.

edit: implemented solution above , seems work. check here - https://github.com/zupper/so-answers/tree/master/raghavcombos


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -