Algorithm for condensing/consolidating number combinations -


using horse race betting scenario, have number of separate bets predicting first 4 finishers of race (superfecta).

the bets follows...

1/2/3/4   1/2/3/5   1/2/4/3   1/2/4/5   1/2/5/3   1/2/5/4  

what want combine or condense these separate combinations as possible. bets above, can condensed 1 line...

1/2/3,4,5/3,4,5 

but if removed last bet list: 1/2/5/4 ...

1/2/3/4   1/2/3/5   1/2/4/3   1/2/4/5   1/2/5/3   

the condensed bets instead have 2 lines:

1/2/3,4/3,4,5   1/2/5/3   

what algorithm this?

i find easiest think kind of thing pretty pictures. how try build graphs?

first example

1/2/3/4   1/2/3/5   1/2/4/3   1/2/4/5   1/2/5/3   1/2/5/4  

...could this, in graph form:

example 1 before

each path top bottom (e.g. 1->2->4->3) corresponds row in initial format.

if start graph, (perhaps) can run little algorithm on graph simplify in way you're looking for. here's we'll try:

  • start @ top of graph, , move down level level. (the first level contains blue node 1.)
  • for each node in current level, count number of children. if there 1 child, skip node. (since blue node 1 has 1 child, we'll skip green node 2.)
  • for each of multiple children, construct set contains child , grandchildren. (the red node 3 has set {3,4,5}, red 4 has set {3,4,5}, , red 5 has set {3,4,5}.)
  • if of these sets identical, replace associated children/grandchildren single node containing children, pointing grandchild contains set. (since 3 red nodes have identical sets, replaced.)

example 1 after


second example

1/2/3/4   1/2/3/5   1/2/4/3   1/2/4/5   1/2/5/3  

...could this, in graph form:

example 2 before

the red nodes 3 , 4 have identical sets (i.e. {3,4,5}), replaced. red node 5 doesn't have same set red nodes 3 , 4, leave alone.

example 2 after

as before, each path through simplified tree represents 1 row of output.

(i haven't covered happens if replace children/grandchildren when there great-grandchildren. should start @ bottom row , work way upwards.)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -