void compute_path( Path *p ){ int i; if(p->num + 1 == numCities){ for(i = 0; i < numCities; i++){ if(p->visited[i] == 0){ add_city(p, i); } } if(check_best_path(p) == 1){ Path* curr_best_path; curr_best_path = copy_paths(p); #pragma omp critical best_path = curr_best_path; } remove_last(p); remove_last(p); } else { for( i = 0; i < numCities; i++){ if(p->visited[i] == 0){ add_city(p, i); compute_path(p); } } remove_last(p); } }
char *find_path(char *name, t_array *paths, int *info) { DIR *dir; struct dirent *tmp; char *result; *info = 0; while (paths) { if ((dir = opendir(paths->data))) { while ((tmp = readdir(dir))) { if (!ft_strcmp(tmp->d_name, name)) { if ((result = compute_path(paths->data, name))) return (result); *info = 1; } } closedir(dir); } paths = paths->next; } return (!ft_strncmp("./", name, 2) ? ft_strdup(name) : NULL); }
void compute_path(int i, int j, int n, int cost[100][100]) { int ok = 0, k = 1; while(k <= n && !ok) { if(i != k && j != k) if(cost[i][j] == cost[i][k] + cost[k][j]) { compute_path(i, k, n, cost); compute_path(k, j, n, cost); ok = 1; } k++; } if(!ok) { printf("%d", j); } }
void print_path (int first, int last, int cost[100][100], int n) { if(cost[first][last] < 9000) { printf("\n The path from %d to %d has the weight %d", first, last, cost[first][last]); printf("\n The minimum cost path is: %d", first); compute_path(first,last, n, cost); } else printf("\n There is no path between %d to %d", first, last); }
int main(int argc, char *argv[]){ int i; get_input(argv[1]); init_best_path(best_path); #pragma omp parallel for for(i = 1; i < numCities;i++){ Path *curr_path; curr_path = malloc( sizeof( Path)); init_path(curr_path); add_city(curr_path, i); compute_path(curr_path ); } print_path(best_path); return 0; }