Ejemplo n.º 1
0
int main()
{
    list_t graph;
    list_create(&graph, 5 * 5);

    for (int x = 0; x < 5; ++x)
    {
        for (int y = 0; y < 5; ++y)
        {
            point_t* vertex = graph_vertex(graph, 5, 5, x, y, costs[x][y]);

            printf("inserting vertex id=%d\n", vertex->id);
        }
    }

    point_t* start = graph_find(graph, 5, 5, 0, 0);
    point_t* goal = graph_find(graph, 5, 5, 4, 4);

    a_star(graph, 5, 5, start, goal);

    printf("Shortest path from (%d,%d) to (%d,%d)\n",
            start->x, start->y,
            goal->x, goal->y
          );

    graph_traverse(start, goal, (list_cb_t) &print_point, NULL);

    printf("Shortest distance from (%d,%d) to (%d,%d) = %d\n", 
            start->x, start->y, 
            goal->x, goal->y, 
            graph_distance(start, goal) // = goal->dist
            );

    int total_heuristic = 0;
    graph_traverse(start, goal, (list_cb_t) &count_heuristics, &total_heuristic);

    printf("Total heuristic: %d\n", total_heuristic);

    list_free(graph, (list_cb_t) &free, NULL);

    return 0;
}
Ejemplo n.º 2
0
int main()
{
    list_t graph;
    list_create(&graph, 5 * 5);
    
    for (int x = 0; x < 5; ++x)
    {
        for (int y = 0; y < 5; ++y)
        {
            // sets everything to have a distance of infinity
            point_t* vertex = graph_vertex(graph, 5, 5, x, y, costs[x][y]);

            printf("inserting vertex id=%d\n", vertex->id);
        }
    }

    point_t* start = graph_find(graph, 5, 5, 0, 0);
    point_t* goal = graph_find(graph, 5, 5, 4, 4);

    dijkstra(graph, 5, 5, start);

    printf("Shortest path from (%d,%d) to (%d,%d)\n",
            start->x, start->y,
            goal->x, goal->y
          );

    graph_traverse(start, goal, (list_cb_t) &print_point, NULL);

    printf("Shortest distance from (%d,%d) to (%d,%d) = %d\n", 
            start->x, start->y, 
            goal->x, goal->y, 
            graph_distance(start, goal) // = goal->dist
            );

    list_free(graph, (list_cb_t) &free, NULL);

    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
	if(argc!=2){
		std::cerr<<"Must specify n.\n";
		return 1;
	}
	int n=atoi(argv[1]);
	
	std::vector<node> graph=build_graph(n);
	
	dump_graph(graph);
		
	// The run-time can vary, depending on where you start from. How should you
	// take that into account when timing it?
	int start=rand()%n;
	// Note that it is only graph_distance that we care about
	std::vector<int> tmp=graph_distance(graph, start);
	for(int i=0;i<tmp.size();i++){
		fprintf(stdout, "dist(%d->%d) = %d\n", start, i, tmp[i]);
	}
	
	return 0;
}