Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Created December 23, 2015 02:14
Show Gist options
  • Save ryanmr/b87949000eab2fe22223 to your computer and use it in GitHub Desktop.
Save ryanmr/b87949000eab2fe22223 to your computer and use it in GitHub Desktop.
/*
create_graph
1. sets basic defaults on all nodes
2. finds the root nodes and sets them to READY
3. finds the number of parents of each node
*/
void create_graph(node_t **nodes, int node_count) {
// initially set all to inelgible
int g = 0;
for (; g < node_count; g++) {
nodes[g]->status = INELIGIBLE;
nodes[g]->num_parents = 0;
nodes[g]->num_parents_completed = 0;
}
/*
Begin at node 0, as it is the root node
The loop's length is limited by lowest index'd child of the current element
Then check node 1, and so on, until k < threshold breaks out
*/
int threshold = node_count;
int k = 0;
while (k < threshold) {
int j = 0;
node_t *n = nodes[k];
for (; j < n->num_children; j++) {
if ( n->children[j] < threshold ) {
threshold = n->children[j];
}
}
n->status = READY;
k++;
}
// set the number of parents
// the root nodes are less than threshold (e.g) the ones that
// will run, because they have no parents
int i = threshold;
for (; i < node_count; i++) {
node_t *n = nodes[i];
int parent_count = 0;
int target_id = nodes[i]->id;
int j = 0;
for (; j < i; j++) {
node_t *m = nodes[j];
int h = 0;
for (; h < m->num_children; h++) {
if ( target_id == m->children[h] ) {
parent_count++;
}
}
}
n->num_parents = parent_count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment