int main(void) { int node_num, edge_num; struct Matrix_Graph mat_graph; struct Point_Graph poi_graph; while (scanf("%d %d", &node_num, &edge_num) != EOF) { initGraph(&mat_graph, &poi_graph, node_num); input(&mat_graph, &poi_graph, edge_num); printf("matrix multiplication:\n"); matrix_mult(&mat_graph); output(&mat_graph); clearChoose(&mat_graph, &poi_graph); printf("floyd warshall:\n"); floyd_warshall(&mat_graph); output(&mat_graph); clearChoose(&mat_graph, &poi_graph); printf("johnson:\n"); johnson(&mat_graph, &poi_graph); output(&mat_graph); clearEdge(&poi_graph); } return 0; }
int main() { while( scanf("%d %d", &n, &m) == 2 && m != 0 && n != 0 ) { for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) c[i][j] = INF; for( int i = 0; i < n; i++ ) c[i][i] = 0; for( int i = 0; i < m; i++ ) { int a, b, d; scanf( "%d %d %d", &a, &b, &d ); c[a][b] = d; c[b][a] = d; } int k; scanf( "%d", &k ); for( int i = 0; i < k; i++ ) { int a, b; scanf( "%d %d", &a, &b ); c[a][b] = INF; c[b][a] = INF; } floyd_warshall(); int q; scanf( "%d", &q ); for( int i = 0; i < q; i++ ) { int a, b; scanf( "%d %d", &a, &b ); printf( "%d\n", c[a][b] == INF ? -1 : c[a][b] ); } } return 0; }
int main() { int graph[V][V] = { {0, 5, INF, 10}, {INF, 0, 3, INF}, {INF, INF, 0, 1}, {INF, INF, INF, 0}, }; floyd_warshall(graph); return 0; }
int main(int argc, char *argv[]) { FILE *fin = fopen("dist.txt", "r"); fscanf(fin, "%d", &n); int i, j; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) fscanf(fin, "%d", &dist[i][j]); fclose(fin); floyd_warshall(); return 0; }
int main( ) { int dist[n][n] = { {0, 3, 10}, {3, 0, 1000}, {10, 1000, 0} }; floyd_warshall(dist); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) std::cout << "Menor distancia de " << i << " para " << j << ": " << dist[i][j] << "\n"; return 0; }
int main() { struct timer tm ; init_timer(&tm) ; start_timer(&tm) ; int i ; for(i=0 ; i<NUM_ITER ; i++) floyd_warshall(path1) ; stop_timer(&tm) ; print_time(&tm) ; return 0 ; }
int main() { int cases; int N; // city number int R; // road number int C1, C2; // the number of city int P; // Path int S; // start city int E; // destination city int T; // the numer of visitors int i, j; int matrix[MAX][MAX]; int res; scanf("%d", &cases); while(cases--) { scanf("%d %d", &N, &R); for(i = 0 ; i < N; i++) for(j = 0; j < N; j++) matrix[i][j] = 0; for(i = 0; i < R; i++) { scanf("%d %d %d", &C1, &C2, &P); matrix[C1-1][C2-1] = matrix[C2-1][C1-1] = P; } scanf("%d %d %d", &S, &E, &T); res = floyd_warshall(matrix, N, S-1, E-1); printf("%d\n", T/(res-1) + (T % (res - 1) ? 1 : 0)); } return 0; }
int main() { struct timer tm ; init_timer(&tm) ; pthread_barrier_init(&barrier,NULL,2); start_timer(&tm) ; p_floyd_warshall(path2) ; stop_timer(&tm) ; print_time(&tm) ; floyd_warshall(path1) ; if(verify(path1, path2)) printf("VERIFICATION PASS\n") ; else printf("VERIFICATION FAILED\n") ; return 0 ; }
graph_info *graph_info_from_nauty(graph *g, int n) { graph_info *ret = malloc(sizeof(graph_info)); ret->n = n; ret->distances = malloc(n * n * sizeof(*ret->distances)); ret->k = malloc(n * sizeof(*ret->k)); int m = (n + WORDSIZE - 1) / WORDSIZE; ret->m = 0; //total number of edges for (int i = 0; i < n; i++) { ret->k[i] = 0; for (int j = 0; j < n; j++) { if(i == j) ret->distances[n*i + j] = 0; else if(ISELEMENT(GRAPHROW(g, i, m), j)) { ret->distances[n*i + j] = 1; ret->k[i]++; ret->m++; } else ret->distances[n*i + j] = GRAPH_INFINITY; } } ret->m /= 2; ret->max_k = 0; for (int i = 0; i < n; i++) if (ret->k[i] > ret->max_k) ret->max_k = ret->k[i]; floyd_warshall(*ret); ret->sum_of_distances = calc_sum(*ret); ret->diameter = calc_diameter(*ret); ret->nauty_graph = malloc(n * m * sizeof(graph)); ret->gcan = NULL; memcpy(ret->nauty_graph, g, n * m * sizeof(graph)); return ret; }
void prepare() { for (int i = 0; i < 64; ++i) for (int j = 0; j < 64; ++j) g[i][j] = INF; for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) { g[Pos(i, j)][Pos(i, j)] = 0; for (int k = 0; k < 8; ++k) { int ti = i + moves[k][0]; int tj = j + moves[k][1]; if (! valid_pos(ti, tj)) continue; g[Pos(i, j)][Pos(ti, tj)] = 1; } } floyd_warshall(); }
int main() { int a, b, c = 1, cc = 0; clear_graph(); while (std::cin >> a >> b) { if (!(a | b)) { if (cc == 0) { break; } floyd_warshall(); double ans = avg_calc(); cc = 0; printf("Case %i: average length between pages = %.3f clicks\n", c++, ans); // Clear Graphs clear_graph(); } else { cc++; graph[a].push_back(b); } } return 0; }
int main(int argc, char **argv) { int rank, M, j,i, *d_graph; int *local_matrix, *row_matrix, *col_matrix, *res_matrix, *rowIds, *colIds; int P, N, q, p_row, p_col; double start, finish; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &P); MPI_Comm_rank(MPI_COMM_WORLD, &rank); //INPUT HANDLED BY THE ROOT PROCESSOR if (rank == ROOT){ scanf("%d", &N); q = check_fox_conditions(P,N); //Check's if the fox's conditions are met if(q == 0){ MPI_Abort(MPI_COMM_WORLD, 0); return 1; //error } d_graph = (int*)malloc((N*N) * sizeof(int)); for(i=0; i < N; i++){ for(j=0; j < N; j++){ scanf("%d", &d_graph[GET_MTRX_POS(i,j,N)]); if (d_graph[GET_MTRX_POS(i,j,N)] == 0 && i != j) { d_graph[GET_MTRX_POS(i,j,N)] = INF; } } } MPI_Bcast(&q, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); if(q > 1) divide_matrix( d_graph, N, q); } else{ MPI_Bcast(&q, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); } //---------------COMMON------------------ int lngth = N / q; local_matrix = (int*)malloc((lngth*lngth) * sizeof(int)); row_matrix = (int*)malloc((lngth*lngth) * sizeof(int)); col_matrix = (int*)malloc((lngth*lngth) * sizeof(int)); res_matrix = (int*)malloc((lngth*lngth) * sizeof(int)); if(q>1) chnkd_MPI_Recv(local_matrix, lngth*lngth, MPI_INT, 0); else local_matrix = d_graph; p_row = ( rank / q ); p_col = ( rank % q ); //CREATE COMMUNICATORS MPI_Group MPI_GROUP_WORLD; MPI_Comm_group(MPI_COMM_WORLD, &MPI_GROUP_WORLD); MPI_Group row_group, col_group; MPI_Comm row_comm, col_comm, grid_comm; int tmp_row, tmp_col, proc; int row_process_ranks[q], col_process_ranks[q]; for(proc = 0; proc < q; proc++){ row_process_ranks[proc] = (p_row * q) + proc; col_process_ranks[proc] = ((p_col + proc*q) %(q*q)); } radixsort(col_process_ranks, q); radixsort(row_process_ranks, q); MPI_Group_incl(MPI_GROUP_WORLD, q, row_process_ranks, &row_group); MPI_Group_incl(MPI_GROUP_WORLD, q, col_process_ranks, &col_group); MPI_Comm_create(MPI_COMM_WORLD, row_group, &row_comm); MPI_Comm_create(MPI_COMM_WORLD, col_group, &col_comm); if ((rank / q) == (rank % q)) { memcpy(row_matrix, local_matrix, (lngth*lngth) * sizeof(int)); } int ln,d,flag; int step, rotation_src, rotation_dest, src; int count = 0; memcpy(res_matrix, local_matrix, (lngth*lngth) * sizeof(int)); rotation_src = (p_row + 1) % q; rotation_dest = ((p_row - 1) + q) % q; ln = (lngth*q) << 1; start = MPI_Wtime(); for (d = 2; d < ln; d = d << 1) { memcpy(col_matrix, local_matrix, (lngth*lngth) * sizeof(int)); for ( step = 0; step < q; step++) { src = (p_row + step) % q; count++; if (src == p_col) { MPI_Bcast(local_matrix, lngth*lngth, MPI_INT, src, row_comm); floyd_warshall( local_matrix, col_matrix, res_matrix, lngth); } else { MPI_Bcast(row_matrix, lngth*lngth, MPI_INT, src, row_comm); floyd_warshall( row_matrix, col_matrix, res_matrix, lngth); } if( step < q-1) MPI_Sendrecv_replace(col_matrix, lngth*lngth, MPI_INT, rotation_dest, STD_TAG,rotation_src, STD_TAG, col_comm, MPI_STATUS_IGNORE); } memcpy(local_matrix, res_matrix, (lngth*lngth) * sizeof(int)); } int *sol; sol = malloc(N*N*sizeof(int)); MPI_Gather(res_matrix, lngth*lngth, MPI_INT, sol, lngth*lngth, MPI_INT, 0, MPI_COMM_WORLD); if (rank == 0) { finish = MPI_Wtime(); printf("Tempo de execução %f\n",finish - start); } if (rank == 0) { int row, col, pos_x, pos_y, pos, tmp_y, tmp_x; for (i = 0; i < P; i++) { pos_x = i / q; pos_y = i % q; pos = i * lngth*lngth; for (row = 0; row < lngth; row++) { for (col = 0; col < lngth; col++) { tmp_x = GET_MTRX_POS(pos_x,row,lngth); tmp_y = GET_MTRX_POS(pos_y,col,lngth); if (sol[GET_MTRX_POS(row,col,lngth) + pos] == INF) d_graph[GET_MTRX_POS(tmp_x,tmp_y,N)] = 0; else d_graph[GET_MTRX_POS(tmp_x,tmp_y,N)] = sol[GET_MTRX_POS(row,col,lngth) + pos]; } } } prints_matrix(d_graph,N); } MPI_Finalize(); return 0; }
/* Formulates the minimum path problem as a minimum cost flow. Validate the found solution comparing it to Floyd-Warshall. The instances are random graphs with at most 50 vertex and at most 25 * 50 non-parallel edges */ void test_minimum_path(int nv, int ne) { srand(time(NULL)); // random number of vertexs //int n = rand()%100 + 2; int n = nv; Graph_Mean grafo_mean(n); Graph_Cycle grafo_cycle(n); // random number of edges //int m = rand()%(n/2 * n); int m = ne; // force the graph to have a path // between 0 and n - 1 //grafo_mean.set_edge(0, n - 1, 1000 * 1000, 1); //grafo_cycle.set_edge(0, n - 1, 1000 * 1000, 1); // generate the remaining edges randomly for (int i = 0; i < m - 1; ++i) { int u, v, c; // generate a new edge, check if it or its inverse were // already generated. Parallel edges are not allowed. do { u = rand()%n; v = rand()%n; ca = (rand() + 1) %100000; // no zero capacity co = (rand() + 1) %100000; // no zero cost } while (grafo_mean.is_edge_set(u, v) || grafo_mean.is_edge_set(v, u)); grafo_mean.set_edge(u, v, co, ca); grafo_cycle.set_edge(u, v, co, ca); mp_graph[u][v] = 1; mp_cost[u][v] = c; } grafo_mean.set_inbalance(0, 1); grafo_cycle.set_inbalance(0, 1); grafo_mean.set_inbalance(n-1, -1); grafo_cycle.set_inbalance(n-1, -1); int expected_value = floyd_warshall(0, n - 1, mp_graph, mp_cost); // Mean time_t tstart, tend; time(&tstart); assert(grafo_mean.min_cost_flow() == MCF_SUCCESS); //assert(grafo.is_valid_flow()); int value = grafo_mean.calc_flow_cost(); time(&tend); total_time_mean += difftime(tend, tstart); assert(value == expected_value); // Cycle time(&tstart); assert(grafo_cycle.min_cost_flow() == MCF_SUCCESS); //assert(grafo.is_valid_flow()); value = grafo_cycle.calc_flow_cost(); time(&tend); total_time_cycle += difftime(tend, tstart); assert(value == expected_value); cerr<< "test_minimum_path: SUCCESS!"<<'\n'; }
int main(int argc, char **argv){ int num_vertices = 0; int num_edges = 0; int i; // counter to loop through parameters int print = 0; // print resulting adjacency matrix? int oriented = 0; // whether or not the random matrix is oriented int **matrix; // adjacency matrix int **parent_matrix; // matrix used to restore paths int num_bad_edges = 0; // number of bad edges in file int total_distance; int diameter; char filename[100]; // filename if given as param struct timeval start, end; double time; // print usage usage(); // read params for(i=1; i<argc; i++){ if(strcmp(argv[i], "-print") == 0){ print = 1; } else if(strcmp(argv[i], "-read") == 0){ // assume filename is not longer than 100 chars strcpy(filename, argv[++i]); } else if(strcmp(argv[i], "-random") == 0){ num_vertices = atoi(argv[++i]); oriented = atoi(argv[++i]); } else { num_vertices = 4000; oriented = 1; } } if(num_vertices > 0){ // init random adjacency matrix for testing total_distance = init_random_adjacency_matrix(num_vertices, &num_edges, &matrix, &parent_matrix, oriented); } else { // generate adjacency matrix from file total_distance = read_adjacency_matrix(filename, &num_vertices, &num_edges, &matrix, &parent_matrix, &oriented, &num_bad_edges); } fprintf(stderr, "Running ASP with %d rows and %d edges (%d are bad)\n", num_vertices, num_edges, num_bad_edges); if(gettimeofday(&start, 0) != 0){ fprintf(stderr, "Error starting timer\n"); exit(EXIT_FAILURE); } floyd_warshall(matrix, parent_matrix, num_vertices); diameter = calculate_diameter(matrix, num_vertices); if(gettimeofday(&end, 0) != 0){ fprintf(stderr, "Error stopping timer\n"); exit(EXIT_FAILURE); } time = (end.tv_sec + end.tv_usec / 1000000.0) - (start.tv_sec + start.tv_usec / 1000000.0); fprintf(stderr, "Total distance: %d\n", total_distance); fprintf(stderr, "Diameter: %d\n", diameter); fprintf(stderr, "ASP took %10.3f seconds\n", time); if(print){ print_paths(matrix, parent_matrix, num_vertices); } free_matrix(matrix, num_vertices); free_matrix(parent_matrix, num_vertices); return 0; }
int main(){ int n,cont,e,i,j; scanf("%d",&n); while(n!=0){ for(i=1;i<=n;i++){ for(j=1;j<=n;j++) graph[i][j]=INF; graph[i][i]=0; } for(i=1;i<=n;i++){ scanf("%d",&cont); for(j=1;j<=cont;j++){ scanf("%d",&e); if(e==0) break; scanf("%d",&graph[i][e]); } } for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=graph[i][j]; floyd_warshall(n); int max; for(i=1;i<=n;i++){ max=dis[i][1]; for(j=1;j<=n;j++){ if(dis[i][j]!=max && dis[i][j]>max) max=dis[i][j]; } res[i]=max; } int min = INF,pos=1; for(i=1;i<=n;i++) if(res[i]<min) {min = res[i];pos = i;} if(min==INF) printf("disjoint\n"); else printf("%d %d\n",pos,min); scanf("%d",&n); } /* for(i=1;i<=n;i++){ */ /* for(j=1;j<=n;j++){ */ /* if(graph[i][j]==INF) */ /* printf("%5s","INF"); */ /* else */ /* printf("%5d",graph[i][j]); */ /* } */ /* printf("\n"); */ /* } */ /* printf("\n"); */ /* for(i=1;i<=n;i++){ */ /* for(j=1;j<=n;j++){ */ /* if(dis[i][j]==INF) */ /* printf("%5s","INF"); */ /* else */ /* printf("%5d",dis[i][j]); */ /* } */ /* printf("\n"); */ /* } */ return 0; }
int main(void) { floyd_warshall(); return 0; }
int main( int argc, char **argv ) { //Declaring variables double floyd_time, di_time; /* *Handling user input. */ if( find_option( argc, argv, "-h" ) >= 0 ) { printf( "Options:\n" ); printf( "-h to see this help\n" ); printf( "-n <int> to set the number of vertices\n" ); printf( "-no turns off all correctness checks\n"); printf( "-csv outputs in csv format (n, floyd_time, di_time)\n"); return 0; } int n = read_int( argc, argv, "-n", 100 ); /* * Setting up the data structures * All data structures are assumed to be row major. */ // An n*n matrix, where the element in the ith row and jth column // represents the weight of the edge from vertex i to j. float *graph = (float*) malloc( n * n * sizeof(float)); // An n*n matrix, where the element in the ith row and jth column // represents the second vertex on the shortest path from u -> v int *p = (int*) malloc(n * n * sizeof(int)); // An n*n matrix, where the element in the ith row and jth column // represents the penultimate vertex on the shortest path from u -> v int *q = (int*) malloc(n * n * sizeof(int)); // An n*n matrix, where the element in the ith row and jth column // is a list of vertices w for which w -> u -Shortest-Path-> v // is known to be a shortest path std::vector<int> *L = new std::vector<int>[n*n]; // An n*n matrix, where the element in the ith row and jth column // is a list of vertices w for which u -Shortest-Path-> v -> w // is known to be a shortest path std::vector<int> *R = new std::vector<int>[n*n]; // An n*n+1 matrix, where each element holds a list of vertex pairs. // This is the core data structure for our bucket based heap. std::list<int> *bucket_heap = new std::list<int>[n*n + 1]; // An n*n matrix, where the element in the ith row and jth column // is the iterator/pointer to the linked list element holding // the vertex pair (i,j) in bucket_heap. std::list<int>::iterator *iters = new std::list<int>::iterator[n*n]; // An n*n matrix, where the element in the ith row and jth column // is the index of the bucket that holds the vertex pair (i,j) in bucket_heap. int *b_num = (int*) malloc(n * n * sizeof(int)); // Parent and dist arrays for floyd warshall float *fw_dist = (float*) malloc( n * n * sizeof(float)); int *fw_par = (int*) malloc( n * n * sizeof(int)); // Dist array for DI float *di_dist = (float*) malloc( n * n * sizeof(float)); /* * Initializing values. */ srand48(1337); // We set a minimum value to avoid the overflow bucket scenario. float min_allowable_value = (1.0/(n*n)); // Initialize every edge to have a weight between min_allowable_value and 1. for (int i = 0; i < n * n; i++) { float temp; //no edge between an edge and itself. if (i%n == i/n) { graph[i] = INF; } else { temp = (float)drand48(); if (temp < min_allowable_value) temp += min_allowable_value; if (temp < min_edge) min_edge = temp; graph[i] = temp; } } //Initialize dist arrays. for (int i = 0; i < n*n; i++) { fw_dist[i] = graph[i]; di_dist[i] = graph[i]; b_num[i] = 0; } /* * Executing and timing algorithms */ // Running Demetrescu Italiano. di_time = read_timer(); di_apsp(n, di_dist, graph, p, q, L, R, bucket_heap, b_num, iters); di_time = read_timer() - di_time; //Running Floyd Warshall as a standard implementation. floyd_time = read_timer(); floyd_warshall(n, fw_par, fw_dist); floyd_time = read_timer() - floyd_time; /* * Validating DI results against reference Floyd Warshall */ if( find_option( argc, argv, "-no" ) == -1 ) { //Report a failure if anything varies by more than 0.0001 for (int i = 0; i < n*n; i++) { if ((di_dist[i] - fw_dist[i] > 0.0001) || (fw_dist[i] - di_dist[i] > 0.0001) ) { printf("\n\nFAILURE at %d\n\n", i); break; } } //Print out matrices if n is small enough. if (n < 10) { printf("\n---Our Graph---\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%.4f\t", graph[i*n + j]); } printf("\n"); } printf("\n"); printf("\n---Floyd Warshall Dist---\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%.4f\t", fw_dist[i*n + j]); } printf("\n"); } printf("\n"); printf("\n---Floyd Warshall Par---\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d\t", fw_par[i*n + j]); } printf("\n"); } printf("\n"); printf("\n---Demetrescu Italiano Dist---\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%.4f\t", di_dist[i*n + j]); } printf("\n"); } printf("\n"); } } /* * Print out results */ if( find_option( argc, argv, "-csv" ) == -1 ) { printf("\nTimes:\n"); printf("Floyd Warshall: %f\n", floyd_time); printf("Demetrescu Italiano: %f\n", di_time); } else { printf("%d\t%f\t%f\n", n, floyd_time, di_time); } /* * Free up manually allocated memory. */ free(graph); free(fw_dist); free(fw_par); free(di_dist); free(p); free(q); free(b_num); return 0; }