unsigned int Polygon::add_vertex(const math::Vector2 &v)
 {
   _updated = false;
   unsigned int pos = _vertices.size();
   insert_vertex(v, pos);
   return pos;
 }
Exemple #2
0
bool t_dir_graph::read_format2(istream& input, const string first_line){
	vector<t_vertex> verts;
	int adj;
	t_vertex v;
	uint datas = atoi(first_line.c_str());

	while(input.peek() == '\n') input.ignore(1); // read the '\n'
	// read vertices
	for(uint i = 0; i < datas; i++){
		input >> v;
		verts.push_back(v);
		while(input.peek() == '\n') input.ignore(1); // read the '\n'
	}
	for(uint i=0; i < verts.size(); i++) insert_vertex(verts[i]);
	// read adjacency matrix
	for(uint i=0; i < verts.size(); i++){
		for(uint j=0; j < verts.size(); j++){
			input >> adj;
			if(adj > 0)
				insert_arc(t_arc(verts[i], verts[j]));
			while(input.peek() == ' ') input.ignore(1); // read the ' '
			while(input.peek() == '\n') input.ignore(1); // read the '\n'
		}
	}
	return true;
}
Exemple #3
0
// read a graph from the given file, return success
bool t_dir_graph::read_format1(istream& input, const string first_line){
	string arcs;
	vector<t_vertex> vert_vec;
	vector<t_vertex> arc_vec;

	input >> arcs;
	tokenize(first_line, vert_vec, ",");
	tokenize(arcs, arc_vec, "),(");

	for(uint i=0; i < vert_vec.size(); i++)
		insert_vertex(vert_vec[i]);
	size_t arrow_pos = 0;
	for(uint i=0; i < arc_vec.size(); i++){
		if((arrow_pos = arc_vec[i].find("->")) != string::npos){
			dbgcout << "arc: " << arc_vec[i].substr(0,arrow_pos) << "," << arc_vec[i].substr(arrow_pos+2) << "\n";
			insert_arc(t_arc(arc_vec[i].substr(0,arrow_pos),arc_vec[i].substr(arrow_pos+2)));
		} else {
			i++;
			if(i < arc_vec.size()){
				dbgcout << "arc: " << arc_vec[i-1] << "," << arc_vec[i] << "\n";
				insert_arc(t_arc(arc_vec[i-1],arc_vec[i]));
			}
		}
	}

	return true;
}
Exemple #4
0
int main(int argc, char* argv[]) {
  Adjacency_Matrix* g = (Adjacency_Matrix*) malloc(sizeof(Adjacency_Matrix));
  Adjacency_Matrix* tp;
  int* adjacency;
  empty_graph(g);

  int option, v, a1, a2;

  do {
    menu();
    scanf("%d", &option);
    switch (option) {
      case INSERT_VERTEX:
        printf("How many vertex would you like to insert? ");
        scanf("%d", &v);
        insert_vertex(g, v);
        break;
      case REMOVE_VERTEX:
        printf("Which vertex would you like to remove? ");
        scanf("%d", &v);
        remove_vertex(g, v);
        break;
      case INSERT_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        insert_arc(g, a1, a2, 1);
        break;
      case REMOVE_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        remove_arc(g, a1, a2);
        break;
      case VERTEX_ADJACENCY:
        printf("Which vertex would you like to verify adjacency?");
        scanf("%d", &v);
        adjacency = get_adjacency(g, v);
        print_adjacency(adjacency);
        free(adjacency);
        pause();
        break;
      case TRANSPOSE_GRAPH:
        tp = transpose_graph(g);
        print_graph(tp);
        free(tp);
        pause();
        break;
      case PRINT_GRAPH: 
        print_graph(g);
        pause();
        break;
    }
  } while (option != EXIT);

  return 0;
}
int main()
{
    int source,destination,ver;
    char option;
    v_node *head=NULL;
    while(1)
    {
        system("cls");
        printf("\t\t\t\tAdjacency List Implementation\n\n");
        printf("Choose Option :-\n\n1 - Insert Vertex\n2 - Insert Edge\n3 - Delete Edge\n4 - Delete Vertex\n5 - Display\n\tBackspace - Exit\n");
        option = getch();
        system("cls");
        switch(option)
        {
        case '1':
            printf("\t\t\t\tHere you can insert vertex\n\n");
            printf("Enter vertex to be inserted : ");
            scanf("%d",&ver);
            head=insert_vertex(head,ver);
            hold_screen();
            break;
        case '2':
            printf("\t\t\t\tHere you can insert an edge\n\n");
            printf("Enter source : ");
            scanf("%d",&source);
            printf("Enter destination : ");
            scanf("%d",&destination);
            insert_edge(head,source,destination);
            hold_screen();
            break;
        case '3':
            printf("\t\t\t\tHere you can delete an edge\n\n");
            printf("Enter source : ");
            scanf("%d",&source);
            printf("Enter destination : ");
            scanf("%d",&destination);
            delete_edge(head,source,destination);
            hold_screen();
            break;
        case '4':
            printf("Enter vertex to be deleted : ");
            scanf("%d",&ver);
            head=delete_vertex(head,ver);
            hold_screen();
            break;
        case '5':
            display(head);
            hold_screen();
            break;
        case 8:
            return 0;
        }
    }
}
Exemple #6
0
void pnodeparam_ (wgraph_s *g)
{
    vertex_s *v;
    
    if (GTNEXT()->type == T_COMMA)
    if (GTNEXT()->type == T_ID) {
        v = vertex_s_(stream_);
        vhashinsert (v, g->nvert);
        insert_vertex (g, v);
        pnodeparam_(g);
    }
}
Exemple #7
0
void split(struct _sphere_context *ctx, struct _triangle *tr)
{    
    struct _vertex tmp;    
    struct _triangle u, v;
    unsigned long i, pos = tr->c;
    struct _vertex *a = &ctx->vertices[tr->a];
    struct _vertex *b = &ctx->vertices[tr->b];
    struct _vertex *c = &ctx->vertices[tr->c];    
    
    insert_vertex(ctx, pos, a->x + (c->x - a->x), 
                            a->y + (c->y - a->y), 
                            a->z + (c->z - a->z));    
}
Exemple #8
0
int main()
{
	struct web_graph webg;
	int i;
	urlq_t queue;
	int num;
	tpool_t *tpool;

	if (init_webg(&webg) == INIT_FAIL)
	{
		printf("初始化图失败,退出程序!\n");
		exit(1);
	}
	if (queue_init(&queue) == INIT_QUEUE_FAIL)
	{
		printf("初始化队列失败,退出程序!\n");
		exit(1);
	}

	if ((tpool = tpool_create(MAX_THREADS, &webg, &queue)) == NULL)
	{
		printf("初始化线程池失败!\n");
		exit(1);
	}
	printf("start work!\n");
	//首先要将index.html加入点集和队列中
	pthread_mutex_lock(&tpool->lock);
	num = insert_vertex(&webg, "/techqq/index.html");
	queue_push(&queue, num);
	pthread_mutex_unlock(&tpool->lock);
	pthread_cond_signal(&tpool->cond);

	while (queue.size > 0 || tpool->at_work != 0)
	{
		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@queue_size: %d@@@@@@@@@@@@@@@@@@@@@@@\n", queue.size);
		printf("front: %d\n", queue.front);
		printf("tail: %d\n", queue.tail);
		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@list_size: %d@@@@@@@@@@@@@@@@@@@@@@@\n", webg.all_url_list_size);
		printf("$$$$$$$$$$$$$$$$$edge_set_size:  %d\n", webg.edge_set_size);
		printf("at_work: %d\n", tpool->at_work);
		printf("---------------existed_page_size: %d\n", webg.existed_page_size);
		sleep(2);
	}
	printf("finish work!\n");
	tpool_destroy();
	printf("size: %d\n", webg.all_url_list_size);
	printf("queue_size: %d\n", queue.size);
	print_webg_to_file(&webg);
	destroy_webg(&webg);
	output_result_file();
}
Exemple #9
0
void Boundary::split_edge (edge_iterator eit, const vec_t &newv) {
    int nedge_id = my_edge.size ();
    int nvert_id = insert_vertex (newv);
    edge_t E = *eit;
    E.vert0 = nvert_id;
    E.prev  = eit.my_position;
    if (E.next != INVALID_EDGE) {
        edge (E.next).prev = nedge_id;
    }
    my_edge.push_back (E);
    edge (eit).vert1 = nvert_id;
    edge (eit).next = nedge_id;
    // check that everything is alright
    assert_valid_link_structure ();
}
void table(int vertex_num,int matrix[SIZE][SIZE],struct vertex vert[SIZE])
{
 int i,j;
 for(i=0;i<vertex_num;i++)
 {
  vert[i].visit=F;
  vert[i].vertex_no=i+1;
  vert[i].info='A'+i;
  vert[i].path_lenth=0;
  vert[i].edge_ptr=NULL;
 }
 for(i=0;i<vertex_num;i++)
   for(j=0;j<vertex_num;j++)
    if(matrix[i][j]>0)
     vert[i].edge_ptr=insert_vertex(j,vert[i].edge_ptr);
}
Exemple #11
0
void pnodelist_ (wgraph_s *g)
{
    vertex_s *v;
    
    if (*(uint16_t *)stream_->lexeme == *(uint16_t *)"V") /*if(!strcmp(stream_->lexeme,"v"))*/
    if (GTNEXT()->type == T_EQU)
    if (GTNEXT()->type == T_OPENBRACE)
    if (GTNEXT()->type == T_ID) {
        v = vertex_s_(stream_);
        vhashinsert (v, g->nvert);
        insert_vertex (g, v);
        pnodeparam_(g);
    }
    if (stream_->type == T_CLOSEBRACE)
        return;
    printf ("Syntax Error while parsing nodes: %s", stream_->lexeme);
    exit(EXIT_FAILURE);
}
main()
{
	int i;
	GraphType g;
	
	graph_init(&g);
	// 인접 리스트 생성 
	for(i=0;i<4;i++)
		insert_vertex(&g, i);
	insert_edge(&g,0,1);
	insert_edge(&g,1,0);
	insert_edge(&g,0,3);
	insert_edge(&g,3,0);
	insert_edge(&g,1,2);
	insert_edge(&g,2,1);
	insert_edge(&g,1,3);
	insert_edge(&g,3,1);
	insert_edge(&g,2,3);
	insert_edge(&g,3,2);
	bfs_mat(&g, 0);
}
Exemple #13
0
int main()
{
    unsigned long i;
    struct _sphere_context ctx;
    init_sphere(&ctx);
    
    srand(time(NULL));
    for (i = 0; i < 10000; ++i)
    {
        if (insert_vertex(&ctx, rand() % 10000, 1, 1, 1) < 0)
            break;
    }
    
    for (i = 0; i < ctx.v_count; ++i)
    {
        printf("%f %f %f\n", ctx.vertices[i].x, 
                             ctx.vertices[i].y, 
                             ctx.vertices[i].z);
    }
    printf ("Total vertices: %d\n", ctx.v_count);
    return 0;
}
Exemple #14
0
int main()
{
	struct graph *G;
	int opt1,opt2,w;
	char name,s,e;
	G=(struct graph *)create_graph(G);
	FILE *fp;
	fp=fopen("graph","r");
	for(fscanf(fp,"%c",&s);s!='$';fscanf(fp,"%c",&s))
		insert_vertex(G,s);
	for(fscanf(fp,"%c%c%d",&s,&e,&w);s!='$';fscanf(fp,"%c%c%d",&s,&e,&w))
		insert_edge(G,s,e,w);
	fclose(fp);
	do
	{
		printf("\n\n\tEnter the option : \n\t1. for Insert Vertex\n\t2. for Delete Vertex\n\t3. for insert Edge\n\t");
		printf("4. for Delete Edge\n\t5. for Fix the Graph\n\t6. for Exit\n\t\t\t: "); 
		scanf("%d",&opt1);
		switch(opt1)
		{
			case 1:
				printf("\n\tEnter the name : ");
				getchar();
				scanf("%c",&name);
				if(!find_vertex(G,name))
				{
					insert_vertex(G,name);
					printf("\tVertex Inserted Successfully\n\n");
				}
				else
					printf("\tVertex is already Exist\n\n");
				break;
			case 2:	
				printf("Enter the vertex for Delete :\t");
				getchar();
				scanf("%c",&name);
				if(find_vertex(G,name))
				{
					vertex_delete(G,name);
					printf("\tVertex Deleted Successfully\n\n");
				}
				else
					printf("\tVertex not Exist\n\n");
				break;
			case 3:	
				printf("\n\tEnter the start point of edge :\t");
				getchar();
				scanf("%c",&s);
				printf("\n\tEnter the end point of edge :\t");
				getchar();
				scanf("%c",&e);
				printf("\n\tEnter the Weight of Edge :\t");
				scanf("%d",&w);
				if(!find_edge(G,s,e))
				{
					if(insert_edge(G,s,e,w))
						printf("\tEdge Inserted Successfully\n\n");
					else
						printf("\tSuch Vertices not Exist\n\n");
				}
				else
					printf("\tEdge is already Exist\n\n");
				break;
			case 4:	
				printf("\n\tEnter the start point of edge :\t");
				getchar();
				scanf("%c",&s);
				printf("\n\tEnter the end point of edge :\t");
				getchar();
				scanf("%c",&e);
				if(find_edge(G,s,e))
				{
					edge_delete(G,s,e);
					printf("\tEdge Deleted Successfully\n\n"); 
				}
				else
					printf("\tEdge Not Exist\n\n"); 
				break;
			case 5:
				if(G->count>0)
				{
					do
					{
						printf("\n\n\n\tEnter the option for DIRECTED GRAPH : \n\t1. for Matrix Representation\n\t2. for List Representation\n\t3. for Topological Sort Order\n\t4. for Bellman Ford Algorithm for shortest path\n\t5. for Dijkestra Algorithm for shortest path \n\t6. for Vertex Details \n\t7. for Edit Graph\n\t8. for Exit\n\t\t\t: "); 
						scanf("%d",&opt2);
						option1(G,opt2);
					}while(opt2-7);
				}
				else
					printf("\n\tNo vertex exist\n\n");
				break;
			case 6:	
				free_graph(G);
				break;
			default:printf(" wrong input \n");
		}
	}while(opt1-6);	
}
Exemple #15
0
void * do_crawler(void *item)
{
	char *url_ptr;
	int clientfd ;
	static int pages = 0;
	static int error = 0;
	char *buf = NULL;
	urlq_t *url_list_head = NULL, *p, *p_pre;
	char cur_dir[256];
	hash_table *hash_in, *hash_out;
/*	int tid = pthread_self();*/
	int j = 0;
	int i= 0;
	int pos_found = 0;
	char temp[256];
	int status = 0;
	static int a = 0;
	static int b = 0;
	static int c = 0;
	static int d = 0;
	static int e = 0;
	static int f = 0;
	static int g = 0;
	while(1){	
		pthread_mutex_lock(&mutex);
		while (urlqueue.head_ptr->next == NULL){
			pthread_cond_wait(&ready, &mutex);
		}

		pthread_mutex_lock(&((threadpool_item *)item)->waitlock);
		((threadpool_item *)item)->idle = 0;
		pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
		url_ptr = queue_pop(&urlqueue);
		g++;
		pthread_mutex_unlock(&mutex);
		

		pthread_mutex_lock(&mutex);
		/*if not visited, set flag = 1*/
		if(has_visited(hash, url_ptr) == 1){
			pthread_mutex_unlock(&mutex);
			free(url_ptr); 
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);
			d++;	
			continue;	
		}
		hash_out = has_url(hash, url_ptr);
		e++;
		pthread_mutex_unlock(&mutex);
		if (hash_out == NULL){
			printf("error\n");
			getchar();
		}
		*temp = '\0';
		cur_dir[0] = '\0';
		strcpy(cur_dir, url_ptr);
		j = strlen(cur_dir);
		for (;cur_dir[j] != '/' && j != 0; j--) ;
		if(j == 0)
			cur_dir[j] = '\0';
		else
			cur_dir[j+1] = '\0';

		for (i = 0; i < 3; i++){
			if((clientfd = open_tcp("127.0.0.1", 80)) < 0){
				close_fd(clientfd);
				continue;
			}

			if( http_do_get(clientfd, rootdir, "127.0.0.1", url_ptr) < 0){
				close_fd(clientfd);
				continue;
			}

			if(recv_line(clientfd, temp) <= 0){
				close_fd(clientfd);
				continue;
			}
			if((status = http_response_status(temp))  == 4){
				printf("%s error %d\n",url_ptr, error++);
				pthread_mutex_lock(&mutex);
				set_status(hash, url_ptr, 4);
				set_webg_status(webg, hash_out, 4);
				pthread_mutex_unlock(&mutex);
			
				pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
				((threadpool_item *)item)->idle =1;
				pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
				close_fd(clientfd);
				break;	
			}
			buf = http_response_body(clientfd);
			close_fd(clientfd);
			break;
		}
		if (status == 4)
			continue;
		if(i == 3){
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
			close_fd(clientfd);
			continue;	
		}
		if (buf == NULL){
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
			continue;
		}
		printf("%s pages %d\n", url_ptr,pages++);
		extract_link(buf, cur_dir, &url_list_head);
		free(buf);
		buf = NULL;	
		p = url_list_head->next;
		p_pre = url_list_head;
		while (p != NULL){
			
			if(strcmp(url_ptr, p->url_ptr) == 0){
				p_pre->next = p->next;
				free(p->url_ptr);
				free(p);
				a++;
				p = p_pre->next;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
				continue;	
			}			
			pthread_mutex_lock(&mutex);
			hash_in = has_url(hash, p->url_ptr);	
			if (hash_in != NULL ){
				insert_edge(webg, hash_in, hash_out);
				pthread_mutex_unlock(&mutex);
				p_pre->next = p->next;
				free(p->url_ptr);
				free(p);
				p = p_pre->next;
				b++;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
				continue;
			}
			else{
				pos_found = insert_vertex(webg, hash_out, p->url_ptr);
				insert_hash_item(hash, p->url_ptr, pos_found, 0);	
				pthread_mutex_unlock(&mutex);
				c++;
				p_pre = p;
				p = p->next;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
			}
		}
		
		if(p_pre != url_list_head){
			pthread_mutex_lock(&mutex);
			queue_push(&urlqueue, url_list_head->next, p_pre);
			f++;
			pthread_mutex_unlock(&mutex);
		}
		free(url_list_head);
		p = p_pre = url_list_head = NULL;
	
		pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
		((threadpool_item *)item)->idle = 1;
		pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
/*printf("next time!\n");*/
	}
/*printf("over!\n");*/
	return NULL;	
}
int main()
{
	graph_link gl;

	init_graph(&gl);

	insert_vertex(&gl, 'A');
	insert_vertex(&gl, 'B');
	insert_vertex(&gl, 'C');
	insert_vertex(&gl, 'D');
	insert_vertex(&gl, 'E');
	insert_vertex(&gl, 'F');
	insert_vertex(&gl, 'G');
	insert_vertex(&gl, 'H');
	insert_vertex(&gl, 'I');
	insert_vertex(&gl, 'J');
	insert_vertex(&gl, 'K');
	insert_vertex(&gl, 'L');
	insert_vertex(&gl, 'M');

	insert_edge(&gl, 'A', 'B');
	insert_edge(&gl, 'A', 'C');
	insert_edge(&gl, 'A', 'F');
	insert_edge(&gl, 'A', 'L');
	insert_edge(&gl, 'B', 'M');
	insert_edge(&gl, 'L', 'J');
	insert_edge(&gl, 'L', 'M');
	insert_edge(&gl, 'J', 'M');

	insert_edge(&gl, 'D', 'E');

	insert_edge(&gl, 'G', 'H');
	insert_edge(&gl, 'G', 'I');
	insert_edge(&gl, 'G', 'K');
	insert_edge(&gl, 'H', 'K');

	printf("\n");
	show_graph(&gl);

	printf("Depth First Search(DFS) all nodes of the graph: \n");
	depth_first_search(&gl, 'D');
	printf("Nul\n");

	printf("Breadth First Search(BFS) all nodes of the graph: \n");
	breadth_first_search(&gl, 'A');
	printf("Nul\n");

	printf("Non connect graph DFS: \n");
	components(&gl);
	printf("Nul\n");

	//int v = get_first_neighbor(&gl, 'A');
	//printf("The first neighbor node of 'A' is: %d\n", v);

	//int v1 = get_next_neighbor(&gl, 'B', 'E');
	//printf("The next neighbor node of 'B' and 'E' is: %d\n", v1);
	//printf("\n");

	//delete_edge(&gl, 'B', 'C');
	//show_graph(&gl);

	//delete_vertex(&gl, 'C');
	destroy_graph(&gl);
}