Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the extreme for priority function / alphabet order

Tags:

algorithm

math

We have an array of elements a1,a2,...aN from an alphabet E. Assuming |N| >> |E|.

For each symbol of the alphabet we define an unique integer priority = V(sym). Let's define V{i} := V(symbol(ai)) for the simplicity.

How can I find the priority function V for which:

Count(i)->MAX | V{i} < V{i+1}

In other words, I need to find the priorities / permutation of the alphabet for which the number of positions i, satisfying the condition V{i}<V{i+1}, is maximum.

Edit-1: Please, read carefully. I'm given an array ai and the task is to produce a function V. It's not about sorting the input array with a priority function.

Edit-2: Example

E = {a,b,c}; A = 'abcab$'; (here $ = artificial termination symbol, V{$}=+infinity)

One of the optimal priority functions is: V{a}=1,V{b}=2,V{c}=3, which gives us the following signs between array elements: a<b<c>a<b<$, resulting in 4 '<' signs of 5 total.

like image 906
kvark Avatar asked Feb 10 '11 16:02

kvark


2 Answers

If elements could not have tied priorities, this would be trivial. Just sort by priority. But you can have equal priorities.

I would first sort the alphabet by priority. Then I'd extract the longest rising subsequence. That is the start of your answer. Extract the longest rising subsequence from what remains. Append that to your answer. Repeat the extraction process until the entire alphabet has been extracted.

I believe that this gives an optimal result, but I haven't tried to prove it. If it is not perfectly optimal, it still will be pretty good.

Now that I think I understand the problem, I can tell you that there is no good algorithm to solve it.

To see this let us first construct a directed graph whose vertices are your elements, and whose edges indicate how many times one element immediately preceeded another. You can create a priority function by dropping enough edges to get a directed acyclic graph, use the edges to create a partially ordered set, and then add order relations until you have a full linear order, from which you can trivially get a priority function. All of this is straightforward once you have figured out which edges to drop. Conversely given that directed graph and your final priority function, it is easy to figure out which set of edges you had to decide to drop.

Therefore your problem is entirely equivalent to figuring out a minimal set of edges you need to drop from athat directed graph to get athat directed acyclic graph. However as http://en.wikipedia.org/wiki/Feedback_arc_set says, this is a known NP hard problem called the minimum feedback arc set. begin update It is therefore very unlikely that there is a good algorithm for the graphs you can come up with. end update

If you need to solve it in practice, I'd suggest going for some sort of greedy algorithm. It won't always be right, but it will generally give somewhat reasonable results in reasonable time.

Update: Moron is correct, I did not prove NP-hard. However there are good heuristic reasons to believe that the problem is, in fact, NP-hard. See the comments for more.

like image 61
btilly Avatar answered Dec 23 '22 12:12

btilly


There's a trivial reduction from Minimum Feedback Arc Set on directed graphs whose arcs can be arranged into an Eulerian path. I quote from http://www14.informatik.tu-muenchen.de/personen/jacob/Publications/JMMN07.pdf :

To the best of our knowledge, the complexity status of minimum feedback arc set in such graphs is open. However, by a lemma of Newman, Chen, and Lovász [1, Theorem 4], a polynomial algorithm for [this problem] would lead to a 16/9 approximation algorithm for the general minimum feedback arc set problem, improving over the currently best known O(log n log log n) algorithm [2].

  1. Newman, A.: The maximum acyclic subgraph problem and degree-3 graphs. In: Proceedings of the 4th International Workshop on Approximation Algorithms for Combinatorial Optimization Problems, APPROX. LNCS (2001) 147–158

  2. Even,G.,Naor,J.,Schieber,B.,Sudan,M.:Approximatingminimumfeedbacksets and multi-cuts in directed graphs. In: Proceedings of the 4th International Con- ference on Integer Programming and Combinatorial Optimization. LNCS (1995) 14–28

like image 43
user614296 Avatar answered Dec 23 '22 12:12

user614296