int dijkstra(int myID) { int i,v, iDist; while (qcount() < NUM_NODES-1){ for (i=myID*(NUM_NODES/PROCESSORS); (i<(myID+1)*(NUM_NODES/PROCESSORS)+(myID+1==PROCESSORS && NUM_NODES%PROCESSORS!=0 ? NUM_NODES%PROCESSORS:0)); i++) { if(rgnNodes[i].iCatched==0 && rgnNodes[i].iDist<uVertex[myID].iDist && rgnNodes[i].iDist!=0){ uVertex[myID].iDist=rgnNodes[i].iDist; uVertex[myID].iNID=i; } } actuateBarrier(&myBarrier); /* Actuate the barrier */ if(myID==0){ globalMiniCost=NONE; for(i=0;i<PROCESSORS;i++){ /* Get the global minimum cost */ if(globalMiniCost>uVertex[i].iDist){ globalMiniCost = uVertex[i].iDist; u=uVertex[i].iNID; } } g_qCount++; } actuateBarrier(&myBarrier); /* Actuate the barrier */ for (v=myID*(NUM_NODES/PROCESSORS); (v<(myID+1)*(NUM_NODES/PROCESSORS)+(myID+1==PROCESSORS && NUM_NODES%PROCESSORS!=0 ? NUM_NODES%PROCESSORS:0)); v++) { if(v==u){ rgnNodes[v].iCatched = 1; continue; } if((rgnNodes[v].iCatched==0 && rgnNodes[v].iDist>(rgnNodes[u].iDist+AdjMatrix[u][v]))){ /* dist(v)>dist(u)+w(u,v) */ rgnNodes[v].iDist=rgnNodes[u].iDist+AdjMatrix[u][v]; rgnNodes[v].iPrev = u; } } uVertex[myID].iDist = NONE; //Reset } }
int dijkstra(int myID) { int x,i,v,iDist; int chStart, chEnd; int u =-1; for(x=tasks[myID][0]; x<tasks[myID][1]; x++){ chStart = nodes_tasks[x][0]; //Start node chEnd = nodes_tasks[x][1]; //End node u=-1; //Initialize and clear uVertex[myID].iDist=NONE; uVertex[myID].iPID=myID; uVertex[myID].iNID=NONE; g_qCount[myID] = 0; u=-1; for (v=0; v<NUM_NODES; v++) { rgnNodes[myID][v].iDist = AdjMatrix[chStart][v]; rgnNodes[myID][v].iPrev = NONE; rgnNodes[myID][v].iCatched = 0; } //Start working while (qcount(myID) < NUM_NODES-1){ for (i=0; i<NUM_NODES; i++) { if(rgnNodes[myID][i].iCatched==0 && rgnNodes[myID][i].iDist<uVertex[myID].iDist && rgnNodes[myID][i].iDist!=0){ uVertex[myID].iDist=rgnNodes[myID][i].iDist; uVertex[myID].iNID=i; } } globalMiniCost[myID]=NONE; if(globalMiniCost[myID]>uVertex[myID].iDist){ globalMiniCost[myID] = uVertex[myID].iDist; u=uVertex[myID].iNID; g_qCount[myID]++; } for (v=0; v<NUM_NODES; v++) { if(v==u){ rgnNodes[myID][v].iCatched = 1; continue; } if((rgnNodes[myID][v].iCatched==0 && rgnNodes[myID][v].iDist>(rgnNodes[myID][u].iDist+AdjMatrix[u][v]))){ rgnNodes[myID][v].iDist=rgnNodes[myID][u].iDist+AdjMatrix[u][v]; rgnNodes[myID][v].iPrev = u; } } uVertex[myID].iDist = NONE; //Reset } printResult(myID,chStart,chEnd); } }
int dijkstra(int chStart, int chEnd) { for (ch = 0; ch < NUM_NODES; ch++) { rgnNodes[ch].iDist = NONE; rgnNodes[ch].iPrev = NONE; } if (chStart == chEnd) { pthread_mutex_lock(&mutex_print); printf("Shortest path is 0 in cost. Just stay where you are.\n"); pthread_mutex_unlock(&mutex_print); } else { rgnNodes[chStart].iDist = 0; rgnNodes[chStart].iPrev = NONE; enqueue_static (chStart, 0, NONE); while (qcount() > 0) { dequeue_static (&iNode, &iDist, &iPrev); for (i = 0; i < NUM_NODES; i++) { if ((iCost = AdjMatrix[iNode][i]) != NONE) { if ((NONE == rgnNodes[i].iDist) || (rgnNodes[i].iDist > (iCost + iDist))) { rgnNodes[i].iDist = iDist + iCost; rgnNodes[i].iPrev = iNode; enqueue_static (i, iDist + iCost, iNode); } } } } pthread_mutex_lock(&mutex_print); fprintf(fileout_dijkstra,"Shortest path is %d in cost. ", rgnNodes[chEnd].iDist); fprintf(fileout_dijkstra,"Path is: "); print_path(rgnNodes, chEnd); fprintf(fileout_dijkstra,"\n"); pthread_mutex_unlock(&mutex_print); } return 1; }
void dijkstra(int chStart, int chEnd) { for (ch = 0; ch < NUM_NODES; ch++) { rgnNodes[ch].iDist = NONE; rgnNodes[ch].iPrev = NONE; } if (chStart == chEnd) { puts("f**k\r\n"); } else { rgnNodes[chStart].iDist = 0; rgnNodes[chStart].iPrev = NONE; enqueue (chStart, 0, NONE); while (qcount() > 0) { dequeue (&iNode, &iDist, &iPrev); for (i = 0; i < NUM_NODES; i++) { if ((iCost = AdjMatrix[iNode][i]) != NONE) { if ((NONE == rgnNodes[i].iDist) || (rgnNodes[i].iDist > (iCost + iDist))) { rgnNodes[i].iDist = iDist + iCost; rgnNodes[i].iPrev = iNode; enqueue (i, iDist + iCost, iNode); } } } puts("w"); int_print(g_qCount); puts("\r\n"); } /* printf("Shortest path is %d in cost. ", rgnNodes[chEnd].iDist); printf("Path is: "); print_path(rgnNodes, chEnd); printf("\n"); */ } }
int dijkstra(int chStart, int chEnd) { for (ch = 0; ch < NUM_NODES; ch++) { rgnNodes[ch].iDist = NONE; rgnNodes[ch].iPrev = NONE; } if (chStart == chEnd) { printf("Shortest path is 0 in cost. Just stay where you are.\n"); } else { rgnNodes[chStart].iDist = 0; rgnNodes[chStart].iPrev = NONE; enqueue (chStart, 0, NONE); int x=0; while (qcount() > 0) { dequeue (&iNode, &iDist, &iPrev); for (i = 0; i < NUM_NODES; i++) { if ((iCost = AdjMatrix[iNode][i]) != NONE) { if ((NONE == rgnNodes[i].iDist) || (rgnNodes[i].iDist > (iCost + iDist))) { rgnNodes[i].iDist = iDist + iCost; rgnNodes[i].iPrev = iNode; enqueue (i, iDist + iCost, iNode); } } } x++; } printf("x=%d\n",x); printf("Shortest path is %d in cost. ", rgnNodes[chEnd].iDist); printf("Path is: "); print_path(rgnNodes, chEnd); printf("\n"); } }
int dijkstra(int chStart, int chEnd) { for (ch = 0; ch < NUM_NODES; ch++) { rgnNodes[ch].iDist = NONE; rgnNodes[ch].iPrev = NONE; } if (chStart == chEnd) { return 0; } else { rgnNodes[chStart].iDist = 0; rgnNodes[chStart].iPrev = NONE; enqueue (chStart, 0, NONE); while (qcount() > 0) { QITEM *tmp = dequeue (&iNode, &iDist, &iPrev); if(tmp != 0) free(tmp); for (i = 0; i < NUM_NODES; i++) { iCost = AdjMatrix[iNode][i]; if (iCost != NONE) { if ((NONE == rgnNodes[i].iDist) || (rgnNodes[i].iDist > (iCost + iDist))) { rgnNodes[i].iDist = iDist + iCost; rgnNodes[i].iPrev = iNode; enqueue (i, iDist + iCost, iNode); } } } } } return rgnNodes[chEnd].iDist; }
void dijkstra(int myID) { int x,i,v; int chStart, chEnd; int u =-1; Echo("My id"); Echo(itoa(myID)); Echo("GRUPO 0"); /*Echo(itoa(tasks[0][0])); Echo(itoa(tasks[1][0])); Echo(itoa(tasks[2][0])); Echo(itoa(tasks[3][0]));*/ Echo(itoa(tasks[myID][0])); Echo("GRUPO 1"); Echo(itoa(tasks[myID][1])); Echo("fim "); //RealTime(103349, 103349, 11822, 0); for(x=tasks[myID][0]; x<tasks[myID][1]; x++){ chStart = nodes_tasks[x][0]; //Start node chEnd = nodes_tasks[x][1]; //End node u=-1; //Initialize and clear uVertex[myID].iDist=NONE; uVertex[myID].iPID=myID; uVertex[myID].iNID=NONE; g_qCount[myID] = 0; u=-1; for (v=0; v<NUM_NODES; v++) { rgnNodes[myID][v].iDist = AdjMatrix[chStart][v]; rgnNodes[myID][v].iPrev = NONE; rgnNodes[myID][v].iCatched = 0; } //Start working while (qcount(myID) < NUM_NODES-1){ for (i=0; i<NUM_NODES; i++) { if(rgnNodes[myID][i].iCatched==0 && rgnNodes[myID][i].iDist<uVertex[myID].iDist && rgnNodes[myID][i].iDist!=0){ uVertex[myID].iDist=rgnNodes[myID][i].iDist; uVertex[myID].iNID=i; } } globalMiniCost[myID]=NONE; if(globalMiniCost[myID]>uVertex[myID].iDist){ globalMiniCost[myID] = uVertex[myID].iDist; u=uVertex[myID].iNID; g_qCount[myID]++; } for (v=0; v<NUM_NODES; v++) { if(v==u){ rgnNodes[myID][v].iCatched = 1; continue; } if((rgnNodes[myID][v].iCatched==0 && rgnNodes[myID][v].iDist>(rgnNodes[myID][u].iDist+AdjMatrix[u][v]))){ rgnNodes[myID][v].iDist=rgnNodes[myID][u].iDist+AdjMatrix[u][v]; rgnNodes[myID][v].iPrev = u; } } uVertex[myID].iDist = NONE; //Reset } sendResult(myID,chStart,chEnd); qtdEnvios++; } Message msg; msg.length = 33; msg.msg[0] = -1; Send(&msg, print); Echo("finaliza\n"); }