Suppose we wish to create a speech. Assume we have $n$ words to choose from numbered from $1$ to $n$. Every word holds a list of words that can come after it $S_i$. For example: if $2 \in S_1$ then the speech can contain the sequence of words $1,2$. Every word has a length $n_i$ and a positive weight $w_i$ signifying how 'effective' this word is to the crowd.
Describe an algorithm, as efficient as possible finding a speech of exactly $l$ letters of maximal weight, given the linguistic constraints as were denoted.
For runtime complexity purposes assume $\sum S_i = s$.
An algorithm solving this in linear time is emitted using DFS. Even so, I noticed this question is very simple to define as an LP and I wondered if it can have an efficient solution. I have provided a layout of a possible linear program. I'd like to know if this question can be solved efficiently using LP.
So I've thought of a way to compute this using a graph $G=(V,E)$ s.t:
- $V = \{s,t\} \cup[n]\times[l]\cup\{0\}$
- $E=\{((s,0),(i,n_i))|n_i\leq l\}\cup\\\quad\quad \{((i,m),(j,m+n_j))|j\in S_i\land m+n_j \leq l\}\cup\\\quad\quad\{((i,l),(t,0))|i\in[n]\}$
- The weight function is the $w'$ is $-w$ for all edges except the ones entering $t$ which will be 0.
This is a DAG and thus i can find SSSP from $s$ to $t$ in $\approx O(n+s)$.
Now say i module the problem in the following manner:
Look for a vector $x$ s.t we maximize $\sum x_iw_{x_i}$ whilst holding:
- $\sum n_{x_i}=l$
- $x_i \in [n]$
Now I can transform this into standard form and run simplex algorithm.
Thanks in advance!