Implementation of directed graphs + 3-way matching