Esempio n. 1
0
// 1 color a 3 node, 2 edge graph
// the node with the most edges should be spilled
void test_chaitin_spill() {
    DEBUG_PRINT("\ntest_chaitin_spill:\n");
    DEBUG_PRINT("  creating graph...\n");

    graph* g = create_graph();

    vertex* a = graph_add_vertex(g, 'a');
    vertex* b = graph_add_vertex(g, 'b');
    vertex* c = graph_add_vertex(g, 'c');

    graph_add_edge(a, b);
    graph_add_edge(a, c);
    
    assert(a->color == -1);
    assert(b->color == -1);
    assert(c->color == -1);

    DEBUG_PRINT("  coloring graph...\n");

    color_graph(g, 1);

    #ifdef DEBUG
    print_graph(g);
    #endif

    assert(a->color == -1); // spilled node
    assert(b->color == 0);
    assert(c->color == 0);

    destroy_graph(g);

    printf("+ algorithm test (spill) passed!\n");
}
Esempio n. 2
0
void graph_add_edge(struct graph *graph, void *from, void *to)
{
	/* Make sure both from and to actually exist, otherwise add */
	graph_add_vertex(graph, from);
	graph_add_vertex(graph, to);

	int from_pos = vindex_lookup(graph, from);
	int to_pos = vindex_lookup(graph, to);

	vertex_add_edge(&graph->vertices[from_pos], to_pos);
}
Esempio n. 3
0
struct GRAPH *graph_prim(struct GRAPH *g) {
	struct EDGE *e;
	struct GRAPH *tree=NULL;
	struct EDGE_LIST *el=NULL, **tmp, **insert, *ne;
	struct VERTEX *v1, *v2;
	int a;
	if(!g)
		return;
	graph_add_vertex(&tree, g->vertex->point);
	for(; g; g=g->next)
		for(e=g->vertex->neighbour; e; e=e->next) {
			for(tmp=⪙ *tmp; tmp=&((*tmp)->next))
				if((*tmp)->weight>e->weight)
					break;
			ne=malloc(sizeof(struct EDGE_LIST));
			ne->next=*tmp;
			ne->weight=e->weight;
			ne->v1=g->vertex;
			ne->v2=e->vertex;
			*tmp=ne;
		}
	do {
		for(tmp=⪙ *tmp; tmp=&(*tmp)->next) {
			ne=*tmp;
			for(g=tree, a=0; g; g=g->next) {
				if(g->vertex->point.x==ne->v1->point.x&&g->vertex->point.y==ne->v1->point.y) {
					v1=g->vertex;
					a++;
				}
				if(g->vertex->point.x==ne->v2->point.x&&g->vertex->point.y==ne->v2->point.y) {
					v2=g->vertex;
					a+=2;
				}
			}
			if(!a)
				continue;
			else if(a==1)
				graph_add_edge(v1, graph_add_vertex(&tree, ne->v2->point), ne->weight, DIRECTION_SINGLE);
			else if(a==2)
				graph_add_edge(v2, graph_add_vertex(&tree, ne->v1->point), ne->weight, DIRECTION_SINGLE);
			
			*tmp=ne->next;
			free(ne);
			break;
		}
	} while(el);
	return tree;
}
Esempio n. 4
0
struct vertex * graph_make_vertex(struct graph *g, int value) {
    struct vertex *v = graph_find_vertex(g, value);
    if (v == NULL) {
        v = graph_add_vertex(g, value);
    }
    return v;
}
Esempio n. 5
0
// create and use a simple graph
void test_graph() {
    DEBUG_PRINT("\ntest_graph:\n");
    DEBUG_PRINT("  creating graph...\n");

    // allocate a new graph
    graph* my_graph = create_graph();
    assert(my_graph != NULL);
    assert(my_graph->size == 0);
    assert(my_graph->vertices == NULL);

    DEBUG_PRINT("  adding a new vertex...\n");

    // add a vertex
    vertex* first_node = graph_add_vertex(my_graph, 'a');
    assert(my_graph->size == 1);
    assert(my_graph->vertices == first_node);
    assert(first_node->color == -1);
    assert(first_node->removed == 0);
    assert(first_node->element == 'a');
    assert(first_node->num_edges == 0);
    assert(first_node->edges == NULL);
    assert(first_node->next == NULL);

    DEBUG_PRINT("  adding edge to a new vertex...\n");

    vertex* second_node = graph_add_vertex(my_graph, 'b');
    assert(my_graph->size == 2);
    // add edge from 'a' to 'b'
    graph_add_edge(first_node, second_node);
    assert(first_node->edges->to == second_node);
    assert(first_node->edges->next == NULL);
    assert(second_node->edges->to == first_node);
    
    // print it
    #ifdef DEBUG
    print_graph(my_graph);
    #endif
    
    DEBUG_PRINT("  freeing graph...\n");

    // free the graph
    destroy_graph(my_graph);

    printf("+ graph test passed!\n");
}
Esempio n. 6
0
int main(int argc, char **argv) {
	int i, j;
	struct GRAPH *g, *tree=NULL;
	struct VERTEX *v;
	struct POINT p;
	DARNIT_KEYS keys;
	srand(time(NULL));
	d_init("Graphs", "uppg8", NULL);
	
	for(i=0; i<256; i++) {
		circle[i]=d_render_circle_new(32, 1);
		line[i]=d_render_line_new(1, 1);
	}
	
	for(j=0; j<4; j++)
		for(i=0; i<7; i++) {
			p.x=i*100+50;//(rand()%80)*10;
			p.y=j*100+50;//(rand()%48)*10;
			v=graph_add_vertex(&graph, p);
			for(g=graph; g; g=g->next)
				if(!(rand()%6))
					graph_add_edge(v, g->vertex, rand()%100, DIRECTION_DOUBLE);
		}
	tree=graph_prim(graph);
	draw_graph(graph);
	for(g=graph;;) {
		keys=d_keys_get();
		if(keys.start) {
			g=g==graph?tree:graph;
			draw_graph(g);
		}
		d_keys_set(keys);
		d_render_begin();
		d_render_tint(0xFF, 0xFF, 0xFF, 0xFF);
		for(i=0; i<vertices; i++)
			d_render_circle_draw(circle[i]);
		for(i=0; i<edges; i++) {
			d_render_tint(length[i], 0x7F, 0x0, 0xFF);
			d_render_line_draw(line[i], 1);
		}
		
		d_render_end();
		d_loop();
	}
	
	d_quit();
	return 0;
}
Esempio n. 7
0
int main(){
	setup();

	for(int i=0; i<10; i++){
		graph_add_vertex(gp, v[i]);
	}

	graph_add_edge(gp, v[0], v[1]);
	graph_add_edge(gp, v[0], v[2]);
	graph_add_edge(gp, v[0], v[3]);
	graph_add_edge(gp, v[0], v[4]);

	graph_add_edge(gp, v[1], v[5]);
	graph_add_edge(gp, v[1], v[8]);
	graph_add_edge(gp, v[1], v[2]);

	graph_add_edge(gp, v[2], v[5]);
	graph_add_edge(gp, v[2], v[7]);

	graph_add_edge(gp, v[3], v[1]);
	graph_add_edge(gp, v[3], v[6]);
	graph_add_edge(gp, v[3], v[7]);

	graph_add_edge(gp, v[4], v[5]);

	graph_add_edge(gp, v[5], v[6]);

	graph_add_edge(gp, v[6], v[7]);

	graph_add_edge(gp, v[7], v[8]);

	graph_add_edge(gp, v[8], v[9]);

	graph_add_edge(gp, v[9], v[0]);

	graph_dump(gp, print);

	List lst;
	list_init(&lst, NULL, NULL);
	bfs(gp, v[0], &lst);
	list_dump(&lst, print_list);

	enddown();
	return 0;
}
Esempio n. 8
0
/* -Su
 * @param targets list of strings
 * @param hashdb hash database
 */
static int upgrade_pkgs(alpm_list_t *targets, struct pw_hashdb *hashdb)
{
	alpm_list_t *i;
	alpm_list_t *target_pkgs = NULL;
	alpm_list_t *final_targets = NULL;
	struct graph *graph;
	struct stack *topost;
	int ret = 0;

	char cwd[PATH_MAX];
	if (!targets) {
		return 0;
	}

	if (!getcwd(cwd, PATH_MAX)) {
		return error(PW_ERR_GETCWD);
	}

	if (chdir(powaur_dir)) {
		return error(PW_ERR_CHDIR);
	}

	/* Enable debugging resolution */
	graph_enable_debug_resolve();

	graph = graph_new((pw_hash_fn) sdbm, (pw_hashcmp_fn) strcmp);
	topost = stack_new(sizeof(int));

	for (i = targets; i; i = i->next) {
		target_pkgs = alpm_list_add(target_pkgs, i->data);
		/* Insert into outdated AUR packages to avoid dling up to date ones */
		hash_insert(hashdb->aur_outdated, (void *) i->data);
		/* Add the targets into graph to prevent those w/o deps to not get upgraded */
		graph_add_vertex(graph, (void *) i->data);
	}

	printf("Resolving dependencies... Please wait\n");
	/* Build dep graph for all packages */
	build_dep_graph(&graph, hashdb, target_pkgs, RESOLVE_IMMEDIATE);
	ret = graph_toposort(graph, topost);
	if (ret) {
		printf("Cyclic dependencies detected!\n");
		goto cleanup;
	}

	graph_disable_debug_resolve();

	final_targets = topo_get_targets(hashdb, graph, topost);
	if (final_targets) {
		topo_install(hashdb, final_targets);
	}

cleanup:
	/* Install in topo order */
	graph_free(graph);
	stack_free(topost);
	alpm_list_free(target_pkgs);
	alpm_list_free(final_targets);
	if (chdir(cwd)) {
		return error(PW_ERR_RESTORECWD);
	}

	return ret;
}
Esempio n. 9
0
void add_vertex(char *s1, char *s2) {
	graph_add_vertex(s1);
}
Esempio n. 10
0
int main(){
	setup();

	for(int i=0; i<10; i++){
		graph_add_vertex(gp, u[i]);
	}

	graph_add_edge(gp, u[0], u[1]);
	graph_add_edge(gp, u[0], u[2]);
	graph_add_edge(gp, u[0], u[3]);
	graph_add_edge(gp, u[0], u[4]);

	graph_add_edge(gp, u[1], u[5]);
	graph_add_edge(gp, u[1], u[8]);
	graph_add_edge(gp, u[1], u[2]);

	graph_add_edge(gp, u[2], u[5]);
	graph_add_edge(gp, u[2], u[7]);

	graph_add_edge(gp, u[3], u[1]);
	graph_add_edge(gp, u[3], u[6]);
	graph_add_edge(gp, u[3], u[7]);

	graph_add_edge(gp, u[4], u[5]);

	graph_add_edge(gp, u[5], u[6]);

	graph_add_edge(gp, u[6], u[7]);

	graph_add_edge(gp, u[7], u[8]);

	graph_add_edge(gp, u[8], u[9]);

	graph_add_edge(gp, u[9], u[0]);

	graph_dump(gp, print);

	puts("Drop 0-item");
	graph_drop_vertex(gp, u[0]);
	graph_dump(gp, print);
	puts("");

	puts("Remove 9-item(will failure, because 8-item adjacent to 9-item)");
	graph_remove_vertex(gp, u[9]);
	graph_dump(gp, print);
	puts("");

	puts("Remove edge from 8-item to 9-item");
	graph_remove_edge(gp, u[8], u[9]);
	graph_dump(gp, print);
	puts("");

	puts("Remove 9-item(will success)");
	graph_remove_vertex(gp, u[9]);
	graph_dump(gp, print);
	puts("");	

	puts("Remove edge from 1-item to 5-item");
	graph_remove_edge(gp, u[1], u[5]);
	graph_dump(gp, print);
	puts("");

	puts("Add edge from 1-item to 5-item");
	graph_add_edge(gp, u[1], u[5]);
	graph_dump(gp, print);
	puts("");

	graph_dump(gp, print);


	enddown();
	return 0;
}
Esempio n. 11
0
void graph_init_vertices(struct graph *g, int *vertices, int size) {
  int i;
  for (i = 0 ; i < size; i++) { 
    graph_add_vertex(g, vertices[i]);
  }
}