code/graph.c
graph_t *read_graph(FILE *infile) {
    char linebuf[MAXLINE];
    int nnode, nedge;
    int i, hid, tid;
    int nid, eid;

    // Read header information
    while (fgets(linebuf, MAXLINE, infile) != NULL) {
	if (!is_comment(linebuf))
	    break;
    }
    if (sscanf(linebuf, "%d %d", &nnode, &nedge) != 2) {
	outmsg("ERROR. Malformed graph file header (line 1)\n");
	return NULL;
    }
    graph_t *g = new_graph(nnode, nedge);
    if (g == NULL)
	return g;

    nid = -1;
    // We're going to add self edges, so eid will keep track of all edges.
    eid = 0;  
    for (i = 0; i < nedge; i++) {
	while (fgets(linebuf, MAXLINE, infile) != NULL) {
	    if (!is_comment(linebuf))
		break;
	}
	if (sscanf(linebuf, "%d %d", &hid, &tid) != 2) {
	    outmsg("Line #%u of graph file malformed\n", i+2);
	    return false;
	}
	if (hid < 0 || hid >= nnode) {
	    outmsg("Invalid head index %d on line %d\n", hid, i+2);
	    return false;
	}
	if (tid < 0 || tid >= nnode) {
	    outmsg("Invalid tail index %d on line %d\n", tid, i+2);
	    return false;
	}
	if (hid < nid) {
	    outmsg("Head index %d on line %d out of order\n", hid, i+2);
	    return false;
	    
	}
	// Starting edges for new node(s)
	while (nid < hid) {
	    nid++;
	    g->neighbor_start[nid] = eid;
	    // Self edge
	    g->neighbor[eid++] = nid;
	}
	g->neighbor[eid++] = tid;
    }
    while (nid < nnode-1) {
	// Fill out any isolated nodes
	nid++;
	g->neighbor[eid++] = nid;
    }
    g->neighbor_start[nnode] = eid;
    outmsg("Loaded graph with %d nodes and %d edges\n", nnode, nedge);
#if DEBUG
    show_graph(g);
#endif
    return g;
}
/* Read in graph file and build graph data structure */
Read in graph file and build graph data structure
Read in graph file and build graph data structure
graph_t *read_graph(FILE *infile) {
    char linebuf[MAXLINE];
    int nnode, nedge;
    int i, hid, tid;
    int nid, eid;
    while (fgets(linebuf, MAXLINE, infile) != NULL) {
	if (!is_comment(linebuf))
	    break;
    }
    if (sscanf(linebuf, "%d %d", &nnode, &nedge) != 2) {
	outmsg("ERROR. Malformed graph file header (line 1)\n");
	return NULL;
    }
    graph_t *g = new_graph(nnode, nedge);
    if (g == NULL)
	return g;
    nid = -1;
    eid = 0;  
    for (i = 0; i < nedge; i++) {
	while (fgets(linebuf, MAXLINE, infile) != NULL) {
	    if (!is_comment(linebuf))
		break;
	}
	if (sscanf(linebuf, "%d %d", &hid, &tid) != 2) {
	    outmsg("Line #%u of graph file malformed\n", i+2);
	    return false;
	}
	if (hid < 0 || hid >= nnode) {
	    outmsg("Invalid head index %d on line %d\n", hid, i+2);
	    return false;
	}
	if (tid < 0 || tid >= nnode) {
	    outmsg("Invalid tail index %d on line %d\n", tid, i+2);
	    return false;
	}
	if (hid < nid) {
	    outmsg("Head index %d on line %d out of order\n", hid, i+2);
	    return false;
	}
	while (nid < hid) {
	    nid++;
	    g->neighbor_start[nid] = eid;
	    g->neighbor[eid++] = nid;
	}
	g->neighbor[eid++] = tid;
    }
    while (nid < nnode-1) {
	nid++;
	g->neighbor[eid++] = nid;
    }
    g->neighbor_start[nnode] = eid;
    outmsg("Loaded graph with %d nodes and %d edges\n", nnode, nedge);
#if DEBUG
    show_graph(g);
#endif
    return g;
}
graph_t * read_graph ( FILE * infile ) { char linebuf [ MAXLINE ] ; int nnode , nedge ; int i , hid , tid ; int nid , eid ; while ( fgets ( linebuf , MAXLINE , infile ) != NULL ) { if ( ! is_comment ( linebuf ) ) break ; } if ( sscanf ( linebuf , " " , & nnode , & nedge ) != 2 ) { outmsg ( " \n " ) ; return NULL ; } graph_t * g = new_graph ( nnode , nedge ) ; if ( g == NULL ) return g ; nid = -1 ; eid = 0 ; for ( i = 0 ; i < nedge ; i ++ ) { while ( fgets ( linebuf , MAXLINE , infile ) != NULL ) { if ( ! is_comment ( linebuf ) ) break ; } if ( sscanf ( linebuf , " " , & hid , & tid ) != 2 ) { outmsg ( " \n " , i + 2 ) ; return false ; } if ( hid < 0 || hid >= nnode ) { outmsg ( " \n " , hid , i + 2 ) ; return false ; } if ( tid < 0 || tid >= nnode ) { outmsg ( " \n " , tid , i + 2 ) ; return false ; } if ( hid < nid ) { outmsg ( " \n " , hid , i + 2 ) ; return false ; } while ( nid < hid ) { nid ++ ; g -> neighbor_start [ nid ] = eid ; g -> neighbor [ eid ++ ] = nid ; } g -> neighbor [ eid ++ ] = tid ; } while ( nid < nnode - 1 ) { nid ++ ; g -> neighbor [ eid ++ ] = nid ; } g -> neighbor_start [ nnode ] = eid ; outmsg ( " \n " , nnode , nedge ) ; #if DEBUG show_graph ( g ) ; #endif return g ; }
Read in graph file and build graph data structure
Read in graph file and build graph data structure
// Read header information // We're going to add self edges, so eid will keep track of all edges. // Starting edges for new node(s) // Self edge // Fill out any isolated nodes