I have a problem in a Data Structures course I am taking. The question is as follows:
Given a number of variables $x_1,\dots,x_n$ and given $m$ equality and inequality constraints (in total $m$ constraints). Find if there exists a valid assignment that satisfies all constraints in $O((m+n)\log^*n)$ and in $O(m+n)$.
For example $x_1=x_2,x_2=x_3,x_1\neq x_3$ doesn't have a valid assignment.
I believe this question is a known one but I can't find it's name or know what to search for.
My idea is:
- Assign a $null$ value to all variables.
- For each $1\le i \le n$:
- If $x_i=null$ then assign $x_i \leftarrow i$.
- Go over all of $x_i$ equality constraints and assign for each index $j$ the value $x_j \leftarrow x_i$.
- Go over all of the inequality constraints and check if they hold. If at least one doesn't hold, return that there is no such valid assignment. If all of them hold return the current assignment that we have.
Using that algorithm we can guarantee that all equality assignment will be satisfied. Thus checking if the inequality assignments hold is enough to check if a valid assignment is possible.
Analyzing this algorithm. Let $m_i$ be the number of equality constraints on $x_i$ and let $m_i'$ be the number of inequality constraints. Therefore $\sum_{i=1}^{n}(m_i + m_i')=m$. The first step costs $n$ operations. The second step costs $\sum_{i=1}^{n} m_i$. The third step costs $m-\sum_{i=1}^{n} m_i$.
In total we get $n + (\sum_{i=1}^{n} m_i) + (m - \sum_{i=1}^{n} m_i)=n + m\in O(n+m)$.
I am not sure if this satisfies the second request of the question as I am not sure my calculations are valid. I would appreciate if someone can refer me to a source on this question or tell me if my calculation is correct. I would also appreciate if someone can help me offer a solution that'll take $O((m+n)log^*n)$.
Thanks in advance.