//============================================================================ // Name : Graph_AL.cpp // Author : Arnaud Lange // Version : 1.0 // Copyright : Your copyright notice // Description : Classic Dijkstra //============================================================================ //======================================================================= // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= #include #include #include #include #include //using namespace std; #include #include #include #include using namespace boost; int main(int, char *[]) { typedef adjacency_list < listS, vecS, undirectedS, no_property, property < edge_weight_t, float > > graph_t; typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; typedef std::pair Edge; time_t start, end; double dif; time(&start); //const int num_nodes = 5; //enum nodes { A, B, C, D, E }; int NX, NY, num_nodes, num_arcs; int i, j, k, kv; std::cout << "NX: "; std::cin >> NX;std::cout << std::endl; std::cout << "NY: "; std::cin >> NY;std::cout << std::endl; num_nodes = NX*NY; num_arcs = 2*(NX-1)*(NY-1)+NY+NX-2; std::cout << "Num nodes: " << num_nodes << std::endl; std::cout << "Num arcs: " << num_arcs << std::endl; //int nodes[]={0,1,2,3,4}; //char name[] = "ABCDE"; /* Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E), Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) };*/ /* Edge edge_array[] = { Edge(0, 2), Edge(1, 1), Edge(1, 3), Edge(1, 4), Edge(2, 1), Edge(2, 3), Edge(3, 4), Edge(4, 0), Edge(4, 1) }; */ // Initialize random seed //srand(time(NULL)); //int nodes[num_nodes]; Edge edge_array[num_arcs]; float weights[num_arcs]; int n1, n2; k=0;kv=0; for (j=0; j<=NY-2; j++) { for (i=0; i<=NX-2; i++) { n1=i+j*NX;n2=(i+1)+j*NX; edge_array[k]=Edge(n1, n2); //weights[k]=1+rand()%10; weights[k]=0.5; //std::cout << "Weight " << k << " : " << weights[k] << std::endl; k++; n2=i+(j+1)*NX; edge_array[k]=Edge(n1, n2); //weights[k]=1+rand()%10; weights[k]=0.5; //std::cout << "Weight " << k << " : " << weights[k] << std::endl; k++; //nodes[kv]=kv; kv++; } n1=(NX-1)+j*NX;n2=(NX-1)+(j+1)*NX; edge_array[k]=Edge(n1, n2); //weights[k]=1+rand()%10; weights[k]=0.5; //std::cout << "Weight " << k << " : " << weights[k] << std::endl; k++; //nodes[kv]=kv; kv++; } for (i=0; i<=NX-2; i++) { n1=i+(NY-1)*NX;n2=(i+1)+(NY-1)*NX; edge_array[k]=Edge(n1, n2); //weights[k]=1+rand()%10; weights[k]=0.5; //std::cout << "Weight " << k << " : " << weights[k] << std::endl; k++; //nodes[kv]=kv; kv++; } //nodes[kv]=kv; time(&end); dif = difftime(end,start); std::cout << "Construction time = " << dif << std::endl; // int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; //int num_arcs = sizeof(edge_array) / sizeof(Edge); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 graph_t g(num_nodes); property_map::type weightmap = get(edge_weight, g); for (std::size_t j = 0; j < num_arcs; ++j) { edge_descriptor e; bool inserted; tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g); weightmap[e] = weights[j]; } #else graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); //property_map::type weightmap = get(edge_weight, g); #endif std::vector p(num_vertices(g)); std::vector d(num_vertices(g)); int ini_v; //ini_v = rand()%num_nodes; ini_v=0; std::cout << "Init vertex: " << ini_v << std::endl; vertex_descriptor s = vertex(ini_v, g); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the named parameters mechanism property_map::type indexmap = get(vertex_index, g); dijkstra_shortest_paths(g, s, &p[0], &d[0], weightmap, indexmap, std::less(), closed_plus(), (std::numeric_limits::max)(), 0, default_dijkstra_visitor()); #else dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); #endif std::cout << "distances and parents:" << std::endl; graph_traits < graph_t >::vertex_iterator vi, vend; k=0; for (tie(vi, vend) = vertices(g); (vi != vend)&&(k<15); ++vi) { //std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; // std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::endl; std::cout << "distance(" << *vi << ") = " << d[*vi] << ", "; std::cout << "parent(" << *vi << ") = " << p[*vi] << std::endl; k++; } std::cout << std::endl; /* std::ofstream dot_file("figs/dijkstra-eg.dot"); dot_file << "digraph D {\n" << " rankdir=LR\n" << " size=\"4,3\"\n" << " ratio=\"fill\"\n" << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; graph_traits < graph_t >::edge_iterator ei, ei_end; for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { graph_traits < graph_t >::edge_descriptor e = *ei; graph_traits < graph_t >::vertex_descriptor u = source(e, g), v = target(e, g); dot_file << name[u] << " -> " << name[v] << "[label=\"" << get(weightmap, e) << "\""; if (p[v] == u) dot_file << ", color=\"black\""; else dot_file << ", color=\"grey\""; dot_file << "]"; } dot_file << "}"; */ time(&end); dif = difftime(end,start); std::cout << "Computation time = " << dif << std::endl; return EXIT_SUCCESS; }