Beispiel #1
0
void test_graph_search_sccs() {
  GRAPH* graph = graph_create();
  graph_add_edge(graph, 6, 0);
  graph_add_edge(graph, 3, 6);
  graph_add_edge(graph, 0, 3);
  graph_add_edge(graph, 8, 6);
  graph_add_edge(graph, 5, 8);
  graph_add_edge(graph, 2, 5);
  graph_add_edge(graph, 8, 2);
  graph_add_edge(graph, 7, 5);
  graph_add_edge(graph, 1, 7);
  graph_add_edge(graph, 4, 1);
  graph_add_edge(graph, 7, 4);

  GRAPH* reverse = graph_reverse(graph);

  GRAPH_SEARCH* graph_search1 = graph_search_create(reverse);
  graph_search_finish_times(reverse, graph_search1);
  graph_destroy(reverse);

  GRAPH_SEARCH* graph_search2 = graph_search_create(graph);
  graph_search_sccs(graph, graph_search2, graph_search1->finish_times);

  graph_search_destroy(graph_search1);
  graph_search_destroy(graph_search2);
  graph_destroy(graph);
}
Beispiel #2
0
graph_t *graph_new_dims(graph_type_t type, uint32_t m, uint32_t n, size_t nnz, bool fixed_rows) {
    graph_t *graph = malloc(sizeof(graph_t));
    graph->m = m;
    graph->fixed_rows = fixed_rows;
    graph->n = n;
    graph->type = type;
    graph->indptr = uint32_array_new_size(m + 1);
    if (graph->indptr == NULL) {
        graph_destroy(graph);
        return NULL;
    }

    if (!fixed_rows) {
        uint32_array_push(graph->indptr, 0);
    }

    if (nnz > 0) {
        graph->indices = uint32_array_new_size(nnz);
    } else {
        graph->indices = uint32_array_new();
    }

    if (graph->indices == NULL) {
        graph_destroy(graph);
        return NULL;
    }

    return graph;
}
Beispiel #3
0
int main()
{
    srandom(time(0));
	graph_t a;
	vector_int v1;
	vector_int_init_value_end(&v1, -1, 1,5, 2,5, 1,2, 2,1, 0,3, 0,2, 3,4, 3,6, 4,6, 6,4, -1,7, -1);
	new_graph(&a, &v1, 0, GRAPH_DIRECTED);
	print_graph_vectors(&a, stdout);
	printf("ec=%d\t",graph_edges_count(&a));
	printf("vc=%d\n",graph_vertices_count(&a));

	int vc = graph_vertices_count(&a);
	vector_int mem;
	vector_int_init(&mem, vc);
	vector_int_fill(&mem, -1);
	vector_int cs;
	vector_int_init(&cs, 0);
	int cc;
	graph_clusters_strong(&a,&mem,&cs,&cc);
	printf("mem:");
	print_vector_int(&mem, stdout);
	printf("<<<combine vertices\n");
	graph_t b;
	graph_combine_vertices(&a, &mem, &b);
	print_graph_vectors(&b, stdout);
	printf("ec=%d\t",graph_edges_count(&b));
	printf("vc=%d\n",graph_vertices_count(&b));

	double re;
	printf(">>>>randomly attack\n");
	re = graph_fault_propagation(&b, 0.3, 0.2, GRAPH_ATK_RANDOM);
	printf("random re=%f\n",re);
	printf(">>>>outgoing based attack\n");
	re = graph_fault_propagation(&b, 0.3, 0.2, GRAPH_ATK_OUTGOING);
	printf("outgoing re=%f\n",re);
	printf(">>>>incoming based attack\n");
	re = graph_fault_propagation(&b, 0.3, 0.2, GRAPH_ATK_INCOMING);
	printf("incoming re=%f\n",re);

	vector_int inf;
	vector_int_init(&inf, 0);
	int cascading_nodes_count = graph_cascading_nodes_count(&b, &inf, 0.3, 0.2, GRAPH_ATK_RANDOM);
	assert(cascading_nodes_count == vector_int_sum(&inf));
	vector_int_destroy(&inf);

	vector_int_destroy(&mem);
	vector_int_destroy(&cs);
	vector_int_destroy(&v1);
	graph_destroy(&b);
	graph_destroy(&a);
	return 0;
}
Beispiel #4
0
int main(void) {
    /* read graph from stdin */
    graph_t graph = graph_from_file(stdin);
    assert(graph != NULL);
    /* run kruskal */
    graph_t mst = kruskal(graph);
    /* dump graph */
    graph_dump(mst, stdout);
    /* dump total weight */
    printf("\n# MST : %u\n", mst_total_weight(mst));
    /* destroy both graphs */
    graph = graph_destroy(graph);
    mst = graph_destroy(mst);
}
Beispiel #5
0
graph_t *graph_read(FILE *f) {
    graph_t *g = malloc(sizeof(graph_t));
    if (g == NULL) return NULL;

    g->indptr = NULL;
    g->indices = NULL;

    if (!file_read_uint32(f, &g->m) ||
        !file_read_uint32(f, &g->n) ||
        !file_read_uint8(f, (uint8_t *)&g->fixed_rows)) {
        goto exit_graph_allocated;
    }

    uint64_t len_indptr;

    if (!file_read_uint64(f, &len_indptr)) {
        goto exit_graph_allocated;
    }

    uint32_array *indptr = uint32_array_new_size(len_indptr);
    if (indptr == NULL) {
        goto exit_graph_allocated;
    }

    for (int i = 0; i < len_indptr; i++) {
        if (!file_read_uint32(f, indptr->a + i)) {
            goto exit_graph_allocated;
        }
    }
    indptr->n = (size_t)len_indptr;

    g->indptr = indptr;

    uint64_t len_indices;

    if (!file_read_uint64(f, &len_indices)) {
        goto exit_graph_allocated;
    }

    uint32_array *indices = uint32_array_new_size(len_indices);
    if (indices == NULL) {
        goto exit_graph_allocated;
    }

    for (int i = 0; i < len_indices; i++) {
        if (!file_read_uint32(f, indices->a + i)) {
            goto exit_graph_allocated;
        }
    }

    indices->n = (size_t)len_indices;

    g->indices = indices;
    
    return g;

exit_graph_allocated:
    graph_destroy(g);
    return NULL;
}
Beispiel #6
0
int main(int argc, char **args)
{
	s_graph graph;
	graph_init(&graph, 5);

	for (int i = 0; i < 5; i++)
	{
		graph_set_vertex(&graph, i, i);
	}

	graph_set_arccell(&graph, 0, 1, 15);
	graph_set_arccell(&graph, 1, 4, 71);
	graph_set_arccell(&graph, 1, 3, 23);
	graph_set_arccell(&graph, 3, 4, 42);
	graph_set_arccell(&graph, 2, 4, 36);
	graph_set_arccell(&graph, 2, 0, 27);
	graph_set_arccell(&graph, 3, 3, 61);
	graph_set_arccell(&graph, 2, 1, 92);

	graph_display(&graph);

	graph_destroy(&graph);

	return 0;
}
Beispiel #7
0
int main(int argc, char **args)
{
	s_graph graph;
	graph_init(&graph, 6);

	for (int i = 0; i < 6; i++)
	{
		graph_set_vertex(&graph, i, i);
	}

	graph_set_arccell(&graph, 0, 2, 10);
	graph_set_arccell(&graph, 0, 4, 30);
	graph_set_arccell(&graph, 0, 5, 100);
	graph_set_arccell(&graph, 1, 2, 5);
	graph_set_arccell(&graph, 2, 3, 50);
	graph_set_arccell(&graph, 3, 5, 10);
	graph_set_arccell(&graph, 4, 3, 20);
	graph_set_arccell(&graph, 4, 5, 60);

	graph_display(&graph);

	graph_shortest_path(&graph);

	graph_destroy(&graph);

	return 0;
}
Beispiel #8
0
void test_graph_search_finish_times() {
  GRAPH* graph = graph_create();
  graph_add_edge(graph, 0, 6);
  graph_add_edge(graph, 6, 3);
  graph_add_edge(graph, 3, 0);
  graph_add_edge(graph, 6, 8);
  graph_add_edge(graph, 8, 5);
  graph_add_edge(graph, 5, 2);
  graph_add_edge(graph, 2, 8);
  graph_add_edge(graph, 5, 7);
  graph_add_edge(graph, 7, 1);
  graph_add_edge(graph, 1, 4);
  graph_add_edge(graph, 4, 7);

  GRAPH_SEARCH* graph_search = graph_search_create(graph);
  graph_search_finish_times(graph, graph_search);

  assert(6 == graph_search->finish_times[0]);
  assert(3 == graph_search->finish_times[1]);
  assert(0 == graph_search->finish_times[2]);
  assert(8 == graph_search->finish_times[3]);
  assert(5 == graph_search->finish_times[4]);
  assert(7 == graph_search->finish_times[5]);
  assert(1 == graph_search->finish_times[6]);
  assert(4 == graph_search->finish_times[7]);
  assert(2 == graph_search->finish_times[8]);

  graph_destroy(graph);
  graph_search_destroy(graph_search);
}
Beispiel #9
0
int g_main(int argc, char ** argv)
{
    graph_t * g = adj_list_read<vex_t, weight_t>(cin, DINET);
    cout << g << endl;
    floyd(g);
    graph_destroy(g);
    return 0;
}
Beispiel #10
0
int g_main(int argc, char ** argv)
{
    graph_t * g = adj_list_read<char>(cin, UNDIGRAPH);
    cout << g << endl;
    cout << "dfs: " << endl;
    dfs(g);
    graph_destroy(g);
    return 0;
}
Beispiel #11
0
int g_main(int argc, char ** argv)
{
    graph_t * g = adj_list_read<char,weight_t>(cin, UNDINET);
    cout << g << endl;

    kruskal(g);
    graph_destroy(g);
    return 0;
}
Beispiel #12
0
int
main ()
{
    int i, data;
    int v_id[V_COUNT];
    DfsVertex *dfs_vs[V_COUNT];

    Graph graph;
    DfsVertex *dfs_vertex;
    List ordered;
    ListElmt *element;

    graph_init (&graph, &match, NULL);

    for (i = 0; i < V_COUNT; i++)
    {
        v_id[i] = i;
        if ((dfs_vertex = (DfsVertex *) malloc (sizeof(DfsVertex))) == NULL)
            return -1;
        dfs_vertex->data = &v_id[i];
        dfs_vertex->color = white;
        dfs_vs[i] = dfs_vertex;
        graph_ins_vertex (&graph, (void *) dfs_vertex);
        /* printf ("vertex[%d] addr=%d\n", i, (void *) &vertex[i]);  */
    }

    printf ("graph vcount=%d\n", graph_vcount (&graph));

    /* Graph as in figure 11.8 Network hops.  */
    /* graph_ins_edge (&graph, (void *) dfs_vs[0], (void *) dfs_vs[1]);  */
    graph_ins_edge (&graph, (void *) dfs_vs[0], (void *) dfs_vs[2]);
    graph_ins_edge (&graph, (void *) dfs_vs[2], (void *) dfs_vs[1]);
    graph_ins_edge (&graph, (void *) dfs_vs[1], (void *) dfs_vs[3]);
    /* graph_ins_edge (&graph, (void *) dfs_vs[2], (void *) dfs_vs[4]);  */
    graph_ins_edge (&graph, (void *) dfs_vs[3], (void *) dfs_vs[4]);
    graph_ins_edge (&graph, (void *) dfs_vs[4], (void *) dfs_vs[5]);
    graph_ins_edge (&graph, (void *) dfs_vs[1], (void *) dfs_vs[6]);

    printf ("graph ecount=%d\n", graph_ecount (&graph));

    dfs (&graph, &ordered);
    printf ("size of ordered list=%d\n", list_size (&ordered));

    for (element = list_head (&ordered); element != NULL;
         element = list_next (element))
    {
        dfs_vertex = (list_data (element));
        printf ("vertex id=%d,\tcolour=%d\n",
                *(int *) dfs_vertex->data,
                (int) dfs_vertex->color);
    }

    list_destroy (&ordered);
    graph_destroy (&graph);
    return 0;
}
Beispiel #13
0
Datei: map.c Projekt: 91he/Test
int main(){
	/*初始化*/
	Graph *graph = graph_init();

	/*struct Path *add_edge(struct Graph *graph, int from, int to, int dut);
	 *from&to:一条边的两个顶点。from为起点,to为终点。from和to可为任意整数.
	 *dut:边的权值,不可大于INF即16777216。
	 */
	add_edge(graph, 1, 2, 7);
	add_edge(graph, 2, 1, 7);
	add_edge(graph, 1, 3, 9);
	add_edge(graph, 3, 1, 9);
	add_edge(graph, 1, 6, 14);
	add_edge(graph, 6, 1, 14);
	add_edge(graph, 2, 3, 10);
	add_edge(graph, 3, 2, 10);
	add_edge(graph, 3, 6, 2);
	add_edge(graph, 6, 3, 2);
	add_edge(graph, 3, 4, 11);
	add_edge(graph, 4, 3, 11);
	add_edge(graph, 5, 6, 9);
	add_edge(graph, 6, 5, 9);
	add_edge(graph, 4, 5, 6);
	add_edge(graph, 5, 4, 6);
	add_edge(graph, 4, 7, 8);
	add_edge(graph, 7, 4, 8);
	add_edge(graph, 6, 7, 4);
	add_edge(graph, 7, 6, 4);
	add_edge(graph, 5, 7, 2);
	add_edge(graph, 7, 5, 2);

	/*
	 *road_min(struct Graph *grap, int from, int to);
	 *返回from--->to之间的最短路径————一个单向链表。
	 */
	int from = 1;
	int to = 5;

	Path *path = road_min(graph, from, to);
	Path *tmp;
	
	while(path){
		tmp = path->next;
		if(tmp)
			printf("%d->", path->node->id);
		else
			printf("%d->", path->node->id);
		path = tmp;
	}
	printf("%d\n", to);

	/*销毁*/
	graph_destroy(graph);

	return 0;
}
Beispiel #14
0
void enddown(){
	graph_destroy(gp);
	free(gp);
	gp = NULL;

	for(int i=0; i<10; i++){
		free(u[i]);
		u[i] = NULL;
	}
}
Beispiel #15
0
void test_graph_search_create() {
  GRAPH* graph = graph_create();
  graph_add_edge(graph, 0, 1);
  graph_add_edge(graph, 0, 2);
  graph_add_edge(graph, 1, 2);
  graph_add_edge(graph, 2, 1);

  GRAPH_SEARCH* graph_search = graph_search_create(graph);
  graph_destroy(graph);
}
Beispiel #16
0
int main(void) {
    /* read graph from stdin */
    graph_t graph = graph_from_file(stdin);
    assert(graph != NULL);
    /* run kruskal */
    graph_t mst = kruskal(graph);
    /* dump graph */
    graph_dump(mst, stdout);
    /* dump total weight */
    printf("\n# MST : %u\n", mst_total_weight(mst));
    /* dump whether has a cycle */
    if (graph_has_cycle(mst)) {
        printf("# Has a cycle: YES\n");
    } else {
        printf("# Has a cycle: NO\n");
    }
    /* dump total connected components */
    printf("# Connected components: %u\n", graph_connected_components(mst));
    /* destroy both graphs */
    graph = graph_destroy(graph);
    mst = graph_destroy(mst);
}
Beispiel #17
0
int main(int argc, char *argv[]) {
  struct graph *g = graph_new();
  if (g) {
    graph_join(g, 12, 34);
    graph_join(g, 34, 12);
    graph_join(g, 11, 9);
    graph_join(g, 9, 100);
    graph_join(g, 25, 89);
    graph_join(g, 100, 100);
    graph_dump(g);
    graph_destroy(g);
  }

  return 0;
}
Beispiel #18
0
int main()
{
    unsigned int iseed = (unsigned int)time(NULL);
    srand(iseed);

    graph g1;
    graph_init(&g1);

    graph_init_infectious(&g1);
    graph_init_ring(&g1, 1);
    graph_rewire(&g1, 0.05);

    //graph_print_adjacency(&g1);

    graph_destroy(&g1);
    return 0;
}
Beispiel #19
0
int main(int argc, char **argv)
{
    int width, height;
    double colour[3];

    struct wav_file *wav;
    float samples[CHUNK_SIZE];
    int sample_count;

    struct graph *gr;
    cairo_surface_t *surface;


    /* Validate and parse command-line arguments */
    if(parse_arguments(argc, argv, &width, &height, colour) != 0)
        return 1;

    /* Open input file */
    if( !(wav = wav_open(argv[1])))
    {
        fprintf(stderr, "Unable to open input audio file %s\n", argv[1]);
        return 1;
    }

    /* Buffer all samples from input file. */
    gr = graph_init();
    while((sample_count = wav_read_samples(wav, samples, CHUNK_SIZE)) > 0)
        graph_buffer_samples(gr, samples, sample_count);

    /* Close input file */
    wav_close(wav);

    /* Draw graph and output to PNG file using Cairo */
    surface = graph_draw(gr, width, height, colour);
    if(cairo_surface_write_to_png(surface, argv[2]) != CAIRO_STATUS_SUCCESS)
    {
        fprintf(stderr, "Error writing graph to PNG file\n");
        return 1;
    }

    graph_surface_destroy(surface);
    graph_destroy(gr);

    return 0;
}
Beispiel #20
0
int main(int argc, char **argv){
	Graph g;
	int i;
	int j;
	
	g = graph_create(TEST_SIZE);
	assert(graph_vertex_count(g) == TEST_SIZE);
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			assert(graph_has_edge(g, i, j) == 0);
		}
	}
	
	for (i = 0; i < TEST_SIZE; i++){
		assert(graph_out_degree(g, i) == 0);
		graph_foreach(g, i, match_sink, 0);
	}
	
	assert(graph_edge_count(g) == 0);
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			if (i < j){
				graph_add_edge(g, i, j);
			}
		}
	}
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			assert(graph_has_edge(g, i, j) == (i < j));
		}
	}
	
	assert(graph_edge_count(g) == (TEST_SIZE*(TEST_SIZE-1)/2));
	
	graph2dot(g);
	
	graph_destroy(g);
	
	return 0;
}
Beispiel #21
0
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;
}
Beispiel #22
0
Datei: tnd.c Projekt: evatux/kaa
int tnd(TMatrix_DCSR *matr, real threshold)
{
    TWGraph gr;
    int *perm, *invp;
    int err = 0;

#ifdef _DEBUG_LEVEL_0
printf("[debug(0)]{tnd}: graph_builder\n");
#endif
    err = build_graph(&gr, matr);
    if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_builder failed", err);

#ifdef _DEBUG_LEVEL_0
printf("[debug(0)]{tnd}: find_permutation\n");
#endif
    err = find_permutation(&gr,&perm, &invp, threshold);    // !!!REORDERING!!!
    if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: find_permutation failed", err);

#ifdef _DEBUG_LEVEL_0
printf("[debug(0)]{tnd}: graph_reoder\n");
#endif
    err = graph_reorder(&gr, perm, invp);
    if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_reorder failed", err);

    if (perm) free(perm);
    if (invp) free(invp);

    err = graph_last_stage_reorder(&gr, threshold);
    if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_last_stage_reorder failed", err);

#ifdef _DEBUG_LEVEL_0
printf("[debug(0)]{tnd}: matrix_builder\n");
#endif
    err = build_matrix(&gr, matr, 0);
    if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: matrix_builder failed", err);

    graph_destroy(&gr);

    return ERROR_NO_ERROR;
}
Beispiel #23
0
int main(int argc, char *argv[]) {
    graph_t *graph;
    int i, n, *dist, *parent;

    graph = graph_create(NUM_ELEM);
    graph_add_weighted_edge(graph, 0, 9, 200);
    graph_add_edge(graph, 0, 2);
    graph_add_edge(graph, 0, 4);
    graph_add_edge(graph, 0, 6);
    graph_add_edge(graph, 2, 4);
    graph_add_edge(graph, 2, 7);
    graph_add_edge(graph, 7, 9);
    printf("\nAll edges: \n");
    graph_print_edges(graph);

    printf("\nAll edges from 0: \n");
    graph_foreach_weighted(graph, 0, &graph_print_edge_weight, NULL);

    printf("\nDijkstra: \n");
    n = graph_vertex_count(graph);
    dist = malloc(sizeof(int) * n);
    parent = malloc(sizeof(int) * n);

    dijkstra(graph, 0, dist, parent);

    for (i=0; i<n; i++)
        if (dist[i] == INT_MAX)
            printf(" no ");
        else
            printf("%3d ", dist[i]);
    printf("\n");
    for (i=0; i<n; i++)
        printf("%3d ", parent[i]);
    printf("\n");

    graph_destroy(graph);

    return EXIT_SUCCESS;
}
Beispiel #24
0
int main(int argc, char** argv) {
  graph_p g = graph_init(8);
  edge_t e;
  e.v = 1;
  e.w = 3;

  printf("inserting edge\n");
  graph_insert_edge(g, e);
  graph_print(g);

  /*
  edge_t edges[8 * 7 / 2];
  printf("edges %d\n", graph_edges(g, edges));
  */

  printf("removing edge\n");
  graph_remove_edge(g, e);
  graph_print(g);

  graph_destroy(g);
  return EXIT_SUCCESS;
}
Beispiel #25
0
static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
    size_t *gl_array_num)
{
  size_t i;

  if ((gl_array == NULL) || (gl_array_num == NULL))
    return;

#define ARRAY_PTR  (*gl_array)
#define ARRAY_SIZE (*gl_array_num)

  for (i = 0; i < ARRAY_SIZE; i++)
  {
    graph_destroy (ARRAY_PTR[i]);
    ARRAY_PTR[i] = NULL;
  }
  free (ARRAY_PTR);
  ARRAY_PTR = NULL;
  ARRAY_SIZE = 0;

#undef ARRAY_SIZE
#undef ARRAY_PTR
} /* }}} void gl_destroy */
Beispiel #26
0
/*
 * Take a grid graph and make it into a maze. 
 */
void mazeify(grid_t *grid) {
    graph_t *subgraph;
    int num_additional_edges = grid->graph->num_edges / 100; 
    int i, e;

    /* Construct minimum spanning tree */
    prim(grid->graph);

    /* Select additional edges at random to create imperfect maze */
    for (i = 0; i < num_additional_edges; ++i) {
        e = rand() % grid->graph->num_edges;
        grid->graph->edges[e].selected = 1;
    }

    /* Select both directions of each undirected edge */
    /* (Note: Parallelization of this step requires some thought.) */
    graph_select_reverse_edges(grid->graph);

    /* Replace the graph with the maze */
    subgraph = graph_subgraph(grid->graph);
    graph_destroy(grid->graph);
    grid->graph = subgraph;
}
Beispiel #27
0
cmph_t *bmz8_new(cmph_config_t *mph, double c)
{
	cmph_t *mphf = NULL;
	bmz8_data_t *bmz8f = NULL;
	cmph_uint8 i;
	cmph_uint8 iterations;
	cmph_uint8 iterations_map = 20;
	cmph_uint8 *used_edges = NULL;
	cmph_uint8 restart_mapping = 0;
	cmph_uint8 * visited = NULL;
	bmz8_config_data_t *bmz8 = (bmz8_config_data_t *)mph->data;

	if (mph->key_source->nkeys >= 256)
	{
		if (mph->verbosity) fprintf(stderr, "The number of keys in BMZ8 must be lower than 256.\n");
		return NULL;
	}
	if (c == 0) c = 1.15; // validating restrictions over parameter c.
	DEBUGP("c: %f\n", c);
	bmz8->m = (cmph_uint8) mph->key_source->nkeys;
	bmz8->n = (cmph_uint8) ceil(c * mph->key_source->nkeys);
	DEBUGP("m (edges): %u n (vertices): %u c: %f\n", bmz8->m, bmz8->n, c);
	bmz8->graph = graph_new(bmz8->n, bmz8->m);
	DEBUGP("Created graph\n");

	bmz8->hashes = (hash_state_t **)malloc(sizeof(hash_state_t *)*3);
	for(i = 0; i < 3; ++i) bmz8->hashes[i] = NULL;

	do
	{
	  // Mapping step
	  cmph_uint8 biggest_g_value = 0;
	  cmph_uint8 biggest_edge_value = 1;
	  iterations = 100;
	  if (mph->verbosity)
	  {
		fprintf(stderr, "Entering mapping step for mph creation of %u keys with graph sized %u\n", bmz8->m, bmz8->n);
	  }
	  while(1)
	  {
		int ok;
		DEBUGP("hash function 1\n");
		bmz8->hashes[0] = hash_state_new(bmz8->hashfuncs[0], bmz8->n);
		DEBUGP("hash function 2\n");
		bmz8->hashes[1] = hash_state_new(bmz8->hashfuncs[1], bmz8->n);
		DEBUGP("Generating edges\n");
		ok = bmz8_gen_edges(mph);
		if (!ok)
		{
			--iterations;
			hash_state_destroy(bmz8->hashes[0]);
			bmz8->hashes[0] = NULL;
			hash_state_destroy(bmz8->hashes[1]);
			bmz8->hashes[1] = NULL;
			DEBUGP("%u iterations remaining\n", iterations);
			if (mph->verbosity)
			{
				fprintf(stderr, "simple graph creation failure - %u iterations remaining\n", iterations);
			}
			if (iterations == 0) break;
		}
		else break;
	  }
	  if (iterations == 0)
	  {
		graph_destroy(bmz8->graph);
		return NULL;
	  }

	  // Ordering step
	  if (mph->verbosity)
	  {
		fprintf(stderr, "Starting ordering step\n");
	  }

	  graph_obtain_critical_nodes(bmz8->graph);

	  // Searching step
	  if (mph->verbosity)
	  {
		fprintf(stderr, "Starting Searching step.\n");
		fprintf(stderr, "\tTraversing critical vertices.\n");
	  }
	  DEBUGP("Searching step\n");
	  visited = (cmph_uint8 *)malloc((size_t)bmz8->n/8 + 1);
	  memset(visited, 0, (size_t)bmz8->n/8 + 1);
	  used_edges = (cmph_uint8 *)malloc((size_t)bmz8->m/8 + 1);
	  memset(used_edges, 0, (size_t)bmz8->m/8 + 1);
	  free(bmz8->g);
	  bmz8->g = (cmph_uint8 *)calloc((size_t)bmz8->n, sizeof(cmph_uint8));
	  assert(bmz8->g);
	  for (i = 0; i < bmz8->n; ++i) // critical nodes
	  {
                if (graph_node_is_critical(bmz8->graph, i) && (!GETBIT(visited,i)))
		{
		  if(c > 1.14) restart_mapping = bmz8_traverse_critical_nodes(bmz8, i, &biggest_g_value, &biggest_edge_value, used_edges, visited);
		  else restart_mapping = bmz8_traverse_critical_nodes_heuristic(bmz8, i, &biggest_g_value, &biggest_edge_value, used_edges, visited);
		  if(restart_mapping) break;
		}
	  }
	  if(!restart_mapping)
	  {
	        if (mph->verbosity)
	        {
		  fprintf(stderr, "\tTraversing non critical vertices.\n");
		}
		bmz8_traverse_non_critical_nodes(bmz8, used_edges, visited); // non_critical_nodes
	  }
	  else
	  {
 	        iterations_map--;
		if (mph->verbosity) fprintf(stderr, "Restarting mapping step. %u iterations remaining.\n", iterations_map);
	  }

	  free(used_edges);
	  free(visited);

	}while(restart_mapping && iterations_map > 0);
	graph_destroy(bmz8->graph);
	bmz8->graph = NULL;
	if (iterations_map == 0)
	{
		return NULL;
	}
	mphf = (cmph_t *)malloc(sizeof(cmph_t));
	mphf->algo = mph->algo;
	bmz8f = (bmz8_data_t *)malloc(sizeof(bmz8_data_t));
	bmz8f->g = bmz8->g;
	bmz8->g = NULL; //transfer memory ownership
	bmz8f->hashes = bmz8->hashes;
	bmz8->hashes = NULL; //transfer memory ownership
	bmz8f->n = bmz8->n;
	bmz8f->m = bmz8->m;
	mphf->data = bmz8f;
	mphf->size = bmz8->m;
	DEBUGP("Successfully generated minimal perfect hash\n");
	if (mph->verbosity)
	{
		fprintf(stderr, "Successfully generated minimal perfect hash function\n");
	}
	return mphf;
}
Beispiel #28
0
int main(int argc, char **argv)
{
	int n, a, b, choice, ris, i, j;
	int *path;
	char tmp1[MAX_STR], tmp2[MAX_STR];
	Hash_table ht;
	Graph g;
	FILE *fp;

	if(argc < 2){
		fprintf(stderr, "Parameters error!\nUsage: %s <input file>\n", argv[0]);
		return -1;
	}

	if((fp=fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "Can't open file %s\n", argv[1]);
		return -2;
	}

	fscanf(fp, "%d", &n);
	ht = hash_table_init(n);
	g  = graph_init(n);

	while(fscanf(fp, "%s %s", tmp1, tmp2) == 2)
	{
		if((a = hash_table_get(ht, tmp1)) == -1)
			a = hash_table_insert(ht, tmp1);
		if((b = hash_table_get(ht, tmp2)) == -1)
			b = hash_table_insert(ht, tmp2);
		graph_insert(g, create_edge(a,b));
	}
	fclose(fp);

	printf( "===== G R A P H =====\n"
		"1 - Shortest simple path between two nodes\n"
		"2 - Longest simple path between two nodes\n"
		"3 - Number of all simple paths between two nodes\n"
		"4 - Show strong connected nodes\n"
		"5 - Show edges to strong connect the graph\n"
		"6 - Exit\n");

	do{
		printf("\nChoice: ");
		scanf("%d", &choice);
		switch(choice)
		{
			case 1:
				printf("Start node: ");
				scanf("%s", tmp1);
				printf("End node:   ");
				scanf("%s", tmp2);
				if(hash_table_get(ht, tmp1) != -1 && hash_table_get(ht, tmp2) != -1)
				{
					ris = graph_get_shortest_path(g, hash_table_get(ht, tmp1), hash_table_get(ht, tmp2), &path);
					if(ris > 0)
					{
						printf("\nDistance = %d\n\nPath:\n", ris);
						for(i=0; i<=ris; i++)
							printf("%s\n", hash_table_get_name(ht, path[i]));
						free(path);
					}
					else
						printf("There is no path between these two nodes\n");
				}
				else
					printf("Error, inexistent nodes\n");
			break;

			case 2:
				printf("Start node: ");
				scanf("%s", tmp1);
				printf("End node:   ");
				scanf("%s", tmp2);
				if(hash_table_get(ht, tmp1) != -1 && hash_table_get(ht, tmp2) != -1)
				{
					ris = graph_get_longest_path(g, hash_table_get(ht, tmp1), hash_table_get(ht, tmp2), &path);
					if(ris > 0)
					{
						printf("\nDistance = %d\n\nPath:\n", ris);
						for(i=0; i<=ris; i++)
							printf("%s\n", hash_table_get_name(ht, path[i]));
						free(path);
					}
					else
						printf("There is no path between these two nodes\n");
				} 
				else
					printf("Error, inexistent nodes\n");
			break;
			
			case 3:
				printf("Start node: ");
				scanf("%s", tmp1);
				printf("End node:   ");
				scanf("%s", tmp2);
				if(hash_table_get(ht, tmp1) != -1 && hash_table_get(ht, tmp2) != -1)
					printf("Number of simple paths: %d\n",
							graph_number_of_simple_path(g, hash_table_get(ht, tmp1), hash_table_get(ht, tmp2)));	
				else
					printf("Error, inexistent nodes\n");
			break;
			case 4:
				path = graph_get_scc(g);
				//for every scc group
				for(i=0; i<n; i++)
				{
					a = 0;	//counter
					for(j=0; j<n; j++)
						if(path[j] == i)
						{
							printf("%s\n", hash_table_get_name(ht, j));
							a++;
						}
					if(a != 0)
						printf("============\n");
					else
						break;
				}
			break;
		}
	}while(choice != 6);

	hash_table_destroy(ht);
	graph_destroy(g);

	return 0;
}
Beispiel #29
0
int main(int argc, char **args)
{
	//邻接表
	s_graph graph;
	graph_init(&graph, 13, &visit_int, &visit_int);

	//顶点数据项
	int *t0 = (int *) malloc(sizeof(int));
	int *t1 = (int *) malloc(sizeof(int));
	int *t2 = (int *) malloc(sizeof(int));
	int *t3 = (int *) malloc(sizeof(int));
	int *t4 = (int *) malloc(sizeof(int));
	int *t5 = (int *) malloc(sizeof(int));
	int *t6 = (int *) malloc(sizeof(int));
	int *t7 = (int *) malloc(sizeof(int));
	int *t8 = (int *) malloc(sizeof(int));
	int *t9 = (int *) malloc(sizeof(int));
	int *t10 = (int *) malloc(sizeof(int));
	int *t11 = (int *) malloc(sizeof(int));
	int *t12 = (int *) malloc(sizeof(int));
	//顶点数据
	*t0 = 0;
	*t1 = 1;
	*t2 = 2;
	*t3 = 3;
	*t4 = 4;
	*t5 = 5;
	*t6 = 6;
	*t7 = 7;
	*t8 = 8;
	*t9 = 9;
	*t10 = 10;
	*t11 = 11;
	*t12 = 12;

	//设置顶点数据
	graph_set_vertex(&graph, 0, t0);
	graph_set_vertex(&graph, 1, t1);
	graph_set_vertex(&graph, 2, t2);
	graph_set_vertex(&graph, 3, t3);
	graph_set_vertex(&graph, 4, t4);
	graph_set_vertex(&graph, 5, t5);
	graph_set_vertex(&graph, 6, t6);
	graph_set_vertex(&graph, 7, t7);
	graph_set_vertex(&graph, 8, t8);
	graph_set_vertex(&graph, 9, t9);
	graph_set_vertex(&graph, 10, t10);
	graph_set_vertex(&graph, 11, t11);
	graph_set_vertex(&graph, 12, t12);

	//插入边数据
	graph_insert_arccell(&graph, 0, 1, 0, null);
	graph_insert_arccell(&graph, 0, 2, 0, null);
	graph_insert_arccell(&graph, 1, 3, 0, null);
	graph_insert_arccell(&graph, 1, 4, 0, null);
	graph_insert_arccell(&graph, 2, 5, 0, null);
	graph_insert_arccell(&graph, 2, 6, 0, null);
	graph_insert_arccell(&graph, 3, 4, 0, null);
	graph_insert_arccell(&graph, 3, 7, 0, null);
	graph_insert_arccell(&graph, 5, 8, 0, null);
	graph_insert_arccell(&graph, 6, 9, 0, null);
	graph_insert_arccell(&graph, 6, 10, 0, null);
	graph_insert_arccell(&graph, 8, 11, 0, null);
	graph_insert_arccell(&graph, 10, 11, 0, null);
	graph_insert_arccell(&graph, 10, 12, 0, null);

	//显示顶点和边的关系
	graph_visit(&graph);

	//深度优先遍历图
	graph_depth_first_search(&graph);

	//销毁邻接表
	graph_destroy(&graph);

	return 0;
}
Beispiel #30
0
int main(int argc, char *argv[])
{
    /* setup */
    char *path = "./data/ct/";
	char *path2 = "/home/gyc/Sources/linux.doc/kernel/";
	vector_char str;
	vector_char_init(&str,100);
	VECTOR(str)[0] = '\0';

    SetupLexer();
    dic_setup();
    struct dictionary *dict = new_dictionary(10000);

    graph_t lkn;
    vector_int edges;
    catch_function_call_dir(path, dict, &edges);
    printf("capacity=%d,size=%d\n",dict_capacity(dict), dict_size(dict));
    new_graph(&lkn, &edges, 0, GRAPH_DIRECTED);

	struct dictionary *filedict = new_dictionary(4);
	vector_funcP flist;
	vector_funcP_init_(&flist, dict_size(dict));
	get_function_filename(path2, dict, filedict, &flist);
    printf("filedict: capacity=%d,size=%d\n",dict_capacity(filedict), dict_size(filedict));

	/* reciprocal */
	printf("reciprocal = %f \n", graph_reciprocal(&lkn));
	vector_double res;
	vector_double_init(&res, 0);
	graph_betweenness(&lkn, &res, graph_vss_all(), GRAPH_DIRECTED);
	printf("betweenness directed:"); print_vector_double(&(res),stdout);
	vector_double_destroy(&res);

	/* degree */
    graph_degree(&lkn, &edges, graph_vss_all(), GRAPH_OUT, GRAPH_NO_LOOPS);
    printf(">>>out, no loops");
	int min, max, sum;
	double ave;
	graph_degree_minmax_avesum(&lkn, &min, &max, &ave, &sum, GRAPH_OUT, GRAPH_NO_LOOPS);
	printf("minout=%d\nmaxout=%d\nsumout=%d\naveout=%f\n",min,max,sum,ave);
	graph_degree_minmax_avesum(&lkn, &min, &max, &ave, &sum, GRAPH_IN, GRAPH_NO_LOOPS);
	printf("minin=%d\nmaxin=%d\nsumin=%d\navein=%f\n",min,max,sum,ave);

	/* fast community */
	graph_reverse(&lkn);
	vector_int v1;
	vector_int_init(&v1,0);
	int ncom = 0;
	double modularity = graph_community_fastgreedy(&lkn, &v1, &ncom);
	
	printf("modularity = %f,ncom = %d\n",modularity,ncom);
	FILE *f = fopen("funccom.fc.xlsx","w");
	fprintf(f, "comID\tname\n");
	for (int i = 0; i < dict_size(dict);i++) {
		fprintf(f, "%d\t", VECTOR(v1)[i]);
		struct rb_node* e = dict_ele(dict, i);
		dic_traceback_string(e, &str);
		fprintf(f, "%s\n",VECTOR(str));
	}
	fclose(f);
	f = fopen("comID.fc.xlsx","w");
	output_com_filename(&flist, &v1, graph_vertices_count(&lkn), ncom, filedict, f);
	fclose(f);

	//print_vector_int(&v1, stdout);

	print_communities(&lkn, &v1, "community.fc.xlsx", "comedge.fc.xlsx");

	vector_funcP_destroy(&flist);
	vector_int_destroy(&v1);
	vector_char_destroy(&str);
    vector_int_destroy(&edges);
    graph_destroy(&lkn);
    return 0;
}