简体   繁体   中英

C Input for Directed Graphs

We are told our input file would be a simple list of numbers:

1 3 4

2 3

3 4

4 1 2

Where the first number is the source node, and the proceeding numbers are it's adjacent nodes.

I am trying to figure out how to best store this. I wanted to firstly initialize a "graph", an array that contains all these nodes. Then upon reading the file, line by line, I would store the root node into the graph array, and then update the node's outlist (adjacent nodes) with the following numbers until we reach the end of the line, repeating this for each line until EOF.

However I'm struggling on how to initialize the graph, do I just assume a certain size and realloc() once the size is hit? Do I read the file first and count the number of lines to find out the size, then re-read the file to store the nodes? Is there any other way?

Here is the code for my data structures:

int initialize (Graph *mygraph, int MaxSize) {
  mygraph->MaxSize = MaxSize;
  mygraph->table = (Node *)malloc(sizeof(Node) * MaxSize);
  return 0;
}

  int insert_node (Graph *mygraph, int n, char *name) {
  mygraph->table[n].name = strdup(name);
  mygraph->table[n].outdegree = 0;
  return 0;
}

  int insert_link (Graph *mygraph, int source, int target) {
  List *newList = (List *)malloc(sizeof(List));
  newList->index = target;
  newList->next = mygraph->table[source].outlist;
  mygraph->table[source].outlist = newList;
  return 0;
}

So upon reading the file,

  1. I initialize the graph.

  2. I read the first number, store it as a new graph node.

  3. I read the next numbers until hitting "\\n", and store these as graph links to the above root node.

  4. I do this for each line until hitting EOF.

As you can see I have no idea what the "MaxSize" until the whole file is read.

Thanks! I'm rather new to C so sorry if I've done anything silly.

You could have some initial guess for MaxSize (eg 8) and grow when needed your data (perhaps by graph->MaxSize += graph->MaxSize/2 ) using realloc , or just by malloc -ing a bigger new chunk, copying the older chunk inside, then free -ing that older chunk). Don't forget to check the successful result of any malloc or calloc or realloc call, they could (rarely) fail.

Notice that I have no idea of how your Graph and Node type is declared (just guessing).

I am assuming and guessing you have declared something like

 typedef struct node_st Node;
 typedef struct graph_st Graph;
 struct node_st {
    char*name; // strdup-ed
    unsigned outdegree;
 };
 struct graph_st {
   unsigned MaxSize;
   Node* table; //calloc-ed, of allocated size MaxSize
 };

So for example your insert_node function might be

void insert_node (Graph *mygraph, int n, char *name) {
  assert (mygraph != NULL);
  assert (n >= 0);
  assert (name != NULL && *name != (char)0);
  unsigned maxsize = mygraph->MaxSize;
  if (maxsize <= n) {
    unsigned newmaxsize = n + maxsize/2 + 1;
    Node* newtable = calloc (newmaxsize, sizeof(Node));
    if (!newtable) 
       perror("growing table in graph"), exit(EXIT_FAILURE);
    for (unsigned i=0; i<maxsize; i++) 
       newtable[i] = mygraph->table[i];
    free (mygraph->table);
    mygraph->table = newtable;
    mygraph->MaxSize = newmaxsize;
  };
  mygraph->table[n].name = strdup(name);
  mygraph->table[n].outdegree = 0;
}

You probably don't need insert_node to return a value (otherwise you won't always return 0). So I made it a void returning function (ie a "procedure" or "routine").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM