2

I've been looking for an efficient word partitioning algorithm but without much success. For example, given the word hello I want to obtain all the possible partitions of that word: {h,e,l,l,o},{h,e,l,lo},{h,e,llo},...,{hello}. Everything I found talks about word splitting which isn't what I mean.

Thank you in advance!

3 Answers 3

6

You show some examples, where we can concentrate on the commas. Either there is a comma or not.

 Word        Commas
{h,e,l,l,o}  1111
{h,e,l,l o}  1110
{h,e,l l o}  1100
...
{h e l l o}  0000

So it seems obvious, that at 4 positions, there may be a comma or not, independently from each other. You need 4 Bit to encode the partitions, which is 2^4 possibilities, I guess that is 16.

So you can form a loop:

for (int i = 0; i < 15; ++i)
    bitsplit ("hello", i);

and iterate through your word while iterating over the bits of the binary representation of i. For example for 11, you have the bits: 8+2+1 = 1011 set. That means {h,el,l,o}.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! It seems matters are simpler than we expect :) I got it running ;)
1

The problem is NP complete and needs to be solved by backtracking.

The idea is at each level, you decide whether this character belongs to current partition or should go to a new one. Do this recursively and every time you reach end of the word, you have one partition.

5 Comments

I don't think so. You can define an enumeration of all solutions, and translate it as shown above.
What you have mentioned would have the same complexity :). But it's true, your approach is better.
this is not NP complete. what you probably mean is that it takes exponential time in the size of the input, which is understandable seeing as how the size of the output is likewise exponential in the size of the input.
@Martin: Yes, that's what I actually wanted imply. Perhaps, I have mixed up in the definition of NP complete.
to simplify a lot, NP is a class of decision problems that take polynomial time to verify that a solution is valid if you're magically given one. NP-hard is the class of problems in NP that take exponential time to actually solve. NP-complete is a class of problems that every NP-hard problem can be reduced to in polynomial time.
0

Most likey you want to construct a suffix-trie.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.