Implementation for disallowing self-loops and parallel edges