Distributed Algorithms for Networks Formation in a Scalable Internet of Things