TITLE: Integer Linear Programming Conditional Constraints
QUESTION [6 upvotes]: How can I create a constraint that reflects the following: if $x_{ij} = 1$ AND $x_{jk} = 1$ THEN $x_{ik} = 1$?
All my variables $x_{ij}$ are binary. To provide some context:
I'm trying to create a linear equation system to be solved via the simplex algorithm that provides a solution to the problems schools face when creating class groups. Each student chooses 5 other students that he would like to be with in the following year. The school promises that each student will be in a class with at least one of the students they chose. To create the equation system I decided that my variables will be boolean and represent the following: $x_{ij} = 1$ if student $i$ is with student $j$ and $x_{ij} = 0$ otherwise. Thus, $x_{ij}= x_{ji}$ and $x_{ii} = 1$.
However, I'm having trouble with the following constraint: if student $i$ is together with student $j$ and student $j$ is together with student $k$, then inevitably student $i$ will be together with student $k$. This is represented by the constraint I mentioned at the beginning of the question.
I tried using the big M approach as mentioned in other questions but to no avail. In these questions there was only one condition but I have two.
Even if I solve this problem, how can this be scalable? For example: if $x_{12} = x_{23} = x_{14} = 1$ then $x_{13} = x_{34} = x_{24} = 1$. Maybe the variables I chose are not correct and I'm overcomplicating things. If this is the case, any guidance in the right direction would be more than welcomed.
Thanks for the help in advance!

REPLY [2 votes]: You can obtain the constraint via conjunctive normal form as follows:
$$
(x_{i,j} \land x_{j,k}) \implies x_{i,k} \\
\lnot (x_{i,j} \land x_{j,k}) \lor x_{i,k} \\
\lnot x_{i,j} \lor \lnot x_{j,k} \lor x_{i,k} \\
(1- x_{i,j}) + (1- x_{j,k}) + x_{i,k} \ge 1 \\
x_{i,j} + x_{j,k} - x_{i,k} \le 1
$$
You need only enforce these constraints for $i < j < k$.