| 1 | // copied and modified from boost/libs/graph/example/knights-tour.cpp
|
|---|
| 2 |
|
|---|
| 3 | //=======================================================================
|
|---|
| 4 | // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
|
|---|
| 5 | //
|
|---|
| 6 | // Distributed under the Boost Software License, Version 1.0. (See
|
|---|
| 7 | // accompanying file LICENSE_1_0.txt or copy at
|
|---|
| 8 | // http://www.boost.org/LICENSE_1_0.txt)
|
|---|
| 9 | //=======================================================================
|
|---|
| 10 | #include <boost/config.hpp>
|
|---|
| 11 | #include <stdlib.h>
|
|---|
| 12 | #include <iostream>
|
|---|
| 13 | #include <stack>
|
|---|
| 14 | #include <queue>
|
|---|
| 15 | #include <vector>
|
|---|
| 16 | #include <boost/operators.hpp>
|
|---|
| 17 | #include <boost/graph/breadth_first_search.hpp>
|
|---|
| 18 | #include <boost/graph/visitors.hpp>
|
|---|
| 19 | #include <boost/property_map/property_map.hpp>
|
|---|
| 20 | #include <boost/graph/astar_search.hpp>
|
|---|
| 21 | #include <boost/shared_container_iterator.hpp>
|
|---|
| 22 |
|
|---|
| 23 | using namespace std;
|
|---|
| 24 | using namespace boost;
|
|---|
| 25 |
|
|---|
| 26 | struct vertex_t
|
|---|
| 27 | {
|
|---|
| 28 | vertex_t() : i(0) {}
|
|---|
| 29 | explicit vertex_t(int i) : i(i) {}
|
|---|
| 30 | vertex_t(vertex_t const& rhs) : i(rhs.i) {}
|
|---|
| 31 |
|
|---|
| 32 | bool operator==(vertex_t const& rhs) const {
|
|---|
| 33 | return i == rhs.i;
|
|---|
| 34 | }
|
|---|
| 35 | bool operator!=(vertex_t const& rhs) const {
|
|---|
| 36 | return !operator==(rhs);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | int i;
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | struct edge_t
|
|---|
| 43 | {
|
|---|
| 44 | edge_t() {}
|
|---|
| 45 | edge_t(vertex_t const& u, vertex_t const& v) : u(u), v(v) {}
|
|---|
| 46 | edge_t(edge_t const& rhs) : u(rhs.u), v(rhs.v) {}
|
|---|
| 47 |
|
|---|
| 48 | bool operator==(edge_t const& rhs) const {
|
|---|
| 49 | return u == rhs.u && v == rhs.v;
|
|---|
| 50 | }
|
|---|
| 51 | bool operator!=(edge_t const& rhs) const {
|
|---|
| 52 | return !operator==(rhs);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | vertex_t u;
|
|---|
| 56 | vertex_t v;
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | struct graph_t
|
|---|
| 60 | {
|
|---|
| 61 | typedef std::vector<vertex_t>::iterator vertex_iterator;
|
|---|
| 62 | typedef std::vector<vertex_t>::size_type vertices_size_type;
|
|---|
| 63 | typedef vertex_t vertex_descriptor;
|
|---|
| 64 | typedef edge_t edge_descriptor;
|
|---|
| 65 | typedef directed_tag directed_category;
|
|---|
| 66 | typedef disallow_parallel_edge_tag edge_parallel_category;
|
|---|
| 67 |
|
|---|
| 68 | struct category_t : public incidence_graph_tag, public vertex_list_graph_tag {};
|
|---|
| 69 | typedef category_t traversal_category;
|
|---|
| 70 |
|
|---|
| 71 | typedef shared_container_iterator<vector<edge_t> > out_edge_iterator;
|
|---|
| 72 | typedef size_t degree_size_type;
|
|---|
| 73 |
|
|---|
| 74 | typedef void adjacency_iterator;
|
|---|
| 75 | typedef void in_edge_iterator;
|
|---|
| 76 | typedef void edge_iterator;
|
|---|
| 77 | typedef void edges_size_type;
|
|---|
| 78 |
|
|---|
| 79 | explicit graph_t(int size)
|
|---|
| 80 | {
|
|---|
| 81 | for (int i=0; i<size; i++) {
|
|---|
| 82 | m_vertices.push_back(vertex_t(i));
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public:
|
|---|
| 87 | std::vector<vertex_t> m_vertices;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | vertex_t source(edge_t const& e, graph_t const&)
|
|---|
| 91 | {
|
|---|
| 92 | return e.u;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | vertex_t target(edge_t const& e, graph_t const&)
|
|---|
| 96 | {
|
|---|
| 97 | return e.v;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | std::pair<graph_t::out_edge_iterator, graph_t::out_edge_iterator>
|
|---|
| 101 | out_edges(vertex_t const& v, graph_t const& g)
|
|---|
| 102 | {
|
|---|
| 103 | boost::shared_ptr<vector<edge_t> > c(new vector<edge_t> );
|
|---|
| 104 | if (v.i > 0) {
|
|---|
| 105 | c->push_back(edge_t(vertex_t(v.i-1), v));
|
|---|
| 106 | }
|
|---|
| 107 | if (static_cast<size_t>(v.i) < g.m_vertices.size()-1) {
|
|---|
| 108 | c->push_back(edge_t(v, vertex_t(v.i+1)));
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return make_shared_container_range(c);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | size_t out_degree(vertex_t const& v, graph_t const& g)
|
|---|
| 115 | {
|
|---|
| 116 | std::pair<graph_t::out_edge_iterator, graph_t::out_edge_iterator> eis = out_edges(v, g);
|
|---|
| 117 | return std::distance(eis.first, eis.second);
|
|---|
| 118 |
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | std::pair<graph_t::vertex_iterator, graph_t::vertex_iterator>
|
|---|
| 122 | vertices(graph_t& g) {
|
|---|
| 123 | return make_pair(g.m_vertices.begin(), g.m_vertices.end());
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | graph_t::vertices_size_type num_vertices(graph_t const& g) {
|
|---|
| 127 | return g.m_vertices.size();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | struct vertex_index_map_t
|
|---|
| 131 | {
|
|---|
| 132 | typedef int value_type;
|
|---|
| 133 | typedef int reference;
|
|---|
| 134 | typedef vertex_t key_type;
|
|---|
| 135 | typedef readable_property_map_tag category;
|
|---|
| 136 | };
|
|---|
| 137 | int get(vertex_index_map_t, vertex_t const& v) {
|
|---|
| 138 | return v.i;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | struct edge_weight_map_t
|
|---|
| 142 | {
|
|---|
| 143 | typedef double value_type;
|
|---|
| 144 | typedef double reference;
|
|---|
| 145 | typedef edge_t key_type;
|
|---|
| 146 | typedef readable_property_map_tag category;
|
|---|
| 147 | };
|
|---|
| 148 | double get(edge_weight_map_t, edge_t const& e) {
|
|---|
| 149 | return abs(e.u.i - e.u.i);
|
|---|
| 150 | }
|
|---|
| 151 | ///////////////////////////////////////////////////////////////////////////////
|
|---|
| 152 |
|
|---|
| 153 | struct h : public astar_heuristic<graph_t, double>
|
|---|
| 154 | {
|
|---|
| 155 | double operator()(vertex_t const& v) const
|
|---|
| 156 | {
|
|---|
| 157 | return 0.0;
|
|---|
| 158 | }
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | int main(int argc, char *argv[])
|
|---|
| 162 | {
|
|---|
| 163 | graph_t g(10);
|
|---|
| 164 |
|
|---|
| 165 | astar_search
|
|---|
| 166 | ( g
|
|---|
| 167 | , vertex_t(0)
|
|---|
| 168 | , h()
|
|---|
| 169 | , weight_map(edge_weight_map_t())
|
|---|
| 170 | .vertex_index_map(vertex_index_map_t())
|
|---|
| 171 | );
|
|---|
| 172 |
|
|---|
| 173 | return 0;
|
|---|
| 174 | }
|
|---|