- Main Page
- Namespaces
- Classes
- Files

- Search for |

# graph.cpp

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 // "graph.cpp"
    00004
    00005 // (c) OPAC Team, LIFL, 2003-2006
    00006
    00007 /* LICENCE TEXT
    00008
    00009    Contact: paradiseo-help@lists.gforge.inria.fr
    00010 */
    00011
    00012 #include <fstream>
    00013 #include <iostream>
    00014 #include <math.h>
    00015
    00016 #include "graph.h"
    00017
    00018 namespace Graph {
    00019
    00020   static std :: vector <std :: pair <double, double> > vectCoord ; // Coordinates
    00021
    00022   static std :: vector <std :: vector <unsigned int> > dist ; // Distances Mat.
    00023
    00024   unsigned size ()
    00025   {
    00026     return dist.size () ;
    00027   }
    00028
    00029   void computeDistances ()
    00030   {
    00031
    00032     // Dim.
    00033     unsigned int numCities = vectCoord.size () ;
    00034     dist.resize (numCities) ;
    00035     for (unsigned int i = 0 ; i < dist.size () ; i ++)
    00036       {
    00037         dist [i].resize (numCities) ;
    00038       }
    00039
    00040     // Computations.
    00041     for (unsigned int i = 0 ; i < dist.size () ; i ++)
    00042       {
    00043         for (unsigned int j = i + 1 ; j < dist.size () ; j ++)
    00044           {
    00045             double distX = (double)(vectCoord [i].first - vectCoord [j].first) ;
    00046             double distY = (double)(vectCoord [i].second - vectCoord [j].second) ;
    00047             dist [i] [j] = dist [j] [i] = (unsigned) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ;
    00048           }
    00049       }
    00050   }
    00051
    00052   void load (const char * __fileName)
    00053   {
    00054
    00055     std :: ifstream f (__fileName) ;
    00056
    00057     std :: cout << ">> Loading [" << __fileName << "]" << std :: endl ;
    00058
    00059     if (f)
    00060       {
    00061         unsigned int num_vert ;
    00062
    00063         f >> num_vert ;
    00064         vectCoord.resize (num_vert) ;
    00065
    00066         for (unsigned int i = 0 ; i < num_vert ; i ++)
    00067           {
    00068             f >> vectCoord [i].first >> vectCoord [i].second ;
    00069           }
    00070
    00071         f.close () ;
    00072
    00073         computeDistances () ;
    00074       }
    00075     else
    00076       {
    00077
    00078         std :: cout << __fileName << " doesn't exist !!!" << std :: endl ;
    00079         // Bye !!!
    00080         exit (1) ;
    00081       }
    00082   }
    00083
    00084   float distance (unsigned int __from, unsigned int __to)
    00085   {
    00086     return (float)(dist [__from] [__to]) ;
    00087   }
    00088 }
    00089
    00090