Exemplo n.º 1
0
int maxim(arb *a){
    if(a){
        if(a->inf>max)max=a->inf;
        max=maxim(a->st);
        max=maxim(a->dr);
    }
    return max;
}
Exemplo n.º 2
0
Node*rotateLeft(Node*y)
{
  Node*x=y->right;
  Node*T2=x->left;
  x->left=y;
  y->right=T2;
    y->height=maxim(height(y->left),height(y->right));
    x->height=maxim(height(x->left),height(x->right));
    return x;
}
Exemplo n.º 3
0
Node*rotateRight(Node*x)
{
    Node*y=x->left;
    Node*T2=y->right;
    y->right=x;
    x->left=T2;
    x->height=maxim(height(x->left),height(x->right))+1;
    y->height=maxim(height(y->left),height(y->right))+1;
    return y;

}
Exemplo n.º 4
0
Node*insert(Node*node,int data)
{
   if(node==NULL) return (createNode(data));
   if(data< node->data) node->left=insert(node->left,data);
      else node->right=insert(node->right,data);
   node->height=maxim(height(node->left),height(node->right))+1;
   int balan=balanced(node);

   if (balan>1 && data<node->left->data) return rotateRight(node);

   if (balan<-1 && data>node->right->data) return rotateLeft(node);

   if (balan>1 && data>node->left->data)
       {
           node->left=rotateLeft(node->left);
        return(rotateRight(node));
       }

    if (balan<-1 && data<node->right->data)
    {
      node->right=rotateRight(node->right);
      return(rotateLeft(node));
    }
    return node;


}
Exemplo n.º 5
0
int getHeights(NodeT *root) //computes all the heights of the nodes from the tree that has been rotated;
{
    if(root == NULL)
        return 0;
    else
    {
        root->height=maxim(getHeights(root->left), getHeights(root->right))+1;
        return root->height;
    }
}
Exemplo n.º 6
0
int main(){
    int n,in,k,i,p;
    scanf ("%d%d%d",&n,&in,&k);
    p=0;
    for(i=1;i<=n;i++){
        scanf ("%d%lld",&x[i],&w[i]);
        if (x[i]<in) p++;
        w[i]+=w[i-1];
    }
    int j=p+1;
    long long max=0;
    for(i=1;i<=p;i++){
        long long aux=0;
        if (in-x[i]<=k){
            aux=w[p]-w[i-1];
            if ((in-x[i])*2<=k){
                int c=in+k-(in-x[i])*2;
                for(;j<=n &&x[j]<=c;j++) ;
                j--;
                aux+=(w[j]-w[p]);
            }
        }
        max=maxim(max,aux);
    }
    p++;
    j=p-1;
    for(i=n;i>=p;i--){
        long long aux=0;
        if (x[i]-in<=k){
            aux=w[i]-w[p-1];
            if ((x[i]-in)*2<=k){
                int c=in-(k-(x[i]-in)*2);
                for(;j>0 &&x[j]>=c;j--) ;
                j++;
                aux+=(w[p-1]-w[j-1]);
            }
        }
        max=maxim(max,aux);
    }
    printf ("%lld",max);
    return 0;
}
int pseudorand(Element x[SIZE][SIZE],int source,int n,int k,int q,int p)
{   
    
    int q1 = GetRand(0,99);
    if(q1> q )
    return random1(x,source,n,k);
    if(q1> p )
    return nn(x,source,n,k) ;
    else
    return maxim(x,source,n,k);
}
Exemplo n.º 8
0
int main()
{  
	 
	///lectura de imagen
	char carName[50];
	printf("\n Introduzca el nombre de la imagen extension *BMP (ej:lena.bmp) : \n");
	scanf("%s", carName);

	IMAGE *img;
	//el formato de entrada de la imagen es nombre.extención, para este caso solo lee imagenes BMP.
	loadBMP(carName, &img);//el formato de entrada de la imagen es nombre.extención, para este caso solo lee imagenes BMP.
	//variables de entrada a la funcion
	uint8_t Rpixel;
	uint8_t Bpixel;
	uint8_t Gpixel;
	uint8_t maxi;
	uint8_t mini;
	float filtro;
	float H;
	float sat;
	int i;
	float *x;
	

	//recorriendo la imagen
	for (i = 0; i < img->width*img->height; i++) {
		Rpixel=(uint8_t)img->data[i].r;
		Bpixel=(uint8_t)img->data[i].b;
		Gpixel=(uint8_t)img->data[i].g;

		// se definen las funciones max y min para el calculo de los canales hsv
		maxi = (uint8_t)maxim(Rpixel, Gpixel, Bpixel);
		mini = (uint8_t)minim(Rpixel, Gpixel, Bpixel);
		sat = Saturacion(maxi, mini);//calculo de la saturacion del pixel
		H =Color(maxi,mini,Rpixel, Gpixel, Bpixel);//calculando canal de color H
		filtro=FiltroExperimental(H,sat,(float)(maxi)/255);//definiendo filtro multiplicador de color

		//retornando de hsv a rgb
		x=ConvHsvRgb(filtro*H,sat,(float)(maxi)/(255));

		//salida a imagen 
		img->data[i].r = x[1];
		img->data[i].g = x[2];
		img->data[i].b = x[3];
		}
	

	writeBMP("img.bmp", img); //guardado de imagen
	freeBMP(img);// libera memoria de la imagen.
	free(img);
	return 0;
}
Exemplo n.º 9
0
int main(){
    arb *a;

    a = creare();
    printf("\nRSD:");RSD(a);
    printf("\nSRD:");SRD(a);
    printf("\nSDR:");SDR(a);

    printf("\nSuma:%d",suma(a));
    printf("\nMaxim:%d",maxim(a));
    printf("\nNr frunze:%d",frunze(a));
    printf("\nInaltimea:%d",inaltime(a));

    return 0;
}
Exemplo n.º 10
0
int bestmove2(int**maze,int s1,int s2,int x1,int y1,int x2,int y2){
	//visited[x1][y1]++;
	node root;
	int direction;
	int i,j,depth=0;
	for(i=0;i<MBON;i++)
		root.bonus[i]=0;
	for(i=0;i<SIZE;i++)
		for(j=0;j<SIZE;j++)
			if(maze[i][j]!=-1 && maze[i][j]!=0)
				root.bonus[label[i][j]]=1;
	root.s1=s1;root.s2=s2;root.x1=x1;root.y1=y1;
	root.x2=x2;root.y2=y2;
	int alpha=-INF,beta=INF;
	maxim(maze,root,alpha,beta,depth,&direction);
	return direction;
}
Exemplo n.º 11
0
NodeT *insertNode(NodeT *root, int value)
{
    if(root == NULL)
    {
        NodeT *p=createNode(value);
        return p;
    }
    else
    {
        if(value > root->data)
            root -> right = insertNode(root->right, value);
        else
            root -> left = insertNode(root->left, value);

        root->height = maxim(compHeight(root->left), compHeight(root->right)) +1;

        return Balance(root);
    }
}
Exemplo n.º 12
0
void CybOBB::buildOBB(CybVector3D<double>* covMatrix, cybMesh<cybSurfaceTriTraits>* mesh)
{
	//Calculating the eigen vectors and the eigen values of the covariance matrix.
	//The eigen vectors will be the OBB's orienting vectors.
	CybVector3D<double> eigenVectors[3];
	double eigenValues[3];
	calculateEigenSystem(covMatrix, eigenValues, eigenVectors);
	
	//Organizing the orientation matrix
	for(int j = 0; j < 3; ++j)
	{
		for(int i = 0; i < 3; ++i)
		{
			orientation[j][i] = covMatrix[i][j];
		}
		orientation[j].normalize();
	}

	//Calculating the maximum and minimum values on each axis, projecting onto the orientation vectors.
	CybVector3D<double> minim(1e10, 1e10, 1e10), maxim(-1e10, -1e10, -1e10);
	CybVector3D<double> p_prime(0,0,0);
	CybVector3D<double> aux(0,0,0);
	int qttPoints = mesh->getNumberOfVertices();
	sVertex* cPoint; //current point
	for(int i = 0; i < qttPoints; ++i)
	{
		cPoint = mesh->getVertex(i);
		aux(cPoint->getCoord(0), cPoint->getCoord(1), cPoint->getCoord(2));
		p_prime(orientation[0] ^ aux, orientation[1] ^ aux, orientation[2] ^ aux);
		minim = minim.min(p_prime);
		maxim = maxim.max(p_prime);
	}
	
	//Cálculo do centro (ponto médio dos eixos) e dos semi-eixos.
	CybVector3D<double> cAux = (maxim + minim) * 0.5;
	center(orientation[0] ^ cAux, orientation[1] ^ cAux, orientation[2] ^ cAux);
	sizes = (maxim - minim) * 0.5;
}
Exemplo n.º 13
0
Arquivo: abc.cpp Projeto: okiwan/adts
template <class C, class I> C ABC<C, I>::maxim(void) {
	return maxim(&arrel);
}
Exemplo n.º 14
0
void main(int argc, char* argv[]) {
	FILE *fp;
	int nrAni = atoi(argv[1]);
	fp = fopen(argv[2], "r");
	if (fp == NULL) {
		puts("Cannot open file!");
		//return;
	}
	int pMin, pMax, n;
	fscanf(fp, "%i %i %i",&pMin, &pMax, &n);
	printf("pMin, pMax, n: %i, %i, %i \n", pMin, pMax, n);
	
	//Avem 3 matrici in care pastram datele necesare problemei
	//tip, pret, buget
	int r[n][n], r1[n][n], p[n][n], p1[n][n], b[n][n], b1[n][n], c[n][n];
	//rezultatele le retinem intr-o matrice cu nrAni linii si 4 coloane
	//pe fiecare linie avem datele pentru anul corespunzator
	int rezultate[nrAni][4];
	int i, j, k ,l;
	int min, max; //le folosim cand calculam resursa cu cost minim
	int cost, costRes;
	int pretMaxA = pMin, pretMaxB = pMin, nrA = 0, nrB = 0;
	//tip resurse
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			fscanf(fp, "%i", &r[i][j]);
			//printf("%i ", r[i][j]);
		}
		//puts(" ");
	}
	//puts("\n");
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			fscanf(fp, "%i", &p[i][j]);
			//printf("%i ", p[i][j]);
		}
		//puts(" ");
	}
	//puts("\n");
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			fscanf(fp, "%i", &b[i][j]);
			//printf("%i ", b[i][j]);
		}
		//puts(" ");
	}
	//puts("\n");
	fclose(fp);
	
	//Start programming
	//Pentru fiecare colonist
	int an = 0;	
	int pret, pret1;
	int ok = -1;
	for (an = 1; an <= nrAni; an++) {
		ok = an % 2;
		nrA = 0; nrB = 0;
		pretMaxA = pMin;
		pretMaxB = pMin;
		#pragma omp parallel
		{
		#pragma omp for private(i,j,l,k,pret,cost,costRes) reduction(+:nrA,nrB) nowait
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {		
				cost = 3*n + pMax;
				costRes = 3*n + pMax;
				//Determinam pretul minim cu care poate cumpara o resursa
				for (k = 0; k < n; k++) {
					for (l = 0; l < n; l++) {
						if (ok == 1){
							pret = p[k][l] + (abs(i - k) + abs(j - l)); 
							//pentru resursa complementara
							if (r[k][l] == 1 - r[i][j]) {
								if (pret < cost)
									cost = pret; 
							}
							else {
								//pentru resursa proprie
								if (pret < costRes)
									costRes = pret; 
							}
						}
						else {
								pret = p1[k][l] + (abs(i - k) + abs(j - l)); 
								//pentru resursa complementara
								if (r1[k][l] == 1 - r1[i][j]) {
									if (pret < cost)
										cost = pret; 
								}
								else {
									//pentru resursa proprie
									if (pret < costRes)
										costRes = pret; 
								}
							}
					}
				}
				//pentru an impar
				if (ok == 1) {
					r1[i][j] = r[i][j];
					b1[i][j] = cost;
					if (cost > b[i][j]) {
						p1[i][j] = p[i][j] + cost - b[i][j];
					}
					else {
						if (cost < b[i][j])
							p1[i][j] = p[i][j] + (int)((cost - b[i][j])/2);
						else
							p1[i][j] = costRes + 1;
					}
					if (p1[i][j] < pMin)
						p1[i][j] = maxim(p1[i][j], pMin);
					if (p1[i][j] > pMax) {
						p1[i][j] = (int)((pMin + pMax)/2);
						r1[i][j] = 1 - r[i][j];
						b1[i][j] = pMax;
					}
					//numaram colonistii de un anumit tip
					//si det. preturile maxime
					if (r1[i][j] == 0) {
						nrA++;
						#pragma omp critical
						if (p1[i][j] > pretMaxA)
							pretMaxA = p1[i][j];
					}
					else {
						nrB++;
						#pragma omp critical
						if (p1[i][j] > pretMaxB)
							pretMaxB = p1[i][j];
					}
				}
				//daca este an par
				else {
					r[i][j] = r1[i][j];
					b[i][j] = cost;
					if (cost > b1[i][j])
						p[i][j] = p1[i][j] + cost - b1[i][j];
					else {
						if (cost < b1[i][j])
							p[i][j] = p1[i][j] + (int)((cost - b1[i][j])/2);
						else
							p[i][j] = costRes + 1;
					}
					if (p[i][j] < pMin)
						p[i][j] = maxim(p[i][j], pMin);
					if (p[i][j] > pMax) {
						p[i][j] = (int)((pMin + pMax)/2);
						r[i][j] = 1 - r1[i][j];
						b[i][j] = pMax;
					}
					//numaram colonistii de un anumit tip
					if (r[i][j] == 0) {
						nrA++;
						#pragma omp critical
						if (p[i][j] > pretMaxA)
							pretMaxA = p[i][j];	
					}
					else {
						nrB++;
						#pragma omp critical
						if (p[i][j] > pretMaxB)
							pretMaxB = p[i][j];	
					}
				}
			}
		}
		//retinem rezultatele in matrice
		rezultate[an-1][0] = nrA;
		rezultate[an-1][1] = pretMaxA;
		rezultate[an-1][2] = nrB;
		rezultate[an-1][3] = pretMaxB;
	}
	}
	//se afiseaza matricea finala si rezultatele pe fiecare an
	FILE *fp1 = fopen(argv[3], "w");
	if (fp1 == NULL) {
		puts("Cannot open file to write!!!");
	}
	for (i = 0; i < nrAni; i++) {
			fprintf(fp1,"%i %i %i %i", rezultate[i][0], rezultate[i][1], rezultate[i][2], rezultate[i][3]);
			fprintf(fp1,"%s", "\n");
		}
	printf("n este: %i\n", n);
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			if (nrAni % 2 == 1)
				fprintf(fp1,"(%i,%i,%i) ", r1[i][j], p1[i][j], b1[i][j]);
			else
				fprintf(fp1,"(%i,%i,%i) ", r[i][j], p[i][j], b[i][j]);
		}
		fprintf(fp1,"%s", "\n");
	}
	fclose(fp1);
}
Exemplo n.º 15
0
int main(int argc, char *argv[])
{ int    K=-1,KB=-1;
  char **Seqs;
  char **Names;
  char **SeqsB;
  char **NamesB;
  char *seqfilename=NULL,*dbfilename=NULL;
  int internalCompare=0; /* whether query and database sequences are the same */
  int *Profiles;
  int *ProfilesB=NULL;
  int ori;
  int first=1;
  FILE *seqfile=NULL, *dbfile=NULL;
  int *kmerCounts,*kmerIndex;
  int *len,*lenB;
  int *frontDiscount;
  int i,j;
  int maxlen=0;
  int minlen=40;
  int doTrimming=0;

  argc = AS_configure(argc, argv);

  { /* Parse the argument list using "man 3 getopt". */
    int ch,errflg=0;
    optarg = NULL;
    while (!errflg && ((ch = getopt(argc, argv, "fFg:hk:q:d:m:t")) != EOF))
      {
	switch(ch) {
	case 'f':
	  fullnames=1;
	  break;
	case 'F':
	  dbfullnames=1;
	  break;
	case 'g':
	  GRANULARITY = atoi(optarg);
	  assert(GRANULARITY>0&&GRANULARITY<500);
	  break;
	case 'h':
	  usage(argv[0]);
	  break;
	case 'q':
	  if(seqfile!=NULL)fclose(seqfile);
	  seqfile=fopen(optarg,"r");
	  assert(seqfile!=NULL);
	  seqfilename=optarg;
	  if(dbfile==NULL){
	    dbfile=fopen(optarg,"r");
	    assert(dbfile!=NULL);
	    dbfilename=optarg;
	  }
	  break;
	case 'd':
	  if(dbfile!=NULL){
	    fclose(dbfile);
	  }
	  dbfile=fopen(optarg,"r");
	  assert(dbfile!=NULL);
	  dbfilename=optarg;
	  break;
	case 'k':
	  ksize=atoi(optarg);
	  assert(ksize>0&&ksize<14);
	  break;
	case 'm':
	  minlen=atoi(optarg);
	  assert(minlen>=40&&minlen<1400);
	  break;
	case 't':
	  doTrimming=1;
	  fprintf(stderr,"Will trim to common range\n");
	  break;
	case '?':
	  errflg++;
	  usage(argv[0]);
	  break;
	default :
	  fprintf( stderr, "Unrecognized option -%c\n", optopt);
	  errflg++;
	  usage(argv[0]);
	}
      }
  }

  assert(seqfile!=NULL);
  assert(dbfile!=NULL);
  if(seqfilename==dbfilename || strcmp(seqfilename,dbfilename)==0){
    internalCompare=1;
  } else {
    internalCompare=0;
  }

  get_sequences(seqfile,&K,&Seqs,&Names);
  assert(K>=0);
  fprintf(stderr,"Read in %d query sequences\n",K+1);
  len = (int *)safe_malloc(sizeof(int)*(K+1));
  for(i=0;i<=K;i++){
    len[i]=strlen(Seqs[i]);
    if(maxlen<len[i]){
      maxlen=len[i];
    }
  }

  if(dbfullnames)fullnames=1;

  get_sequences(dbfile,&KB,&SeqsB,&NamesB);
  assert(KB>=0);
  fprintf(stderr,"Read in %d database sequences\n",KB+1);
  lenB = (int *)safe_malloc(sizeof(int)*(KB+1));
  frontDiscount = (int *)safe_malloc(sizeof(int)*(KB+1));
  for(i=0;i<=KB;i++){
    lenB[i]=strlen(SeqsB[i]);
    frontDiscount[i]=0;
  }

  for (i = 0; i < 128; i++){
    Map[i] = -1;
  }
  Map['a'] = Map['A'] = 0;
  Map['c'] = Map['C'] = 1;
  Map['g'] = Map['G'] = 2;
  Map['t'] = Map['T'] = 3;

  calc_kmer_members((const char **)SeqsB,ksize,&kmerCounts,&kmerIndex,KB+1);

  fprintf(stderr,"Built index\n");


  {
    int **hitCounts=NULL;

    hitCounts = (int **) safe_malloc(maxlen*sizeof(int*));

    for(j= 0; j<maxlen; j++){
      hitCounts[j] = (int *) safe_malloc((KB+1)*sizeof(int));
    }

    for (i = 0; i <= K; i++){
      int k;
      int kword = 0;
      int h = -ksize;
      int bestfront=-1, bestback=-1;
      int bestscore=-1,bestloc=-1;
      int bestsimple=-1,simplescore=-1;
      int ilen,ilenlessone;
      int startbestmatch=0;
      int endbestmatch = len[i]-1;

      ilen=len[i];
      ilenlessone=ilen-1;

      for(k=0;k<ksize;k++){
	for(j= 0; j<=KB; j++){
	  hitCounts[k][j] = 0;
	}
      }

      for(j=0;j<ksize-1;j++){
	int x = Map[(int) (Seqs[i][j])];
	if (x >= 0){
	  kword = (kword << 2) | x;
	}else{
	  kword <<= 2;
	  h = j-(ksize-1);
	}
      }


      while(Seqs[i][j]!='\0'){
	int x = Map[(int) (Seqs[i][j])];
	if (x >= 0){
	  kword = ((kword << 2) | x) & kmax;
	}else{
	  kword <<= 2;
	  h = j-(ksize-1);
	}
	for(k=0;k<=KB;k++){
	  hitCounts[j][k]=hitCounts[j-1][k];
	}
	if (j >= h+ksize){
	  for(k=kmerCounts[kword];k<kmerCounts[kword+1];k++){
	    if(internalCompare && kmerIndex[k]==i)continue;
	    if(k==kmerCounts[kword]||(kmerIndex[k]!=kmerIndex[k-1])){
	      hitCounts[j][kmerIndex[k]]++;
	    }
	  }
	}
	j++;
      }

      for( k=0;k<=KB;k++){
	if(hitCounts[ilenlessone][k]>simplescore){
	  simplescore = hitCounts[ilenlessone][k];
	  bestsimple=k;
	}
      }

      {
	ALNoverlap *ovl=NULL;
	double erate=0.02;
	// below, .9 fudge factor may be necessary to handle cases where some matching kmers are random out of order matches
	int minovl=simplescore *.9;
	while(erate<.4){
	  ovl = DP_Compare(Seqs[i], SeqsB[bestsimple],
                           -lenB[bestsimple]+minovl, len[i]-minovl,
                           strlen(Seqs[i]), strlen(SeqsB[bestsimple]),
                           0,
                           erate,
                           1e-6,
                           maxim(minlen,minovl),
                           AS_FIND_LOCAL_ALIGN_NO_TRACE);
	  if(ovl!=NULL)break;
	  erate*=2.;
	}
	if(ovl!=NULL){
	  if(ovl->begpos>0){
	    startbestmatch=ovl->begpos;
	  }
	  if(ovl->endpos<0){
	    endbestmatch=len[i]+ovl->endpos;
	    assert(endbestmatch>0);
	  }
	}
      }

      //      printf("startbestmatch init at %d\n",startbestmatch);
      //      printf("endbestmatch init at %d\n",endbestmatch);

      for(k=0;k<=KB;k++){
	frontDiscount[k]=hitCounts[startbestmatch+ksize-2][k];
      }


      for(j=startbestmatch+ksize-1;j<=endbestmatch;j+=GRANULARITY){
	int maxfront=-1;
	int maxback=-1;
	int whichfront=-1,whichback=-1;
	for(k=0;k<=KB;k++){
	  if(hitCounts[j][k]-frontDiscount[k]>maxfront){
	    maxfront=hitCounts[j][k]-frontDiscount[k];
	    whichfront=k;
	  }
	  if(hitCounts[endbestmatch][k]-hitCounts[j][k]>maxback){
	    maxback=hitCounts[endbestmatch][k]-hitCounts[j][k];
	    whichback=k;
	  }
	}
	if(maxfront+maxback>bestscore){
	  bestscore=maxfront+maxback;
	  bestfront=whichfront;
	  bestback=whichback;
	  bestloc=j;
#ifdef DEBUG_SEGMENTATION
	  fprintf(stderr,"New best %d: loc %d seqs %s / %s bestfront %d bestback %d\n",
		  bestscore,
		  bestloc,NamesB[bestfront],NamesB[bestback],bestfront,bestback);
#endif
	}
      }

      if(doTrimming){
	int frontstart=startbestmatch;
	int frontend=endbestmatch;

	int backstart=startbestmatch;
	int backend=endbestmatch;

	ALNoverlap *ovl=NULL;
	double erate=0.02;
	// below, .9 fudge factor may be necessary to handle cases where some matching kmers are random out of order matches
	int minovl=hitCounts[bestloc][bestfront]*.9;
	if(bestfront!=bestsimple){
	  while(erate<.4){
	    ovl = DP_Compare(Seqs[i], SeqsB[bestfront],
                             -lenB[bestfront]+minovl, len[i]-minovl,
                             strlen(Seqs[i]), strlen(SeqsB[bestfront]),
                             0,
                             erate,
                             1e-6,
                             maxim(minlen,minovl),
                             AS_FIND_LOCAL_ALIGN_NO_TRACE);
	    if(ovl!=NULL)break;
	    erate*=2.;
	  }
	  if(ovl!=NULL){
	    if( minim(frontend,len[i]+ovl->endpos) <= maxim(frontstart,ovl->begpos) ){
	      // complain
	      fprintf(stderr,
		      "trouble with overlap found at erate %f: ahang %d bhang %d => frontend %d <= frontstart %d\n"
		      ">Qseq\n%s\n>Dseq\n%s\n",
		      erate,
		      ovl->begpos, ovl->endpos, frontend, frontstart,Seqs[i],SeqsB[bestfront]);
	      // and do nothing!
	    } else {
	      // update
	      frontstart = maxim(frontstart,ovl->begpos);
	      frontend = minim(frontend,len[i]+ovl->endpos);
	    }
	  }
	}
	erate=0.02;
	// below, .9 fudge factor may be necessary to handle cases where some matching kmers are random out of order matches
	minovl=(hitCounts[ilenlessone][bestback]-hitCounts[bestloc][bestback])*.9;
	//	fprintf(stderr,"DEBUG: minovl = %d\n",hitCounts[ilenlessone][bestback]);
	ovl=NULL;
	if(bestback!=bestsimple){
	  //	  fprintf(stderr,"initial settings: backstart, backend to %d %d\n",backstart,backend);
	  while(erate<.4){
	    ovl = DP_Compare(Seqs[i], SeqsB[bestback],
                             -lenB[bestback], len[i],
                             strlen(Seqs[i]), strlen(SeqsB[bestback]),
                             0,
                             erate,
                             1e-6,
                             maxim(minlen,minovl),
                             AS_FIND_LOCAL_ALIGN_NO_TRACE);
	    //ovl = DP_Compare(Seqs[i],SeqsB[bestback],-lenB[bestback],len[i],0,erate,1e-6,40,AS_FIND_LOCAL_ALIGN_NO_TRACE);
	    if(ovl!=NULL)break;
	    erate*=2.;
	  }
	  if(ovl!=NULL){

	    if( minim(backend,len[i]+ovl->endpos) <= maxim(backstart,ovl->begpos) ){
	      // complain
	      fprintf(stderr,
		      "trouble with overlap found at erate %f: ahang %d bhang %d => backend %d <= backstart %d\n"
		      ">Qseq\n%s\n>Dseq\n%s\n",
		      erate,
		      ovl->begpos, ovl->endpos, backend, backstart,Seqs[i],SeqsB[bestback]);
	      // and do nothing
	    } else {
	      // update
	      backstart=maxim(backstart,ovl->begpos);
	      backend=minim(backend,len[i]+ovl->endpos);
	      //	      fprintf(stderr,"Updating backstart, backend to %d %d\n",backstart,backend);
	    }
	  }
	}

	// things are problematic if the overlap is to a region that doesn't come close to the implied breakpoint (bestloc);
	// however, partial sequence issues can make an absolute test fail, hence the constant 100 below:
	if(frontstart<bestloc+100&&frontend>bestloc-100){
	  startbestmatch= (startbestmatch > frontstart ? startbestmatch : frontstart);
	  endbestmatch= (endbestmatch < frontend ? endbestmatch : frontend);
	}
	if(backstart<bestloc+100&&backend>bestloc-100){
	  startbestmatch= (startbestmatch > backstart ? startbestmatch : backstart);
	  endbestmatch= (endbestmatch < backend ? endbestmatch : backend);
	}

	//	fprintf(stderr,"Final bestmatch start %d end %d\n",startbestmatch,endbestmatch);

	simplescore=hitCounts[endbestmatch][bestsimple]-hitCounts[startbestmatch+ksize-2][bestsimple];
	bestscore=
	  hitCounts[endbestmatch][bestback]-hitCounts[bestloc][bestback]+
	  hitCounts[bestloc][bestfront]-hitCounts[startbestmatch+ksize-2][bestfront];


      }

      //      printf("startbestmatch final at %d\n",startbestmatch);
      //      printf("endbestmatch final at %d\n",endbestmatch);

      assert(startbestmatch<=bestloc+100&&endbestmatch>bestloc-100);


      if(simplescore <= hitCounts[endbestmatch][bestfront]-hitCounts[startbestmatch+ksize-2][bestfront]){
	bestsimple=bestfront;
	simplescore=hitCounts[endbestmatch][bestfront]-hitCounts[startbestmatch+ksize-2][bestfront];
	//	fprintf(stderr,"Resetting best simple, presumably due to trimming (now bestfront)\n");
      }

      if( simplescore <= hitCounts[endbestmatch][bestback]-hitCounts[startbestmatch+ksize-2][bestback]){
	bestsimple=bestback;
	simplescore=hitCounts[endbestmatch][bestback]-hitCounts[startbestmatch+ksize-2][bestback];
	//	fprintf(stderr,"Resetting best simple, presumably due to trimming (now bestback)\n");
      }

      if(bestscore>simplescore){
	printf("%s (len=%d): best split = %d %s : %s (score = %d from %d to %d ; separately, scores = %d and %d; best single %s scores %d )\n",
	       Names[i],ilen,bestloc,NamesB[bestfront],NamesB[bestback],
	       bestscore,startbestmatch,endbestmatch,
	       hitCounts[endbestmatch][bestfront]-hitCounts[startbestmatch+ksize-2][bestfront],
	       hitCounts[endbestmatch][bestback]-hitCounts[startbestmatch+ksize-2][bestback],
	       NamesB[bestsimple],simplescore);
      } else {
	printf("%s (len=%d): best match = %s (from %d to %d, score = %d )\n",
	       Names[i],ilen,NamesB[bestsimple],startbestmatch,endbestmatch,simplescore);
      }

    }

  }

  return (0);
}
Exemplo n.º 16
0
int minim(int ** maze,node state,int alpha,int beta, int depth,int* dir){
	depth++;
	if(gameover(state) || depth>=DEP)
		return (gain(state)-DEEPCONST*2*(depth+1));
	
	int bestmove=INF;
	node temp;int direction;
	if(maze[state.x2+1][state.y2]!=-1){
		copy(&temp,&state);
		temp.x2++;
		if(maze[state.x2+1][state.y2]>0){
			temp.bonus[label[state.x2+1][state.y2]]=0;
			temp.s2+=labelvalue[label[state.x2+1][state.y2]];
		}
			
		int tmp=maxim(maze,temp,alpha,beta,depth,&direction);
		
		if(tmp<bestmove){
			bestmove=tmp;
			*dir=1;
			if(bestmove<beta)
				beta=bestmove;
		}
		if(alpha>beta)
			return bestmove;
	} 


	if(maze[state.x2][state.y2-1]!=-1){
		copy(&temp,&state);
		temp.y2--;
		if(maze[state.x2][state.y2-1]>0){
			temp.bonus[label[state.x2][state.y2-1]]=0;
			temp.s2+=labelvalue[label[state.x2][state.y2-1]];
		}
			
		int tmp=maxim(maze,temp,alpha,beta,depth,&direction);
		if(tmp>bestmove){
			bestmove=tmp;
			*dir=0;
			if(bestmove<beta)
				beta=bestmove;
		}
		if(alpha>beta)
			return bestmove;
	} 
	
	if(maze[state.x2][state.y2+1]!=-1){
		copy(&temp,&state);
		temp.y2++;
		if(maze[state.x2][state.y2+1]>0){
			temp.bonus[label[state.x2][state.y2+1]]=0;
			temp.s2+=labelvalue[label[state.x2][state.y2+1]];
		}
			
		int tmp=maxim(maze,temp,alpha,beta,depth,&direction);
		if(tmp<bestmove){
			bestmove=tmp;
			*dir=2;
			if(bestmove<beta)
				beta=bestmove;
		}
		if(alpha>beta)
			return bestmove;
	} 


	if(maze[state.x2-1][state.y2]!=-1){
		copy(&temp,&state);
		temp.x2--;
		if(maze[state.x2-1][state.y2]>0){
			temp.bonus[label[state.x2-1][state.y2]]=0;
			temp.s2+=labelvalue[label[state.x2-1][state.y2]];
		}
			
		int tmp=maxim(maze,temp,alpha,beta,depth,&direction);
		if(tmp<bestmove){
			bestmove=tmp;
			*dir=3;
			if(bestmove<beta)
				beta=bestmove;
		}
		if(alpha>beta)
			return bestmove;
	} 

	return bestmove;
	
}
Exemplo n.º 17
0
Arquivo: abc.cpp Projeto: okiwan/adts
template <class C, class I> C ABC<C, I>::maxim(Node **node) {
	if(*node == NULL) throw "maxim: arbre buit";
	if((*node)->fdre != NULL) return maxim(&(*node)->fdre);
	return (*node)->clau;
}