示例#1
0
main(){
	graph **adj_list;
	int n, m, n1, n2;

	//Creating the adjacency list
	scanf("%d %d", &n, &m);
	adj_list=create_empty_list(adj_list, n);
	
	//Creating edges
	int i;
	for(i=0;i<m;++i){
		scanf("%d %d", &n1, &n2);
		create_edge(n1,n2,adj_list,n);
		create_edge(n2,n1,adj_list,n);
	}
	print_list(adj_list, n);

	//Marking elements as visited
	int *visited;
	visited=(int *)malloc(n*sizeof(int));
	for(i=0;i<n;++i){
		visited[i]=0;
	}

	//Printing the connected componenets
	printf("\n\nThe connected components are: \n");
	for(i=0;i<n;++i){
		if(visited[i]==0){
			printf("{");
			dfs_connected(adj_list,i,visited);
			printf("\b} ");
		}
	}
}
示例#2
0
struct NodeStorage* new_from_file( FILE* in,unsigned edge_stride ) {
	struct NodeStorage* result = malloc( sizeof (struct NodeStorage) );
	result->n_nodes = 0;
	result->edge_stride = edge_stride;
	result->nodes = malloc( 0 );

	char* line = NULL;
	unsigned row_no = 0;
	size_t n = 0;
	while( getline( &line,&n,in )!=-1 ) {
		char* field;
		unsigned field_no = 0;
		field = strtok( line," \t\n" );

		while( field ) {
			if( result->n_nodes<field_no + 1 ) {
				result->nodes = realloc( result->nodes,( field_no + 1 )*sizeof (Node) );
				result->nodes[ field_no ].out.n_edges = 0;
				result->nodes[ field_no ].out.first.data = malloc( edge_stride*sizeof (Edge) );
				result->nodes[ field_no ].out.first.next = NULL;
				result->n_nodes++;
			}

			if( strcmp( field,"0" )&& strcmp( field,"0.0" )&& strcmp( field,"." ) ) {
				create_edge( result,row_no,field_no );
			}

			field_no++;
			field = strtok( NULL," \t\n" );
		}
	}
	free( line );

	return result;
}
示例#3
0
int main(void) {
    int T;
    scanf("%d", &T);
    int i;
    for(i = 0; i < T; i++) {
        Graph *graph = (Graph *)malloc(sizeof(Graph));
        int vertex_num, edge_num;
        scanf("%d %d", &vertex_num, &edge_num);
        graph->vertex_num = vertex_num;
        graph->in_degrees = (int *)malloc(sizeof(int) * graph->vertex_num);
        memset(graph->in_degrees, 0, sizeof(int) * graph->vertex_num);
        graph->adj_lists = (Edge **)malloc(sizeof(Edge *) * graph->vertex_num);
        memset(graph->adj_lists, 0, sizeof(Edge *) * graph->vertex_num);
        int k;
        for(k = 0; k < edge_num; k++) {
            int from, to;
            scanf("%d %d", &from, &to);
            from--; to--;
            Edge *edge = create_edge(to, graph->adj_lists[from]);
            graph->adj_lists[from] = edge;
            graph->in_degrees[to] += 1;
        }
        if(is_dag(graph)) {
            printf("Correct\n");
        }
        else {
            printf("Wrong\n");
        }
        destroy_graph(graph);
    }
    return 0;
}
示例#4
0
文件: object.c 项目: johnh530/electro
void recv_create_edge(void)
{
    int i = recv_index();
    int j = recv_index();

    create_edge(i, j);
}
示例#5
0
void add_vertex_to_edge(half_edge e1, void* v3) {
  
  assert(e1->next == NULL);
  assert(e1->opp->prev == NULL);

  half_edge e2 = create_edge(e1->opp->vertex, v3);
  half_edge e3 = create_edge(v3, e1->vertex);

  half_edge ne1 = e3->opp;
  half_edge ne2 = e1->opp;
  half_edge ne3 = e2->opp;

  e1->next = ne1; ne1->prev = e1;
  e2->next = ne2; ne2->prev = e2;
  e3->next = ne3; ne3->prev = e3;
}
示例#6
0
half_edge create_triangle(void* v1, void* v2, void* v3) {
  
  half_edge e1 = create_edge(v1,v2);
  half_edge e2 = create_edge(v2,v3);
  half_edge e3 = create_edge(v3,v1);

  half_edge ne1 = e3->opp;
  half_edge ne2 = e1->opp;
  half_edge ne3 = e2->opp;

  e1->next = ne1; ne1->prev = e1;
  e2->next = ne2; ne2->prev = e2;
  e3->next = ne3; ne3->prev = e3;
  
  return e1;
}
示例#7
0
文件: object.c 项目: johnh530/electro
int send_create_edge(int i, int j)
{
    send_event(EVENT_CREATE_EDGE);
    send_index(i);
    send_index(j);

    return create_edge(i, j);
}
示例#8
0
void add_connection(t_graph graph, int orig_node_id, int dest_node_id) {

	t_edge target_list = graph->vertexs[orig_node_id]->edges;
	t_edge new_edge = create_edge(dest_node_id);
	new_edge->next = target_list;
	graph->vertexs[orig_node_id]->edges = new_edge;

}
示例#9
0
void graphNodelist :: create_edge_from_container_to_fluid(void* x1, void* x2) {
	Container* f1 = (Container*)x1;
	Fluid* f2 = (Fluid*)x2;
	// create nodes if needed
	if (f1->node == NULL) {
		f1->node = create_node_with_type(f1->name, f1->type);
	}
	if (f2->node == NULL) {
		f2->node = create_node_with_type(f2->original_name, f2->type);
	}
	// connect the nodes
	create_edge(f1->node, f2->node);
}
示例#10
0
void MxPropSlim::collect_edges()
{
	MxVertexList star;

	for(MxVertexID i=0; i<m->vert_count(); i++)
	{
		star.reset();
		m->collect_vertex_star(i, star);

		for(unsigned int j=0; j<(unsigned int)star.length(); j++)
			if( i < star(j) )  // Only add particular edge once
				create_edge(i, star(j));
	}
}
示例#11
0
void close_triangle(half_edge e1, half_edge e2) {
  
  assert(e1->next == NULL);
  assert(e1->opp->prev == NULL);
  assert(e2->next == NULL);
  assert(e2->opp->prev == NULL);
  assert(e2->vertex == e1 -> opp -> vertex);

  half_edge e3 = create_edge(e2->opp->vertex,e1->vertex);

  half_edge ne1 = e3->opp;
  half_edge ne2 = e1->opp;
  half_edge ne3 = e2->opp;

  e1->next = ne1; ne1->prev = e1;
  e2->next = ne2; ne2->prev = e2;
  e3->next = ne3; ne3->prev = e3;
}
示例#12
0
void Search::readStrGraph(Patterns &patterns,StrGraph &desc) {
    clear();
    map<StrGraph::Node*,Node*> recollect;
    for(StrGraph::node_iter ni = desc.nodes().begin(); ni!=desc.nodes().end(); ++ni) {
        Node *n = create_node(gd<Name>(*ni));
        recollect[*ni] = n;
        SearchStage &stage = gd<SearchStage>(n);
        DString limit = gd<StrAttrs>(*ni).look("limit","50");
        stage.limit = atoi(limit.c_str());
        DString action = gd<StrAttrs>(*ni).look("action","union");
        if(action=="union")
            stage.type = UnionInquiry;
        else if(action=="intersection")
            stage.type = IntersectionInquiry;
        else if(action=="pattern") {
            stage.type = PatternInquiry;
            DString pattern = gd<StrAttrs>(*ni).look("pattern","");
            Patterns::iterator pi = patterns.find(pattern);
            if(pi==patterns.end())
                throw UndefinedPattern(pattern);
            stage.pattern = &pi->second;
        }
        else if(action=="path") {
            stage.type = PathInquiry;
            DString ways = gd<StrAttrs>(*ni)["ways"];
            if(ways=="in")
                stage.goIn = true, stage.goOut = false;
            else if(ways=="both")
                stage.goIn = stage.goOut = true;
            else  // ways=="out" default
                stage.goIn = false, stage.goOut = true;
            stage.firstOnly = gd<StrAttrs>(*ni)["firstonly"]=="true";
            stage.shortest = gd<StrAttrs>(*ni)["shortest"]=="true";
            stage.weightattr = gd<StrAttrs>(*ni).look("weightattr","weight");
        }
        else
            throw UnknownAction(action);
    }
    for(StrGraph::graphedge_iter ei = desc.edges().begin(); ei!=desc.edges().end(); ++ei) {
        Edge *e = create_edge(recollect[(*ei)->tail],recollect[(*ei)->head]).first;
        DString input = gd<StrAttrs>(*ei).look("input","");
        gd<Name>(e) = input;
    }
}
示例#13
0
文件: callgraph.cpp 项目: nealey/vera
//--------------------------------------------------------------------------
int callgraph_t::walk_func(func_t *func, funcs_walk_options_t *opt, int level)
{
  // add a node for this function
  ea_t func_start = func->startEA;
  int id = add(func_start);
  func_item_iterator_t fii;
  for ( bool fi_ok=fii.set(func); fi_ok; fi_ok=fii.next_code() )
  {
    xrefblk_t xb;
    for ( bool xb_ok = xb.first_from(fii.current(), XREF_FAR);
      xb_ok && xb.iscode;
      xb_ok = xb.next_from() )
    {
      func_t *f = get_func(xb.to);
      if ( f == NULL )
        continue;
      int id2;
      if ( !visited(f->startEA, &id2) )
      {
        if ( func_contains(func, xb.to) )
          continue;

        bool skip = false;

        if ( opt != NULL )
        {
          skip =
            // skip lib funcs?
            (((f->flags & FUNC_LIB) != 0) && ((opt->flags & FWO_SKIPLIB) != 0))
            // max recursion is off, and limit is reached?
            || (  ((opt->flags & FWO_RECURSE_UNLIM) == 0)
            && (level > opt->recurse_limit) );
        }
        if ( skip )
          id2 = add(f->startEA);
        else
          id2 = walk_func(f, opt, level+1);
      }
      create_edge(id, id2);
    }
  }
  return id;
}
示例#14
0
文件: read_graph.c 项目: shliu/UKY
/* Read a graph from file "filename" */
void readgraph(char *filename) {
    FILE *fp;
    char buf[MAXLEN]; 	    /* Buffer for a line from the text file */
    char *tmp[MAXNODE*2];   /* Pointers to the names of nodes, and lengths if available */
    char sep_ch[]=" \n";    /* names and nodes are separated by space and newline */
    char nodename;          /* name of the node */
    int  ni;		/* index to the node */
    int  desti;		/* the destination index */
    edge_t *ep, *ep1;	/* pointer to the edge structure */
    int  i, j;		

    fp = fopen(filename, "r");
    if (fp==NULL) {printf("File %s does not exist!\n", filename);
                   exit(1);
                  }
    num_of_nodes = 0; 
    while ( fgets (buf, MAXLEN, fp) != NULL ) {	/* read from the file until the end */
       j=0;
       tmp[j] = strtok(buf, sep_ch);		/* separate line by space and new line */
           while (tmp[j] != NULL) {
               j++;
	       tmp[j] = strtok(NULL, (char *) sep_ch);
               }
           if (j>0) {nodename = tmp[0][0];  	/* get the name of the node  */
               ni=nodename - 'A';		/* get the index of the node */
	       if (num_of_nodes < ni + 1) num_of_nodes = ni+1;  /* set the number of nodes */
               graph[ni].name = nodename;
	       graph[ni].edge_list = NULL;
               ep = NULL;
               for (i=1; i<j; i++) {		/* process each edge from the node */
                   desti=tmp[i][0] - 'A';	/* get the destination node index  */
	   	   ep1 = create_edge(desti); 		/* allocate an edge  */
                   if (ep==NULL) graph[ni].edge_list = ep1;
                            else ep->next = ep1;   	/* put the edge to the link list */
                   ep = ep1; 		/* ep points to the end of the list */
		   }
               }
       }
    fclose(fp);
}
示例#15
0
void subdivide_element(Element *e, long process_id)
{
    float quarter_area ;
    ElemVertex *ev_12, *ev_23, *ev_31 ;
    Edge *e_12_23, *e_23_31, *e_31_12 ;
    Element *enew, *ecenter ;
    long rev_12, rev_23, rev_31 ;

    /* Lock the element before checking the value */
    LOCK(e->elem_lock->lock);

    /* Check if the element already has children */
    if( ! _LEAF_ELEMENT(e) )
        {
            UNLOCK(e->elem_lock->lock);
            return ;
        }

    /* Subdivide edge structures */
    subdivide_edge( e->e12, (float)0.5, process_id ) ;
    subdivide_edge( e->e23, (float)0.5, process_id ) ;
    subdivide_edge( e->e31, (float)0.5, process_id ) ;
    ev_12 = e->e12->ea->pb ;
    ev_23 = e->e23->ea->pb ;
    ev_31 = e->e31->ea->pb ;

    /* Then create new edges */
    e_12_23 = create_edge( ev_12, ev_23, process_id ) ;
    e_23_31 = create_edge( ev_23, ev_31, process_id ) ;
    e_31_12 = create_edge( ev_31, ev_12, process_id ) ;

    /* Area parameters */
    quarter_area = e->area * (float)0.25 ;

    /* (1) Create the center patch */
    enew = get_element(process_id) ;
    ecenter = enew ;
    enew->parent= e ;
    enew->patch = e->patch ;
    enew->ev1   = ev_23 ;
    enew->ev2   = ev_31 ;
    enew->ev3   = ev_12 ;
    enew->e12   = e_23_31 ;
    enew->e23   = e_31_12 ;
    enew->e31   = e_12_23 ;
    enew->area  = quarter_area ;
    enew->rad   = e->rad ;

    /* (2) Create the top patch */
    rev_12 = EDGE_REVERSE( e->e12, e->ev1, e->ev2 ) ;
    rev_23 = EDGE_REVERSE( e->e23, e->ev2, e->ev3 ) ;
    rev_31 = EDGE_REVERSE( e->e31, e->ev3, e->ev1 ) ;

    enew = get_element(process_id) ;
    e->top = enew ;
    enew->parent= e ;
    enew->patch = e->patch ;
    enew->ev1   = e->ev1 ;
    enew->ev2   = ev_12 ;
    enew->ev3   = ev_31 ;
    enew->e12   = (!rev_12)? e->e12->ea : e->e12->eb ;
    enew->e23   = e_31_12 ;
    enew->e31   = (!rev_31)? e->e31->eb : e->e31->ea ;
    enew->area  = quarter_area ;
    enew->rad   = e->rad ;

    /* (3) Create the left patch */
    enew = get_element(process_id) ;
    e->left = enew ;
    enew->parent= e ;
    enew->patch = e->patch ;
    enew->ev1   = ev_12 ;
    enew->ev2   = e->ev2 ;
    enew->ev3   = ev_23 ;
    enew->e12   = (!rev_12)? e->e12->eb : e->e12->ea ;
    enew->e23   = (!rev_23)? e->e23->ea : e->e23->eb ;
    enew->e31   = e_12_23 ;
    enew->area  = quarter_area ;
    enew->rad   = e->rad ;

    /* (4) Create the right patch */
    enew = get_element(process_id) ;
    e->right = enew ;
    enew->parent= e ;
    enew->patch = e->patch ;
    enew->ev1   = ev_31 ;
    enew->ev2   = ev_23 ;
    enew->ev3   = e->ev3 ;
    enew->e12   = e_23_31 ;
    enew->e23   = (!rev_23)? e->e23->eb : e->e23->ea ;
    enew->e31   = (!rev_31)? e->e31->ea : e->e31->eb ;
    enew->area  = quarter_area ;
    enew->rad   = e->rad ;

    /* Finally, set e->center */
    e->center = ecenter ;

    /* Unlock the element */
    UNLOCK(e->elem_lock->lock);
}
示例#16
0
Mesh marching_cubes(Grid& grid, double isovalue, SurfaceType surface_type)
{
    Mesh mesh;
    int offset[8];
    Vertex* vertices[12];
    unordered_set<Vertex*, vertex_hash, vertex_equals> vertex_set;
    
    compute_offset(grid, offset);

    for(int z = 0; z < grid.get_axis(2)-1; z++) {
        for(int y = 0; y < grid.get_axis(1)-1; y++) {
            for(int x = 0; x < grid.get_axis(0)-1; x++) {
                int ic = grid.index(x,y,z); 
                int table_index = get_index(grid, ic, offset, isovalue);

                for(int i = 0; i < 12; i++) {
                    if(edge_table[table_index] & (1 << i)) {
                        int v[3], u[3];
                        get_grid_edge(x, y, z, i, v, u);
                        double sv = grid[grid.index(v[0],v[1],v[2])];
                        double su = grid[grid.index(u[0],u[1],u[2])];
                        double vc[3] = {(v[0]-grid.get_axis(0)/2.0)*grid.get_spacing(0), 
                                        (v[1]-grid.get_axis(1)/2.0)*grid.get_spacing(1), 
                                        (v[2]-grid.get_axis(2)/2.0)*grid.get_spacing(2)};
                        double uc[3] = {(u[0]-grid.get_axis(0)/2.0)*grid.get_spacing(0),
                                        (u[1]-grid.get_axis(1)/2.0)*grid.get_spacing(1), 
                                        (u[2]-grid.get_axis(2)/2.0)*grid.get_spacing(2)};
                        if(surface_type == NO_SURFACE) {
                            vertices[i] = lerp(vc, sv, uc, su, isovalue);
                        } else {
                            vertices[i] = find_cut_point(vc, sv, uc, su, isovalue, surface_type);
                        }
                        vertices[i] = merge_vertex(vertices[i], vertex_set, mesh);
                    }
                }

                for(int i = 0; triangle_table[table_index][i] != -1; i += 3) {
                    Vertex *v0, *v1, *v2;

                    v0 = vertices[triangle_table[table_index][i]];
                    v1 = vertices[triangle_table[table_index][i+1]];
                    v2 = vertices[triangle_table[table_index][i+2]];

                    if(!is_degenerate(v0, v1, v2)) {
                        Face* f = new Face;
                        
                        f->v.push_back(v0);
                        f->v.push_back(v1);
                        f->v.push_back(v2);
                        v0->f.push_back(f);
                        v1->f.push_back(f);
                        v2->f.push_back(f);

                        Edge* e0 = create_edge(v0, v1, f, mesh);
                        Edge* e1 = create_edge(v1, v2, f, mesh);
                        Edge* e2 = create_edge(v2, v0, f, mesh);
                    
                        f->e.push_back(e0);
                        f->e.push_back(e1);
                        f->e.push_back(e2);
                        mesh.add(f);
                    }
                }
            }
        }
    }
    compute_normals(mesh);
    return mesh;
}
示例#17
0
文件: 1.c 项目: enbarberis/algoritmi
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;
}