/* * graph_neighbor_subgraph - calculate the @subgraph consists of the neighbors of vertex @s * used for calculate the local efficiency. * * @neis: neighbors' id */ int graph_neighbor_subgraph(const graph_t *graph, graph_t *subgraph, vector_int *neis, int s, graph_neimode_t mode) { int vc = graph_vertices_count(graph); graph_neighbors(graph, neis, s, mode); vector_int edges; vector_int_init(&edges, 0); vector_int neis2; vector_int_init(&neis2, 0); int v, w; for (int i = 0; i < vector_int_size(neis); i++) { v = VECTOR(*neis)[i]; if (v == s) continue; graph_neighbors(graph, &neis2, v, GRAPH_OUT); for (int j = 0; j < vector_int_size(&neis2); j++) { w = VECTOR(neis2)[j]; if (w == s || w == v) continue; int wid = vector_int_whereis(neis, w); if (-1 != wid) { vector_int_push_back(&edges, i); vector_int_push_back(&edges, wid); } } } new_graph(subgraph, &edges, vector_int_size(neis), graph_is_directed(graph)); vector_int_destroy(&neis2); vector_int_destroy(&edges); return 0; }
/* * graph_has_multiple */ int graph_has_multiple(const graph_t *graph) { int vc = graph_vertices_count(graph); int ec = graph_edges_count(graph); int dflag = graph_is_directed(graph); if (vc == 0 || ec == 0) return 0; vector_int neis; vector_int_init(&neis, 0); int found = 0, n; for (int i = 0; i < vc && !found; i++) { graph_neighbors(graph, &neis, i, GRAPH_OUT); n = vector_int_size(&neis); for (int j = 1; j < n; j++) { if (VECTOR(neis)[j-1] == VECTOR(neis)[j]) { if (dflag) /* Directed, so this is a real multiple edge */ return 1; else if (VECTOR(neis)[j-1] != i) /* Undirected, but not a loop edge */ return 1; else if (j < n-1 && VECTOR(neis)[j] == VECTOR(neis)[j+1]) /* Undirected, loop edge, multiple times */ return 1; } } } return 0; }
/* * graph_edge */ int graph_edge(const graph_t *graph, int eid, int *from, int *to) { *from = VECTOR(graph->from)[eid]; *to = VECTOR(graph->to)[eid]; if (!graph_is_directed(graph) && *from > *to) { int tmp = *from; *from = *to; *to = tmp; } return 0; }
void _meta(graph_t *g) { uint32_t i; uint16_t nmsgs; char *msg; nmsgs = graph_log_num_msgs(g); printf("num nodes: %u\n", graph_num_nodes(g)); printf("num edges: %u\n", graph_num_edges(g)); printf("directed: %u\n", graph_is_directed(g)); printf("log messages:\n"); for (i = 0; i < nmsgs; i++) { msg = graph_log_get_msg(g, i); printf(" %3u: %s\n", i, msg); } }
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; }