dfs(graph *g, int v) { int i; /* counter */ int y; /* successor vertex */ if (finished) return; /* allow for search termination */ discovered[v] = TRUE; process_vertex(v); for (i=0; i<g->degree[v]; i++) { y = g->edges[v][i]; if (valid_edge(g->edges[v][i]) == TRUE) { if (discovered[y] == FALSE) { parent[y] = v; dfs(g,y); } else if (processed[y] == FALSE) process_edge(v,y); } if (finished) return; } processed[v] = TRUE; }
bfs(graph *g, int start) { queue q; /* queue of vertices to visit */ int v; /* current vertex */ int i; /* counter */ init_queue(&q); enqueue(&q,start); discovered[start] = TRUE; while (empty(&q) == FALSE) { v = dequeue(&q); process_vertex(v); processed[v] = TRUE; for (i=0; i<g->degree[v]; i++) if (valid_edge(g->edges[v][i]) == TRUE) { if (discovered[g->edges[v][i]] == FALSE) { enqueue(&q,g->edges[v][i]); discovered[g->edges[v][i]] = TRUE; parent[g->edges[v][i]] = v; } if (processed[g->edges[v][i]] == FALSE) process_edge(v,g->edges[v][i]); } } }
inline void append_failure_impl(std::set<Node>& visited, const Node& lhs, const Edge& rhs) { if (visited.find(lhs) == visited.end()) { visited.insert(lhs); if (valid_edge(lhs->on_failure)) { if (!overwrote_edge(lhs->on_failure, rhs)) append_failure_impl(visited, node_of(lhs->on_failure), rhs); } else lhs->on_failure = rhs; if (valid_edge(lhs->on_success) && !overwrote_edge(lhs->on_success, rhs)) append_success_impl(visited, node_of(lhs->on_success), rhs); if (valid_edge(lhs->on_invalid) && !overwrote_edge(lhs->on_invalid, rhs)) append_success_impl(visited, node_of(lhs->on_invalid), rhs); } }
int process_vertex(int v) { int i; bool valid_vertex=0; /* Evitamos los cálculos en el nodo si éste ha sido marcado como "congelado" (Alg. Rec.)*/ /* Igualmente, no calculamos los nodos invalidos */ /* Acumula el consumo total (Y002) por los receptores y guarda dicho valor en el nodo */ for (i=0; i< g.degree[v]; i++){ fprintf(flog,"Agregando consumo para nodo %s\n",n[v].label); n[v].utilizacion += g.consumo[v][i]; fprintf(flog,"Utilizacion = %.4f\n",n[v].utilizacion); } /* Revisa que la utilizacion no sea mayor que la capacidad cuando existe capacidad limitante */ if(n[v].flag_capacity){ // Reducimos la capacidad para evitar fugas de dinero // Usando fabs() estaba generando una alerta ?? double diff = abs(n[v].utilizacion - n[v].capacidad) / n[v].capacidad; if( diff < TOLERANCIA){ fprintf(flog,"Ajustando capacidad de nodo %d de %.2f a %.2f\n",v,n[v].capacidad,n[v].utilizacion); n[v].capacidad = n[v].utilizacion; } else { printf("Nodo %s tiene capacidad no utilizada.\nUtilizacion: %.2f\nCapacidad: %.2f\n", n[v].label, n[v].utilizacion, n[v].capacidad); } if (n[v].utilizacion > n[v].capacidad) { printf("Utilizacion %.2f excede capacidad %.2f para nodo %d con capacidad limitante\n",n[v].utilizacion,n[v].capacidad,v); exit(EXIT_FAILURE); } } else { // Nodo no tiene capacidad limitante -- su utilización es su capacidad n[v].capacidad = n[v].utilizacion; } /* Calcula tasa fija (RT01) y proporcional (RT02) para el nodo y guarda dicho valor en el nodo */ if(n[v].capacidad){ n[v].tasa_fija = (n[v].costo_primario_fijo + n[v].costo_secundario_fijo) / n[v].capacidad; } if(n[v].utilizacion) { n[v].tasa_proporcional = (n[v].costo_primario_proporcional + n[v].costo_secundario_proporcional) / n[v].utilizacion; } /* Procesamos las aristas que salen del nodo */ for (i=0; i< g.degree[v]; i++) if (valid_edge(v,i)) /* Verifica que el consumo sea mayor a cero */ process_edge(v,i); /* Calcula costos secundarios y los acumula en la estructura del nodo receptor */ fprintf(flog,"Se proceso el nodo %d (congelado=%d)\n",v,n[v].congelar_tasas); n[v].flag_tasa=1; return 0; }
void bfs(graph *g, int start) { queue q; init_queue(&q); enqueue(&q, start); discovered[start] = true; while (empty_queue(&q) == false) { int v = dequeue(&q); process_vertex_early(v); processed[v] = true; edgenode *p = g->edges[v]; while (p != NULL) { int y = p->y; if (valid_edge(p)) { if ((!processed[y]) || g->directed) { process_edge(v, y); } if (!discovered[y]) { enqueue(&q, y); discovered[y] = true; parent[y] = v; } } p = p->next; } process_vertex_late(v); } }
int find_first_step( ROOM_INDEX_DATA * src, ROOM_INDEX_DATA * target, int maxdist ) { int curr_dir, count; EXIT_DATA *pexit; if( !src || !target ) { bug( "%s", "Illegal value passed to find_first_step (track.c)" ); return BFS_ERROR; } if( src == target ) return BFS_ALREADY_THERE; if( src->area != target->area ) return BFS_NO_PATH; room_enqueue( src ); MARK( src ); /* * first, enqueue the first steps, saving which direction we're going. */ for( pexit = src->first_exit; pexit; pexit = pexit->next ) if( valid_edge( pexit ) ) { curr_dir = pexit->vdir; MARK( pexit->to_room ); room_enqueue( pexit->to_room ); bfs_enqueue( pexit->to_room, curr_dir ); } count = 0; while( queue_head ) { if( ++count > maxdist ) { bfs_clear_queue( ); clean_room_queue( ); return BFS_NO_PATH; } if( queue_head->room == target ) { curr_dir = queue_head->dir; bfs_clear_queue( ); clean_room_queue( ); return curr_dir; } else { for( pexit = queue_head->room->first_exit; pexit; pexit = pexit->next ) if( valid_edge( pexit ) ) { curr_dir = pexit->vdir; MARK( pexit->to_room ); room_enqueue( pexit->to_room ); bfs_enqueue( pexit->to_room, queue_head->dir ); } bfs_dequeue( ); } } clean_room_queue( ); return BFS_NO_PATH; }