Changed connected-graph algorithm