// graph.c #include #include #include #include "graph.h" // initialize a graph of a given number of cities void init_graph(struct graph* graph, int size) { graph->size = size; // allocate space for the matrix and labels graph->matrix = malloc(sizeof(int*) * size); graph->labels = malloc(sizeof(char*) * size); for (int i = 0; i < size; i++) { graph->matrix[i] = malloc(sizeof(unsigned int) * size); graph->labels[i] = malloc(sizeof(char) * 64); } // set the distances all to "infiniti" for (int i = 0; i < graph->size; i++) { for (int j = 0; j < graph->size; j++) { graph->matrix[i][j] = INT_MAX; } } } // add a label to the graph void set_label(struct graph* graph, int node, char* label) { strcpy(graph->labels[node], label); } // get the value of a node const char* get_label(struct graph* graph, int index) { return graph->labels[index]; } // lookup a node number by the label int lookup(struct graph* graph, const char* label) { for (int i = 0; i < graph->size; i++) { if (strcmp(graph->labels[i], label) == 0) { return i; } } return -1; } // insert an edge between two nodes in the graph void insert_edge(struct graph* graph, int from, int to, int cost) { // insert the edge graph->matrix[from][to] = cost; } // return the cost of an edge in the graph unsigned int get_cost(struct graph* graph, int from, int to) { return graph->matrix[from][to]; }