int main(int argc, char *argv[]) { int i; printf("Traveling salesman, sequential version\n"); totalnumnodes = NUMNODES; startnodeindex = STARTNODE; creategraph(); /* compute initial bound for path length */ bestpathlen = estpathlength(); /* init state and start search */ nodearray[startnodeindex].nmark = 1; nummarkednodes = 1; currpathindex = 0; bestpathindex = 0; currpatharray[currpathindex++] = startnodeindex; search(startnodeindex, 0.0); /* output best path at end */ printf("Best path: [len=%.1f] ", bestpathlen); for (i = 0; i < bestpathindex; i++) printf("%d ",bestpatharray[i]); printf("\n"); }
/****************************** 主程序******************************/ int main() { graph ptr; int node[20][2] = { {1, 2}, {2, 1}, /* 边线数组 */ {1, 3}, {3, 1}, {1, 4}, {4, 1}, {2, 5}, {5, 2}, {2, 6}, {6, 2}, {3, 7}, {7, 3}, {4, 7}, {4, 4}, {5, 8}, {8, 5}, {6, 7}, {7, 6}, {7, 8}, {8, 7} }; int i; //clrscr(); for ( i = 1; i <= 8; i++ ) /* 顶点数组初始化 */ { head[i].vertex = i; /* 设定顶点值 */ head[i].nextnode = NULL; /* 指针为空 */ visited[i] = 0; /* 设定遍历初始标志 */ } creategraph(node,20); /* 建立邻接表 */ printf("Content of the gragh's ADlist is:\n"); for ( i = 1; i <= 8; i++ ) { printf("vertex%d ->",head[i].vertex); /* 顶点值 */ ptr = head[i].nextnode; /* 顶点位置 */ while ( ptr != NULL ) /* 遍历至链表尾 */ { printf(" %d ",ptr->vertex); /* 印出顶点内容 */ ptr = ptr->nextnode; /* 下一个顶点 */ } printf("\n"); /* 换行 */ } printf("\nThe end of the dfs are:\n"); dfs(1); /* 打印输出遍历过程 */ printf("\n"); /* 换行 */ puts(" Press any key to quit..."); // getch(); }
/* main function */ int main(){ gnode *adj1[max]; int n,i,x,y,z,s,t,j,r,des; long int e; //printf("Enter the no of test cases:\n"); scanf("%d",&t); for(j=0;j<t;j++){ printf("Enter the no of nodes:\n"); scanf("%d",&n); printf("Enter the no of edges:\n"); scanf("%d",&e); creategraph(adj1,n); //srand(time(NULL)); //printf("Enter the vertices in which edge exists and their corresponding weight:\n"); for(i=0;i<e;i++){ x=1+rand()%n; y=1+rand()%n; z=1+rand()%6; r=1+rand()%200; if(x!=y){ inputgraph(adj1,n,x-1,y-1,z,r); inputgraph(adj1,n,y-1,x-1,z,r); } //inputgraph(adj1,n,x-1,y-1,r); //inputgraph(adj1,n,y-1,x-1,r); } printgraph(adj1,n); printf("Enter the source vertex:\n"); scanf("%d",&s); printf("Enter the destination vertex:\n"); scanf("%d",&des); dijkshtra(adj1,n,s-1); if(d[des-1]==99999) printf("-1"); else printf("%d",d[des-1]); printf("\n"); } return 0; }
int main() { int node[7][3] = { {1,2,35}, {2,3,45}, {2,4,30}, {3,5,25}, {4,5,45}, {4,6,130}, {5,6,100} }; int i,j; for(i = 1; i <= 6; i++) { for(j = 1; j <=6; j++) { cost[i][j] = MAXLEN; //设置数组最长距离 } } creategraph(node,7); //创建加权图 printf("加权图的邻接矩阵内容:\n"); for(i = 1; i <= 6; i++) { for(j = 1; j <= 6; j++) { printf(" %4d ",cost[i][j]); //输出加权数组内容 } printf("\n"); } printf("\n从顶点1到各顶点最近距离计算过程:\n"); shortestpath(1,6); //查找最短路径 return 0; }
// compilation d'une somme // [locals nom] int Compiler::parsesum() { int k; // on crée le bloc type TABSET(m,newref,REF_VAL,INTTOVAL(0)); TABSET(m,newref,REF_CODE,INTTOVAL(CODE_SUM)); // [local name] int loop=1; do { if (!parser->next(0)) { PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found EOF)\n"); return MTLERR_SN; } if (islabel(parser->token)) { if (k=STRPUSH(m,parser->token)) return k; // on crée le bloc champ int* newcons=MALLOCCLEAR(m,REF_LENGTH); if (!newcons) return MTLERR_OM; TABSET(m,newcons,REF_NAME,STACKPULL(m)); if (k=STACKPUSH(m,PNTTOVAL(newcons))) return MTLERR_OM; // [newcons local name] addreftopackage(newcons,newpackage); STACKDROP(m); // [local name] TABSET(m,newcons,REF_VAL,TABGET(newref,REF_VAL)); TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation if (k=createnodetype(TYPENAME_FUN)) return k; if ((parser->next(0))&&((!strcmp(parser->token,"|"))||(!strcmp(parser->token,";;")))) { parser->giveback(); if (k=createnodetuple(0)) return k; TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS0)); } else { parser->giveback(); if (k=creategraph(parser,PNTTOVAL(newpackage),1,locals)) return k; if (k=createnodetuple(1)) return k; TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS)); } TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun STACKDROP(m); TABSET(m,VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH+1,TABGET(newref,REF_TYPE)); // attachement du type resultat au noeud fun TABSET(m,newcons,REF_TYPE,STACKPULL(m)); outputbuf->reinit(); outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newcons,REF_NAME)))); echograph(outputbuf,VALTOPNT(TABGET(newcons,REF_TYPE))); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); } else { PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found '%s')\n",parser->token); return MTLERR_SN; } if (!parser->next(0)) { PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found EOF)\n"); return MTLERR_SN; } if (!strcmp(parser->token,";;")) loop=0; else if (strcmp(parser->token,"|")) { PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found '%s')\n",parser->token); return MTLERR_SN; } } while(loop); STACKDROPN(m,2); outputbuf->reinit(); outputbuf->printf("Compiler : "); echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); outputbuf->printf(" : constructor\n"); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); return 0; }
// compilation d'une structure (le premier '[' a été lu) // [locals nom] int Compiler::parsestruct() { int k; // on crée le bloc type TABSET(m,newref,REF_VAL,INTTOVAL(0)); TABSET(m,newref,REF_CODE,INTTOVAL(CODE_STRUCT)); // [local name] int loop=1; do { if (!parser->next(0)) { PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found EOF)\n"); return MTLERR_SN; } if (islabel(parser->token)) { if (k=STRPUSH(m,parser->token)) return k; // on crée le bloc champ int* newfield=MALLOCCLEAR(m,REF_LENGTH); if (!newfield) return MTLERR_OM; TABSET(m,newfield,REF_NAME,STACKPULL(m)); TABSET(m,newfield,REF_CODE,INTTOVAL(CODE_FIELD)); if (k=STACKPUSH(m,PNTTOVAL(newfield))) return MTLERR_OM; // [newfield local name] addreftopackage(newfield,newpackage); STACKDROP(m); // [local name] if (k=STACKPUSH(m,TABGET(newref,REF_VAL))) return k; if (k=STACKPUSH(m,PNTTOVAL(newref))) return k; if (k=DEFTAB(m,FIELD_LENGTH)) return k; TABSET(m,newfield,REF_VAL,STACKPULL(m)); TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation if (k=createnodetype(TYPENAME_FUN)) return k; if (k=STACKPUSH(m,TABGET(newref,REF_TYPE))) return k; if (k=createnodetuple(1)) return k; TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun STACKDROP(m); if ((!parser->next(0))||(strcmp(parser->token,":"))) { parser->giveback(); k=createnodetype(TYPENAME_WEAK); } else k=creategraph(parser,PNTTOVAL(newpackage),1,locals); if (k) return k; TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun STACKDROP(m); TABSET(m,newfield,REF_TYPE,STACKPULL(m)); outputbuf->reinit(); outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newfield,REF_NAME)))); echograph(outputbuf,VALTOPNT(TABGET(newfield,REF_TYPE))); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); } else if (!strcmp(parser->token,"]")) loop=0; else { PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found '%s')\n",parser->token); return MTLERR_SN; } } while(loop); if (k=parser->parsekeyword(";;")) return k; STACKDROPN(m,2); outputbuf->reinit(); outputbuf->printf("Compiler : "); echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); outputbuf->printf(" : struct (%d)\n",VALTOINT(TABGET(newref,REF_VAL))); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); return 0; }
// compilation d'un prototype // [name] -> 0 int Compiler::parseproto() { int k; if (!parser->next(0)) { PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found EOF)\n"); return MTLERR_SN; } int nbarg=-1; if (!strcmp(parser->token,"=")) { if (k=creategraph(parser,PNTTOVAL(newpackage),0)) return k; int vp=STACKGET(m,0); if (vp!=NIL) { int* p=VALTOPNT(vp); if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_FUN)) { vp=TABGET(p,TYPEHEADER_LENGTH); if (vp!=NIL) { int* p=VALTOPNT(vp); if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_TUPLE)) { nbarg=TABLEN(p)-TYPEHEADER_LENGTH; } } } } if (nbarg<0) { PRINTF(m)(LOG_COMPILER,"Compiler : function type expected\n"); return MTLERR_SN; } } else if (isdecimal(parser->token)) { nbarg=mtl_atoi(parser->token); if (k=createnodetype(TYPENAME_FUN)) return k; int i;for(i=0;i<nbarg;i++) if (k=createnodetype(TYPENAME_WEAK)) return k; if (k=createnodetuple(nbarg)) return k; TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun STACKDROP(m); if (k=createnodetype(TYPENAME_WEAK)) return k; // noeud résultat TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun STACKDROP(m); } if (nbarg<0) { PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found '%s')\n",parser->token); return MTLERR_SN; } if (k=parser->parsekeyword(";;")) return k; // on crée le bloc fonction newref=MALLOCCLEAR(m,REF_LENGTH); if (!newref) return MTLERR_OM; TABSET(m,newref,REF_TYPE,STACKPULL(m)); TABSET(m,newref,REF_NAME,STACKPULL(m)); TABSET(m,newref,REF_CODE,INTTOVAL(nbarg)); TABSET(m,newref,REF_PACKAGE,INTTOVAL(ifuns++)); if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref] addreftopackage(newref,newpackage); STACKDROP(m); outputbuf->reinit(); outputbuf->printf("Compiler : proto %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); return 0; }
int main(int argc, char *argv[]) { int i,fd,res,tmp; Adjptr edge; printf("Traveling salesman, process version\n"); printf("Number of processes: %d, Prefix length: %d\n",MAXPROC,PREFLEN); totalnumnodes = NUMNODES; startnodeindex = STARTNODE; creategraph(); /* mmap the prefix array and the best path length */ fd = open("prefix.dat", O_RDWR|O_CREAT|O_TRUNC,0600); if (fd == -1) { fprintf(stderr,"open failed\n"); exit(-1); } /* make file long enough by writing a zero byte at the right point */ res = lseek(fd,FILESIZE,SEEK_SET); if (res == -1) { fprintf(stderr,"lseek failed\n"); exit(-1); } res = write(fd,"", 1); if (res != 1) { fprintf(stderr,"write failed\n"); exit(-1); } /* allocate space for the array plus a few rows */ shbestpathlen = (double *) mmap(NULL,FILESIZE,PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (shbestpathlen == MAP_FAILED) { fprintf(stderr,"mmap failed\n"); exit(-1); } /* use last half as (int) prefixindex */ prefixindex = (int *) &(shbestpathlen[1]); /* now compute possible path prefixes (from starting node) and * store them in prefix array. function is recursive, uses * currpatharray in similar manner to search function */ *prefixindex = -1; currpathindex = 0; currpatharray[currpathindex++] = STARTNODE; fillprefix(STARTNODE,0.0); /* compute initial upper bound for path length */ mybestpathlen = estpathlength(); *shbestpathlen = mybestpathlen; /* now fork MAXPROC-1 new processes */ parent = 1; for (i = 0; i < MAXPROC-1; i++) { tmp = fork(); if (tmp < 0) { fprintf(stderr, "fork failed \n"); exit(-1); } if (tmp == 0) /* child */ { parent = 0; break; } } /* set pid for output */ pid = (int) getpid(); printf("Process %d starting ",pid); if (parent) printf("(parent)\n"); else printf("(child)\n"); /* each process loops, getting next free prefix & searching it */ bestpathindex = 0; while ((tmp = (*prefixindex)--) >= 0) { nummarkednodes = 0; /* copy prefix, mark nodes and edges, and start search */ for (i = 0; i < PREFLEN; i++) { currpatharray[i] = prefix[tmp][i]; if (nodearray[currpatharray[i]].nmark == 0) { /* increment count of marked nodes */ nodearray[currpatharray[i]].nmark = 1; nummarkednodes++; } /* mark edge out from this node, if not last in prefix */ if (i < PREFLEN-1) { edge = nodearray[prefix[tmp][i]].adjlist; while (edge != NULL && edge->nodeindex != prefix[tmp][i+1]) edge = edge->next; if (edge == NULL) { fprintf(stderr,"missing edge in graph\n"); exit(-1); } edge->amark = 1; } } currpathindex = PREFLEN; search(currpatharray[currpathindex-1], dist[tmp]); /* now loop again and undo marks from above */ for (i = 0; i < PREFLEN; i++) { nodearray[currpatharray[i]].nmark = 0; edge = nodearray[currpatharray[i]].adjlist; while (edge != NULL) { edge->amark = 0; edge = edge->next; } } } /* have each process output its best path */ if (parent) { printf("Parent (%d) exiting: its best path = [len=%.1f] [vis=%d] ", pid, mybestpathlen,bestpathindex); for (i = 0; i < bestpathindex; i++) printf("%d ", bestpatharray[i]); printf("\n"); while (waitpid(-1,NULL,0) > 0); printf("Shortest overall path length: %.1f\n", *shbestpathlen); } else { printf("Child (%d) exiting: its best path = [len=%.1f] ", pid, mybestpathlen); for (i = 0; i < bestpathindex; i++) printf("%d ", bestpatharray[i]); printf("\n"); } }