/* dfs: * Return the number of reversed edges for this component. */ static int dfs(Agraph_t * g, Agnode_t * t, int hasCycle) { Agedge_t *e; Agedge_t *f; Agnode_t *h; ND_mark(t) = 1; ND_onstack(t) = 1; for (e = agfstout(g, t); e; e = f) { f = agnxtout(g, e); if (agtail(e) == aghead(e)) continue; h = aghead(e); if (ND_onstack(h)) { if (agisstrict(g)) { if (agedge(g, h, t, 0, 0) == 0) addRevEdge(g, e); } else { char* key = agnameof (e); if (!key || (agedge(g, h, t, key, 0) == 0)) addRevEdge(g, e); } agdelete(g, e); hasCycle = 1; } else if (ND_mark(h) == 0) hasCycle |= dfs(g, h, hasCycle); } ND_onstack(t) = 0; return hasCycle; }
static void write_hdr(Agraph_t * g, GVJ_t * job, int top, state_t* sp) { char *name; name = agnameof(g); indent(job, sp->Level); gvprintf(job, "\"name\": \"%s\"", stoj (name, sp)); if (top) { gvputs(job, ",\n"); indent(job, sp->Level); gvprintf(job, "\"directed\": %s,\n", (agisdirected(g)?"true":"false")); indent(job, sp->Level); gvprintf(job, "\"strict\": %s", (agisstrict(g)?"true":"false")); } }
grafo le_grafo(FILE *input) { Agraph_t *ag = agread(input, 0); if(!(ag && agisstrict(ag))) return NULL; // struct grafo *g = malloc(sizeof(struct grafo)); grafo g = malloc(sizeof(struct grafo)); if( !g ) return NULL; g->vertices = constroi_lista(); g->nome = malloc(sizeof(char) * strlen(agnameof(ag)+1)); strcpy(g->nome, agnameof(ag)); g->direcionado = agisdirected(ag); g->n_vertices = (unsigned int)agnnodes(ag); g->n_arestas = (unsigned int)agnedges(ag); g->ponderado = 0; for( Agnode_t *v = agfstnode(ag); v; v = agnxtnode(ag,v) ){ vertice vt = malloc(sizeof(struct vertice)); vt->nome = malloc(sizeof(char) * strlen(agnameof(v))+1); strcpy( vt->nome, agnameof(v) ); vt->visitado = 0; vt->coberto = 0; vt->arestas_saida = constroi_lista(); vt->arestas_entrada = constroi_lista(); insere_lista(vt, g->vertices); } for( Agnode_t *v = agfstnode(ag); v; v = agnxtnode(ag,v) ){ vertice vt = busca_vertice(g->vertices, agnameof(v)); if( g-> direcionado ){ for( Agedge_t *e = agfstout(ag,v); e; e = agnxtout(ag,e) ){ aresta at = cria_aresta(g->vertices, e); if( at->peso != 0 ) g->ponderado = 1; insere_lista(at, vt->arestas_saida); } for( Agedge_t *e = agfstin(ag,v); e; e = agnxtin(ag,e) ){ aresta at = cria_aresta(g->vertices, e); if( at->peso != 0 ) g->ponderado = 1; insere_lista(at, vt->arestas_entrada); } } else { for( Agedge_t *e = agfstedge(ag,v); e; e = agnxtedge(ag,e,v) ){ if( agtail(e) != v ) continue; aresta at = cria_aresta(g->vertices, e); if( at->peso != 0 ) g->ponderado = 1; insere_lista(at, at->origem->arestas_saida); insere_lista(at, at->destino->arestas_saida); } } } if( agclose(ag) ) return NULL; return g; }