Created
September 18, 2015 06:59
-
-
Save Zia-/b3504c393ee6e68d39e5 to your computer and use it in GitHub Desktop.
Modified form of "example/dijkstra-example.cpp" code (https://github.com/boostorg/graph/blob/master/example/dijkstra-example.cpp) showing Dijkstra with predecessor and distance maps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/config.hpp> | |
#include <iostream> | |
#include <fstream> | |
#include <boost/graph/graph_traits.hpp> | |
#include <boost/graph/adjacency_list.hpp> | |
#include <boost/graph/dijkstra_shortest_paths.hpp> | |
#include <boost/property_map/property_map.hpp> | |
int | |
main() | |
{ | |
// Select the graph type we wish to use | |
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS, | |
boost::no_property, boost::property < boost::edge_weight_t, int > > DirectedGraph; | |
typedef boost::graph_traits < DirectedGraph >::vertex_descriptor vertex_descriptor; | |
typedef std::pair<int, int> Edge; | |
const int num_nodes = 5; | |
enum nodes { A, B, C, D, E }; | |
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) | |
}; | |
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; | |
int num_arcs = sizeof(edge_array) / sizeof(Edge); | |
DirectedGraph digraph(edge_array, edge_array + num_arcs, weights, num_nodes); | |
// Following two are for the predecessor_maps and distance_maps | |
std::vector<vertex_descriptor> p(boost::num_vertices(digraph)); | |
std::vector<int> d(boost::num_vertices(digraph)); | |
// Start point | |
vertex_descriptor s = vertex(A, digraph); | |
boost::dijkstra_shortest_paths(digraph, s, | |
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, digraph))). | |
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, digraph)))); | |
std::cout << "distances and parents:" << std::endl; | |
boost::graph_traits < DirectedGraph >::vertex_iterator vi, vend; | |
for (boost::tie(vi, vend) = boost::vertices(digraph); vi != vend; ++vi) { | |
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; | |
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std:: | |
endl; | |
} | |
std::cout << std::endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment