/*------------------------------------------------------------------- Input/Output routines for graphs. This program was developed for the course MTH 607 - Graph Theory. Released under the GNU public license. (c) Peter Danziger -------------------------------------------------------------------*/ #include #include /* The maximum number of vertices we can handle. Up this for larger graphs. */ #ifndef MAX_N #define MAX_N 100 #endif /*------------------------------------------------------------------- The top level routine is called parse_args() and should be called from main. It will read in a graph file specified on the command line: graph [ []] If no input name is given outputs to stdin, if no output name defaults to stdout It also sets the file handle "file_out" for the output file. The graph is stored internally as a weighted adjacency matrix in the array "graph" This file also contains two low level routines, read_graph and write_graph which read and write graphs to and from the array "graph" to the file pointed to by the handle fhandle. -------------------------------------------------------------------*/ int parse_args(int argc, char *argv[]); int read_graph(FILE *fhandle, int graph[][MAX_N]); void write_graph(FILE *fhandle, int graph[][MAX_N], int v); /*------------------------------------------------------------------- The array "graph" holds the initial graph adjacency[i][j] = weight of edge from i to j, graph[i][j] = 0 means no edge ij -------------------------------------------------------------------*/ /* Note that this should really be done with malloc*/ int graph[MAX_N][MAX_N]; /* The number of vertices */ int n; int source, sink; /* File handles for i/o files */ FILE *file_in, *file_out; char *name[2][2]; /* Reads a graph into array "graph" from file given by handle */ int read_graph(FILE *fhandle, int graph[][MAX_N]) { int i, j; fscanf(fhandle, " %d \n", &n); for(i = 0; i < n; i++) for(j = 0; j < n; j++) if(fscanf(fhandle, " %d ", &(graph[i][j])) != 1) { printf("Error reading input file\n"); exit(-1); } if((i = fscanf(fhandle, " %d %d ", &source, &sink)) != 2) { if(i != 1) source = 0; sink = n - 1; } return 1; } /* Writes v vertex graph stored in array "graph" to the file pointed to by fhandle Uses a format readable by read_graph */ void write_graph(FILE *fhandle, int graph[][MAX_N], int v) { int i, j; fprintf(fhandle, " %d \n", v); for(i = 0; i < v; i++) { for(j = 0; j < v; j++) fprintf(fhandle, " %d ", graph[i][j]); fprintf(fhandle, "\n"); } } /*------------------------------------------------------------------- Parses the command line arguments returns non-zero if graph not read -------------------------------------------------------------------*/ const char *usage = "graph [ []]\nIf no input name is given outputs to stdin, if no output name defaults to stdout\n"; int parse_args(int argc, char *argv[]) { int i; /* Open relavent files */ i = 0; switch(argc) { case 1: /* If no input file is given, inputs from stdin */ file_in = stdin; i++; case 2: /* No output file specified - default to stdout. */ file_out = stdout; i++; case 3: if(i < 2) /* At least one command line argument: -p or input file */ { /* Argument -p (print out name) */ if(*argv[1] == '-') { if(argv[1][1] == 'p') { for(i = 0; i < 2; i++) { if(name[i][0] == NULL || *name[i][0] == 0) continue; printf("%s, %s\n", name[i][0], name[i][1]); } } else printf("Unknown switch %s\n", argv[1]); return 1; } if((file_in = fopen(argv[1],"r")) == NULL) { printf("Error opening read file %s\n", argv[1]); return -1; } } if(!i) /* At least two command line arguments - 2nd is output file */ { if((file_out = fopen(argv[2],"w")) == NULL) { printf("Error opening write file %s\n", argv[2]); return -1; } } break; default: printf("%s", usage); return -1; } /* Read in graph */ read_graph(file_in, graph); return 0; } #if 0 int my_code(); /* Sample main */ int main(int argc, char *argv[]) { int ret_val; if(parse_args(argc, argv)) return -1; /* Your routine goes here */ ret_val = my_code(); /* Write output graph if required, otherwise rem this out */ write_graph(file_out, graph, n); return ret_val; } /*------------------------------------------------------------------- Your code goes here. At this point the graph is in the array "graph" The value returned by this routine will be returned by the program. -------------------------------------------------------------------*/ int my_code() { /* int ret_val; */ printf("source: %d, sink: %d\n", source, sink); return 1; } #endif