#include #include #include #include #include "Graph.h" #include "GraphAdjListRep.h" #include "graphReader.h" #include "Queue.h" int shortestPath(Graph g, int src, int dest, int path[]); int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "usage: %s \n", argv[0]); exit(EXIT_FAILURE); } char *graphFile = argv[1]; Graph g = readGraph(graphFile); int src; int dest; char c; if (sscanf(argv[2], "%d%c", &src, &c) != 1) { fprintf(stderr, "error: invalid source vertex '%s'\n", argv[2]); exit(EXIT_FAILURE); } if (src < 0 || src >= GraphNumVertices(g)) { fprintf(stderr, "error: invalid source vertex '%s'\n", argv[2]); exit(EXIT_FAILURE); } if (sscanf(argv[3], "%d%c", &dest, &c) != 1) { fprintf(stderr, "error: invalid destination vertex '%s'\n", argv[3]); exit(EXIT_FAILURE); } if (dest < 0 || dest >= GraphNumVertices(g)) { fprintf(stderr, "error: invalid destination vertex '%s'\n", argv[3]); exit(EXIT_FAILURE); } printf("-----\n"); printf("Graph\n"); printf("-----\n"); GraphShow(g); printf("\n"); int *path = calloc(GraphNumVertices(g), sizeof(int)); int pathLength = shortestPath(g, src, dest, path); if (pathLength == 0) { printf("No path from %d to %d\n", src, dest); } else { printf("Shortest path from %d to %d:", src, dest); for (int i = 0; i < pathLength; i++) { printf(" %d", path[i]); } printf("\n"); if (path[0] != src || path[pathLength - 1] != dest) { printf("Warning: path should start with %d and end with %d\n", src, dest); } } GraphFree(g); free(path); return 0; } // !!! DO NOT MODIFY THE CODE ABOVE !!! // (unless you are adding function prototypes) //////////////////////////////////////////////////////////////////////// // Your task int shortestPath(Graph g, int src, int dest, int path[]) { // TODO return 0; }