Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jevin Olano
- // jolano
- // CSE 101
- // November 22, 2019
- // FindPath.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "Graph.h"
- #include "List.h"
- int main (int argc, char* argv[]) {
- if (argc != 3) {
- fprintf(stderr, "Usage %s <input> <output>\n", argv[0]);
- exit(1);
- }
- FILE *in = fopen(argv[1], "r");
- FILE *out = fopen(argv[2], "w");
- if (in == NULL) {
- printf("Unable to read from file %s\n", argv[1]);
- exit(1);
- }
- if (out == NULL) {
- printf("Unable to write to file %s\n", argv[2]);
- exit(1);
- }
- int n = 0; // for newGraph(n) later
- fscanf(in, "%d", &n);
- Graph G = newGraph(n); // creates a new Graph object
- int tempSource = 0; // temp source vertex for addEdge()
- int tempDest = 0; // temp destination vertex for addEdge()
- int source = 0; // source for BFS algorithm
- int destination = 0; // destination for BFS algorithm
- while (fgetc(in) != EOF) { // EOF = end of file, recognized by C
- fscanf(in, "%d", &tempSource);
- fscanf(in, "%d", &tempDest);
- if (tempSource == 0 && tempDest == 0) {
- break;
- }
- addEdge(G, tempSource, tempDest);
- }
- printGraph(out, G);
- fprintf(out, "\n");
- while (fgetc(in) != EOF) { // EOF = end of file, recognized by C
- fscanf(in, "%d", &source);
- fscanf(in, "%d", &destination);
- if (source == 0 && destination == 0) {
- break;
- }
- List L = newList();
- BFS(G, source);
- getPath(L, G, destination);
- if (getDist(G, destination) != INF) {
- fprintf(out, "The distance from %d to %d is %d\n", source, destination, length(L) - 1);
- fprintf(out, "A shortest %d-%d path is: ", source, destination);
- printList(out, L);
- fprintf(out, "\n\n");
- } else {
- fprintf(out, "The distance from %d to %d is infinity\n", source, destination);
- fprintf(out, "No %d-%d path exists\n\n", source, destination);
- }
- freeList(&L);
- }
- freeGraph(&G);
- fclose(in);
- fclose(out);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment