Ejemplo n.º 1
0
int main()
{
    int **w,i,j;
    w = CreateGraphAM(9);
    AddEdgeW(1,2,5,&w);
    AddEdgeW(1,4,10,&w);
    AddEdgeW(1,3,9,&w);
    AddEdgeW(4,2,2,&w);
    AddEdgeW(3,4,1,&w);
    AddEdgeW(3,5,8,&w);
    AddEdgeW(6,4,9,&w);
    AddEdgeW(5,9,1,&w);
    AddEdgeW(5,8,4,&w);
    AddEdgeW(7,8,2,&w);
    w = FloydWarshall(w,9);
    for(i=1;i<=9;i++)
    {
        for(j=1;j<=9;j++)
        printf("%d ",w[i][j]);
        printf("\n");
    }

    return 0;

}
Ejemplo n.º 2
0
int main()
{
    CreateMatrix();
    printf("The number of vertices is %d\n\n",NrOfVertices);
    printf("The adjacency matrix is:\n");
    PrintMatrix();

    int choice;
    printf("\nChoose which algorithm you want to apply:\n");
    printf("   press 1 for Dijkstra \n         2 for Floyd-Warshall\n");
    scanf("%d",&choice);

    switch(choice)
    {
    case 1:
        printf("\nDijkstra\n");
        int FirstPosition,LastPosition;

        //I've tried to do what's in the comment,but it didn't work. I couldn't read LastVertex
        //I saw on the internet that are some problems with scanf and char.. and I used gets. But still no success
/*
        int i;
        char FirstVertex,LastVertex;
        printf("\nInput the vertex from which you want to start Dijkstra's algorithm\n");
        //scanf("%d ",&FirstVertex);
        gets(FirstVertex);

        printf("Input the vertex where you want to stop Dijkstra's algorithm\n");
        //scanf("%d ",&LastVertex);
        gets(LastVertex);

        for(i=0; i<NrOfVertices; i++)
        {
            if(FirstVertex==vertices[i])
                FirstPosition=i;
            if(LastVertex==vertices[i])
                LastPosition=i;
        }
*/
        printf("\nInput the number of the vertex from where you want to start\n");
        scanf("%d",&FirstPosition);

        printf("\nInput the number of vertex where you want to stop\n");
        scanf("%d",&LastPosition);

        Dijkstra(FirstPosition,LastPosition);
        break;

    case 2:
        printf("\nThe matrix of Floyd-Warshall algorithm is\n");
        FloydWarshall();
        break;

    default:
        printf("\nInvalid number\n");
    }

    return 0;
}
Ejemplo n.º 3
0
    double *getDistanceMat(const ROMol &mol,
				   const std::vector<int> &activeAtoms,
				   const std::vector<const Bond *> &bonds,
				   bool useBO,
				   bool useAtomWts){
  
      const int nAts=activeAtoms.size();
  
      double *dMat = new double[nAts*nAts];
      int i,j;
      // initialize off diagonals to LOCAL_INF and diagonals to 0
      for(i=0;i<nAts*nAts;i++) dMat[i] = LOCAL_INF;
      for(i=0;i<nAts;i++) dMat[i*nAts+i]=0.0;

      for(std::vector<const Bond *>::const_iterator bi=bonds.begin();
	  bi!=bonds.end();bi++){
	const Bond *bond=*bi;
	i=std::find(activeAtoms.begin(),activeAtoms.end(),
		    static_cast<int>(bond->getBeginAtomIdx())) - activeAtoms.begin();
	j=std::find(activeAtoms.begin(),activeAtoms.end(),
		    static_cast<int>(bond->getEndAtomIdx())) - activeAtoms.begin();
	double contrib;
	if(useBO){
	  if(!bond->getIsAromatic()){
	    contrib = 1./bond->getBondTypeAsDouble();
	  } else {
	    contrib = 2. / 3.;
	  }
	} else {
	  contrib = 1.0;
	}
	dMat[i*nAts+j]=contrib;
	dMat[j*nAts+i]=contrib;
      }

      int *pathMat = new int[nAts*nAts];
      memset(static_cast<void *>(pathMat),0,nAts*nAts*sizeof(int));
      FloydWarshall(nAts,dMat,pathMat);
      delete [] pathMat;

      if(useAtomWts){
	for(i=0;i<nAts;i++){
	  int anum = mol.getAtomWithIdx(activeAtoms[i])->getAtomicNum();
	  dMat[i*nAts+i] = 6.0/anum;
	}
      }
      return dMat;
    };
Ejemplo n.º 4
0
int main(void)
{
	int t, a, b, w, q;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d", &n);
		apspl = malloc(sizeof(int) * n * n);
		init();
		scanf("%d", &m);
		while(m--)
		{
			scanf("%d", &a);
			scanf("%d", &b);
			scanf("%d", &w);
			if(w < apspl[a * n + b])
				apspl[a * n + b] = w;
		}
		FloydWarshall();
		scanf("%d", &q);
		servequeries(q);
	}
}
Ejemplo n.º 5
0
int main () {

   int algoritmo = 2;

   /*Dijkstra*/
   if (algoritmo == 0) {
       /* Posição dos vértices (slide 24): */
       /*                                  */
       /*            1       2             */
       /*       0                3         */
       /*            5       4             */
       /*                                  */

       /*Número de vértices: */
       int V = 6;

       /*Inicializando o Grafo direcionado G: */
       Graph *G = criar_grafo (V);

       /*Adicionando as arestas:*/
       adicionar_aresta (G, 0, 1, 10);
       adicionar_aresta (G, 1, 2, 1);
       adicionar_aresta (G, 2, 3, 2);
       adicionar_aresta (G, 4, 3, 5);
       adicionar_aresta (G, 4, 2, 6);
       adicionar_aresta (G, 2, 4, 4);
       adicionar_aresta (G, 5, 2, 9);
       adicionar_aresta (G, 5, 1, 3);
       adicionar_aresta (G, 1, 5, 2);
       adicionar_aresta (G, 0, 5, 5);
       adicionar_aresta (G, 4, 0, 7);
       adicionar_aresta (G, 5, 4, 2);

       /*Caminhos mínimos por Dijkstra: */
       int source = 0;
       Dijkstra (G, source);
   }
   /*Bellman-Ford*/
   else if (algoritmo == 1) {

       /* Posição dos vértices (slide 51): */
       /*                                  */
       /*              1       2           */
       /*       0                          */
       /*              4       3           */
       /*                                  */

       /*Número de vértices: */
       int V = 5;

       /*Inicializando o Grafo direcionado G: */
       Graph *G = criar_grafo (V);

       /*Adicionando as arestas:*/
       adicionar_aresta (G, 0, 1, +6);
       adicionar_aresta (G, 1, 2, +5);
       adicionar_aresta (G, 2, 1, -2);
       adicionar_aresta (G, 3, 2, +7);
       adicionar_aresta (G, 1, 3, -4);
       adicionar_aresta (G, 4, 2, -3);
       adicionar_aresta (G, 1, 4, +8);
       adicionar_aresta (G, 3, 0, +2);
       adicionar_aresta (G, 0, 4, +7);
       adicionar_aresta (G, 4, 3, +9);

       /*Caminhos mínimos por Dijkstra: */
       int source = 0;
       BellmanFord (G, source);
   }
   /*Floyd-Warshall*/
   else if (algoritmo == 2) {

       /* Posição dos vértices (slide 70): */
       /*                                  */
       /*               1                  */
       /*       0              2           */
       /*                                  */
       /*           4      3               */

       /*Número de vértices: */
       int V = 5;

       /*Inicializando o Grafo direcionado G: */
       Graph *G = criar_grafo (V);

       /*Adicionando as arestas:*/
       adicionar_aresta (G, 0, 1, +3);
       adicionar_aresta (G, 0, 2, +8);
       adicionar_aresta (G, 0, 4, -4);
       adicionar_aresta (G, 1, 3, +1);
       adicionar_aresta (G, 1, 4, +7);
       adicionar_aresta (G, 2, 1, +4);
       adicionar_aresta (G, 3, 2, -5);
       adicionar_aresta (G, 3, 0, +2);
       adicionar_aresta (G, 4, 3, +6);

       /*Caminhos mínimos entre todos os pares: */
       FloydWarshall (G);
   }

   return 0;
}
Ejemplo n.º 6
0
    double *getDistanceMat(const ROMol &mol,bool useBO,
				   bool useAtomWts,
				   bool force,
				   const char *propNamePrefix){
      std::string propName;
      boost::shared_array<double> sptr;
      if(propNamePrefix){
	propName = propNamePrefix;
      } else {
	propName = "";
      }
      propName+="DistanceMatrix";
      // make sure we don't use the nonBO cache for the BO matrix and vice versa:
      if(useBO) propName+="BO";
      if(!force && mol.hasProp(propName)){
	mol.getProp(propName,sptr);
	return sptr.get();
      }    
      int nAts=mol.getNumAtoms();
      double *dMat = new double[nAts*nAts];
      int i,j;
      // initialize off diagonals to LOCAL_INF and diagonals to 0
      for(i=0;i<nAts*nAts;i++) dMat[i] = LOCAL_INF;
      for(i=0;i<nAts;i++) dMat[i*nAts+i]=0.0;

      ROMol::EDGE_ITER firstB,lastB;
      boost::tie(firstB,lastB) = mol.getEdges();
      while(firstB!=lastB){
	const BOND_SPTR bond = mol[*firstB];
	i = bond->getBeginAtomIdx();
	j = bond->getEndAtomIdx();
	double contrib;
	if(useBO){
	  if(!bond->getIsAromatic()){
	    contrib = 1./bond->getBondTypeAsDouble();
	  } else {
	    contrib = 2. / 3.;
	  }
	} else {
	  contrib = 1.0;
	}
	dMat[i*nAts+j]=contrib;
	dMat[j*nAts+i]=contrib;
	++firstB;
      }

      int *pathMat = new int[nAts*nAts];
      memset(static_cast<void *>(pathMat),0,nAts*nAts*sizeof(int));
      FloydWarshall(nAts,dMat,pathMat);
    
      if(useAtomWts){
	for (i = 0; i < nAts; i++) {
	  int anum = mol.getAtomWithIdx(i)->getAtomicNum();
	  dMat[i*nAts+i] = 6.0/anum;
	}
      }
      sptr.reset(dMat);
      mol.setProp(propName,sptr,true);
      boost::shared_array<int> iSptr(pathMat);
      mol.setProp(propName+"_Paths",iSptr,true);
  
      return dMat;
    };