grafo le_grafo(FILE *input) { int i; Agnode_t *v; Agedge_t *a; Agraph_t *g_cgraph; g_cgraph = agread(input, NULL); if ( !g_cgraph ) return NULL; // Cria grafo grafo g = (grafo) malloc(sizeof(struct grafo)); g->tipo = agisdirected(g_cgraph); g->vertice = (vertice_t) malloc(agnnodes(g_cgraph) * sizeof(struct vertice_t)); //Copia nome do grafo g->nome = (char *) malloc(strlen(agnameof(g_cgraph))+1); strcpy(g->nome, agnameof(g_cgraph)); if (!g->vertice) return NULL; i = 0; // Copia vertices for (v=agfstnode(g_cgraph ); v; v=agnxtnode(g_cgraph ,v), i++) { // copia nome g->vertice[i].nome = (char *) malloc(strlen(agnameof(v))+1); strcpy(g->vertice[i].nome, agnameof(v)); // inicializa lista de arestas g->vertice[i].aresta = NULL; // seta next vertice if (i != agnnodes(g_cgraph) - 1) g->vertice[i].next = &g->vertice[i+1]; //set atribute Agnodeinfo_t *p; p = (Agnodeinfo_t*) agbindrec(v,"Agnodeinfo_t",sizeof(Agnodeinfo_t),TRUE); p->ref_v = &g->vertice[i]; } g->vertice[i-1].next = NULL; //Copia arestas i = 0; for (v=agfstnode(g_cgraph); v; v=agnxtnode(g_cgraph,v), i++) { if (g->tipo == NAODIRECIONADO){ for (a=agfstedge(g_cgraph,v); a; a=agnxtedge(g_cgraph,a,v)) add_aresta(g, &g->vertice[i], v, a); } else { for (a=agfstout(g_cgraph,v); a; a=agnxtout(g_cgraph,a)) add_aresta(g, &g->vertice[i], v, a); } } free(g_cgraph); return g; }
static cluster_data* cluster_map(graph_t *mastergraph, graph_t *g) { /* search meta-graph to find clusters */ graph_t *mg, *subg; node_t *mm, *mn; node_t *n; edge_t *me; /* array of arrays of node indices in each cluster */ int **cs,*cn; int i,j,nclusters=0; bool* assigned = N_NEW(agnnodes(g), bool); cluster_data *cdata = GNEW(cluster_data); cdata->ntoplevel = agnnodes(g); mm = mastergraph->meta_node; mg = mm->graph; for (me = agfstout(mg, mm); me; me = agnxtout(mg, me)) { mn = me->head; subg = agusergraph(mn); if (!strncmp(subg->name, "cluster", 7)) { nclusters++; } } cdata->nvars=0; cdata->nclusters = nclusters; cs = cdata->clusters = N_GNEW(nclusters,int*); cn = cdata->clustersizes = N_GNEW(nclusters,int); /* fprintf(stderr,"search %d clusters...\n",nclusters); */ for (me = agfstout(mg, mm); me; me = agnxtout(mg, me)) { mn = me->head; subg = agusergraph(mn); /* clusters are processed by separate calls to ordered_edges */ if (!strncmp(subg->name, "cluster", 7)) { int *c; *cn = agnnodes(subg); cdata->nvars += *cn; c = *cs++ = N_GNEW(*cn++,int); /* fprintf(stderr,"Cluster with %d nodes...\n",agnnodes(subg)); */ for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) { node_t *gn; int ind = 0; for (gn = agfstnode(g); gn; gn = agnxtnode(g, gn)) { if(gn->id==n->id) break; ind++; } /* fprintf(stderr," node=%s, id=%d, ind=%d\n",n->name,n->id,ind); */ *c++=ind; assigned[ind]=true; cdata->ntoplevel--; } } }
/* makeInfo: * For each node in the graph, create a Info data structure */ static void makeInfo(Agraph_t * graph) { Agnode_t *node; int i; Info_t *ip; nsites = agnnodes(graph); geominit(); nodeInfo = N_GNEW(nsites, Info_t); node = agfstnode(graph); ip = nodeInfo; pmargin = expFactor (graph); for (i = 0; i < nsites; i++) { ip->site.coord.x = ND_pos(node)[0]; ip->site.coord.y = ND_pos(node)[1]; makePoly(&ip->poly, node, pmargin); ip->site.sitenbr = i; ip->site.refcnt = 1; ip->node = node; ip->verts = NULL; node = agnxtnode(graph, node); ip++; } }
int main() { FILE * fp = fopen("test", "r"); assert(fp); Agraph_t * graph = agread(fp, NULL); assert(graph); printf("Number of nodes is %d\n", agnnodes(graph)); printf("Number of edges is %d\n", agnedges(graph)); /* Iterate through nodes */ Agnode_t * node = agfstnode(graph); while (node != NULL) { assert(node); printf("Iterating through yet another node\n"); char * label = agget(node, "label"); printf("Node label is %s\n", label); Agedge_t * edge = agfstout(graph, node); while (edge != NULL) { assert(edge); printf("Iterating through yet another edge \n"); assert(agtail(edge) == node); edge = agnxtout(graph, edge); } /* Move on to the next node */ printf("\n"); node = agnxtnode(graph, node); } /* Free graph structure */ agclose(graph); return 0; }
grafo le_grafo(FILE *input){ if (!input) return NULL; Agraph_t *Ag = agread(input, NULL); if(!Ag) return NULL; grafo g = cria_grafo(agnameof(Ag), agisdirected(Ag), contem_pesos(Ag), agnnodes(Ag)); for (Agnode_t *Av=agfstnode(Ag); Av; Av=agnxtnode(Ag,Av)) { cria_vertice(g, agnameof(Av)); } for (Agnode_t *Av=agfstnode(Ag); Av; Av=agnxtnode(Ag,Av)) { for (Agedge_t *Ae=agfstout(Ag,Av); Ae; Ae=agnxtout(Ag,Ae)) { vertice u = v_busca(g, agnameof(agtail(Ae))); vertice v = v_busca(g, agnameof(aghead(Ae))); cria_vizinhanca(g, u, v, get_peso(Ae)); } } agclose(Ag); agfree(Ag, NULL); return g; }
//------------------------------------------------------------------------------ static Agraph_t *mostra_grafo(Agraph_t *g) { if ( ! g ) return NULL; direcionado = agisdirected(g); n_vertices = agnnodes(g); n_arestas = agnedges(g); lista_arestas = constroi_lista(); printf("strict %sgraph \"%s\" {\n\n", agisdirected(g) ? "di" : "", agnameof(g) ); mostra_vertices(g); printf("\n"); mostra_arestas(); printf("}\n"); destroi_lista(lista_arestas, NULL); return g; }
/* processClusterEdges: * Look for cluster edges. Replace cluster edge endpoints * corresponding to a cluster with special cluster nodes. * Delete original nodes. * Return 0 if no cluster edges; 1 otherwise. */ int processClusterEdges(graph_t * g) { int rv; node_t *n; node_t *nxt; edge_t *e; graph_t *clg; agxbuf xb; Dt_t *map; Dt_t *cmap = mkClustMap (g); unsigned char buf[SMALLBUF]; map = dtopen(&mapDisc, Dtoset); clg = agsubg(g, "__clusternodes",1); agbindrec(clg, "Agraphinfo_t", sizeof(Agraphinfo_t), TRUE); agxbinit(&xb, SMALLBUF, buf); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { if (IS_CLUST_NODE(n)) continue; for (e = agfstout(g, n); e; e = agnxtout(g, e)) { checkCompound(e, clg, &xb, map, cmap); } } agxbfree(&xb); dtclose(map); rv = agnnodes(clg); for (n = agfstnode(clg); n; n = nxt) { nxt = agnxtnode(clg, n); agdelete(g, n); } agclose(clg); if (rv) SET_CLUST_EDGE(g); dtclose(cmap); return rv; }
/* populates rank lists of g. there are some key details: 1) the input graph ordering must be respected (in left to right initialization) 2) connected components are separated and marked with indices 3) series-parallel graphs (includes trees, obviously) must not have crossings */ static void build_ranks(Agraph_t *g, boolean down) { queue *q; component_t c; int r; Agnode_t *n; Agedge_t *e; c = build_components(g, down); /* process each each component */ q = new_queue(agnnodes(g)+1); for (r = 0; r < c.r; r++) { enqueue(q,c.root[r]); if ((r + 1 >= c.r)||(ND_component(c.root[r])!=ND_component(c.root[r+1]))) { while ((n = dequeue(q))) { install(g,n); if (down) { for (e = agfstout(g,n); e; e = agnxtout(g,e)) if (--ND_priority(e->head) == 0) enqueue(q,e->head); } else { for (e = agfstin(g,n); e; e = agnxtin(g,e)) if (--ND_priority(e->tail) == 0) enqueue(q,e->head); } } } } free_queue(q); }
void fdp_init_node_edge(graph_t * g) { attrsym_t *E_len; node_t *n; edge_t *e; int nn = agnnodes(g); int i; ndata* alg = N_NEW(nn, ndata); processClusterEdges(g); GD_neato_nlist(g) = N_NEW(nn + 1, node_t *); for (i = 0, n = agfstnode(g); n; n = agnxtnode(g, n)) { neato_init_node (n); ND_alg(n) = alg + i; GD_neato_nlist(g)[i] = n; ND_id(n) = i++; } E_len = agfindattr(g->proto->e, "len"); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { for (e = agfstout(g, n); e; e = agnxtout(g, e)) { init_edge(e, E_len); } } initialPositions(g); }
/* processClusterEdges: * Look for cluster edges. Replace cluster edge endpoints * corresponding to a cluster with special cluster nodes. * Delete original nodes. * Return 0 if no cluster edges; 1 otherwise. */ int processClusterEdges(graph_t * g) { int rv; node_t *n; edge_t *e; graph_t *clg; agxbuf xb; Dt_t *map; unsigned char buf[SMALLBUF]; map = dtopen(&mapDisc, Dtoset); clg = agsubg(g, "__clusternodes"); agxbinit(&xb, SMALLBUF, buf); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { for (e = agfstout(g, n); e; e = agnxtout(g, e)) { checkCompound(e, clg, &xb, map); } } agxbfree(&xb); dtclose(map); rv = agnnodes(clg); for (n = agfstnode(clg); n; n = agnxtnode(clg, n)) { agdelete(g, n); } agclose(clg); if (rv) SET_CLUST_EDGE(g); return rv; }
static void emit (Agraph_t* g, int root) { int n_edges = agnedges(g); int n_nodes = agnnodes(g); int n_cc = 0; int n_cl = 0; char* file = 0; if (flags & CC) n_cc = cc_decompose (g); if (flags & CL) n_cl = GD_cl_cnt(g); if (root) file = fname; wcp (n_nodes, n_edges, n_cc, n_cl, g->name, file); if (root) { n_graphs++; tot_edges += n_edges; tot_nodes += n_nodes; tot_cc += n_cc; tot_cl += n_cl; } }
/* mkMaze: */ maze* mkMaze (graph_t* g, int doLbls) { node_t* n; maze* mp = NEW(maze); boxf* rects; int i, nrect; cell* cp; double w2, h2; boxf bb, BB; mp->ngcells = agnnodes(g); cp = mp->gcells = N_NEW(mp->ngcells, cell); BB.LL.x = BB.LL.y = MAXDOUBLE; BB.UR.x = BB.UR.y = -MAXDOUBLE; for (n = agfstnode (g); n; n = agnxtnode(g,n)) { w2 = ND_xsize(n)/2.0; if (w2 < 1) w2 = 1; h2 = ND_ysize(n)/2.0; if (h2 < 1) h2 = 1; bb.LL.x = ND_coord(n).x - w2; bb.UR.x = ND_coord(n).x + w2; bb.LL.y = ND_coord(n).y - h2; bb.UR.y = ND_coord(n).y + h2; BB.LL.x = MIN(BB.LL.x, bb.LL.x); BB.LL.y = MIN(BB.LL.y, bb.LL.y); BB.UR.x = MAX(BB.UR.x, bb.UR.x); BB.UR.y = MAX(BB.UR.y, bb.UR.y); cp->bb = bb; cp->flags |= MZ_ISNODE; ND_alg(n) = cp; cp++; } if (doLbls) { } BB.LL.x -= MARGIN; BB.LL.y -= MARGIN; BB.UR.x += MARGIN; BB.UR.y += MARGIN; rects = partition (mp->gcells, mp->ngcells, &nrect, BB); #ifdef DEBUG if (odb_flags & ODB_MAZE) psdump (mp->gcells, mp->ngcells, BB, rects, nrect); #endif mp->cells = N_NEW(nrect, cell); mp->ncells = nrect; for (i = 0; i < nrect; i++) { mp->cells[i].bb = rects[i]; } free (rects); mp->sg = mkMazeGraph (mp, BB); return mp; }
/* find_longest_path: * Find and return longest path in tree. */ static nodelist_t* find_longest_path(Agraph_t* tree) { Agnode_t* n; Agedge_t* e; Agnode_t* common = 0; nodelist_t* path; nodelist_t* endPath; int maxlength = 0; int length; if (agnnodes(tree) == 1) { path = mkNodelist(); n = agfstnode(tree); appendNodelist(path, NULL, n); SET_ONPATH(n); return path; } for(n = agfstnode(tree); n; n = agnxtnode(tree, n)) { int count = 0; for(e = agfstedge(tree, n); e; e = agnxtedge(tree, e, n)) { count++; } if(count == 1) measure_distance(n, n, 0, NULL); } /* find the branch node rooted at the longest path */ for(n = agfstnode(tree); n; n = agnxtnode(tree, n)) { length = DISTONE(n) + DISTTWO(n); if(length > maxlength) { common = n; maxlength = length; } } path = mkNodelist(); for (n = LEAFONE(common); n != common; n = TPARENT(n)) { appendNodelist(path, NULL, n); SET_ONPATH(n); } appendNodelist(path, NULL, common); SET_ONPATH(common); if (DISTTWO(common)) { /* 2nd path might be empty */ endPath = mkNodelist(); for (n = LEAFTWO(common); n != common; n = TPARENT(n)) { appendNodelist(endPath, NULL, n); SET_ONPATH(n); } reverseAppend(path, endPath); } return path; }
/* scan_graph_mode: * Prepare the graph and data structures depending on the layout mode. * If Reduce is true, eliminate singletons and trees. Since G may be a * subgraph, we remove the nodes from the root graph. * Return the number of nodes in the reduced graph. */ int scan_graph_mode(graph_t * G, int mode) { int i, lenx, nV, nE, deg; char *str; node_t *np, *xp, *other; double total_len = 0.0; if (Verbose) fprintf(stderr, "Scanning graph %s, %d nodes\n", G->name, agnnodes(G)); /* Eliminate singletons and trees */ if (Reduce) { for (np = agfstnode(G); np; np = xp) { xp = agnxtnode(G, np); deg = degreeKind(G, np, &other); if (deg == 0) { /* singleton node */ agdelete(G->root, np); } else if (deg == 1) { agdelete(G->root, np); xp = prune(G, other, xp); } } } nV = agnnodes(G); nE = agnedges(G); lenx = agindex(G->root->proto->e, "len"); if (mode == MODE_KK) { Epsilon = .0001 * nV; getdouble(G, "epsilon", &Epsilon); if ((str = agget(G->root, "Damping"))) Damping = atof(str); else Damping = .99; GD_neato_nlist(G) = N_NEW(nV + 1, node_t *); for (i = 0, np = agfstnode(G); np; np = agnxtnode(G, np)) { GD_neato_nlist(G)[i] = np; ND_id(np) = i++; ND_heapindex(np) = -1; total_len += setEdgeLen(G, np, lenx); } } else {
static void circular_init_node_edge(graph_t * g) { node_t *n; edge_t *e; int i = 0; ndata* alg = N_NEW(agnnodes(g), ndata); GD_neato_nlist(g) = N_NEW(agnnodes(g) + 1, node_t *); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { neato_init_node(n); ND_alg(n) = alg + i; GD_neato_nlist(g)[i++] = n; } for (n = agfstnode(g); n; n = agnxtnode(g, n)) { for (e = agfstout(g, n); e; e = agnxtout(g, e)) { circular_init_edge(e); } } }
/* twopi_layout: */ void twopi_layout(Agraph_t * g) { Agnode_t *ctr = 0; char *s; twopi_init_graph(g); s = agget(g, "root"); if (s && (*s != '\0')) { ctr = agfindnode(g, s); if (!ctr) { agerr(AGWARN, "specified root node \"%s\" was not found.", s); agerr(AGPREV, "Using default calculation for root node\n"); } } if (agnnodes(g)) { Agraph_t **ccs; Agraph_t *sg; Agnode_t *c = NULL; int ncc; int i; ccs = ccomps(g, &ncc, 0); if (ncc == 1) { circleLayout(g, ctr); adjustNodes(g); spline_edges(g); } else { pack_info pinfo; pack_mode pmode = getPackMode(g, l_node); for (i = 0; i < ncc; i++) { sg = ccs[i]; if (ctr && agcontains(sg, ctr)) c = ctr; else c = 0; nodeInduce(sg); circleLayout(sg, c); adjustNodes(sg); } spline_edges(g); pinfo.margin = getPack(g, CL_OFFSET, CL_OFFSET); pinfo.doSplines = 1; pinfo.mode = pmode; pinfo.fixed = 0; packSubgraphs(ncc, ccs, g, &pinfo); } for (i = 0; i < ncc; i++) { agdelete(g, ccs[i]); } free(ccs); } dotneato_postprocess(g); }
/* fdp_tLayout: * Given graph g with ports nodes, layout g respecting ports. * If some node have position information, it may be useful to * reset temperature and other parameters to reflect this. */ void fdp_tLayout (graph_t* g, xparams* xpms) { int i; int reset; bport_t* pp = PORTS(g); double temp; Grid* grid; pointf ctr; Agnode_t* n; reset = init_params(g, xpms); temp = T0; ctr = initPositions (g, pp); if (T_useGrid) { grid = mkGrid (agnnodes (g)); adjustGrid (grid, agnnodes (g)); for (i = 0; i < loopcnt; i++) { temp = cool (temp, i); gAdjust (g, temp, pp, grid); } delGrid (grid); } else { for (i = 0; i < loopcnt; i++) { temp = cool (temp, i); adjust (g, temp, pp); } } if ((ctr.x != 0.0) || (ctr.y != 0.0)) { for (n = agfstnode(g); n; n = agnxtnode(g,n)) { ND_pos(n)[0] += ctr.x; ND_pos(n)[1] += ctr.y; } } dumpstat (g); if (reset) reset_params (); }
static void select_anchors(Agraph_t* g, mat x, int* anchors, int k) { int i, j, max_dist, max_index; Agnode_t* n; double *d, *dmin; n = agfstnode(g); d = dijkstra(g,n); dmin = (double*) malloc(sizeof(double)*agnnodes(g)); darrset(dmin, agnnodes(g), DBL_MAX); max_dist = d[0]; max_index = 0; for(i = 0; i < agnnodes(g); i++) { if(max_dist < d[i]) { max_dist = d[i]; max_index = i; } } for(i = 0; i < k; i++) { n = get_node(max_index); anchors[i] = max_index; free(d); d = dijkstra(g,n); for(j = 0; j < agnnodes(g); j++) { x->m[mindex(i,j,x)] = d[j]; dmin[j] = MIN(dmin[j],d[j]); } max_dist = dmin[0]; max_index = 0; for(j = 0; j < agnnodes(g); j++) { if(max_dist < dmin[j]) { max_dist = dmin[j]; max_index = j; } } } free(d); free(dmin); }
static void attachPos (Agraph_t* g) { node_t* np; double* ps = N_NEW(2*agnnodes(g), double); for (np = agfstnode(g); np; np = agnxtnode(g, np)) { ND_pos(np) = ps; ps[0] = PS2INCH(ND_coord(np).x); ps[1] = PS2INCH(ND_coord(np).y); ps += 2; } }
/* Run the network simplex algorithm on each component. */ void rank1(graph_t* g) { int maxiter = MAXINT; int c; char *s; if ((s = agget(g,"nslimit1"))) maxiter = atof(s) * agnnodes(g); for (c = 0; c < GD_comp(g).size; c++) { GD_nlist(g) = GD_comp(g).list[c]; rank(g,(GD_n_cluster(g) == 0?1:0),maxiter); /* TB balance */ } }
static v_data *makeGraph(Agraph_t* gg, int *nedges) { int i; int ne = agnedges(gg); int nv = agnnodes(gg); v_data *graph = N_NEW(nv, v_data); int *edges = N_NEW(2 * ne + nv, int); /* reserve space for self loops */ float *ewgts = N_NEW(2 * ne + nv, float); Agnode_t *np; Agedge_t *ep; Agraph_t *g = NULL; int i_nedges; ne = 0; i=0; // for (i = 0; i < nv; i++) { for (np = agfstnode(gg); np; np = agnxtnode(gg, np)) { graph[i].edges = edges++; /* reserve space for the self loop */ graph[i].ewgts = ewgts++; #ifdef STYLES graph[i].styles = NULL; #endif i_nedges = 1; /* one for the self */ if (!g) g = agraphof(np); for (ep = agfstedge(g, np); ep; ep = agnxtedge(g, ep, np)) { Agnode_t *vp; Agnode_t *tp = agtail(ep); Agnode_t *hp = aghead(ep); assert(hp != tp); /* FIX: handle multiedges */ vp = (tp == np ? hp : tp); ne++; i_nedges++; // *edges++ = ((temp_node_record *) AGDATA(vp))->TVref; *edges++ = ND_TVref(vp); *ewgts++ = 1; } graph[i].nedges = i_nedges; graph[i].edges[0] = i; graph[i].ewgts[0] = 1 - i_nedges; i++; } ne /= 2; /* each edge counted twice */ *nedges = ne; return graph; }
static void process(Agraph_t* G) { Agnode_t* n; Agraph_t* map; int nc = 0; float nontree_frac; int Maxdegree; Stack stack; sccstate state; aginit(G,AGRAPH,"scc_graph",sizeof(Agraphinfo_t),TRUE); aginit(G,AGNODE,"scc_node",sizeof(Agnodeinfo_t),TRUE); state.Comp = state.ID = 0; state.N_nodes_in_nontriv_SCC = 0; if (Verbose) nc = countComponents(G,&Maxdegree,&nontree_frac); initStack(&stack, agnnodes(G) + 1); map = agopen("scc_map",Agdirected,(Agdisc_t *)0); for (n = agfstnode(G); n; n = agnxtnode(n)) if (getval(n) == 0) visit(n,map,&stack,&state); freeStack(&stack); if (!Silent) agwrite(map,stdout); agclose(map); if (Verbose) fprintf(stderr,"%d %d %d %d %.4f %d %.4f\n", agnnodes(G), agnedges(G), nc, state.Comp, state.N_nodes_in_nontriv_SCC / (double) agnnodes(G), Maxdegree, nontree_frac); else fprintf(stderr,"%d nodes, %d edges, %d strong components\n", agnnodes(G), agnedges(G), state.Comp); }
static mat get_positions(Agraph_t* g, int dim) { int i; mat z = mat_new(agnnodes(g),dim); for(i = 0; i < z->r; i++) { Agnode_t* n = get_node(i); char* pos = agget(n,"pos"); if(dim == 2) { sscanf(pos,"%lf,%lf",&z->m[mindex(i,0,z)],&z->m[mindex(i,1,z)]); } else { sscanf(pos,"%lf,%lf,%lf",&z->m[mindex(i,0,z)],&z->m[mindex(i,1,z)],&z->m[mindex(i,2,z)]); } } return z; }
static void get_graph_node_attribute(Agraph_t* g, char *tag, char *format, size_t len, void *x_default_val, int *nn, void *x, int *flag){ /* read a node attribute. Example {real x0[2]; real *x = NULL; x0[0] = x0[1] = 0.; get_graph_node_attribute(g, "pos", "%lf,%lf", sizeof(real)*2, x0, &n, &x, &flag) or, do not supply x0 if you want error flagged when pos tag is not available: get_graph_node_attribute(g, "pos", "%lf,%lf", sizeof(real)*2, NULL, &n, x, &flag); assert(!flag); FREE(x); } */ Agnode_t* n; int nnodes; *flag = 0; if (!g) { *flag = -1; return; } nnodes = agnnodes (g); *nn = nnodes; for (n = agfstnode (g); n; n = agnxtnode (g, n)) { if (agget(n, tag)){ if (strcmp(format,"%s") == 0){ strcpy(x, agget(n, tag)); } else { sscanf(agget(n, tag), format, x); } } else if (x_default_val){ memcpy(x, x_default_val, len); } else { *flag = -1; return; } x += len; } }
/* init_node_edge: * initialize node and edge attributes */ static void init_node_edge(Agraph_t * g) { node_t *n; edge_t *e; int nG = agnnodes(g); attrsym_t *N_pos = agfindattr(g->proto->n, "pos"); attrsym_t *N_pin = agfindattr(g->proto->n, "pin"); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { neato_init_node(n); user_pos(N_pos, N_pin, n, nG); /* set user position if given */ } for (n = agfstnode(g); n; n = agnxtnode(g, n)) { for (e = agfstout(g, n); e; e = agnxtout(g, e)) common_init_edge(e); } }
/* dumpE: */ void dumpE(graph_t * g, int derived) { Agnode_t *n; Agedge_t *e; Agedge_t **ep; Agedge_t *el; int i; int deg; prIndent(); fprintf(stderr, "Graph %s : %d nodes %d edges\n", g->name, agnnodes(g), agnedges(g)); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { deg = 0; for (e = agfstout(g, n); e; e = agnxtout(g, e)) { deg++; prIndent(); fprintf(stderr, " %s -- %s\n", e->tail->name, e->head->name); if (derived) { for (i = 0, ep = (Agedge_t **) ED_to_virt(e); i < ED_count(e); i++, ep++) { el = *ep; prIndent(); fprintf(stderr, " %s -- %s\n", el->tail->name, el->head->name); } } } if (deg == 0) { /* no out edges */ if (!agfstin(g, n)) /* no in edges */ fprintf(stderr, " %s\n", n->name); } } if (!derived) { bport_t *pp; if ((pp = PORTS(g))) { int sz = NPORTS(g); fprintf(stderr, " %d ports\n", sz); while (pp->e) { fprintf(stderr, " %s : %s -- %s\n", pp->n->name, pp->e->tail->name, pp->e->head->name); pp++; } } } }
static void twopi_init_node_edge(graph_t * g) { node_t *n; edge_t *e; int i = 0; GD_neato_nlist(g) = N_NEW(agnnodes(g) + 1, node_t *); for (n = agfstnode(g); n; n = agnxtnode(g, n)) { GD_neato_nlist(g)[i++] = n; twopi_init_node(n); } for (n = agfstnode(g); n; n = agnxtnode(g, n)) { for (e = agfstout(g, n); e; e = agnxtout(g, e)) { twopi_init_edge(e); } } }
/* remove_pair_edges: * Create layout skeleton of ing. * Why is returned graph connected? */ static Agraph_t *remove_pair_edges(Agraph_t * ing) { int counter = 0; int nodeCount; Agraph_t *outg; Agraph_t *g; deglist_t *dl; Agnode_t *currnode, *adjNode; Agedge_t *e; outg = clone_graph(ing, &g); nodeCount = agnnodes(g); dl = getList(g); while (counter < (nodeCount - 3)) { currnode = firstDeglist(dl); /* Remove all adjacent nodes since they have to be reinserted */ for (e = agfstedge(g, currnode); e; e = agnxtedge(g, e, currnode)) { adjNode = e->head; if (currnode == adjNode) adjNode = e->tail; removeDeglist(dl, adjNode); } find_pair_edges(g, currnode, outg); for (e = agfstedge(g, currnode); e; e = agnxtedge(g, e, currnode)) { adjNode = e->head; if (currnode == adjNode) adjNode = e->tail; DEGREE(adjNode)--; insertDeglist(dl, adjNode); } agdelete(g, currnode); counter++; } agclose(g); freeDeglist(dl); return outg; }
/* makeInfo: * For each node in the graph, create a Info data structure */ static int makeInfo(Agraph_t * graph) { Agnode_t *node; int i; Info_t *ip; expand_t pmargin; int (*polyf)(Poly *, Agnode_t *, float, float); nsites = agnnodes(graph); geominit(); nodeInfo = N_GNEW(nsites, Info_t); node = agfstnode(graph); ip = nodeInfo; pmargin = sepFactor (graph); if (pmargin.doAdd) { polyf = makeAddPoly; /* we need inches for makeAddPoly */ pmargin.x = PS2INCH(pmargin.x); pmargin.y = PS2INCH(pmargin.y); } else polyf = makePoly; for (i = 0; i < nsites; i++) { ip->site.coord.x = ND_pos(node)[0]; ip->site.coord.y = ND_pos(node)[1]; if (polyf(&ip->poly, node, pmargin.x, pmargin.y)) { free (nodeInfo); nodeInfo = NULL; return 1; } ip->site.sitenbr = i; ip->site.refcnt = 1; ip->node = node; ip->verts = NULL; node = agnxtnode(graph, node); ip++; } return 0; }
void init_graph(Agraph_t* g) { int i = 0; Agnode_t* n; weight = agattr(g, AGEDGE, "weight", "1.0"); pos = agattr(g, AGNODE, "pos", ""); color = agattr(g, AGNODE, "color", "black"); comment = agattr(g, AGRAPH, "cmd", ""); nodes = (Agnode_t**) malloc(sizeof(Agnode_t*)*agnnodes(g)); for(n = agfstnode(g); n; n = agnxtnode(g,n)) { agbindrec(n, (char*)"nodedata", sizeof(nodedata_t),1); setid(n,i); nodes[i] = n; i++; } }