added support for disconnected graphs