static void place_arc_x(int x, int y) { canvas_leftbut_proc = null_proc; canvas_middlebut_proc = null_proc; canvas_rightbut_proc = null_proc; canvas_locmove_proc = null_proc; canvas_ref_proc = null_proc; adjust_pos(x, y, fix_x, fix_y, &x, &y); translate_arc(new_a, x - fix_x, y - fix_y); if (return_proc == copy_selected) { add_arc(new_a); } else { list_add_arc(&objects.arcs, new_a); clean_up(); set_lastposition(fix_x, fix_y); set_newposition(x, y); set_action_object(F_MOVE, O_ARC); set_latestarc(new_a); set_modifiedflag(); } redisplay_arc(new_a); /* turn back on all relevant markers */ update_markers(new_objmask); (*return_proc) (); draw_mousefun_canvas(); }
/*recebe o nome do arquivo que contem a definicao do probelma e constroi o grafo correspondente*/ Graph le_entrada(char* nome_entrada){ FILE* input = fopen(nome_entrada, "r"); /*abre arquivo de entrada*/ int n; /*numero de vertices*/ Vertex origem, destino, u, v; Graph g; double cost, demanda; if(input == NULL){ puts("O arquivo passado como argumento nao exite!"); exit(-2); } fscanf(input, "%d", &n);/*le o numero de vertices*/ fscanf(input, "%d", &origem); /*le o vertice de origem*/ fscanf(input, "%d", &destino); /*le o vertice de destino*/ fscanf(input, "%lf", &demanda); /*le a quantidade de produto escoada*/ /*inicializa a rede*/ g = init_graph(n, origem, destino, demanda); while(fscanf(input, "%d %d %lf", &u,&v,&cost) != EOF){ /*equanto o arquivo nao acabar, le vertice_origem, vertice_destino e custo_aresta*/ add_arc(g,u,v,cost,0); /*adiciona arco lido na rede*/ } fclose(input); printf("A configuracao presente em %s foi lida com sucesso.\n", nome_entrada); return g; /*retona o grafo descrito pela entrada*/ }
void Graph::invert_arc(int from, int to) { /* inverser. */ int length = get_arc(from, to).length; delete_arc(from, to); add_arc(to, from, length); }
int main () { Automaton *a = create_automaton(); int e0 = add_state(a); int e1 = add_state(a); int e2 = add_state(a); set_initial(a, e1); set_final(a, e2); add_arc(a, e0, e1, 1); add_arc(a, e1, e1, 2); add_arc(a, e1, e2, 3); print_automaton(a); return 0; }
/* * See Figure 11-2, "The Hungarian method", page 251. * * Corresponds to lines 12--17. */ static void hm_construct_auxiliary_graph( hm_data *hm ) { int i, j; A.size = 0; for EACH_V( i ) { EXPOSED( i ) = blank; LABEL( i ) = blank; /* * The following data structure is not included in the Figure 11-2 * pseudo-code implementation. It has been added to account for * "labeling" on certain vertices described within Example 11.1 that * would otherwise be missing from the Figure 11-2 implementation. * * count[v] for any v \in V is equal to the size of the set * { u \in U : nhbor[u] = v }. When this set is non-empty, v is * considered to be "labeled". The use of this new data structure is * only to complete the conditional check on "labeled" statuses when * updating alpha within "procedure modify". */ COUNT( i ) = 0; } for EACH_U( j ) { SLACK( j ) = INT_MAX; /* * The following initialization of nhbor[] is necessary for proper usage * of the count[] array, whose addition and purpose is described above. */ NHBOR( j ) = blank; } for EACH_V( i ) { for EACH_U( j ) { if ( ALPHA( i ) + BETA( j ) == C( i, j ) ) { if ( MATE( j ) == blank ) { EXPOSED( i ) = j; } else if ( i != MATE( j ) ) { add_arc( &A, i, MATE( j ) ); } } } } }
/** * 读入并初始化一个图 * * @return 图的地址 * @author jianzhang.zj */ graph_type *read_graph() { int node_num, arc_num; printf("请输入图中节点的数量:\n"); scanf("%d", &node_num); printf("请输入图中边的数量:\n"); scanf("%d", &arc_num); graph_type *graph = get_graph(node_num, arc_num); printf("请输入图中所有的边:\n"); int i, u, v, value; for (i = 0; i < arc_num; i ++) { scanf("%d%d%d", &u, &v, &value); add_arc(graph, u, v, value); add_arc(graph, v, u, value); // 仅仅是无向图的时候需要添加反向边 } return graph; }
void Graph::create_acyclic_clique(vector<int> vertices, vector<int> lengths) { /* create_acyclic_clique */ for (int i=0; i<vertices.size(); i++) { int from = vertices[i]; for (int j = i+1; j < vertices.size(); j++) { debug("adding arc from %d to %d of length %d\n", from, vertices[j], lengths[i]); add_arc(from, vertices[j], lengths[i]); } } }
static void init_fliparc(F_arc *old_a, int px, int py) { F_arc *new_a; set_temp_cursor(wait_cursor); new_a = copy_arc(old_a); flip_arc(new_a, px, py, flip_axis); if (copy) { add_arc(new_a); } else { toggle_arcmarker(old_a); draw_arc(old_a, ERASE); change_arc(old_a, new_a); } /* redisplay objects under this object before it was rotated */ redisplay_arc(old_a); /* and this arc and any other objects on top */ redisplay_arc(new_a); reset_cursor(); }
// read the set of edges: (0-1), (1-2), (2-0), (4-5) void read(){ n = 11; m = 19; G = (vlist **)malloc(n*sizeof(vlist*)); int i = 0; for(; i < n; i++){ G[i] = NULL; } add_arc(0,1); // add arc a->b add_arc(0,2); // add arc a->c add_arc(0,4); // add arc a->e add_arc(1,2); // add arc b->c add_arc(2,3); // add arc c->d add_arc(1,3); // add arc b->d //add_arc(3,1); // add arc d->b uncomment this to make the graph non-dag add_arc(4,3); // add arc e->d add_arc(4,6); // add arc e->g add_arc(4,7); // add arc e->h add_arc(5,4); // add arc f->e add_arc(5,6); // add arc f->g add_arc(5,9); // add arc f->j add_arc(6,8); // add arc g->i add_arc(7,3); // add arc h->d add_arc(7,6); // add arc h->g add_arc(7,8); // add arc h->i add_arc(9,8); // add arc j->i add_arc(10,5); // add arc k->f add_arc(10,9); // add arc k->j }
/* * See Figure 11-2, "The Hungarian method", page 252. * * Corresponds to "procedure modify". */ static bool hm_modify( hm_data *hm ) { int i, j, theta_one; /* * Determine theta_one. */ theta_one = INT_MAX; for EACH_U( j ) { if ( 0 < SLACK( j ) && SLACK( j ) < theta_one ) { theta_one = SLACK( j ); } } theta_one /= 2; /* * Update the dual variable alpha. */ for EACH_V( i ) { /* * The following conditional expression has been changed from its form * in Figure 11-2. Here, an additional check on the count[] array is * performed to account for a certain type of "labeling" that is * mentioned in the Example 11.1 walk-through but is omitted from the * Figure 11-2 implementation. * * See the comments provided near the initialization of count[] in the * function hm_construct_auxiliary_graph(). */ if ( LABEL( i ) != blank || COUNT( i ) > 0 ) { ALPHA( i ) += theta_one; } else { ALPHA( i ) -= theta_one; } } /* * Update the dual variable beta. */ for EACH_U( j ) { if ( SLACK( j ) == 0 ) { BETA( j ) -= theta_one; } else { BETA( j ) += theta_one; } } /* * Update slack and check for new admissible edges. */ for EACH_U( j ) { if ( SLACK( j ) > 0 ) { SLACK( j ) -= 2 * theta_one; if ( SLACK( j ) == 0 ) { if ( MATE( j ) == blank ) { EXPOSED( NHBOR( j ) ) = j; hm_augment( hm, NHBOR( j ) ); return false; /* goto endstage */ } else { /* * The following statement corresponds to a pseudo-code * command that should be removed from the else-clause of * the modify procedure in Figure 11-2. * * LABEL( MATE( j ) ) = NHBOR( j ); * * The inclusion of the above statement causes the arc * added in one of the next statements to never be considered * in following "search" sub-stages during this stage, and it * partially duplicates what would happen in these sub-stages * if the arc were to be considered there. The result of * inclusion is (often) non-optimality of the algorithm's * output. */ /* * The next statement corresponds to a pseudo-code command * (in the same else-clause) that should be modified * slightly. In Figure 11-2, this command "pushes" mate[ u ] * into Q when it should be "pushing" nhbor[ u ] instead. * This is because the purpose of this command is to ensure * that the soon-to-be-added arc will be considered in the * next "search" sub-stage, and consideration is dependent * upon the arc-tail, not the arc-head. */ stack_push( &Q, NHBOR( j ) ); /* Note modification */ add_arc( &A, NHBOR( j ), MATE( j ) ); } } } } return true; }
int Table::read_file(const string &filename) { pair<map<string, size_t>::iterator, bool> ret; reset(); istream *infile; if (filename.empty()) { infile = &cin; } else { infile = new ifstream(filename.c_str()); if (!infile) { error("Cannot open file", filename.c_str()); } } size_t delim_len = delim.length(); size_t linenum = 0; string line; // current line while (getline(*infile, line)) { string from, to; // from and to fields size_t from_idx, to_idx; // indices of from and to nodes size_t pos = line.find(delim); if (pos != string::npos) { from = line.substr(0, pos); trim(from); if (!numeric) { from_idx = insert_mapping(from); } else { from_idx = strtol(from.c_str(), NULL, 10); } to = line.substr(pos + delim_len); trim(to); if (!numeric) { to_idx = insert_mapping(to); } else { to_idx = strtol(to.c_str(), NULL, 10); } add_arc(from_idx, to_idx); } linenum++; if (linenum && ((linenum % 100000) == 0)) { cerr << "read " << linenum << " lines, " << rows.size() << " vertices" << endl; } from.clear(); to.clear(); line.clear(); } cerr << "read " << linenum << " lines, " << rows.size() << " vertices" << endl; nodes_to_idx.clear(); if (infile != &cin) { delete infile; } reserve(idx_to_nodes.size()); return 0; }
int main(int argc, char *argv[]) { BUF *b; int c, n; FILE *fp; int bsize, ch, nused; BUF bufs[2]; setprogname(argv[0]); fp = NULL; while ((ch = getopt(argc, argv, "dlq")) != -1) switch (ch) { case 'd': debug = 1; break; case 'l': longest = 1; break; case 'q': quiet = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; switch (argc) { case 0: fp = stdin; break; case 1: if ((fp = fopen(*argv, "r")) == NULL) err(1, "%s", *argv); break; default: usage(); } for (b = bufs, n = 2; --n >= 0; b++) b->b_buf = grow_buf(NULL, b->b_bsize = 1024); /* parse input and build the graph */ for (n = 0, c = getc(fp);;) { while (c != EOF && isspace(c)) c = getc(fp); if (c == EOF) break; nused = 0; b = &bufs[n]; bsize = b->b_bsize; do { b->b_buf[nused++] = c; if (nused == bsize) b->b_buf = grow_buf(b->b_buf, bsize *= 2); c = getc(fp); } while (c != EOF && !isspace(c)); b->b_buf[nused] = '\0'; b->b_bsize = bsize; if (n) add_arc(bufs[0].b_buf, bufs[1].b_buf); n = !n; } (void)fclose(fp); if (n) errx(1, "odd data count"); /* do the sort */ tsort(); return(0); }