double ProblemAproximace1(Problem* prb, Stav* s, char** str) { double sum=0.0; double min=UINT_MAX; Set* m = SetInvertCopy(s->m); SetRemove(m, s->v); SetAdd(m, 0); //kam muze jit *str = calloc(m->count*PROBLEM_BACKTRACK, sizeof(char)); strcpy(*str, ""); int* filter = SetArray(m); int i,x,y,next=-1; x=s->v; while(m->count>0) { for(i=0; i<m->count; i++) { y=filter[i]; if(y==0 && m->count>1) continue; //do startovniho vrcholu az nakonec min = UINT_MAX; next=-1; if(prb->g->mat[x][y]>=0 && prb->g->mat[x][y]<min) { min=prb->g->mat[x][y]; next = y; } } sum+=min; if(next<0) break; SetRemove(m, next); x = next; free(filter); filter = SetArray(m); sprintf(*str, "%s%i ", *str, next); } SetDestroy(m); free(filter); return sum; }
SArray* ProblemNaslednici(Problem* prb, Stav* s) { SArray* p = SArrayCreate(ProblemStavCompare); Set* mv = SetCopy(s->m); SetAdd(mv,s->v); Stav* n; int i; for(i=0; i<prb->g->n; i++) { if(prb->g->mat[s->v][i]>=0 && (!SetIsIn(mv,i) || (i==0 && mv->count==prb->g->n))) { n = ProblemStavCreate(prb,i,mv); SArrayAdd(p, (void*) n); } } SetDestroy(mv); return p; }
void SetJoin(Set *set, Set *otherset, SetElementCopyFn copy_function) { assert(set != NULL); assert(otherset != NULL); if (set == otherset) return; SetIterator si = SetIteratorInit(otherset); void *ptr = NULL; for (ptr = SetIteratorNext(&si); ptr != NULL; ptr = SetIteratorNext(&si)) { if (copy_function != NULL) { ptr = copy_function(ptr); } SetAdd(set, ptr); } }
double ProblemHeuristika3(Problem* prb, Stav* s) { //soucet minimalnich ohodnoceni hran vedoucich do vrcholu do kterych obch. cest. jeste nevesel double sum=0; double min; Set* m2 = SetInvertCopy(s->m); //kam muze jit Set* m1 = SetCopy(m2); //odkud muze jit SetRemove(m1, s->v); SetAdd(m1, 0); int* filter1 = SetArray(m1); int* filter2 = SetArray(m2); int i,j,x,y; for(i=0; i<m1->count; i++) { x=filter1[i]; min = UINT_MAX; for(j=0; j<m2->count; j++) { y=filter2[j]; if(prb->g->mat[y][x]>=0 && prb->g->mat[y][x]<min) min=prb->g->mat[y][x]; } if(min!=UINT_MAX) sum+=min; } SetDestroy(m1); SetDestroy(m2); free(filter1); free(filter2); return sum; }
int main() { stringset strset; SetNew(&strset, StringCompare, NULL); assert(SetAdd(&strset, "lion")); assert(SetAdd(&strset, "tiger")); assert(SetAdd(&strset, "bear")); SetAdd(&strset, "dorothy"); SetAdd(&strset, "scarecrow"); SetAdd(&strset, "witch"); SetAdd(&strset, "tin man"); SetAdd(&strset, "wizard"); assert(!SetAdd(&strset, "lion")); assert(!SetAdd(&strset, "tiger")); assert(!SetAdd(&strset, "bear")); SetPrint(&strset); printf("\nSet contains %d elements\n", SetSize(&strset)); char *dorothy = SetSearch(&strset, "dorothy"); char *elphaba = SetSearch(&strset, "elphaba"); printf("\n"); printf("Found dorothy: %s\n", dorothy == NULL ? "no" : "yes"); printf("Found elphaba: %s\n", elphaba == NULL ? "no" : "yes"); SetDestroy(&strset); return 0; }
/* Find all nonterminals which will generate the empty string. ** Then go back and compute the first sets of every nonterminal. ** The first set is the set of all terminal symbols which can begin ** a string generated by that nonterminal. */ void FindFirstSets(struct lmno *lmnop) { int i; struct rule *rp; int progress; for(i=0; i<lmnop->nsymbol; i++) { lmnop->symbols[i]->lambda = B_FALSE; } for(i=lmnop->nterminal; i<lmnop->nsymbol; i++) { lmnop->symbols[i]->firstset = SetNew(); } /* First compute all lambdas */ do { progress = 0; for(rp = lmnop->rule; rp; rp = rp->next) { if(rp->lhs->lambda) continue; for(i=0; i<rp->nrhs; i++) { if( rp->rhs[i]->lambda==B_FALSE ) break; } if(i == rp->nrhs) { rp->lhs->lambda = B_TRUE; progress = 1; } } }while( progress ); /* Now compute all first sets */ do { struct symbol *s1, *s2; progress = 0; for(rp = lmnop->rule; rp; rp = rp->next) { s1 = rp->lhs; for(i=0; i<rp->nrhs; i++) { s2 = rp->rhs[i]; if(s2->type == TERMINAL) { progress += SetAdd(s1->firstset,s2->index); break; } else if(s1 == s2) { if(s1->lambda == B_FALSE) break; } else { progress += SetUnion(s1->firstset,s2->firstset); if( s2->lambda==B_FALSE ) break; } } } }while( progress ); return; }