Exemple #1
0
/* DFS */
void DFS(GraphRef G, ListRef S){
   int i, s, top, low;

   moveTo(S, 0);
   s = getCurrent(S);
   G->parent[s] = 0;
   for(i=0;i<=getLength(S);i++){
      if(G->color[s] == 1){
         G->parent[s] = 0;
         visit(G, s);
      }
      if(i<getLength(S)-1){
         moveNext(S);
         s = getCurrent(S);
      }
   }
   makeEmpty(S);
   top = getTime(G);
   while(getLength(S) < getOrder(G)){
      low = 0;
      for(i=1;i<=getOrder(G);i++){
         if(top > G->finish[i] && low < G->finish[i]){
            low = G->finish[i];
            s = i;
         }
      }
      insertBack(S, s);
      top = low;
   }
}
//runs the DFS algorithm on the Graph G, using the List S to order the vertices
//and store the stack of finished vertices.
//Pre: Length of S = order of G, S has all the correct numbers in it(does not check this)
void DFS(Graph G, List S){
	if( G == NULL ) {
      		printf("Graph Error: calling DFS() on NULL Graph reference\n");
      		exit(1);
	}
	if( S == NULL ) {
      		printf("Graph Error: calling DFS() on NULL List reference\n");
      		exit(1);
	}
	if( length(S) != getOrder(G) ) {
      		printf("Graph Error: calling DFS() on wrong length list\n");
      		exit(1);
	}
	for(int i = 1; i <= getOrder(G); i++) {
		G->vcolor[i]='w';
		G->vparent[i]=NIL;
	}
	int time;
	time =0;
	int k;
	k=0;

	moveTo(S, getOrder(G)-1);
	for(int i = 1; i <= getOrder(G); i++) {
		int u;
		u = front(S);
		deleteFront(S);
		if (G->vcolor[u] == 'w'){
			k=k+1;
			time = visit(G, S, u, &time, k);
			G->numcc=k;
		}
	}
}
 virtual void initialize()
   {
     if(isInitialized)
       {
         return;
       }
     ReactionProcess::initialize();
     isPriorityQueued = true;
     if(getOrder() != 1 && getOrder() != 2)
       {
         THROW_EXCEPTION(ValueError, 
                         String(getPropertyInterface().getClassName()) + 
                         "[" + getFullID().asString() + 
                         "]: This PolymerFragmentationProcess requires " +
                         "two substrates.");
       }
     if(p == -1)
       {
         p = k;
       }
     else
       {
         k = p;
       }
   }
CityTradeOptions::Order CityTradeOptions::switchOrder( GoodType type )
{
  if( !isVendor( type ) )
  {
    switch( getOrder( type ) )
    {
    case noTrade: setOrder( type, importing ); break;
    case importing: setOrder( type, noTrade ); break;
    default: break;
    }
  }
  {
    switch( getOrder( type ) )
    {
    case noTrade: setOrder( type, exporting ); break;
    case exporting: setOrder( type, importing ); break;
    case importing: setOrder( type, noTrade ); break;
    default: break;
    }
  }

  _d->updateLists();

  return getOrder( type );
}
//inserts a new directed edge from u to v, i.e. v is added to the adjacency List of
//u (but not u to the adjacency List of v). 
//Pre: u and v between 1 and getOrder(G)
void addArc(Graph G, int u, int v) {
	if( G == NULL ) {
      		printf("Graph Error: calling addArc() on NULL Graph reference\n");
      		exit(1);
	}
	if((1>u) || (u > getOrder(G)) || (1>v) || (v > getOrder(G))) {
      		printf("Graph Error: calling addArc() on invalid vertex value\n");
      		exit(1);
	}
	//insert v into u:
	//goes to beginning of u's neighbor list
	moveTo(G->vneighbor[u],0);
	//moves to correct stop in the list
	while((getIndex(G->vneighbor[u]) > -1) && (getElement(G->vneighbor[u]) < v)) {
		moveNext(G->vneighbor[u]);
	}
	//checks if we are off the end of the list
	if (getIndex(G->vneighbor[u]) == -1) {
		append(G->vneighbor[u], v);
		G->size++;
	}
	//if not off end of list, insert before current position
	else {
		insertBefore(G->vneighbor[u], v);
		G->size++;
	}
}
Exemple #6
0
// BFS Algorithm
void BFS(Graph G, int s) {
    if(G == NULL) {
        printf("Graph Error: calling BFS() on NULL Graph reference");
        exit(1);
    }
    if(s<1 || s>getOrder(G)) {
        printf("Graph Error: calling BFS() with out of bounds 's' value");
        exit(1);
    }
    for(int i=1; i<=getOrder(G); i++) {
        G->color[i] = 0; // initialize 0=white
        G->distance[i] = INF; // initialize INF distance
        G->parent[i] = NIL; // initialize NIL parent
    }
    G->source = s; // set source to s
    G->color[s] = 1; // set color to gray (found)
    G->distance[s] = 0; // no distance
    G->parent[s] = NIL; // NIL parent
    List Q = newList(); // Use list as queue
    append(Q, s);
    for(moveTo(Q, 0); getIndex(Q)>=0; moveNext(Q)) {
        int x = getElement(Q);
        for(moveTo(G->adj[x], 0); getIndex(G->adj[x])>=0; moveNext(G->adj[x])) {
            int y = getElement(G->adj[x]);
            if(G->color[y] == 0) { // if the color is white
                G->color[y] = 1; // set color to gray
                G->distance[y] = G->distance[x]+1;
                G->parent[y] = x;
                append(Q, y);
            }
        }
        G->color[x] = 2; // set color to black
    }
    freeList(&Q); // free the list
}
Exemple #7
0
/* DFS()
 * Pre: List S length == Number of vetices in G
 * Pos: finds the strongly connected components
 */ 
void DFS(GraphRef G, ListRef S){
	if(G == NULL){
		fprintf(stderr,"Graph Error: calling DFS on NULL Graph");
		exit(1);
	}
	if(getLength(S) != getOrder(G)){
		fprintf(stderr,"DFS Error: ListRef size != Graph size");
		exit(1);
	}
	int i;
	int u;
	int j;
	
	for(i=1; i<=getOrder(G);i++){
		G->color[i] = WHITE;
		G->parent[i] = NIL;
	}
	j =1;
	moveTo(S,getLength(S)-1);
	while(!offEnd(S) && j<=getOrder(G)){
		u = getFront(S);

		if(G->color[u] == WHITE){
			Visit(G,S,u);
		}
		deleteFront(S);
		j++;
	} 
}
cln::cl_N* HighPrecisionComplexPolynom::getPrecRoots() {
  if (getOrder()==0) return NULL;
  cln::cl_N* roots = new cln::cl_N[getOrder()];
  Polyrootc(coeff, getOrder(), roots);
 
  return roots;
}
Exemple #9
0
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Flux determinations
double LVector::flux(int maxP) const
{
    if (maxP<0) maxP = getOrder()/2;
    if (maxP > getOrder()/2) maxP=getOrder()/2;
    double retval=0.;
    for (int p=0; p<=maxP; p++)
        retval += (*_v)[PQIndex(p,p).rIndex()];
    return retval;
}
Exemple #10
0
// Adds an undirected edge to the Graph G from u to v
// Pre: 1 <= u <= getOrder(G), 1 <= v <= getOrder(G)
void addEdge(Graph G, int u, int v) {
   if(u < 1 || u > getOrder(G) || v < 1 || v > getOrder(G)) {
     printf("Graph Error: calling addEdge() with verticies out of bounds\n");
     exit(1); 
   }
   addArc(G, u, v);
   addArc(G, v, u);
   G->size--;
}
inline void SpatiocyteNextReactionProcess::calculateOrder()
{
  ReactionProcess::calculateOrder();
  for(VariableReferenceVector::iterator
      i(theVariableReferenceVector.begin());
      i != theVariableReferenceVector.end(); ++i)
    {
      VariableReference aVariableReference(*i);
      long int aCoefficient(aVariableReference.getCoefficient());
      // here assume aCoefficient != 0
      if(aCoefficient == 0)
        {
          THROW_EXCEPTION(InitializationFailed,
                          "[" + getFullID().asString() + 
                          "]: Zero stoichiometry is not allowed.");
        }
    }
  // set theGetPropensityMethodPtr
  if(getOrder() == 0) // no substrate
    {
      theGetPropensityMethodPtr = RealMethodProxy::create<
        &SpatiocyteNextReactionProcess::getPropensity_ZerothOrder>();
    }
  else if(getOrder() == 1)   // one substrate, first order.
    {
      theGetPropensityMethodPtr = RealMethodProxy::create<
        &SpatiocyteNextReactionProcess::getPropensity_FirstOrder>();
    }
  else if(getOrder() == 2)
    { 
      //Two unique substrate species, second order
      //A + B -> products:
      if(getZeroVariableReferenceOffset() == 2)
        {  
          theGetPropensityMethodPtr = RealMethodProxy::
            create<&SpatiocyteNextReactionProcess::
            getPropensity_SecondOrder_TwoSubstrates>();
        }
      //One substrate species, second order
      //A + A -> products:
      else
        {
          theGetPropensityMethodPtr = RealMethodProxy::
            create<&SpatiocyteNextReactionProcess::
            getPropensity_SecondOrder_OneSubstrate>();
        }
    }
  else
    {
      // change code (2011/02/03)
      THROW_EXCEPTION( UnexpectedError,
                       "never get here (" + std::string( __PRETTY_FUNCTION__ )
                       + ")" );
//      NEVER_GET_HERE;
    }
}
Exemple #12
0
/* copyGraph()
 * Pre: none
 * Pos: calls ListCopy to copy the adj list to a new graph
 */
GraphRef copyGraph(GraphRef G){
	int i;
	GraphRef C = newGraph(getOrder(G));
	for(i=1; i <= getOrder(G); i++){
		if(!isEmpty(G->adj[i])){
			C->adj[i] = copyList(G->adj[i]);
		}
	}
	return C;
}
Exemple #13
0
/* transpose()
 * Pos: none
 * Pre: transfers the tranpose of G  to Graph T
 */
GraphRef transpose(GraphRef G){
	int i;
	GraphRef T = newGraph(getOrder(G));
	for(i=1; i<=getOrder(G); i++){
		if(!isEmpty(G->adj[i])){
			transposeHelp(T,G->adj[i],i);
		}
	}
	return T;
} 
Exemple #14
0
/* Pre: 1<=u<=n, 1<=v<=n */
void addEdge(Graph G, int u, int v){
	if(G == NULL){
		printf("Graph Error: calling addEdge() on NULL Graph reference!\n");
		exit(1);
	}else if(u<1 || u>getOrder(G) || v<1 || v>getOrder(G)){
		printf("Graph Error: a vertex is not within bounds of the Graph!\n");
		exit(1);
	}
	addArc(G,u,v);
	addArc(G,v,u);
}
Complex* HighPrecisionComplexPolynom::getRoots() {
  if (getOrder()==0) return NULL;
  cln::cl_N* precRoots = getPrecRoots();
  Complex* roots = new Complex[getOrder()];
  for (int I=0; I<getOrder(); I++) {
    roots[I] = Complex(double_approx(realpart(precRoots[I])), double_approx(imagpart(precRoots[I])));
  }
  
  delete[] precRoots;
  return roots;
}
        static int compareElements (const Component* const first, const Component* const second)
        {
            const int explicitOrder1 = getOrder (first);
            const int explicitOrder2 = getOrder (second);

            if (explicitOrder1 != explicitOrder2)
                return explicitOrder1 - explicitOrder2;

            const int yDiff = first->getY() - second->getY();

            return yDiff == 0 ? first->getX() - second->getX()
                              : yDiff;
        }
Exemple #17
0
void addArc(Graph G, int u, int v){
    if( 1>u || u>getOrder(G) ){
        printf("Graph Error: vertex u is not in |V(G)|");
        exit(1);
    }
    if( 1>v || v>getOrder(G) ){
        printf("Graph Error: vertex v is not in |V(G)|");
        exit(1);
    }

    insertVertex(G->adj[u], v);
    G->size++;
}
/*main------------------------------------------------------------------------*/
int main(int argc, char* argv[]) {

    //input validation
    if (argc != 2) {
        printf("Usage: %s output\n", argv[0]);
        exit(1);
    }

    //open file
    FILE *out;
    out = fopen(argv[1], "w");

    //new graph
    Graph G = newGraph(10);
    Graph T;
    List S = newList();
    int i;
    for (i = 1; i <= 9; i++) {
        addArc(G, i, i + 1);
    }
    addArc(G, 1, 2);

    //add graph and print out the adjacency fields
    for (i = 1; i <= getOrder(G); i++) {
        append(S, i);
    }
    fprintf(out, "\nThe adjacency list of G is:\n");
    printGraph(out, G);

    //Run DFS and get the transpose
    DFS(G, S);
    T = transpose(G);
    fprintf(out, "\nTranspose of G is:\n");
    printGraph(out, T);

    //print out fields
    fprintf(out, "Order of G is %d\n", getOrder(G));
    fprintf(out, "Size of G is %d.\n", getSize(G));
    fprintf(out, "Parent of 10 is %d.\n", getParent(G, 10));
    fprintf(out, "Parent of 3 is %d.\n", getParent(G, 3));
    fprintf(out, "Discover time of 1 is %d.\n", getDiscover(G, 1));
    fprintf(out, "Finish time of 1 is %d.\n", getFinish(G, 1));
    fprintf(out, "Discover time of 9 is %d.\n", getDiscover(G, 9));
    fprintf(out, "Finish time of 9 is %d.\n", getFinish(G, 9));

    //close file
    fclose(out);

    //exit
    return (0);
}
Exemple #19
0
    virtual void initialize()
    {
        ContinuousProcess::initialize();

        calculateOrder();
        
        if( ! ( getOrder() == 1 || getOrder() == 2 ) )
        {
            THROW_EXCEPTION_INSIDE( ValueError, 
                                   asString() +
                                   ": either first or second order scheme is "
                                   "allowed" );
        }
    }    
Exemple #20
0
 void addArc ( Graph G, int u, int v ) 
 {
 
    // pre-condition
    if ( G == NULL ) 
    {
       printf ( "Graph Error: addArc() on NULL graph" );
       exit(1);
    }
    // pre-condition
    if ( u < 1 || u > getOrder(G) ) 
    {
       printf ( "Graph Error: u undefined" );
       exit(1);
    }
    // pre-condition
    if ( v < 1 || v > getOrder(G) )
    {
       printf ( "Graph Error: v undefined" );
       exit(1);
    }
   
   // add u to v 
    if(length(G->adjacency[u]) == 0) { 
       append (G->adjacency[u], v );
    }
    
    else 
    {
      moveTo(G->adjacency[u], 0);
      while(getIndex(G->adjacency[u]) != -1 ) 
      {
          if(v < getElement(G->adjacency[u])) {
             insertBefore(G->adjacency[u], v);
             break;
           }
           else {
           moveNext(G->adjacency[u]);
           }
       }
       
        if(getIndex(G->adjacency[u]) == -1) {
          append ( G->adjacency[u], v );
       }
    }
   
    G->size++;
    
 }
Exemple #21
0
void BFS(Graph G, int s){
    if( G==NULL ){
        printf("Graph Error: cannot run BFS() on NULL Graph pointer");
        exit(1);
    }
    if( 1>s || s>getOrder(G) ){
        printf("Graph Error: vertex s is not in |V(G)|");
        exit(1);
    }

    G->source = s;
    int i, x, y;
    for( i=1; i<=getOrder(G); i++){
        G->color[i] = WHITE;
        G->dist[i]  = INF;
        G->pred[i]  = NIL;
    }
    G->color[s] = GRAY;
    G->dist[s]  = 0;
    G->pred[s]  = NIL;
    List Q      = newList();


    append(Q, s);
    moveFront(Q);

    while( getindex(Q)!=-1 ){

        x = get(Q);
        moveFront(G->adj[x]);

        while( getindex(G->adj[x])!=-1 ){

            y = get(G->adj[x]);
            if( G->color[y]==WHITE ){
                G->color[y] = GRAY;
                G->dist[y]  = G->dist[x] + 1;
                G->pred[y]  = x;
                append(Q, y);
            }
            moveNext(G->adj[x]);
        }

        G->color[x] = BLACK;
        moveNext(Q);
    }
    freeList(&Q);
}
Exemple #22
0
// returns 0 if NOT in check after the move
// return 1 if in check after the move
int simCheck(struct board * b, struct piece * p, struct pos * move, struct piece * k )
{
	struct piece * inactivePiece = NULL;
	struct pos * tempLoc;
	struct board * nBoard = copyBoard(b);

	struct pos * nMove = makeLoc(move->x, move->y);
	nMove->type = move->type;
	if(move->type ==4)
	{
		nMove->additionalM1 = move->additionalM1;
		nMove->additionalM2 = move->additionalM2;
		nMove->additionalP = getSpace(nBoard, move->x, move->y);
	}
 	int temp = getOrder(b, p);
	int temp1;
	if(move->taken != NULL)
	{
		temp1 = getOrder(b,move->taken);
		nMove->taken = nBoard->pieces[temp1];
	}
	else{
		nMove->taken = NULL;
	}

//	printAllMoves(nBoard);
	movePiece(nBoard,nBoard->pieces[temp],nMove);
	updateAllMovesSim(nBoard);
	free(nMove);

	if(move->type == 4)
	{
		if(incheckCheck(nBoard,move->additionalP,move->additionalP->loc) == 1)
		{
			deleteBoard(nBoard);
			return 1;
		}
		deleteBoard(nBoard);
		return 0;
	}
	if(incheckCheck(nBoard,k,k->loc) == 1)
	{
		deleteBoard(nBoard);
		return 1;
	}
	deleteBoard(nBoard);
	return 0;
}
Exemple #23
0
void XC::SectionAggregator::alloc_storage_ptrs(void)
  {
    free_storage_ptrs();
    const size_t order= getOrder();
    if(order > maxOrder)
      {
        std::cerr << getClassName() << "::" << __FUNCTION__
		  << "; order too big, need to modify the maxOrder value"
	          << " in SectionAggregator.cpp to: " << order << std::endl;
        exit(-1);
      }
    //theCode= new ResponseId(codeArea, order); Not sharing area anymore
    //                                          LCPT 19/09/2016
    if(order>0)
      {
        theCode= new ResponseId(order);
        def= new Vector(workArea, order);
        defzero= new Vector(workArea, order);
        s= new Vector(&workArea[maxOrder], order);
        ks= new Matrix(&workArea[2*maxOrder], order, order);
        fs= new Matrix(&workArea[maxOrder*(maxOrder+2)], order, order);
        check_ptrs();
      }
    else
      std::cerr << getClassName() << "::" << __FUNCTION__
		<< "; 0 or negative order; order= " << order << std::endl;
  }
Exemple #24
0
//! @brief Returns the value of the variable which identifier
//! is being passed as parameter.
int XC::SectionAggregator::getVariable(int variableID, double &info)
  {
    int i;
    info= 0.0;

    const int order= getOrder();

    const Vector &e= getSectionDeformation();
    const ResponseId &code= this->getType();

    switch (variableID)
      {
      case 1: // Axial strain
        // Series model ... add all sources of deformation
        for(i= 0; i < order; i++)
          if(code(i) == SECTION_RESPONSE_P)
            info+= e(i);
        return 0;
      case 2: // Curvature about the section z-axis
        for(i= 0; i < order; i++)
          if(code(i) == SECTION_RESPONSE_MZ)
            info += e(i);
          return 0;
      case 3:// Curvature about the section y-axis
        for(i= 0;i<order;i++)
          if(code(i) == SECTION_RESPONSE_MY)
            info += e(i);
        return 0;
      default:
        return -1;
      }
  }
Exemple #25
0
/* addArc()
 * Pre: (u&v)>0 && (u&v)<=getOrder()
 */ 
void addArc(GraphRef G, int u, int v){
	if(G == NULL){
		fprintf(stderr,"Graph Error: calling addArc on NULL Graph");
		exit(1);
	}
	if(u<1 || u>getOrder(G)){
		fprintf(stderr,"addArc Error: addArc(P1,P2,P3) P2 index out of range");
		exit(1);
	}
	if(v<1 || v>getOrder(G)){
		fprintf(stderr,"addArc Error: addArc(P1,P2,P3) P3 index out of range");
		exit(1);
	}
	insertBack(G->adj[u],v);
	G->size++;
}
Exemple #26
0
// Returns the parent of a given vertex
// Pre: 1 <= u <= getOrder(G)
int getParent(Graph G, int u) {
   if(u < 1 || u > getOrder(G)) {
     printf("Graph Error: calling getParent() with vertex out of bounds\n");
     exit(1); 
   }
   return G->parent[u];
}
Exemple #27
0
// Adds a directed edge to the Graph G from u to v
// Pre: 1 <= u <= getOrder(G), 1 <= v <= getOrder(G)
void addArc(Graph G, int u, int v) {
   if(u < 1 || u > getOrder(G) || v < 1 || v > getOrder(G)) {
     printf("Graph Error: calling addArc() with verticies out of bounds\n");
     exit(1); 
   }
   List S = G->adj[u];
   moveFront(S);
   while(index(S) > -1 && v > get(S)) {
      moveNext(S);
   }
   if(index(S) == -1)
      append(S, v);
   else 
      insertBefore(S, v);
   G->size++;
}
Exemple #28
0
//////////////////////////////////////////////////////////////////////////////
///
///	Send result to output
///
//////////////////////////////////////////////////////////////////////////////
void controller::sendResult(long data)
{
 if (GetVerbose())
  SpacePrint("Result:%d\n", data);

 int i;
 unsigned long power;
 unsigned int digit;

 // Negative number
 if (data < 0)
 {
  sendCharacter('-');
  data *= -1;
 }

 for(i=getOrder(data);i>=0;i--)
 {
  power = pow(10, i);
  digit = data/power;
  data -= digit*power;
  sendCharacter(itoa(digit));
 }

 sendCharacter(0xD);
}
Exemple #29
0
int CExpNet::setProbability(double *psamples, int nsamples) {

	int *porder;
	int k, nnode;

	if(!m_loglambdas)
		return ERR_CATNET_INIT;
	
	if(!psamples || nsamples < 1)
		return ERR_CATNET_PARAM;	

	porder = getOrder();
	if(!porder)
		return ERR_CATNET_PROC;

	for(k = 0; k < m_numNodes; k++) {
		/* work with the sample distribution of pCurNet if necessary */
		set_sample_cache();

		nnode = porder[k];

		if(estimateNodeProb(nnode, m_parents[nnode], m_numParents[nnode], psamples, nsamples) != ERR_CATNET_OK)
			break;

		if(m_pCatnetSamples)
			CATNET_FREE(m_pCatnetSamples);
		m_nCatnetSamples = 0;
		m_pCatnetSamples = 0; 
	}

	CATNET_FREE(porder);

	return ERR_CATNET_OK;
}
void constructGraph(int internalDegree, int depth){
    int i, j, k;
    int minLeaf, maxLeaf;
    
    GRAPH graph;
    ADJACENCY adj;
    
    prepareGraph(graph, adj, getOrder(internalDegree, depth));
    
    if(depth==0){
        writeMultiCode(graph, adj, stdout);
        return;
    }
    
    for(i = 0; i < internalDegree; i++){
        addEdge(graph, adj, 1, 2 + i);
    }
    
    minLeaf = 2;
    maxLeaf = 1 + internalDegree;
    
    for(i = 1; i < depth; i++){
        int newLeaf = maxLeaf + 1;
        for(j = minLeaf; j <= maxLeaf; j++){
            for(k = 0; k < internalDegree - 1; k++){
                addEdge(graph, adj, j, newLeaf);
                newLeaf++;
            }
        }
        minLeaf = maxLeaf + 1;
        maxLeaf = newLeaf - 1;
    }
    
    writeMultiCode(graph, adj, stdout);
}