Working on the combination check algorithm

What i am trying to do is to implement an algorithm which will tell us
if these borders can be combined together to create a closed shape.