int main(int argc, char* argv[]) { Adjacency_Matrix* g = (Adjacency_Matrix*) malloc(sizeof(Adjacency_Matrix)); Adjacency_Matrix* tp; int* adjacency; empty_graph(g); int option, v, a1, a2; do { menu(); scanf("%d", &option); switch (option) { case INSERT_VERTEX: printf("How many vertex would you like to insert? "); scanf("%d", &v); insert_vertex(g, v); break; case REMOVE_VERTEX: printf("Which vertex would you like to remove? "); scanf("%d", &v); remove_vertex(g, v); break; case INSERT_ARC: printf("First vertex: "); scanf("%d", &a1); printf("Second vertex: "); scanf("%d", &a2); insert_arc(g, a1, a2, 1); break; case REMOVE_ARC: printf("First vertex: "); scanf("%d", &a1); printf("Second vertex: "); scanf("%d", &a2); remove_arc(g, a1, a2); break; case VERTEX_ADJACENCY: printf("Which vertex would you like to verify adjacency?"); scanf("%d", &v); adjacency = get_adjacency(g, v); print_adjacency(adjacency); free(adjacency); pause(); break; case TRANSPOSE_GRAPH: tp = transpose_graph(g); print_graph(tp); free(tp); pause(); break; case PRINT_GRAPH: print_graph(g); pause(); break; } } while (option != EXIT); return 0; }
static DoubleVector dijkstra(const IntVector &consumers, const IntVector &resources, const DoubleVector &weights, int n) { DEBUG(print_weights(weights, n)); DEBUG(print_adjacency(resources, n)); DEBUG(print_adjacency(consumers, n)); DoubleVector lengths(n*n); /* All distances start as infinite */ for(int i=0; i<n*n; ++i) { lengths[i] = R_PosInf; } /* Diagonal is 0.0 - no cost to moving nowhere */ for(int i=0; i<n; ++i) { lengths[i + i*n] = 0.0; } for(int source=0; source<n; ++source) { DEBUG(Rprintf("source [%d]\n", source)); BoolVector todo(n, true); while(TRUE) { /* Set u to index of node with smallest entry in dist[todo] */ int u = -1; for(int i=0; i<n; ++i) { if(todo[i]) { if(-1==u) u = i; if(lengths[source + n*i] < lengths[source + n*u]) { u = i; } } } if(-1==u || !R_FINITE(lengths[source + n*u])) { break; } todo[u] = false; DEBUG(Rprintf("u [%d]\n", u)); for(int i=0; i<resources[u]; ++i) { const int v = resources[u + (1+i)*n]; const double alt = lengths[source + n*u] + weights[v + u*n]; if(alt<lengths[source + n*v]) { DEBUG(Rprintf("res v [%d]\n", v)); lengths[source + n*v] = alt; } } for(int i=0; i<consumers[u]; ++i) { const int v = consumers[u + (1+i)*n]; const double alt = lengths[source + n*u] + weights[u + v*n]; if(alt<lengths[source + n*v]) { DEBUG(Rprintf("con v [%d]\n", v)); lengths[source + n*v] = alt; } } } } return (lengths); }
void visit(Visitor &visitor) const { // Visits unique trophic chains the network represented by adjancency. DEBUG(Rprintf("DUMP GRAPH\n")); DEBUG(print_adjacency()); bool queue_warning = false; for(int n = 0; n < adjacency_.size(); ++n) { if(adjacency_[n].size() == 0) continue; // Node n has no out-edges, it // cannot start a chain if(is_basal_[n] == 0) continue; // Node n has in-edges, so it cannot // start a chain IntVector path(1, n); DEBUG(print_path("INITIAL PATH", path)); Queue queue; queue.push(path); while(!queue.empty()) { path = queue.front(); queue.pop(); R_ProcessEvents(); if(max_queue_>0 && !queue_warning && queue.size()>max_queue_/2) { REprintf("This network has a lot of paths, possibly too many to " "compute\n"); queue_warning = true; } else if(max_queue_>0 && queue.size()>max_queue_) { throw CheddarException("Unable to compute paths - see the help for " "TrophicChains for more information."); } int m = path.back(); DEBUG(Rprintf("AT NODE [%d]\n", m)); if(adjacency_[m].size() == 0) { DEBUG(print_path("", path)); visitor.chain(path); } else { bool cycle = true; for(int j=0; j<adjacency_[m].size(); ++j) { bool found = false; for(IntVector::const_iterator k=path.begin(); k!=path.end(); ++k) { if(*k == adjacency_[m][j]) { found = true; break; } } if(!found) { path.push_back(adjacency_[m][j]); DEBUG(print_path("EXTEND PATH", path)); queue.push(path); path.pop_back(); cycle = false; } } if(cycle) { DEBUG(print_path("", path)); visitor.chain(path); } } } } }