END_TEST START_TEST (graphWorldBaseInitializeBTest) { DEBUG ("%s", __FUNCTION__); graphWorldBase (g, GRAPH_POLE_B); fail_unless (graphVertexCount (g) == 1); }
/* and bfs if useQueue == 1 */ static void genericSearch(struct searchInfo *r, int root, int useQueue) { /* queue/stack */ struct queue q; /* edge we are working on */ struct edge cur; /* start with empty q */ /* we need one space per edge */ /* plus one for the fake (root, root) edge */ q.e = malloc(sizeof(*q.e) * (graphEdgeCount(r->graph) + 1)); assert(q.e); q.bottom = q.top = 0; /* push the root */ pushEdge(r->graph, root, root, &q); /* while q.e not empty */ while(q.bottom < q.top) { if(useQueue) { cur = q.e[q.bottom++]; } else { cur = q.e[--q.top]; } /* did we visit sink already? */ if(r->parent[cur.v] != SEARCH_INFO_NULL) continue; /* no */ assert(r->reached < graphVertexCount(r->graph)); r->parent[cur.v] = cur.u; r->time[cur.v] = r->reached; r->preorder[r->reached++] = cur.v; if(cur.u == cur.v) { /* we could avoid this if we were certain SEARCH_INFO_NULL */ /* would never be anything but -1 */ r->depth[cur.v] = 0; } else { r->depth[cur.v] = r->depth[cur.u] + 1; } /* push all outgoing edges */ graphForeach(r->graph, cur.v, pushEdge, &q); } free(q.e); }
END_TEST START_TEST (graphWorldBaseInitializeRGTest) { DEBUG ("%s", __FUNCTION__); graphWorldBase (g, GRAPH_POLE_R | GRAPH_POLE_G); /* fail_unless ( graphRegionCount (g) == 1, "Expecting region count of 1, got %d instead", graphRegionCount (g) ); */ fail_unless (graphVertexCount (g) == 2); fail_unless (graphEdgeCount (g) == 3); }
/* you need to do this before passing it to dfs or bfs */ struct searchInfo * searchInfoCreate(Graph g) { struct searchInfo *s; int n; s = malloc(sizeof(*s)); /* space for struct searchInfo, s is pointer to this space */ assert(s); s->graph = g; s->reached = 0; n = graphVertexCount(g); s->preorder = createEmptyArray(n); s->time = createEmptyArray(n); s->parent = createEmptyArray(n); s->depth = createEmptyArray(n); return s; }
/* returns endpoints of a cycle */ static int * genericSearchModified(struct searchInfo *r, int root, int useQueue) { /* queue/stack */ struct queue q; /* edge we are working on */ struct edge cur; int node; int *cycle; cycle = malloc(sizeof(int) * 2); /* start with empty q */ /* we need one space per edge */ /* plus one for the fake (root, root) edge */ q.e = malloc(sizeof(*q.e) * (graphEdgeCount(r->graph) + 1)); assert(q.e); q.bottom = q.top = 0; /* push the root */ pushEdge(r->graph, root, root, &q); /* while q.e not empty */ while(q.bottom < q.top) { if(useQueue) { cur = q.e[q.bottom++]; } else { cur = q.e[--q.top]; } node = 0; /* did we visit sink already? */ if(r->parent[cur.v] != SEARCH_INFO_NULL) { /* yes. we know that cur.u is the not the parent of cur.v. but is this a cycle? */ while(r->preorder[node] != cur.v) { //if(r->preorder[node] != r->parent[cur.u] && r->preorder[node] != cur.v) { if(graphHasEdge(r->graph, cur.u, r->preorder[node])) { /*printf("cycle found containing %d and %d\n", cur.u, r->preorder[node]);*/ cycle[0] = cur.u; cycle[1] = r->preorder[node]; goto out; } // } node++; } continue; } /* no */ assert(r->reached < graphVertexCount(r->graph)); r->parent[cur.v] = cur.u; r->time[cur.v] = r->reached; r->preorder[r->reached++] = cur.v; if(cur.u == cur.v) { /* we could avoid this if we were certain SEARCH_INFO_NULL */ /* would never be anything but -1 */ r->depth[cur.v] = 0; } else { r->depth[cur.v] = r->depth[cur.u] + 1; } /* push all outgoing edges */ graphForeach(r->graph, cur.v, pushEdge, &q); } out: free(q.e); return cycle; }