<p>According to <a href="http://www.sciencedirect.com/science/article/pii/0020019089901890" rel="nofollow">this reference</a> (1), the lexicographically first topological order problem is NLOG-complete. </p>

<p>You may want to take a more thorough look at the article to ensure that it covers the case(s) that you're interested in. In particular, based on the <a href="http://catalog.lib.kyushu-u.ac.jp/handle/2324/3114/rifis-tr-8.pdf" rel="nofollow">technical report version (pdf)</a> of that article, it appears that they're treating the lexicographic ordering of the vertices as strict (e.g.: in your notation, $\lambda(u) \neq \lambda(v) $ for $u \neq v$), but I'm not sure if this affects the applicability of the result.</p>

<ol>
<li>Shoudai, Takayoshi. "<a href="http://www.sciencedirect.com/science/article/pii/0020019089901890" rel="nofollow">The lexicographically first topological order problem is NLOG-complete.</a>" Information processing letters 33.3 (1989): 121-124.</li>
</ol>

CC BY-SA 3.0
2015-07-13T22:47:05.180Z
NLOG-complete is a subset of polynomial-time, and (per the "Pay attention" sentence in the first paragraph of the problem) making the labels of the vertices distinct makes the problem easily solvable by a polynomial-time greedy algorithm. The real question is what happens when the labels are not distinct.
David Eppstein
CC BY-SA 3.0
2015-07-14T02:34:51.553Z
That's a fair point. It's now clear from your answer that the repetition of labels makes the problem more difficult than the case of unique labels.
mhum
CC BY-SA 3.0
2015-07-13T19:50:25.297Z
2015-07-13T19:50:25.297Z
mhum
<p>With multiple copies of the same label allowed, the problem is NP-hard, via a reduction from cliques in graphs. Given a graph $G$ in which you want to find a $k$-clique, make a DAG with a source vertex for each vertex of $G$, a sink vertex for each edge of $G$, and a directed edge $xy$ whenever $x$ is a vertex of $G$ that forms an endpoint of edge $y$. Give the vertices of $G$ the label value $1$ and the edges of $G$ the label value $0$.</p>

<p>Then, there is a $k$-clique in $G$ if and only if the lexicographically first topological order forms a sequence of $k$ $1$'s and $\tbinom{k}{2}$ $0$'s, with $i-1$ $0$'s following the $i$th $1$. E.g. a six-vertex clique would be represented by the sequence $110100100010000100000$. This is the lexicographically smallest sequence that could possibly begin a topological ordering of a labeled DAG given by this construction (replacing any of the $1$'s by $0$'s would give a sequence with more edges than could be found in a simple graph with that many vertices) and it can only be the beginning of a topological ordering when $G$ contains the desired clique.</p>

CC BY-SA 3.0
2015-07-14T05:08:52.277Z
Oh, I hadn't thought about cliques. That's a nice reduction, thanks a lot! So this shows that the *computation problem* is NP-hard, even with the fixed label alphabet $\{0, 1\}$). It also implies that the decision problem "is the lexicographically smallest sequence less than this one" is NP-hard as well (you can use it to compute the minimum with binary search). The only additional question I see is whether the problem "is this exact input sequence the minimal one" is NP-hard as well. (With it, you cannot test easily if the minimal word starts with a prefix.) Do you have any idea for that one?
Antoine Amarilli 'a3nm'
CC BY-SA 3.0
2015-07-14T05:21:14.663Z
My suspicion is that the problem "is this exact sequence achievable" is NP-complete, but I don't have a reduction at hand. "Is this exact sequence the minimal one" should be at the second level of the polynomial hierarchy, since it requires a combination of existential quantification (is it achievable) and universal quantification (are all achievable sequences at least as big).
David Eppstein
CC BY-SA 3.0
2015-07-14T06:07:06.240Z
In fact I already know that testing whether an exact sequence is achievable is NP-hard (on an alphabet with 3 labels) by a reduction from unary 3-partition by Marzio de Biasi sketched  here: http://cstheory.stackexchange.com/a/19415/. But I think it doesn't tell the status of the problem "is this the minimal achievable sequence": when asking about whether a certain sequence is achievable, in general it will stand little chance of being minimal in some lexicographic order. Either way, what your reduction shows is still very interesting, thanks again! :)
Antoine Amarilli 'a3nm'
CC BY-SA 3.0
2015-07-14T01:01:18.393Z
2015-07-14T01:01:18.393Z
David Eppstein
<p>Consider the problem where we are given as input a directed acyclic graph $G = (V, E)$, a labeling function $\lambda$ from $V$ to some set $L$ with a total order $&lt;_L$ (e.g., the integers), and where we are asked to compute the <em>lexicographically smallest</em> topological sort of $G$ in terms of $\lambda$. More precisely, a <em>topological sort</em> of $G$ is an enumeration of $V$ as $\mathbf{v} = v_1, \ldots, v_n$, such that for all $i \neq j$, whenever there is a path from $v_i$ to $v_j$ in $G$, then we must have $i &lt; j$. The <em>label</em> of such a topological sort is the sequence of elements of $S$ obtained as $\mathbf{l} = \lambda(v_1), \ldots, \lambda(v_n)$. The lexicographical order on such sequences (which all have length $|V|$) is defined as $\mathbf{l} &lt;_{\text{LEX}} \mathbf{l'}$ iff there is some position $i$ such that $l_i &lt;_L l_i'$ and $l_j = l'_j$ for all $j &lt; i$. Pay attention to the fact that each label in $S$ can be assigned to multiple vertices in $V$ (otherwise the problem is trivial).</p>

<p>This problem can be stated either in a computation variant ("compute the lexicographically minimal topological sort") or in a decision variant ("is this input word the minimal topological sort?"). My question is, <strong>what is the complexity of this problem</strong>? Is it in PTIME (or in FP, for the computation variant) or is it NP-hard? If the general problem is NP-hard, I am also interested about the version where the set $S$ of possible labels is fixed in advance (i.e., there are only a constant number of possible labels).</p>

<p><em>Remarks:</em></p>

<p>Here is a small real-world example to motivate the problem. We can see the DAG as representing tasks of a project (with a dependency relationship between them) and the labels are integers representing the number of days that each task takes. To finish the project, it will take me the same total amount of time no matter the order I choose for the tasks. However, I would like to impress my boss, and to do this I want to finish as many tasks as possible as fast as possible (in a greedy manner, even if it means being very slow at the end because the harder tasks remain). Choosing the lexicographically minimal order optimises the following criterion: I want to choose an order $o$ such that there is no other order $o'$ and a number of days $n$ where after $n$ days I would have finished more tasks with order $o'$ than with order $o$ (i.e., if my boss looks at time $n$, I give a better impression with $o'$), but for all $m &lt; n$ I have finished no less tasks with order $o'$ than with order $o$.</p>

<p>To give some insight about the problem: I already know from <a href="https://cstheory.stackexchange.com/questions/19073/complexity-of-topological-sort-with-constrained-positions">previous</a> <a href="https://cstheory.stackexchange.com/questions/19393/monotone-bijections-between-lists-of-intervals">answers</a> that the following related problem is hard: "is there a topological sort which achieves the following sequence"? However, the fact here that I want a sequence which is <em>minimal</em> for this lexicographic order seems to constrain a lot the possible topological orders that may achieve it (in particular the reductions in those other answers no longer seem to work). Intuitively, there are much less situations where we have a choice to make.</p>

<p>Note that there seems to be interesting rephrasings of the problems in terms of set cover (when restricting the problem to DAGs that are bipartite, i.e., have height two): given a set of sets, enumerate them in a order $S_1, \ldots, S_n$ that minimises lexicographically the sequence $|S_1|$, $|S_2 \backslash S_1|$, $|S_3 \backslash (S_1 \cup S_2)|$, $\ldots$, $|S_n \backslash (S_1 \cup \cdots \cup S_{n-1})|$. The problem can also be rephrased on undirected graphs (progressively expand a connected area of the graph following the order that minimizes the lexicographic sequence of the uncovered labels). However, because of the fact that the sequence <em>has</em> to be greedy at all times by definition of the lexicographic order, I can't get reductions (e.g., of Steiner tree) to work.</p>

<p>Thanks in advance for your ideas!</p>

<cc.complexity-theory><np-hardness><directed-acyclic-graph><order-theory><topological-sorting>
Lexicographically minimal topological sort of a labeled DAG
Antoine Amarilli 'a3nm'
# Lexicographically minimal topological sort of a labeled DAG

(Asked by: username_0 on 2015-07-12)

Consider the problem where we are given as input a directed acyclic graph $G = (V, E)$, a labeling function $\lambda$ from $V$ to some set $L$ with a total order $<\_L$ (e.g., the integers), and where we are asked to compute the *lexicographically smallest* topological sort of $G$ in terms of $\lambda$. More precisely, a *topological sort* of $G$ is an enumeration of $V$ as $\mathbf{v} = v\_1, \ldots, v\_n$, such that for all $i \neq j$, whenever there is a path from $v\_i$ to $v\_j$ in $G$, then we must have $i < j$. The *label* of such a topological sort is the sequence of elements of $S$ obtained as $\mathbf{l} = \lambda(v\_1), \ldots, \lambda(v\_n)$. The lexicographical order on such sequences (which all have length $|V|$) is defined as $\mathbf{l} <\_{\text{LEX}} \mathbf{l'}$ iff there is some position $i$ such that $l\_i <\_L l\_i'$ and $l\_j = l'\_j$ for all $j < i$. Pay attention to the fact that each label in $S$ can be assigned to multiple vertices in $V$ (otherwise the problem is trivial).

This problem can be stated either in a computation variant ("compute the lexicographically minimal topological sort") or in a decision variant ("is this input word the minimal topological sort?"). My question is, **what is the complexity of this problem**? Is it in PTIME (or in FP, for the computation variant) or is it NP-hard? If the general problem is NP-hard, I am also interested about the version where the set $S$ of possible labels is fixed in advance (i.e., there are only a constant number of possible labels).

*Remarks:*

Here is a small real-world example to motivate the problem. We can see the DAG as representing tasks of a project (with a dependency relationship between them) and the labels are integers representing the number of days that each task takes. To finish the project, it will take me the same total amount of time no matter the order I choose for the tasks. However, I would like to impress my boss, and to do this I want to finish as many tasks as possible as fast as possible (in a greedy manner, even if it means being very slow at the end because the harder tasks remain). Choosing the lexicographically minimal order optimises the following criterion: I want to choose an order $o$ such that there is no other order $o'$ and a number of days $n$ where after $n$ days I would have finished more tasks with order $o'$ than with order $o$ (i.e., if my boss looks at time $n$, I give a better impression with $o'$), but for all $m < n$ I have finished no less tasks with order $o'$ than with order $o$.

To give some insight about the problem: I already know from [previous](https://cstheory.stackexchange.com/questions/19073/complexity-of-topological-sort-with-constrained-positions) [answers](https://cstheory.stackexchange.com/questions/19393/monotone-bijections-between-lists-of-intervals) that the following related problem is hard: "is there a topological sort which achieves the following sequence"? However, the fact here that I want a sequence which is *minimal* for this lexicographic order seems to constrain a lot the possible topological orders that may achieve it (in particular the reductions in those other answers no longer seem to work). Intuitively, there are much less situations where we have a choice to make.

Note that there seems to be interesting rephrasings of the problems in terms of set cover (when restricting the problem to DAGs that are bipartite, i.e., have height two): given a set of sets, enumerate them in a order $S\_1, \ldots, S\_n$ that minimises lexicographically the sequence $|S\_1|$, $|S\_2 \backslash S\_1|$, $|S\_3 \backslash (S\_1 \cup S\_2)|$, $\ldots$, $|S\_n \backslash (S\_1 \cup \cdots \cup S\_{n-1})|$. The problem can also be rephrased on undirected graphs (progressively expand a connected area of the graph following the order that minimizes the lexicographic sequence of the uncovered labels). However, because of the fact that the sequence *has* to be greedy at all times by definition of the lexicographic order, I can't get reductions (e.g., of Steiner tree) to work.

Thanks in advance for your ideas!

**Tags:** cc.complexity-theory, np-hardness, directed-acyclic-graph, order-theory, topological-sorting

---

## 2 answers

### Answer by username_2 on 2015-07-14. Score: 12 | Accepted answer

With multiple copies of the same label allowed, the problem is NP-hard, via a reduction from cliques in graphs. Given a graph $G$ in which you want to find a $k$-clique, make a DAG with a source vertex for each vertex of $G$, a sink vertex for each edge of $G$, and a directed edge $xy$ whenever $x$ is a vertex of $G$ that forms an endpoint of edge $y$. Give the vertices of $G$ the label value $1$ and the edges of $G$ the label value $0$.

Then, there is a $k$-clique in $G$ if and only if the lexicographically first topological order forms a sequence of $k$ $1$'s and $\tbinom{k}{2}$ $0$'s, with $i-1$ $0$'s following the $i$th $1$. E.g. a six-vertex clique would be represented by the sequence $110100100010000100000$. This is the lexicographically smallest sequence that could possibly begin a topological ordering of a labeled DAG given by this construction (replacing any of the $1$'s by $0$'s would give a sequence with more edges than could be found in a simple graph with that many vertices) and it can only be the beginning of a topological ordering when $G$ contains the desired clique.

> **Comments:**
> **username_0**: Oh, I hadn't thought about cliques. That's a nice reduction, thanks a lot! So this shows that the *computation problem* is NP-hard, even with the fixed label alphabet $\{0, 1\}$). It also implies that the decision problem "is the lexicographically smallest sequence less than this one" is NP-hard as well (you can use it to compute the minimum with binary search). The only additional question I see is whether the problem "is this exact input sequence the minimal one" is NP-hard as well. (With it, you cannot test easily if the minimal word starts with a prefix.) Do you have any idea for that one?  
> **username_2**: My suspicion is that the problem "is this exact sequence achievable" is NP-complete, but I don't have a reduction at hand. "Is this exact sequence the minimal one" should be at the second level of the polynomial hierarchy, since it requires a combination of existential quantification (is it achievable) and universal quantification (are all achievable sequences at least as big).  
> **username_0**: In fact I already know that testing whether an exact sequence is achievable is NP-hard (on an alphabet with 3 labels) by a reduction from unary 3-partition by Marzio de Biasi sketched  here: http://cstheory.stackexchange.com/a/19415/. But I think it doesn't tell the status of the problem "is this the minimal achievable sequence": when asking about whether a certain sequence is achievable, in general it will stand little chance of being minimal in some lexicographic order. Either way, what your reduction shows is still very interesting, thanks again! :)  

---

### Answer by username_1 on 2015-07-13. Score: 2

According to [this reference](http://www.sciencedirect.com/science/article/pii/0020019089901890) (1), the lexicographically first topological order problem is NLOG-complete.

You may want to take a more thorough look at the article to ensure that it covers the case(s) that you're interested in. In particular, based on the [technical report version (pdf)](http://catalog.lib.kyushu-u.ac.jp/handle/2324/3114/rifis-tr-8.pdf) of that article, it appears that they're treating the lexicographic ordering of the vertices as strict (e.g.: in your notation, $\lambda(u) \neq \lambda(v) $ for $u \neq v$), but I'm not sure if this affects the applicability of the result.

1. Shoudai, Takayoshi. "[The lexicographically first topological order problem is NLOG-complete.](http://www.sciencedirect.com/science/article/pii/0020019089901890)" Information processing letters 33.3 (1989): 121-124.

> **Comments:**
> **username_2**: NLOG-complete is a subset of polynomial-time, and (per the "Pay attention" sentence in the first paragraph of the problem) making the labels of the vertices distinct makes the problem easily solvable by a polynomial-time greedy algorithm. The real question is what happens when the labels are not distinct.  
> **username_1**: That's a fair point. It's now clear from your answer that the repetition of labels makes the problem more difficult than the case of unique labels.  

---

