Implementation of Graph algorithm