Author | Message |
---|---|
rabbott
Posts: 1649
|
Posted 15:49 Feb 12, 2014 |
Last edited by rabbott at
08:30 Feb 16, 2014.
|
rabbott
Posts: 1649
|
Posted 00:03 Feb 13, 2014 |
Last edited by rabbott at
08:29 Feb 16, 2014.
|
rabbott
Posts: 1649
|
Posted 21:47 Feb 13, 2014 |
I thought Sticks would be an interesting problem to work on. I have what seems to be a solution, but it's not very clean or very clever. (It's in my Sticks package.) Here are the two examples provided by the problem.
The solution to the first example is four sets: {1, 5}, {1, 5}, {1, 5}, {2, 2, 2}. The solution to the second problem is two sets: {1, 4}, {2, 3}. The code works as follows.
I thought that a MultiSet would be a useful collection. (A MultiSet is a set with possible repetitions.) But Scala doesn't have a MultiSet class. While looking that up I found a suggestion to use a TreeMap instead. The TreeMap keys are the (unique) set elements. The TreeMap values are the number of times that element appears in the set. So I decided to implement a MultiSet by using a TreeMap to keep track of the element counts. The MultiSet is in its own file. That wasn't too difficult. The really complex part was searching for the sets. The problem is that there may be many ways to make a set with a certain total. They must all be considered. The method buildTree (embedded in Sticks in the Sticks object) does the search.
It takes a goal (the desired total for each subset) and a MultiSet of pieces. It returns Some(Seq[MultiSet]) if it can find subsets that add up to goal. The subsets are the Seq of MultiSets. If it can't find subsets that add to goal (while using all the pieces) it returns None. It builds a rather involved tree of possibilities. If you can find a simpler way to do it, that would be great.
Last edited by rabbott at
08:29 Feb 16, 2014.
|
abet
Posts: 5
|
Posted 18:02 Feb 17, 2014 |
Any closed loop has a rank of 1. If closed loops intersect, that increases the rank of the combination of nodes. So it might be useful to compute the collections of closed loops. |