Stop connecting nodes to the island when the resultant sum is non positive. And if all the islands resultant goes to non positive, the code should break and display the un connected nodes also.
While connecting the islands, all possible nodes should be connected in the island. If any element is added to the island, the adjacent nodes also is to be considered for adding until the resultant is non positive.