Attempt at graph search based on unionfind