I am no programmer, but surely with the simple example you give.
If
Staff A has vacation period A
Staff B has vacation period B
Staff A wants to swap his entire block for period B
Staff B wants to swap his entire block for period A
There is no sense and evaluating this any further so code should loop out, then after removing these two from the table to another table Maketable and update table and sending to a report or print for processing (as it will not help anyone else thats looking to swap keeping them in the), then both staff are happy and thats that, code should then loop back into looking at the next records for the next easy swaps
and same goes for anyone else that has this simple scenario.
Also If...
Staff A has vacation period A1(=1st3wk block) to swap and thats all he has got.
and Staff B has vacation period B2(=2nd 3wk block) to swap and thats also all he has got.
Staff A wants to swap his 1st block for staff Bs 2nd block
and Staff B is willing, then they swap.
Again, There is no sense in evaluating that table any further at that point so the code should loop out again, after removing these two from the table and sending to a report, print and or Make-table or print for processing as it will not help anyone else thats looking to swap, both of these staff are happy too.
Each time the table or register reduces in size.
So once these swap occur, whats left in the register/table would then become more difficult to deal with.
Like the following:
Staff1 A B Staff 1 swaps with 3 Staff2 D C Staff 2 swaps with 4 Staff3 B D Staff 3 swaps with 2 Staff4 C A Staff 4 swaps with 1
But the main thing as I was talking about earlier is as long as table A contents match table B contents equally then they those staff that are involved all get together and swap, therfore reducing the table to almost nothing.