/* * graph.c - "Graph" abstract data type. */ #include #include #include "graph.h" #include "emalloc.h" struct graph { int maxnodes; /* original maxnodes value passed to new_graph() */ int nnodes; /* how many stored so far */ char **node_name; int *colour; }; /* Create a new empty graph. */ GRAPH new_graph(int maxnodes) { struct graph *g = (struct graph *)emalloc(sizeof(struct graph)); g->maxnodes = maxnodes; g->nnodes = 0; g->node_name = (char **)emalloc(maxnodes * sizeof(char *)); g->colour = (int *)emalloc(maxnodes * sizeof(int)); return g; } /* Delete a graph. */ void free_graph(GRAPH g) { free(g->node_name); free(g->colour); free(g); } /* Add a vertex. */ int add_vertex(GRAPH g, char *name) { g->node_name[g->nnodes] = name; g->colour[g->nnodes] = -1; return g->nnodes++; } /* Add a directed edge. */ void add_edge(GRAPH g, int from, int to) { FILL THIS IN } int get_graph_nodes(GRAPH g) { return g->nnodes; } int is_adjacent(GRAPH g, int from, int to) { FILL THIS IN } char *get_vertex_name(GRAPH g, int i) { return g->node_name[i]; } int get_vertex_colour(GRAPH g, int i) { return g->colour[i]; } /* Colour the graph. */ void colour_graph(GRAPH g) { /* FILL THIS IN */ } /* dump data for testing */ void print_graph(GRAPH g) { int row, col; printf("name col | "); for (col = 0; col < g->nnodes; col++) printf("%-3.3s", g->node_name[col]); printf("\n"); printf("---------+--"); for (col = 0; col < g->nnodes; col++) printf("---"); printf("\n"); for (row = 0; row < g->nnodes; row++) { printf("%-4s %3d | ", g->node_name[row], g->colour[row]); for (col = 0; col < g->nnodes; col++) printf("%-3d", is_adjacent(g, row, col)); printf("\n"); } }