예제 #1
0
void dijkstra(int src, int V, int *graph[])
{
     int sptSet[V];
     int dist[V];

     for (int i = 1; i <= V; i++) {
         sptSet[i] = 0;
         dist[i] = INT_MAX;
     }

     dist[src] = 0;
 
     for (int count = 0; count < V-1; count++)
     {
       int u = minDistance(sptSet, V, dist);
       sptSet[u] = 1;

       for (int v = 1; v <= V; v++) {
           if (!sptSet[v] && graph[u][v] > 0 && dist[u] != INT_MAX 
                                       && dist[u]+  graph[u][v] < dist[v])
            dist[v] = dist[u] +  graph[u][v];
       }
         
     }
     printDist(V, src, dist);
}
예제 #2
0
파일: bellman.c 프로젝트: sandro93/praxis
int main(int argc, char *argv[]) {
	int i, j;
	int w;

	FILE *fin = fopen("dist.txt", "r");
	fscanf(fin, "%d", &n);
	e = 0;

	for (i = 0; i < n; ++i)
		for (j = 0; j < n; ++j) {
			fscanf(fin, "%d", &w);
			if (w != 0) {
				edges[e].u = i;
				edges[e].v = j;
				edges[e].w = w;
				++e;
			}
		}
	fclose(fin);

	/* printDist(); */

	bellman_ford(0);

	printDist();

	return 0;
}
예제 #3
0
void dijkstra(int start,int size) {
	minHeap* h = (minHeap*)malloc(sizeof(minHeap));
	h->size = 0; h->array[0] = unused;
	dist[start] = 0; int count = 0; preDist[start] = 0;
	for (int i = 1; i <= size; i++) {
		push(h, i);
	}
	while (!isEmpty(h)) {
		int top = pop(h);
		printf("------------------------------------------------------\n");
		
		printf("S[%d] : d[%c] = %d\n", count,printVertex(top), dist[top]); count++;
		
		printf("------------------------------------------------------\n");
		for (int i = 1; i <= size; i++) {
			if (graph[top-1][i-1]!=inf) {	//연결	
				if (dist[i] > dist[top] + graph[top-1][i-1]) {
					dist[i] = dist[top] + graph[top-1][i-1];
					buildMinHeap(h);
				}
			}
		}
		printDist(h);
		for(int i=1; i<=size; i++){
			preDist[i] = dist[i];
		}
	}
}
예제 #4
0
bool Graph::dijkstra(Vertex &v)
{
  if(vs->size() < 1)
    return false;
 
  set<int> *S = new set<int>();
  S->insert(v.getNodeId());
 
  set<int> *candidate = new set<int>();
  obtainCandidate(*S, *candidate);
  printS(*S);
  printCandidate(*candidate);
 
  vector<vector<int> > *dist = new vector<vector<int> >();
  int u = v.getNodeId();
  vector<int> *vec = new vector<int>(vs->size());
  for(unsigned int i = 0; i < vs->size(); i++)
    vec->at(i) = INT_MAX;
  vec->at(retrieveVertexById(u)) = 0;
  calcShortestPath(vs->at(retrieveVertexById(u)), *vec, *vec);
  dist->push_back(*vec);
 
  printDist(*dist);
 
  for(unsigned int i = 0; i < vs->size() -1; i++)
  {
    vector<int> *vec = new vector<int>();
    bool nextNode = calcShortestPath(vs->at(retrieveVertexById(u)), dist->at(i), *vec);
    if(false == nextNode) // no neighbour node, finish
       return true;
 
    dist->push_back(*vec);
    
    u = selectU(*candidate, *vec);
    S->insert(u);
    printS(*S);
    obtainCandidate(*S, *candidate);
    printDist(*dist);
  }
 
  return true;
}
/*
    floyd_warshall()
 
    after calling this function dist[i][j] will the the minimum distance
    between i and j if it exists (i.e. if there's a path between i and j)
    or 0, otherwise
*/
void floyd_warshall() {
    int i, j, k;
    for (k = 0; k < n; ++k) {
        printDist();
        for (i = 0; i < n; ++i)
            for (j = 0; j < n; ++j)
                /* If i and j are different nodes and if
                    the paths between i and k and between
                    k and j exist, do */
                if ((dist[i][k] * dist[k][j] != 0) && (i != j))
                    /* See if you can't get a shorter path
                        between i and j by interspacing
                        k somewhere along the current
                        path */
                    if ((dist[i][k] + dist[k][j] < dist[i][j]) ||
                        (dist[i][j] == 0))
                        dist[i][j] = dist[i][k] + dist[k][j];
    }
    printDist();
}
예제 #6
0
파일: paralmpi.c 프로젝트: fteloniatis/xuma
int main(int argc, char *argv[]) {
 
    int my_rank,                /* rank of process */
            num_procs,              /* number of processes */
            slice,
            ypoloipo=0;
    MPI_Status status;
    /* Start up MPI */
    MPI_Init(&argc,&argv);
    /* Find out number of process */
    MPI_Comm_size(MPI_COMM_WORLD,&num_procs);
    /* Find out process rank */
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
 
    ypoloipo= n%(num_procs-1);
    slice= (n-ypoloipo)/(num_procs-1);
 
    /* master code */
    if (my_rank == MASTER) {
      double t1,t2;
      int disable=0,t=3;
      int result[t];
      //initialize dist[][]
      int i, j;
      for (i = 0; i < n; ++i)
          for (j = 0; j < n; ++j)
              if (i==j)
                dist[i][j]=0;
              else
                dist[i][j]= (int)( 11.0 * rand() / ( RAND_MAX + 1.0 ) );// number creator 0-10
 
      printDist();// printout the new array
       t1 = MPI_Wtime();
          for(i=1;i<num_procs;i++)
         MPI_Send(&dist,n*n,MPI_INT,i,WORKTAG,MPI_COMM_WORLD);//send the array dist in every machine
 
      do {
         MPI_Recv(&result,t,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
         if (status.MPI_TAG == DIETAG)
            disable++;
         else
            if (dist[result[1]][result[2]]>result[0])
                dist[result[1]][result[2]]=result[0];
      } while (disable < num_procs-1);
       t2 = MPI_Wtime();
      printDist();// printout again the new array with shortest paths
      printf("total time %f \n",t2-t1);
    }
    /* workers code*/
    else{
 
        int i, j, k,t=3;
        int out[t];
        MPI_Recv(&dist,n*n,MPI_INT,MASTER,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
        if(my_rank+1!=num_procs)
            ypoloipo=0;
        for (k = slice*(my_rank-1); k < slice*(my_rank-1)+slice+ypoloipo; ++k)
            for (i = 0; i < n; ++i)
                for (j = 0; j < n; ++j)
                    /* If i and j are different nodes and if
                        the paths between i and k and between
                        k and j exist, do */
                    if ((dist[i][k] * dist[k][j] != 0) && (i != j))
                        /* See if you can't get a shorter path
                            between i and j by interspacing
                            k somewhere along the current
                            path */
                        if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0)){
                            dist[i][j] = dist[i][k] + dist[k][j];
                            out[0]=dist[i][j];
                            out[1]=i;
                            out[2]=j;
                            MPI_Send(&out,t,MPI_INT,MASTER,0,MPI_COMM_WORLD);
                        }
        MPI_Send(0,0,MPI_INT,MASTER,DIETAG,MPI_COMM_WORLD);
    }
 
    /* Shut down MPI */
    MPI_Finalize();
 
    return 0;
}