/** * Where p is the rewiring probability */ void graph_rewire(graph *g, double p) { int i, j, rewire_vertex; double random; for(i = 0; i < NETWORK_SIZE; i++) { for(j = 0; j < NETWORK_SIZE; j++) { if(g->adjacency_matrix[(i * NETWORK_SIZE) + j] == 1) { random = (double)rand() / (double)RAND_MAX; if(random < p) { do { rewire_vertex = ((double)rand() / (double)RAND_MAX) * NETWORK_SIZE; } while(graph_edge_exists(g, i, rewire_vertex) == 1 && i != rewire_vertex); graph_edge_delete(g, i, j); graph_edge_add(g, i, rewire_vertex); } } } } }
static g_graph mv_cnf_to_graph(mv_cnf input) { register unsigned int ix, iy, iz = 0; g_graph result = graph_new(); for (ix = 0; ix < input->clauses->sz; ix++) { graph_node_add(result, graph_node_new(ix)); } for (ix = 0; ix < input->clauses->sz; ix++) { for (iy = ix + 1; iy < input->clauses->sz; iy++) { if (mv_clause_shares_variables(input->clauses->arr[ix], input->clauses->arr[iy]) > 0) { graph_edge_add(result, graph_edge_new(iz++, ix, iy)); } } } return result; }
int main() { struct graph *g = graph_create(); if (g == NULL) { printf("malloc error\n"); return 1; } int values[] = {0, 1, 2, 3, 4, 5, 6, 7}; int len = sizeof(values)/sizeof(values[0]); struct vertex *vertices[len]; for (int i = 0; i < len; ++i) { vertices[i] = graph_vertex_add(g, values[i]); assert(vertices[i]); printf("Added vertex %p\n", vertices[i]); } graph_edge_add(g, vertices[0], vertices[7], 7); graph_edge_add(g, vertices[0], vertices[5], 5); graph_edge_add(g, vertices[0], vertices[3], 3); graph_edge_add(g, vertices[2], vertices[6], 8); graph_edge_add(g, vertices[5], vertices[4], 9); graph_edge_remove(g, vertices[0], vertices[7]); graph_edge_remove(g, vertices[0], vertices[5]); graph_edge_remove(g, vertices[2], vertices[6]); graph_edge_remove(g, vertices[5], vertices[4]); graph_edge_add(g, vertices[0], vertices[7], 7); graph_vertex_remove(g, vertices[0]); graph_print(g); graph_vertex_remove(g, vertices[1]); graph_vertex_remove(g, vertices[2]); graph_vertex_remove(g, vertices[3]); graph_vertex_remove(g, vertices[4]); graph_vertex_remove(g, vertices[5]); graph_vertex_remove(g, vertices[6]); graph_vertex_remove(g, vertices[7]); assert(graph_vertex_num(g) == 0); graph_destroy(g); return 0; }