/** * Resize method that calls Dijkstra algorithm for each Vertex inside * the Vertexes set of the Graph */ void graph_shortest_path(PPMImage *image, int *path) { Graph graph; init_graph(&graph, image); pri_queue_t priq_s; priq_init(&priq_s, graph.list_size); int i, x, y, dest; Energy *distance = calloc(graph.list_size, sizeof(Energy)); int *previous = calloc(graph.list_size, sizeof(int)); Energy shortest_distance; shortest_distance = ENERGY_MAX; for(x = 0; x < image->width; x++) { i = XY2POS(image, x, image->height - 1); dest = dijkstra(&graph, i, &priq_s, distance, previous, shortest_distance); #ifdef OPT_GRAPH_SHORTEST_PATH_BREAK if(dest == BREAK) continue; else #endif if(dest < 0) { fprintf(stderr, "There is no path\n"); exit(EXIT_FAILURE); } if (distance[dest] < shortest_distance) { shortest_distance = distance[dest]; y = 0; while(dest != i) { path[y++] = POS2X(dest, image); dest = previous[dest]; } path[y] = POS2X(i, image); // assert path length ASSERT_TRUE(y == image->height - 1, fprintf(stderr, "ASSERT: Path length error (must to be %d): %d\n", image->height - 1, y) ); } } priq_free(&priq_s); free_graph(&graph); free(distance); free(previous); }
static void free_handler(state_t s) { array_free(s->failed) ; while (!priq_empty(s->priq)) { item_t item ; priq_get(s->priq, NULL, (void**)&item) ; switch(item->type) { case DROP_UP: event_free(item->u.up.event) ; unmarsh_free(item->u.up.abv) ; break ; case DROP_UPNM: event_free(item->u.upnm.event) ; break ; OTHERWISE_ABORT() ; } record_free(item) ; } priq_free(s->priq) ; }