int main(){ int n/*intersecções*/, m/*numRuas*/, i, j, v, w, p; scanf("%d %d", &n, &m); while(n && m){ int cont=0; Fila* vertices; vertices = filaConstrutor(); for (i = 1; i <= n; i++){ //Matrix de N x N for (j = 1; j <= n; j++){ matrix[i][j]=0; //Zerando todas a matriz } } for (i = 0; i < m; ++i){ scanf("%d%d%d", &v, &w, &p); if (p==2) matrix[w][v]=1; matrix[v][w]=1; } //imprimeMatriz(n,matrix); if (!grafo(w,n,matrix)) printf("0\n"); else{ int aux; for (i = 1; i <= n; ++i){ for (j = i+1; j <= n; ++j){ aux=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=aux; } } // imprimeMatriz(n,matrix); if (grafo(w,n,matrix)) printf("1\n"); } scanf("%d %d", &n, &m); } return 0; }
Grafo lerGrafo(){ Grafo grafo(1000); ifstream infilestream; string line; //N int tipo; string id; //R int no1; int no2; int i=0; infilestream.open("grafo2.txt"); while (infilestream) { infilestream >> line; if(line=="N") { infilestream >> tipo; infilestream >> id; criaNo(tipo,id, &grafo); } if(line=="R") { infilestream >> no1; infilestream >> no2; criaRelacaoBin(no1, no2, &grafo); }
void testear_exacto( int maxN, int step, int cantTests ) { ofstream tiempos; tiempos.open("exacto.data"); for (int n=step; n<=maxN; n+=step) { cout << "n = " << n; double acum = 0; double acumSP = 0; double elmax = 0; double elmin = 1E+37; double elmaxSP = 0; double elminSP = 1E+37; double todos[cantTests]; double todosSP[cantTests]; for (int k=0; k<cantTests; k++) { cout << "."; grafo g = grafo(n); g.generar_aristas_aleatorias(); double duracion; if (n<=MAX_N_SIN_PODAS) { iniciarTimer(); //-> Resolver_exacto sin poda: exacto_sin_poda(g); //-> duracion = finalizarTimer(); acumSP += duracion; todosSP[k] = duracion; if (duracion > elmaxSP) elmaxSP = duracion; if (duracion < elminSP) elminSP = duracion; } iniciarTimer(); //-> Resolver_exacto: exacto(g); //-> duracion = finalizarTimer(); acum += duracion; todos[k] = duracion; if (duracion > elmax) elmax = duracion; if (duracion < elmin) elmin = duracion; } cout << endl; double media = acum/cantTests; double desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); double mediaSP = acumSP/cantTests; double desvioEstandarSP = 0; if (n<=MAX_N_SIN_PODAS) { for (int l =0; l<cantTests; ++l) { double d = todosSP[l]-mediaSP; desvioEstandarSP += (d*d); } desvioEstandarSP = sqrt(desvioEstandarSP/(cantTests-1)); } tiempos << n << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax << " " << fixed << elmin; if (n<=MAX_N_SIN_PODAS) { tiempos << " " << fixed << mediaSP << " " << fixed << mediaSP-desvioEstandarSP << " " << fixed << mediaSP + desvioEstandarSP << " " << fixed << elmaxSP << " " << fixed << elminSP; } tiempos << endl; } tiempos.close(); system("gnuplot exacto.gnuplot"); }
void testear( int maxN, int step, int cantTests ) { ofstream Oexacto; Oexacto.open("exacto.data"); ofstream Ogoloso; Ogoloso.open("goloso.data"); ofstream Olocal; Olocal.open("local.data"); ofstream Olocal2; Olocal2.open("local2.data"); ofstream Otabu; Otabu.open("tabu.data"); ofstream Otabu2; Otabu2.open("tabu2.data"); for (int n=step; n<=maxN; n+=step) { cout << "n = " << n; double acum_e = 0; double acum_g = 0; double acum_l = 0; double acum_l2 = 0; double acum_t = 0; double acum_t2 = 0; double elmax_e = 0; double elmax_g = 0; double elmax_l = 0; double elmax_l2 = 0; double elmax_t = 0; double elmax_t2 = 0; double elmin_e = 1E+37; double elmin_g = 1E+37; double elmin_l = 1E+37; double elmin_l2 = 1E+37; double elmin_t = 1E+37; double elmin_t2 = 1E+37; double todos_e[cantTests]; double todos_g[cantTests]; double todos_l[cantTests]; double todos_l2[cantTests]; double todos_t[cantTests]; double todos_t2[cantTests]; for (int k=0; k<cantTests; k++) { grafo g = grafo(n); g.generar_aristas_aleatorias(); Solucion si = make_pair(vector<bool>(n,false),0); double duracion; iniciarTimer(); exacto(g); duracion = finalizarTimer(); acum_e += duracion; todos_e[k] = duracion; if (duracion > elmax_e) elmax_e = duracion; if (duracion < elmin_e) elmin_e = duracion; iniciarTimer(); goloso(g); duracion = finalizarTimer(); acum_g += duracion; todos_g[k] = duracion; if (duracion > elmax_g) elmax_g = duracion; if (duracion < elmin_g) elmin_g = duracion; iniciarTimer(); if (n>=2) local(g,si,2); duracion = finalizarTimer(); acum_l += duracion; todos_l[k] = duracion; if (duracion > elmax_l) elmax_l = duracion; if (duracion < elmin_l) elmin_l = duracion; iniciarTimer(); if (n>=3) local(g,si,3); duracion = finalizarTimer(); acum_l2 += duracion; todos_l2[k] = duracion; if (duracion > elmax_l2) elmax_l2 = duracion; if (duracion < elmin_l2) elmin_l2 = duracion; iniciarTimer(); tabu(g,si,n,n,1); duracion = finalizarTimer(); acum_t += duracion; todos_t[k] = duracion; if (duracion > elmax_t) elmax_t = duracion; if (duracion < elmin_t) elmin_t = duracion; iniciarTimer(); if (n>=2) tabu(g,si,n,n,2); duracion = finalizarTimer(); acum_t2 += duracion; todos_t2[k] = duracion; if (duracion > elmax_t2) elmax_t2 = duracion; if (duracion < elmin_t2) elmin_t2 = duracion; } cout << endl; double media; double desvioEstandar; float index; media = acum_e/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_e[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.5*step; Oexacto << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_e << " " << fixed << elmin_e << endl; media = acum_g/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_g[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.25*step; Ogoloso << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_g << " " << fixed << elmin_g << endl; media = acum_l/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_l[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.25*step; Olocal << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_l << " " << fixed << elmin_l << endl; media = acum_l2/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_l2[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.5*step; Olocal2 << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_l2 << " " << fixed << elmin_l2 << endl; media = acum_t/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_t[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.15*step; Otabu << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_t << " " << fixed << elmin_t << endl; media = acum_t2/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_t2[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.15*step; Otabu2 << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_t2 << " " << fixed << elmin_t2 << endl; } Oexacto.close(); Ogoloso.close(); Olocal.close(); Olocal2.close(); Otabu.close(); Otabu2.close(); system("gnuplot super_test.gnuplot"); }