Fixed the traverse algorithm by using union set.