void myestimation(struct graph_t *graph, double *lp_x, unsigned char *x) { unsigned char *x1; long i, j; const unsigned long n = graph->vertex_count; if (!x) return; x1 = (unsigned char *) malloc(n); if (!x1) { return; } /* initialization */ #pragma omp parallel for for (i = 0; i < graph->vertex_count; i++) { if (lp_x[i] >= 1 - myTHRESHOLD1) x[i] = 1; else if (lp_x[i] <= myTHRESHOLD0) x[i] = 0; else x[i] = 2; /* undetermined */ } // Set x[i] to 0 if i has a neighbor of value 1 for (i = 0; i < graph->vertex_count; i++) for (j = 0; j < graph->vertex_count; j++) if( graph_edge(graph, i, j) && (x[j]==1)){ x[i]=0; break; } // Set x[i] to 1 if all it's neighbors are 0 int ind_1; for (i = 0; i < graph->vertex_count; i++){ ind_1 = 1; for (j = 0; j < graph->vertex_count; j++){ if( graph_edge(graph, i, j) && (x[j]!=0)){ ind_1 = 0; break; } } if(ind_1) x[i] = 1; } // Undetermined values for (i = 0; i < graph->vertex_count; i++) if(x[i] == 2){ x[i] = 1; for (j = 0; j < graph->vertex_count; j++) if( graph_edge(graph, i, j)) x[j] = 0; } }
/* * graph_combine_vertices */ int graph_combine_vertices(const graph_t *graph, const vector_int *membership, graph_t *res) { int ec = graph_edges_count(graph); vector_int new_edges; vector_int_init(&new_edges, 2 * ec); int from, to, nef, net; int eid = 0; for (int i = 0; i < ec; i++) { graph_edge(graph, i, &from, &to); nef = VECTOR(*membership)[from]; net = VECTOR(*membership)[to]; if (nef == net || nef < 0 || net < 0) continue; VECTOR(new_edges)[2*eid+0] = nef; VECTOR(new_edges)[2*eid+1] = net; eid++; } eid--; vector_int_resize(&new_edges, 2 * eid); new_graph(res, &new_edges, 0, graph->directed); graph_remove_multi_edges(res); return 0; }
int graph_subgraph(const graph_t *graph, graph_t *subgraph, graph_vs_t vids) { int vc = graph_vertices_count(graph); int ec = graph_edges_count(graph); vector_int vss; vector_int_init(&vss, vc); vector_int_fill(&vss, -1); int i,j,u,v; graph_vit_t vit; graph_vit_create(graph, vids, &vit); int nvc = GRAPH_VIT_SIZE(vit); for (GRAPH_VIT_RESET(vit), i=0; !GRAPH_VIT_END(vit); GRAPH_VIT_NEXT(vit), i++) { int vid = GRAPH_VIT_GET(vit); VECTOR(vss)[vid] = i; } graph_vit_destroy(&vit); int nec = 0; vector_int new_edges; vector_int_init(&new_edges, 2 * ec); for (int eid = 0; eid < ec; eid++) { graph_edge(graph, eid, &i, &j); u = VECTOR(vss)[i]; v = VECTOR(vss)[j]; if (-1 == u || -1 == v) continue; VECTOR(new_edges)[2*nec+0] = u; VECTOR(new_edges)[2*nec+1] = v; nec++; } vector_int_resize(&new_edges, 2 * nec); new_graph(subgraph, &new_edges, nvc, graph_is_directed(graph)); vector_int_destroy(&new_edges); vector_int_destroy(&vss); return 0; }
int main(int argc, char **argv) { // keep track of head vertex *root = malloc(sizeof(*root)); root->nextvertex = NULL; root = NULL; // keep track of last vertext vertex *last = malloc(sizeof(*last)); last->nextvertex = NULL; last = root; graph_add('a', &root, &last); graph_add('b', &root, &last); graph_add('c', &root, &last); graph_add('d', &root, &last); graph_add('e', &root, &last); graph_add('f', &root, &last); graph_add('g', &root, &last); graph_add('h', &root, &last); graph_edge('a', 'b', &root, &last); graph_edge('a', 'c', &root, &last); graph_edge('b', 'c', &root, &last); graph_edge('a', 'd', &root, &last); graph_edge('a', 'e', &root, &last); graph_edge('c', 'd', &root, &last); graph_edge('d', 'e', &root, &last); graph_edge('d', 'f', &root, &last); graph_edge('d', 'g', &root, &last); graph_edge('c', 'h', &root, &last); printf("path between %c and %c exists? %d\n", 'a', 'e', graph_initiate('a', 'e', &root, DFS)); printf("path between %c and %c exists? %d\n", 'd', 'e', graph_initiate('d', 'e', &root, DFS)); printf("path between %c and %c exists? %d\n", 'c', 'e', graph_initiate('c', 'e', &root, DFS)); printf("path between %c and %c exists? %d\n", 'b', 'e', graph_initiate('b', 'e', &root, DFS)); printf("path between %c and %c exists? %d\n", 'b', 'a', graph_initiate('b', 'a', &root, DFS)); printf("path between %c and %c exists? %d\n", 'f', 'a', graph_initiate('f', 'a', &root, DFS)); printf("path between %c and %c exists? %d\n", 'b', 'f', graph_initiate('b', 'f', &root, DFS)); printf("path between %c and %c exists? %d\n", 'd', 'f', graph_initiate('d', 'f', &root, DFS)); printf("path between %c and %c exists? %d\n", 'd', 'g', graph_initiate('d', 'g', &root, DFS)); printf("path between %c and %c exists? %d\n", 'c', 'h', graph_initiate('c', 'h', &root, DFS)); printf("shortest path between all nodes:\n"); printf("BFS from node %c exists? %d\n", 'b', graph_initiate('b', '\0', &root, BFS)); printf("BFS from node %c exists? %d\n", 'a', graph_initiate('a', '\0', &root, BFS)); printf("BFS from node %c exists? %d\n", 'x', graph_initiate('x', '\0', &root, BFS)); printf("BFS from node %c exists? %d\n", 'c', graph_initiate('c', '\0', &root, BFS)); // RESET root and last for euler problem // Note, previous nodes have not been removed, so memory is not really clean!!! root = NULL; last = NULL; // Since dealing with directed graphs, simulating undirected by adding an edge from dest->source // Graph which is euler (i.e., has a euler circuit) graph_add('a', &root, &last); graph_add('b', &root, &last); graph_add('c', &root, &last); graph_add('d', &root, &last); graph_edge('a', 'b', &root, &last); graph_edge('b', 'a', &root, &last); graph_edge('a', 'c', &root, &last); graph_edge('c', 'a', &root, &last); graph_edge('c', 'd', &root, &last); graph_edge('d', 'c', &root, &last); graph_edge('b', 'd', &root, &last); graph_edge('d', 'b', &root, &last); graph_initiate('a', '\0', &root, EULER); // Graph which is semi euler (i.e., has a euler path) graph_add('e', &root, &last); graph_add('f', &root, &last); graph_edge('e', 'f', &root, &last); graph_edge('f', 'e', &root, &last); graph_edge('c', 'e', &root, &last); graph_edge('e', 'c', &root, &last); graph_initiate('a', '\0', &root, EULER); // Case to demonstrate a connected graph, when a vertex has no edges graph_add('z', &root, &last); graph_initiate('a', '\0', &root, EULER); // Graph which is not euler graph_add('g', &root, &last); graph_add('h', &root, &last); graph_edge('g', 'h', &root, &last); graph_edge('h', 'g', &root, &last); graph_initiate('a', '\0', &root, EULER); return 0; }