# Résolution de problèmes algorithmiques

You have to maintain a consistent system of equations of the form $x_i - x_j = d$. Write an operation add(i,j,d) that decides if the equation $x_i - x_j = d$ is consistent with the system and if yes adds it to the system. Complexity should be almost constant time per request.

## Augmented union-find

A solution to this general problem consists in maintaining sets of variables that are connected by equations in the system. This is done with a classical union-find structure, where every component is organized in form of a tree. The root is the leader of the component. A precedence table prec indicates for every variable $x_i$ the predecessor variable $x_j$ in the tree with j=prec[i]. For the leader $x_i$ of a component we have prec[i]=i.

We augment this structure by assigning to every variable $x_i$ a potential pot[i] with the idea that if $j=prec[i]$ then $x_i-x_j=pot[i]$. By definition leaders of a component have zero potential. The potential between a variable and the leader of its component is by definition the sum of the potentials along the path from the variable to its leader. We have to update the potential accordingly when flattening the paths during a call to the find request.

Now on the operation add(i,j,d) we have to first verify if the variables $x_i$ and $x_j$ belong to the same component. In this case we can compare their potential and verify whether it satisfies the equality $x_i - x_j = d$, in other words verify whether the request is consistent. In case the variables belong to different components, these components have to be merged by making the leader of one component, say $x_k$, a descent of the leader of the other component. There is a unique way to assign potential pot[k] that would be consistent with the given request.

The following code implements this data-structure. To obtain the right complexity, a correct implementation should maintain a rank for each component and merge only the smaller rank component into the large rank component. However in practice this implementation is good enough, even though in the worst case each request could cost O(log n) time.

## For the special case of integers modulo 3

Some student had this nice idea. We store in a component all variables for which we know that they have the same value. In addition we have two pointers for the leader of a component, called before and after. If r is the leader of a component C and r’ the leader of a component C’ and if we know that the variables in C’ have value 1 plus the value of the variables in C, then we set after[r]=r’ and before[r’]=r. Now it is clear how to handle the requests of the form x=y or y-x=1. This might be easier to implement than the solution above, because it allows to use union-find as a blackbox data structure without modification.