optimized the code for graph.c and graph.h