In this paper, we investigate the grouping behavior of multi-agent systems by exploiting the graph structure. We propose a novel algorithm for designing a network from scratch which yields the desired grouping in a network of agents utilizing a consensus-based algorithm. The proposed algorithm is shown to be optimal in the sense that it consists of the minimum number of links. Furthermore, we examine the effect of adding new vertices and edges to the network on the number of groups formed in the group consensus problem. These results can be further utilized by the network topology designer to restructure the network and achieve the desired grouping. Theoretical results are illustrated with simulation examples.