Implementation with graph class

Modified the earlier implementation to include a class for graph