# Broken stick problem

We have a broken stick. For every part, we know it's length. Our task is to connect all parts (glue them), that we will use as small amount of glue as possible.

The amount of glue need to connect two parts equal the maximum from their sizes. We can only glue two parts at one time.Can we solve this problem in the time complexity smaller than $O(n^3)$? I know only the answer, using dynamic table in this complexity

-
Do the parts have a fixed position (in the "reconstructed" stick)? Or can they be rearranged arbitrarily? –  Vor Jan 9 at 15:36
If I understand correctly, the problem looks like the construction of a Huffman tree. If so, the problem would be solved in O(n log n) time. –  Yoshio Okamoto Jan 9 at 16:03
Could you please explain why you are interested in this question and provide motivation? –  Kaveh Jan 9 at 16:23
@MarzioDeBiasi - No, the parts have fixed order in the glued stick. –  Jonny Jan 9 at 23:21
I have given very similar questions in homework and exams in my undergraduate algorithms course. This should be migrated to CS.SE. –  JeffE Jan 10 at 8:53

## migrated from cstheory.stackexchange.comJan 11 at 4:51

This question came from our site for theoretical computer scientists and researchers in related fields.

You didn't define what 'connecting all parts' is. Do you need to arrange them in an array? Or just connect them?

The way I understood the problem, you can use Prim's algorithm to find the minimum spanning tree, and that's the result. O(n^2)

In case you want them arranged in an array, I think the sorted array is always optimal.

We will call a part "counted" if it is the bigger part in some connection. A part can be counted 0, 1, or 2 times. Let there be 3 elements in the array that are not sorted (A > B > C). Let L be the part placed left from this sequence, and R the part placer right. The ordering is L(permutation(A,B,C))R. You can observe all cases, and see that the permutation ABC is the best because it takes the parts max(L,A), A, B, max(C,R). This proves that all 3 element subsequences should be sorted, which means the sorted array is optimal.

-
Your favorite sawmill charges by length to cut each board of lumber. For example, to make one cut anywhere on an 8 ft. board of lumber costs \$8. The cost of cutting a single board of wood into smaller boards will depend on the order of the cuts. As input, you are given a board of length$n$marked with$k\$ locations to cut