diff --git a/example/accum-compile-times.cpp b/example/accum-compile-times.cpp new file mode 100644 index 00000000..38bfcedb --- /dev/null +++ b/example/accum-compile-times.cpp @@ -0,0 +1,143 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include + +namespace std +{ + template < typename T > + std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return in; + } +} + +namespace boost +{ + enum vertex_compile_cost_t + { vertex_compile_cost = 111 }; // a unique # + BOOST_INSTALL_PROPERTY(vertex, compile_cost); +} + +using namespace boost; + +typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + listS, // Store vertex set in a std::list + directedS, // The file dependency graph is directed + // vertex properties + property < vertex_name_t, + std::string, + property < + vertex_compile_cost_t, float, + property < + vertex_distance_t, float, + property < + vertex_color_t, +default_color_type > >>>, + // an edge property + property < +edge_weight_t, float >> + file_dep_graph2; + +typedef + graph_traits < + file_dep_graph2 >::vertex_descriptor + vertex_t; +typedef + graph_traits < + file_dep_graph2 >::edge_descriptor + edge_t; + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef + graph_traits < + file_dep_graph2 >::vertices_size_type + size_type; + size_type + n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph2 + g(input_begin, input_end, n_vertices); + + typedef + property_map < + file_dep_graph2, + vertex_name_t >::type + name_map_t; + typedef + property_map < + file_dep_graph2, + vertex_compile_cost_t >::type + compile_cost_map_t; + typedef + property_map < + file_dep_graph2, + vertex_distance_t >::type + distance_map_t; + typedef + property_map < + file_dep_graph2, + vertex_color_t >::type + color_map_t; + + name_map_t + name_map = + get(vertex_name, g); + compile_cost_map_t + compile_cost_map = + get(vertex_compile_cost, g); + distance_map_t + distance_map = + get(vertex_distance, g); + color_map_t + color_map = + get(vertex_color, g); + + std::ifstream name_in("makefile-target-names.dat"); + std::ifstream compile_cost_in("target-compile-costs.dat"); + graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + name_in >> name_map[*vi]; + compile_cost_in >> compile_cost_map[*vi]; + } + + graph_property_iter_range < file_dep_graph2, + vertex_compile_cost_t >::iterator ci, ci_end; + tie(ci, ci_end) = get_property_iter_range(g, vertex_compile_cost); + std::cout << "total (sequential) compile time: " + << std::accumulate(ci, ci_end, 0.0) << std::endl; + + return 0; +} diff --git a/example/accumulate-eg.cpp b/example/accumulate-eg.cpp new file mode 100644 index 00000000..8d28afdb --- /dev/null +++ b/example/accumulate-eg.cpp @@ -0,0 +1,57 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +template < typename InputIterator, typename T, typename BinaryOperator > + T accumulate(InputIterator first, InputIterator last, T init, + BinaryOperator binary_op) +{ + for (; first != last; ++first) + init = binary_op(init, *first); + return init; +} + + +int +main() +{ + // using accumulate with a vector + std::vector < double >x(10, 1.0); + double sum1; + sum1 = std::accumulate(x.begin(), x.end(), 0.0, std::plus < double >()); + + // using accumulate with a linked list + std::list < double >y; + double sum2; + // copy vector's values into the list + std::copy(x.begin(), x.end(), std::back_inserter(y)); + sum2 = std::accumulate(y.begin(), y.end(), 0.0, std::plus < double >()); + assert(sum1 == sum2); // they should be equal + + return 0; +} diff --git a/example/bellman-example.cpp b/example/bellman-example.cpp new file mode 100644 index 00000000..e03502c8 --- /dev/null +++ b/example/bellman-example.cpp @@ -0,0 +1,134 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename ParentMap > struct edge_writer +{ + edge_writer(const Graph & g, const ParentMap & p) + : m_g(g), m_parent(p) + { + } + + template < typename Edge > + void operator() (std::ostream & out, const Edge & e) const + { + out << "[label=\"" << get(edge_weight, m_g, e) << "\""; + typename graph_traits < Graph >::vertex_descriptor + u = source(e, m_g), v = target(e, m_g); + if (m_parent[v] == u) + out << ", color=\"black\""; + else + out << ", color=\"grey\""; + out << "]"; + } + const Graph & m_g; + ParentMap m_parent; +}; +template < typename Graph, typename Parent > + edge_writer < Graph, Parent > +make_edge_writer(const Graph & g, const Parent & p) +{ + return edge_writer < Graph, Parent > (g, p); +} + +int +main() +{ + enum + { u, v, x, y, z, N }; + char name[] = { 'u', 'v', 'x', 'y', 'z' }; + typedef std::pair < int, int >E; + const int n_edges = 10; + typedef boost::array < E, n_edges > EdgeList; + EdgeList edge_array = { {E(u, y), E(u, x), E(u, v), E(v, u), + E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z, + x)} + }; + int weight[n_edges] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 }; + + typedef adjacency_list < vecS, vecS, directedS, + no_property, property < edge_weight_t, int >>Graph; + Graph g(edge_array.begin(), edge_array.end()); + graph_traits < Graph >::edge_iterator ei, ei_end; + int i = 0; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i) + get(edge_weight, g)[*ei] = weight[i]; + + std::vector < int >distance(N, std::numeric_limits < short >::max()); + std::vector < int >parent(N); + for (i = 0; i < N; ++i) + parent[i] = i; + distance[z] = 0; + bool r = bellman_ford_shortest_paths(g, int (N), + weight_map(get(edge_weight, g)). + distance_map(&distance[0]). + predecessor_map(&parent[0])); + + if (r) + for (i = 0; i < N; ++i) + std::cout << name[i] << ": " << std::setw(3) << distance[i] + << " " << name[parent[i]] << std::endl; + else + std::cout << "negative cycle" << std::endl; + + std::ofstream dot_file("figs/bellman-eg.dot"); +#if 0 + write_graphviz(dot_file, g, make_label_writer(name), + make_edge_writer(g, &parent[0])); +#else + dot_file << "digraph D {\n" + << " rankdir=LR\n" + << " size=\"5,3\"\n" + << " ratio=\"fill\"\n" + << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; + + { + graph_traits < Graph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { + graph_traits < Graph >::edge_descriptor e = *ei; + graph_traits < Graph >::vertex_descriptor + u = source(e, g), v = target(e, g); + dot_file << name[u] << " -> " << name[v] + << "[label=\"" << get(edge_weight, g, e) << "\""; + if (parent[v] == u) + dot_file << ", color=\"black\""; + else + dot_file << ", color=\"grey\""; + dot_file << "]"; + } + } + dot_file << "}"; +#endif + return EXIT_SUCCESS; +} diff --git a/example/bellman-ford-internet.cpp b/example/bellman-ford-internet.cpp new file mode 100644 index 00000000..eece8033 --- /dev/null +++ b/example/bellman-ford-internet.cpp @@ -0,0 +1,80 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + // ID numbers for the routers (vertices). + enum + { A, B, C, D, E, F, G, H, n_vertices }; + const int n_edges = 11; + typedef std::pair < int, int >Edge; + + // The list of connections between routers stored in an array. + array < Edge, n_edges > edges = { { + Edge(A, B), Edge(A, C), + Edge(B, D), Edge(B, E), Edge(C, E), Edge(C, F), Edge(D, H), + Edge(D, E), Edge(E, H), Edge(F, G), Edge(G, H)} + }; + + // Specify the graph type and declare a graph object + typedef edge_list < array < Edge, n_edges >::iterator > Graph; + Graph g(edges.begin(), edges.end()); + + // The transmission delay values for each edge. + array < float, n_edges > delay = + { {5.0, 1.0, 1.3, 3.0, 10.0, 2.0, 6.3, 0.4, 1.3, 1.2, 0.5} }; + + // Declare some storage for some "external" vertex properties. + char name[] = "ABCDEFGH"; + array < int, n_vertices > parent; + for (int i = 0; i < n_vertices; ++i) + parent[i] = i; + array < float, n_vertices > distance; + distance.assign(std::numeric_limits < float >::max()); + // Specify A as the source vertex + distance[A] = 0; + + bool r = bellman_ford_shortest_paths(g, int (n_vertices), + weight_map(make_iterator_property_map + (delay.begin(), + get(edge_index, g), + delay[0])). + distance_map(&distance[0]). + predecessor_map(&parent[0])); + + if (r) + for (int i = 0; i < n_vertices; ++i) + std::cout << name[i] << ": " << distance[i] + << " " << name[parent[i]] << std::endl; + else + std::cout << "negative cycle" << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/bfs-example.cpp b/example/bfs-example.cpp new file mode 100644 index 00000000..6cc36276 --- /dev/null +++ b/example/bfs-example.cpp @@ -0,0 +1,88 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +using namespace boost; +template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor { + typedef typename property_traits < TimeMap >::value_type T; +public: +bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { + } + template < typename Vertex, typename Graph > + void discover_vertex(Vertex u, const Graph & g) const + { + put(m_timemap, u, m_time++); + } + TimeMap m_timemap; + T & m_time; +}; + + +int +main() +{ + using namespace boost; + // Select the graph type we wish to use + typedef adjacency_list < vecS, vecS, undirectedS > graph_t; + // Set up the vertex IDs and names + enum + { r, s, t, u, v, w, x, y, N }; + const char *name = "rstuvwxy"; + // Specify the edges in the graph + typedef pair < int, int >E; + E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), + E(w, x), E(x, t), E(t, u), E(x, y), E(u, y) + }; + // Create the graph object + const int n_edges = sizeof(edge_array) / sizeof(E); + graph_t g(edge_array, edge_array + n_edges, N); + + // Typedefs + typedef graph_traits < graph_t >::vertex_descriptor Vertex; + typedef graph_traits < graph_t >::vertices_size_type Size; + typedef std::vector < Vertex >::iterator Piter; + typedef std::vector < Size >::iterator Iiter; + + // a vector to hold the discover time property for each vertex + std::vector < Size > dtime(num_vertices(g)); + + Size time = 0; + bfs_time_visitor < Size * >vis(&dtime[0], time); + breadth_first_search(g, vertex(s, g), visitor(vis)); + + // Use std::sort to order the vertices by their discover time + vector < graph_traits < graph_t >::vertices_size_type > discover_order(N); + integer_range < int >range(0, N); + std::copy(range.begin(), range.end(), discover_order.begin()); + std::sort(discover_order.begin(), discover_order.end(), + indirect_cmp < Iiter, std::less < Size > >(dtime.begin())); + + std::cout << "order of discovery: "; + for (int i = 0; i < N; ++i) + std::cout << name[discover_order[i]] << " "; + std::cout << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/bfs-name-printer.cpp b/example/bfs-name-printer.cpp new file mode 100644 index 00000000..4264a6d2 --- /dev/null +++ b/example/bfs-name-printer.cpp @@ -0,0 +1,104 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +using namespace boost; + +template < typename Graph, typename VertexNameMap, typename TransDelayMap > void +build_router_network(Graph & g, VertexNameMap name_map, + TransDelayMap delay_map) +{ + typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e; + a = add_vertex(g); + name_map[a] = 'a'; + b = add_vertex(g); + name_map[b] = 'b'; + c = add_vertex(g); + name_map[c] = 'c'; + d = add_vertex(g); + name_map[d] = 'd'; + e = add_vertex(g); + name_map[e] = 'e'; + + typename graph_traits < Graph >::edge_descriptor ed; + bool inserted; + + tie(ed, inserted) = add_edge(a, b, g); + delay_map[ed] = 1.2; + tie(ed, inserted) = add_edge(a, d, g); + delay_map[ed] = 4.5; + tie(ed, inserted) = add_edge(b, d, g); + delay_map[ed] = 1.8; + tie(ed, inserted) = add_edge(c, a, g); + delay_map[ed] = 2.6; + tie(ed, inserted) = add_edge(c, e, g); + delay_map[ed] = 5.2; + tie(ed, inserted) = add_edge(d, c, g); + delay_map[ed] = 0.4; + tie(ed, inserted) = add_edge(d, e, g); + delay_map[ed] = 3.3; + +} + + +template < typename VertexNameMap > class bfs_name_printer:public default_bfs_visitor { + // inherit default (empty) event point actions +public: +bfs_name_printer(VertexNameMap n_map):m_name_map(n_map) { + } + template < typename Vertex, typename Graph > + void discover_vertex(Vertex u, const Graph &) const + { + std::cout << get(m_name_map, u) << ' '; + } +private: + VertexNameMap m_name_map; +}; + + +int +main() +{ + typedef adjacency_list < listS, vecS, directedS, + property < vertex_name_t, char >, + property < edge_weight_t, double >>graph_t; + graph_t g; + + property_map < graph_t, vertex_name_t >::type name_map = + get(vertex_name, g); + property_map < graph_t, edge_weight_t >::type delay_map = + get(edge_weight, g); + + build_router_network(g, name_map, delay_map); + + typedef property_map < graph_t, vertex_name_t >::type VertexNameMap; + graph_traits < graph_t >::vertex_descriptor a = *vertices(g).first; + bfs_name_printer < VertexNameMap > vis(name_map); + std::cout << "BFS vertex discover order: "; + breadth_first_search(g, a, visitor(vis)); + std::cout << std::endl; + +} diff --git a/example/binary-method-problem.cpp b/example/binary-method-problem.cpp new file mode 100644 index 00000000..e4492895 --- /dev/null +++ b/example/binary-method-problem.cpp @@ -0,0 +1,89 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +class Point +{ +public: + virtual bool equal(const Point * p) const = 0; +}; + +class ColorPoint:public Point +{ +public: + ColorPoint(float x, float y, std::string c):x(x), y(y), color(c) + { + } + virtual bool equal(const ColorPoint * p) const + { + return color == p->color && x == p->x && y == p->y; + } +protected: + float x, y; + std::string color; +}; + +class ColorPoint2:public Point +{ +public: + ColorPoint2(float x, float y, std::string s):x(x), y(y), color(s) + { + } + virtual bool equal(const Point * p) const + { + const ColorPoint2 *cp = dynamic_cast < const ColorPoint2 * >(p); + return color == cp->color && x == cp->x && y == cp->y; + } +protected: + float x, y; + std::string color; +}; + +void +print_equal(const Point * a, const Point * b) +{ + std::cout << std::boolalpha << a->equal(b) << std::endl; +} + +template < typename PointType > void +print_equal2(const PointType * a, const PointType * b) +{ + std::cout << std::boolalpha << a->equal(b) << std::endl; +} + + +int +main() +{ + Point *p = new ColorPoint2(0.0, 0.0, "blue"); + Point *q = new ColorPoint2(0.0, 0.0, "green"); + print_equal(p, q); + ColorPoint *a = new ColorPoint(0.0, 0.0, "blue"); + ColorPoint *b = new ColorPoint(0.0, 0.0, "green"); + print_equal2(a, b); + + + return 0; +} diff --git a/example/cc-internet.cpp b/example/cc-internet.cpp new file mode 100644 index 00000000..9a75a119 --- /dev/null +++ b/example/cc-internet.cpp @@ -0,0 +1,59 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + GraphvizGraph g; + read_graphviz("figs/cc-internet.dot", g); + + std::vector < int >component(num_vertices(g)); + + int num_comp = connected_components(g, + make_iterator_property_map(component. + begin(), + get + (vertex_index, + g))); + + property_map < GraphvizGraph, vertex_attribute_t >::type + vertex_attr_map = get(vertex_attribute, g); + std::string color[] = { + "white", "gray", "black", "lightgray"}; + graph_traits < GraphvizGraph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + vertex_attr_map[*vi]["color"] = color[component[*vi]]; + vertex_attr_map[*vi]["style"] = "filled"; + if (vertex_attr_map[*vi]["color"] == "black") + vertex_attr_map[*vi]["fontcolor"] = "white"; + } + write_graphviz("figs/cc-internet-out.dot", g); + +} diff --git a/example/connected-components.cpp b/example/connected-components.cpp new file mode 100644 index 00000000..401e608e --- /dev/null +++ b/example/connected-components.cpp @@ -0,0 +1,57 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, undirectedS > Graph; + typedef graph_traits < Graph >::vertex_descriptor Vertex; + + const int N = 6; + Graph G(N); + add_edge(0, 1, G); + add_edge(1, 4, G); + add_edge(4, 0, G); + add_edge(2, 5, G); + + std::vector < int >c(num_vertices(G)); + int num = connected_components(G, + make_iterator_property_map(c.begin(), + get(vertex_index, + G))); + + cout << endl; + std::vector < int >::iterator i; + cout << "Total number of components: " << num << endl; + for (i = c.begin(); i != c.end(); ++i) + cout << "Vertex " << i - c.begin() + << " is in component " << *i << endl; + cout << endl; + return EXIT_SUCCESS; +} diff --git a/example/copy-example.cpp b/example/copy-example.cpp new file mode 100644 index 00000000..c524ca0b --- /dev/null +++ b/example/copy-example.cpp @@ -0,0 +1,64 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef int weight_t; + typedef adjacency_list < vecS, vecS, directedS, + property < vertex_name_t, char >>graph_t; + + enum + { a, b, c, d, e, f, g, N }; + graph_t G(N); + property_map < graph_t, vertex_name_t >::type + name_map = get(vertex_name, G); + char name = 'a'; + graph_traits < graph_t >::vertex_iterator v, v_end; + for (tie(v, v_end) = vertices(G); v != v_end; ++v, ++name) + name_map[*v] = name; + + typedef std::pair < int, int >E; + E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), + E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) + }; + for (int i = 0; i < 12; ++i) + add_edge(edges[i].first, edges[i].second, G); + + print_graph(G, name_map); + std::cout << std::endl; + + graph_t G_copy; + copy_graph(G, G_copy); + + print_graph(G_copy, name_map); + + return 0; +} diff --git a/example/cycle-file-dep.cpp b/example/cycle-file-dep.cpp new file mode 100644 index 00000000..9e95b991 --- /dev/null +++ b/example/cycle-file-dep.cpp @@ -0,0 +1,98 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +namespace std +{ + template < typename T > + std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return in; + } +} + +typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The file dependency graph is directed +> file_dep_graph; + +typedef graph_traits < file_dep_graph >::vertex_descriptor vertex_t; +typedef graph_traits < file_dep_graph >::edge_descriptor edge_t; + +bool +has_cycle_dfs(const file_dep_graph & g, vertex_t u, + default_color_type * color) +{ + color[u] = gray_color; + graph_traits < file_dep_graph >::adjacency_iterator vi, vi_end; + for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) + if (color[*vi] == white_color) + if (has_cycle_dfs(g, *vi, color)) + return true; // cycle detected, return immediately + else if (color[*vi] == gray_color) // *vi is an ancestor! + return true; + color[u] = black_color; + return false; +} + +bool +has_cycle(const file_dep_graph & g) +{ + std::vector < default_color_type > color(num_vertices(g), white_color); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + if (color[*vi] == white_color) + if (has_cycle_dfs(g, *vi, &color[0])) + return true; + return false; +} + + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef graph_traits < file_dep_graph >::vertices_size_type size_type; + size_type n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph g(input_begin, input_end, n_vertices); + + std::vector < std::string > name(num_vertices(g)); + std::ifstream name_in("makefile-target-names.dat"); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + name_in >> name[*vi]; + + assert(has_cycle(g) == false); + return 0; +} diff --git a/example/cycle-file-dep2.cpp b/example/cycle-file-dep2.cpp new file mode 100644 index 00000000..e14a656e --- /dev/null +++ b/example/cycle-file-dep2.cpp @@ -0,0 +1,203 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +// can't do using namespace boost because then +// we get conflict with boost::default_dfs_visitor. +using + boost::graph_traits; +using + boost::listS; +using + boost::vecS; +using + boost::directedS; +using + boost::adjacency_list; +using + boost::default_color_type; +using + boost::white_color; +using + boost::gray_color; +using + boost::black_color; + +namespace + std +{ + template < + typename + T > + std::istream & + operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return + in; + } +} + +typedef + adjacency_list < + listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The file dependency graph is directed + > + file_dep_graph; + +typedef + graph_traits < + file_dep_graph >::vertex_descriptor + vertex_t; +typedef + graph_traits < + file_dep_graph >::edge_descriptor + edge_t; + +template < typename Visitor > void +dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color, + Visitor vis) +{ + color[u] = gray_color; + vis.discover_vertex(u, g); + graph_traits < file_dep_graph >::out_edge_iterator ei, ei_end; + for (tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) { + if (color[target(*ei, g)] == white_color) { + vis.tree_edge(*ei, g); + dfs_v1(g, target(*ei, g), color, vis); + } else if (color[target(*ei, g)] == gray_color) + vis.back_edge(*ei, g); + else + vis.forward_or_cross_edge(*ei, g); + } + color[u] = black_color; + vis.finish_vertex(u, g); +} + +template < typename Visitor > void +generic_dfs_v1(const file_dep_graph & g, Visitor vis) +{ + std::vector < default_color_type > color(num_vertices(g), white_color); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + if (color[*vi] == white_color) + dfs_v1(g, *vi, &color[0], vis); + } +} + +struct default_dfs_visitor +{ + template < + typename + V, + typename + G > void + discover_vertex(V, const G &) + { + } + + template < + typename + E, + typename + G > void + tree_edge(E, const G &) + { + } + + template < typename E, typename G > void + back_edge(E, const G &) + { + } + + template < typename E, typename G > void + forward_or_cross_edge(E, const G &) + { + } + + template < typename V, typename G > void + finish_vertex(V, const G &) + { + } +}; + +struct cycle_detector: + public + default_dfs_visitor +{ + cycle_detector(bool & cycle): + has_cycle(cycle) + { + } + void + back_edge(edge_t, const file_dep_graph &) + { + has_cycle = true; + } + bool & has_cycle; +}; + +bool +has_cycle(const file_dep_graph & g) +{ + bool + has_cycle = + false; + cycle_detector + vis(has_cycle); + generic_dfs_v1(g, vis); + return has_cycle; +} + + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef + graph_traits < + file_dep_graph >::vertices_size_type + size_type; + size_type + n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph + g(input_begin, input_end, n_vertices); + + std::vector < std::string > name(num_vertices(g)); + std::ifstream name_in("makefile-target-names.dat"); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + name_in >> name[*vi]; + + assert(has_cycle(g) == false); + return 0; +} diff --git a/example/default-constructor.cpp b/example/default-constructor.cpp new file mode 100644 index 00000000..eb5f448a --- /dev/null +++ b/example/default-constructor.cpp @@ -0,0 +1,62 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include + +using namespace boost; + +template < typename Graph > void +read_graph_file(std::istream & in, Graph & g) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) // Add n vertices to the graph + add_vertex(g); + size_type u, v; + while (in >> u) // Read in pairs of integers as edges + if (in >> v) + add_edge(u, v, g); + else + break; +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The graph is directed + > graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"); + read_graph_file(file_in, g); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/default-constructor2.cpp b/example/default-constructor2.cpp new file mode 100644 index 00000000..8a190e87 --- /dev/null +++ b/example/default-constructor2.cpp @@ -0,0 +1,65 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include + +using namespace boost; + +template < typename Graph > void +read_graph_file(std::istream & in, Graph & g) +{ + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + in >> n_vertices; // read in number of vertices + std::vector < Vertex > vertex_set(n_vertices); + for (size_type i = 0; i < n_vertices; ++i) + vertex_set[i] = add_vertex(g); + + size_type u, v; + while (in >> u) + if (in >> v) + add_edge(vertex_set[u], vertex_set[v], g); + else + break; +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The graph is directed + > graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"); + read_graph_file(file_in, g); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/dfs-example.cpp b/example/dfs-example.cpp new file mode 100644 index 00000000..0fb0cf8d --- /dev/null +++ b/example/dfs-example.cpp @@ -0,0 +1,101 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +using namespace boost; +template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor { + typedef typename property_traits < TimeMap >::value_type T; +public: + dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t) +: m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) { + } + template < typename Vertex, typename Graph > + void discover_vertex(Vertex u, const Graph & g) const + { + put(m_dtimemap, u, m_time++); + } + template < typename Vertex, typename Graph > + void finish_vertex(Vertex u, const Graph & g) const + { + put(m_ftimemap, u, m_time++); + } + TimeMap m_dtimemap; + TimeMap m_ftimemap; + T & m_time; +}; + + +int +main() +{ + // Select the graph type we wish to use + typedef adjacency_list < vecS, vecS, directedS > graph_t; + typedef graph_traits < graph_t >::vertices_size_type size_type; + // Set up the vertex names + enum + { u, v, w, x, y, z, N }; + char name[] = { 'u', 'v', 'w', 'x', 'y', 'z' }; + // Specify the edges in the graph + typedef pair < int, int >E; + E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x), + E(v, y), E(w, y), E(w, z), E(z, z) + }; + graph_t g(N, edge_array, edge_array + sizeof(edge_array) / sizeof(E)); + + // Typedefs + typedef boost::graph_traits < graph_t >::vertex_descriptor Vertex; + typedef std::vector < Vertex >::iterator Piter; + typedef std::vector < size_type >::iterator Iiter; + + // discover time and finish time properties + std::vector < size_type > dtime(num_vertices(g)); + std::vector < size_type > ftime(num_vertices(g)); + size_type t = 0; + dfs_time_visitor < size_type * >vis(&dtime[0], &ftime[0], t); + + depth_first_search(g, visitor(vis)); + + // use std::sort to order the vertices by their discover time + std::vector < size_type > discover_order(N); + integer_range < size_type > r(0, N); + std::copy(r.begin(), r.end(), discover_order.begin()); + std::sort(discover_order.begin(), discover_order.end(), + indirect_cmp < Iiter, std::less < size_type > >(dtime.begin())); + std::cout << "order of discovery: "; + for (int i = 0; i < N; ++i) + std::cout << name[discover_order[i]] << " "; + + vector < size_type > finish_order(N); + std::copy(r.begin(), r.end(), finish_order.begin()); + std::sort(finish_order.begin(), finish_order.end(), + indirect_cmp < Iiter, std::less < size_type > >(ftime.begin())); + std::cout << std::endl << "order of finish: "; + for (int i = 0; i < N; ++i) + cout << name[finish_order[i]] << " "; + std::cout << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/dfs-parenthesis.cpp b/example/dfs-parenthesis.cpp new file mode 100644 index 00000000..78a0af75 --- /dev/null +++ b/example/dfs-parenthesis.cpp @@ -0,0 +1,74 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +char name[] = "abcdefghij"; + +struct parenthesis_visitor:public + boost::default_dfs_visitor +{ + template < + class + Vertex, + class + Graph > void + start_vertex(Vertex v, const Graph &) + { + std::cout << ' '; + } + template < + class + Vertex, + class + Graph > void + discover_vertex(Vertex v, const Graph &) + { + std::cout << "(" << name[v] << ' '; + } + template < class Vertex, class Graph > void + finish_vertex(Vertex v, const Graph &) + { + std::cout << ' ' << name[v] << ")"; + } +}; + +int +main() +{ + using namespace + boost; + GraphvizGraph + g; + read_graphviz("figs/dfs-example.dot", g); + graph_traits < GraphvizGraph >::edge_iterator e, e_end; + for (tie(e, e_end) = edges(g); e != e_end; ++e) + std::cout << '(' << name[source(*e, g)] << ' ' + << name[target(*e, g)] << ')' << std::endl; + parenthesis_visitor + paren_vis; + depth_first_search(g, visitor(paren_vis)); + std::cout << std::endl; + return 0; +} diff --git a/example/dijkstra-example.cpp b/example/dijkstra-example.cpp new file mode 100644 index 00000000..b93eeda8 --- /dev/null +++ b/example/dijkstra-example.cpp @@ -0,0 +1,89 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include + +#include +#include +#include + +using namespace boost; + +int +main(int, char *[]) +{ + typedef adjacency_list < listS, vecS, directedS, + no_property, property < edge_weight_t, int >>graph_t; + typedef graph_traits < graph_t >::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); + graph_t g(num_nodes, edge_array, edge_array + num_arcs, weights); + std::vector < vertex_descriptor > p(num_vertices(g)); + std::vector < int >d(num_vertices(g)); + vertex_descriptor s = vertex(A, g); + dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); + + std::cout << "distances and parents:" << std::endl; + graph_traits < graph_t >::vertex_iterator vi, vend; + for (tie(vi, vend) = vertices(g); vi != vend; ++vi) { + std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; + std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std:: + endl; + } + 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(edge_weight, g, e) << "\""; + if (p[v] == u) + dot_file << ", color=\"black\""; + else + dot_file << ", color=\"grey\""; + dot_file << "]"; + } + dot_file << "}"; + return EXIT_SUCCESS; +} diff --git a/example/edge-connectivity.cpp b/example/edge-connectivity.cpp new file mode 100644 index 00000000..837d6659 --- /dev/null +++ b/example/edge-connectivity.cpp @@ -0,0 +1,192 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include + +namespace boost +{ + template < typename Graph > + std::pair < typename graph_traits < Graph >::vertex_descriptor, + typename graph_traits < Graph >::degree_size_type > + min_degree_vertex(Graph & g) + { + typename graph_traits < Graph >::vertex_descriptor p; + typedef typename graph_traits < Graph >::degree_size_type size_type; + size_type delta = std::numeric_limits < size_type >::max(); + typename graph_traits < Graph >::vertex_iterator i, iend; + for (tie(i, iend) = vertices(g); i != iend; ++i) + if (degree(*i, g) < delta) + { + delta = degree(*i, g); + p = *i; + } + return std::make_pair(p, delta); + } + + template < typename Graph, typename OutputIterator > + void neighbors(const Graph & g, + typename graph_traits < Graph >::vertex_descriptor u, + OutputIterator result) + { + typename graph_traits < Graph >::adjacency_iterator ai, aend; + for (tie(ai, aend) = adjacent_vertices(u, g); ai != aend; ++ai) + *result++ = *ai; + } + template < typename Graph, typename VertexIterator, + typename OutputIterator > void neighbors(const Graph & g, + VertexIterator first, + VertexIterator last, + OutputIterator result) + { + for (; first != last; ++first) + neighbors(g, *first, result); + } + + template < typename VertexListGraph, typename OutputIterator > + typename graph_traits < VertexListGraph >::degree_size_type + edge_connectivity(VertexListGraph & g, OutputIterator disconnecting_set) { + typedef typename graph_traits < + VertexListGraph >::vertex_descriptor vertex_descriptor; + typedef typename graph_traits < + VertexListGraph >::degree_size_type degree_size_type; + typedef color_traits < default_color_type > Color; + typedef typename adjacency_list_traits < vecS, vecS, + directedS >::edge_descriptor edge_descriptor; + typedef adjacency_list < vecS, vecS, directedS, no_property, + property < edge_capacity_t, degree_size_type, + property < edge_residual_capacity_t, degree_size_type, + property < edge_reverse_t, edge_descriptor > >>>FlowGraph; + + vertex_descriptor u, v, p, k; + edge_descriptor e1, e2; + bool inserted; + typename graph_traits < VertexListGraph >::vertex_iterator vi, vi_end; + degree_size_type delta, alpha_star, alpha_S_k; + std::set < vertex_descriptor > S, neighbor_S; + std::vector < vertex_descriptor > S_star, nonneighbor_S; + std::vector < default_color_type > color(num_vertices(g)); + std::vector < edge_descriptor > pred(num_vertices(g)); + + FlowGraph flow_g(num_vertices(g)); + typename property_map < FlowGraph, edge_capacity_t >::type + cap = get(edge_capacity, flow_g); + typename property_map < FlowGraph, edge_residual_capacity_t >::type + res_cap = get(edge_residual_capacity, flow_g); + typename property_map < FlowGraph, edge_reverse_t >::type + rev_edge = get(edge_reverse, flow_g); + + typename graph_traits < VertexListGraph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { + u = source(*ei, g), v = target(*ei, g); + tie(e1, inserted) = add_edge(u, v, flow_g); + cap[e1] = 1; + tie(e2, inserted) = add_edge(v, u, flow_g); + cap[e2] = 1; + rev_edge[e1] = e2; + rev_edge[e2] = e1; + } + + tie(p, delta) = min_degree_vertex(g); + S_star.push_back(p); + alpha_star = delta; + S.insert(p); + neighbor_S.insert(p); + neighbors(g, S.begin(), S.end(), + std::inserter(neighbor_S, neighbor_S.begin())); + std::set_difference(vertices(g).first, vertices(g).second, + neighbor_S.begin(), neighbor_S.end(), + std::back_inserter(nonneighbor_S)); + + while (!nonneighbor_S.empty()) { + k = nonneighbor_S.front(); + alpha_S_k = edmunds_karp_max_flow + (flow_g, p, k, capacity_map(cap).residual_capacity_map(res_cap). + reverse_edge_map(rev_edge).color_map(&color[0]). + predecessor_map(&pred[0])); + if (alpha_S_k < alpha_star) { + alpha_star = alpha_S_k; + S_star.clear(); + for (tie(vi, vi_end) = vertices(flow_g); vi != vi_end; ++vi) + if (color[*vi] != Color::white()) + S_star.push_back(*vi); + } + S.insert(k); + neighbor_S.insert(k); + neighbors(g, k, std::inserter(neighbor_S, neighbor_S.begin())); + nonneighbor_S.clear(); + std::set_difference(vertices(g).first, vertices(g).second, + neighbor_S.begin(), neighbor_S.end(), + std::back_inserter(nonneighbor_S)); + } + + std::vector < bool > in_S_star(num_vertices(g), false); + typename std::vector < vertex_descriptor >::iterator si; + for (si = S_star.begin(); si != S_star.end(); ++si) + in_S_star[*si] = true; + degree_size_type c = 0; + for (si = S_star.begin(); si != S_star.end(); ++si) { + typename graph_traits < VertexListGraph >::out_edge_iterator ei, ei_end; + for (tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei) + if (!in_S_star[target(*ei, g)]) { + *disconnecting_set++ = *ei; + ++c; + } + } + + return c; + } + +} + +int +main() +{ + using namespace boost; + GraphvizGraph g; + read_graphviz("figs/edge-connectivity.dot", g); + + typedef graph_traits < GraphvizGraph >::edge_descriptor edge_descriptor; + typedef graph_traits < GraphvizGraph >::degree_size_type degree_size_type; + std::vector < edge_descriptor > disconnecting_set; + degree_size_type c = + edge_connectivity(g, std::back_inserter(disconnecting_set)); + + std::cout << "The edge connectivity is " << c << "." << std::endl; + + property_map < GraphvizGraph, vertex_attribute_t >::type + attr_map = get(vertex_attribute, g); + + std::cout << "The disconnecting set is {"; + for (std::vector < edge_descriptor >::iterator i = + disconnecting_set.begin(); i != disconnecting_set.end(); ++i) + std:: + cout << "(" << attr_map[source(*i, g)]["label"] << "," << + attr_map[target(*i, g)]["label"] << ") "; + std::cout << "}." << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/edge-function.cpp b/example/edge-function.cpp new file mode 100644 index 00000000..f009fdce --- /dev/null +++ b/example/edge-function.cpp @@ -0,0 +1,146 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + +template < typename Graph, typename VertexNameMap > void +output_adjacent_vertices(std::ostream & out, + typename graph_traits < Graph >::vertex_descriptor u, + const Graph & g, VertexNameMap name_map) +{ + typename graph_traits < Graph >::adjacency_iterator vi, vi_end; + out << get(name_map, u) << " -> { "; + for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) + out << get(name_map, *vi) << " "; + out << "}" << std::endl; +} + +template < typename NameMap > class name_equals_t { +public: + name_equals_t(const std::string & n, NameMap map) + : m_name(n), m_name_map(map) + { + } + template < typename Vertex > bool operator()(Vertex u) const + { + return get(m_name_map, u) == m_name; + } +private: + std::string m_name; + NameMap m_name_map; +}; + +// object generator function +template < typename NameMap > + inline name_equals_t < NameMap > +name_equals(const std::string & str, NameMap name) +{ + return name_equals_t < NameMap > (str, name); +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + graph_traits < graph_type >::vertex_descriptor yow, zag, bar; + // Get vertex name property map from the graph + typedef property_map < graph_type, vertex_name_t >::type name_map_t; + name_map_t name = get(vertex_name, g); + // Get iterators for the vertex set + graph_traits < graph_type >::vertex_iterator i, end; + tie(i, end) = vertices(g); + // Find yow.h + name_equals_t < name_map_t > predicate1("yow.h", name); + yow = *std::find_if(i, end, predicate1); + // Find zag.h + name_equals_t < name_map_t > predicate2("zag.cpp", name); + zag = *std::find_if(i, end, predicate2); + // Find bar.cpp + name_equals_t < name_map_t > predicate3("bar.cpp", name); + bar = *std::find_if(i, end, predicate3); + + graph_traits < graph_type >::edge_descriptor e1, e2; + bool exists; + + // Get the edge connecting yow.h to zag.cpp + tie(e1, exists) = edge(yow, zag, g); + assert(exists == true); + assert(source(e1, g) == yow); + assert(target(e1, g) == zag); + + // Discover that there is no edge connecting zag.cpp to bar.cpp + tie(e2, exists) = edge(zag, bar, g); + assert(exists == false); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/edge-iter-constructor.cpp b/example/edge-iter-constructor.cpp new file mode 100644 index 00000000..636d07be --- /dev/null +++ b/example/edge-iter-constructor.cpp @@ -0,0 +1,72 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +using namespace boost; + +template < typename T > + std::istream & operator >> (std::istream & in, std::pair < T, T > &p) { + in >> p.first >> p.second; + return in; +} + + +int +main() +{ + typedef + adjacency_list < + listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The graph is directed + > + graph_type; + + std::ifstream file_in("makefile-dependencies.dat"); + typedef + graph_traits < + graph_type >::vertices_size_type + size_type; + size_type + n_vertices; + file_in >> n_vertices; // read in number of vertices + + graph_type + g(n_vertices); // create graph with n vertices + + // Read in edges + graph_traits < graph_type >::vertices_size_type u, v; + while (file_in >> u) + if (file_in >> v) + add_edge(u, v, g); + else + break; + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/edmunds-karp-eg.cpp b/example/edmunds-karp-eg.cpp new file mode 100644 index 00000000..288f2cbf --- /dev/null +++ b/example/edmunds-karp-eg.cpp @@ -0,0 +1,70 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + + typedef adjacency_list_traits < vecS, vecS, directedS > Traits; + typedef adjacency_list < listS, vecS, directedS, + property < vertex_name_t, std::string >, + property < edge_capacity_t, long, + property < edge_residual_capacity_t, long, + property < edge_reverse_t, Traits::edge_descriptor > >>>Graph; + + Graph g; + + property_map < Graph, edge_capacity_t >::type + capacity = get(edge_capacity, g); + property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); + property_map < Graph, edge_residual_capacity_t >::type + residual_capacity = get(edge_residual_capacity, g); + + Traits::vertex_descriptor s, t; + read_dimacs_max_flow(g, capacity, rev, s, t); + + long flow = edmunds_karp_max_flow(g, s, t); + + std::cout << "c The total flow:" << std::endl; + std::cout << "s " << flow << std::endl << std::endl; + + std::cout << "c flow values:" << std::endl; + graph_traits < Graph >::vertex_iterator u_iter, u_end; + graph_traits < Graph >::out_edge_iterator ei, e_end; + for (tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) + for (tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) + if (capacity[*ei] > 0) + std::cout << "f " << *u_iter << " " << target(*ei, g) << " " + << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/family-tree-eg.cpp b/example/family-tree-eg.cpp new file mode 100644 index 00000000..9bd98e44 --- /dev/null +++ b/example/family-tree-eg.cpp @@ -0,0 +1,67 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +enum family +{ Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N }; +int +main() +{ + using namespace boost; + const char *name[] = { "Jeanie", "Debbie", "Rick", "John", "Amanda", + "Margaret", "Benjamin" + }; + + adjacency_list <> g(N); + add_edge(Jeanie, Debbie, g); + add_edge(Jeanie, Rick, g); + add_edge(Jeanie, John, g); + add_edge(Debbie, Amanda, g); + add_edge(Rick, Margaret, g); + add_edge(John, Benjamin, g); + + graph_traits < adjacency_list <> >::vertex_iterator i, end; + graph_traits < adjacency_list <> >::adjacency_iterator ai, a_end; + property_map < adjacency_list <>, vertex_index_t >::type + index_map = get(vertex_index, g); + + for (tie(i, end) = vertices(g); i != end; ++i) { + std::cout << name[get(index_map, *i)]; + tie(ai, a_end) = adjacent_vertices(*i, g); + if (ai == a_end) + std::cout << " has no children"; + else + std::cout << " is the parent of "; + for (; ai != a_end; ++ai) { + std::cout << name[get(index_map, *ai)]; + if (boost::next(ai) != a_end) + std::cout << ", "; + } + std::cout << std::endl; + } + return EXIT_SUCCESS; +} diff --git a/example/graph-assoc-types.cpp b/example/graph-assoc-types.cpp new file mode 100644 index 00000000..02aba57c --- /dev/null +++ b/example/graph-assoc-types.cpp @@ -0,0 +1,120 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +using namespace boost; + +template < typename Graph > void +generic_foo(Graph & g) +{ + // Access descriptor types + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + typedef typename graph_traits < Graph >::edge_descriptor Edge; + // Access category types + typedef typename graph_traits < Graph >::directed_category Dir; + typedef typename graph_traits < Graph >::edge_parallel_category Par; + // Access iterator types... + // Access size types... + // Now do something useful... +} + +template < typename Graph > void +generic_bar(Graph & g) +{ + // Declare some vertex and edge descriptor variables + typename graph_traits < Graph >::vertex_descriptor u, v; + typename graph_traits < Graph >::edge_descriptor e1, e2; + // Set u and e1 to valid descriptors... + v = u; // Make v a handle to the same vertex as u. + e2 = e1; // Make e2 a handle to the same edge as e1. + assert(u == v); // Do u and v identify the same vertex? Yes + assert(!(u != v)); // Do u and v identify different vertices? No + assert(e1 == e2); // Do e1 and e2 identify the same edge? Yes + assert(!(e1 != e2)); // Do e1 and e2 identify different edges? No +} + +// This version of foo gets called when g is directed +template < typename Graph > void +foo_dispatch(Graph & g, boost::directed_tag) +{ + //... +} + +// This version of foo gets called when g is undirected +template < typename Graph > void +foo_dispatch(Graph & g, boost::undirected_tag) +{ + //... +} + +template < typename Graph > void +foo(Graph & g) +{ + typedef typename boost::graph_traits < Graph >::directed_category Cat; + foo_dispatch(g, Cat()); +} + +template < typename Digraph > void +foo(Digraph & digraph, + typename graph_traits < Digraph >::vertex_descriptor u, + typename graph_traits < Digraph >::vertex_descriptor v) +{ + std::pair < typename graph_traits < Digraph >::edge_descriptor, bool > e1, + e2; + e1 = edge(u, v, digraph); + e2 = edge(v, u, digraph); + assert(e1.first != e2.first); +} +template < typename Undigraph > void +bar(Undigraph & undigraph, + typename graph_traits < Undigraph >::vertex_descriptor u, + typename graph_traits < Undigraph >::vertex_descriptor v) +{ + std::pair < typename graph_traits < Undigraph >::edge_descriptor, bool > e1, + e2; + e1 = edge(u, v, undigraph); + e2 = edge(v, u, undigraph); + assert(e1.first == e2.first); +} + + +int +main() +{ + + boost::adjacency_list < vecS, vecS, directedS > g(2); + add_edge(0, 1, g); + add_edge(1, 0, g); + generic_foo(g); + generic_bar(g); + foo(g); + foo(g, vertex(0, g), vertex(1, g)); + + boost::adjacency_list < vecS, vecS, undirectedS > ug(2); + add_edge(0, 1, g); + bar(ug, vertex(0, g), vertex(1, g)); + + return 0; +} diff --git a/example/graph-property-iter-eg.cpp b/example/graph-property-iter-eg.cpp new file mode 100644 index 00000000..18fd143d --- /dev/null +++ b/example/graph-property-iter-eg.cpp @@ -0,0 +1,46 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < listS, vecS, directedS, + property < vertex_name_t, std::string > >graph_t; + graph_t g(3); + + const char *vertex_names[] = { "Kubrick", "Clark", "Hal" }; + int i = 0; + graph_property_iter_range < graph_t, vertex_name_t >::iterator v, v_end; + for (tie(v, v_end) = get_property_iter_range(g, vertex_name); + v != v_end; ++v, ++i) + *v = vertex_names[i]; + + tie(v, v_end) = get_property_iter_range(g, vertex_name); + std::copy(v, v_end, std::ostream_iterator < std::string > (std::cout, " ")); + std::cout << std::endl; + return 0; +} diff --git a/example/incremental-components-eg.cpp b/example/incremental-components-eg.cpp new file mode 100644 index 00000000..2ddb8081 --- /dev/null +++ b/example/incremental-components-eg.cpp @@ -0,0 +1,81 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(int, char *[]) +{ + using namespace boost; + // Create a graph + typedef adjacency_list < vecS, vecS, undirectedS > Graph; + typedef graph_traits < Graph >::vertex_descriptor Vertex; + const int N = 6; + Graph G(N); + add_edge(0, 1, G); + add_edge(1, 4, G); + // create the disjoint-sets object, which requires rank and parent vertex properties + std::vector < Vertex > rank(num_vertices(G)); + std::vector < Vertex > parent(num_vertices(G)); + typedef std::vector < graph_traits < + Graph >::vertices_size_type >::iterator Rank; + typedef std::vector < Vertex >::iterator Parent; + disjoint_sets < Rank, Parent > ds(rank.begin(), parent.begin()); + + // determine the connected components, storing the results in the disjoint-sets object + initialize_incremental_components(G, ds); + incremental_components(G, ds); + + // Add a couple more edges and update the disjoint-sets + graph_traits < Graph >::edge_descriptor e; + bool flag; + tie(e, flag) = add_edge(4, 0, G); + ds.union_set(4, 0); + tie(e, flag) = add_edge(2, 5, G); + ds.union_set(2, 5); + + graph_traits < Graph >::vertex_iterator i, end; + for (tie(i, end) = vertices(G); i != end; ++i) + std::cout << "representative[" << *i << "] = " << + ds.find_set(*i) << std::endl;; + std::cout << std::endl; + + typedef component_index < unsigned int >Components; + Components components(parent.begin(), parent.end()); + for (Components::size_type i = 0; i < components.size(); ++i) { + std::cout << "component " << i << " contains: "; + for (Components::value_type::iterator j = components[i].begin(); + j != components[i].end(); ++j) + std::cout << *j << " "; + std::cout << std::endl; + } + + return EXIT_SUCCESS; +} diff --git a/example/incremental_components.cpp b/example/incremental_components.cpp index d5bf792a..d99209b7 100644 --- a/example/incremental_components.cpp +++ b/example/incremental_components.cpp @@ -107,7 +107,7 @@ int main(int , char* []) ds.find_set(*i) << endl;; cout << endl; - typedef component_index Components; + typedef component_index Components; Components components(&parent[0], &parent[0] + parent.size()); for (Components::size_type c = 0; c < components.size(); ++c) { diff --git a/example/iterator-property-map-eg.cpp b/example/iterator-property-map-eg.cpp new file mode 100644 index 00000000..98c499cc --- /dev/null +++ b/example/iterator-property-map-eg.cpp @@ -0,0 +1,37 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +int +main() +{ + using namespace boost; + double x[] = { 0.2, 4.5, 3.2 }; + iterator_property_map < double *, identity_property_map > pmap(x); + std::cout << "x[1] = " << get(pmap, 1) << std::endl; + put(pmap, 0, 1.7); + std::cout << "x[0] = " << pmap[0] << std::endl; + return 0; +} diff --git a/example/johnson-eg.cpp b/example/johnson-eg.cpp new file mode 100644 index 00000000..5fbcd000 --- /dev/null +++ b/example/johnson-eg.cpp @@ -0,0 +1,92 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, directedS, no_property, + property < edge_weight_t, int, property < edge_weight2_t, int >>>Graph; + const int V = 6; + typedef std::pair < int, int >Edge; + Edge edge_array[] = + { Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(0, 5), + Edge(1, 2), Edge(1, 5), Edge(1, 3), Edge(2, 4), Edge(2, 5), + Edge(3, 2), Edge(4, 3), Edge(4, 1), Edge(5, 4) + }; + const int E = sizeof(edge_array) / sizeof(Edge); + Graph g(V, edge_array, edge_array + E); + + property_map < Graph, edge_weight_t >::type w = get(edge_weight, g); + int weights[] = { 0, 0, 0, 0, 0, 3, -4, 8, 1, 7, 4, -5, 2, 6 }; + int *wp = weights; + + graph_traits < Graph >::edge_iterator e, e_end; + for (boost::tie(e, e_end) = edges(g); e != e_end; ++e) + w[*e] = *wp++; + + std::vector < int >d(V, std::numeric_limits < int >::max()); + int D[V][V]; + johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0])); + + + std::cout << " "; + for (int k = 0; k < V; ++k) + std::cout << k << " "; + std::cout << std::endl; + for (int i = 0; i < V; ++i) { + std::cout << i << " -> "; + for (int j = 0; j < V; ++j) { + if (D[i][j] > 20 || D[i][j] < -20) + std::cout << "inf "; + else + std::cout << D[i][j] << " "; + } + std::cout << std::endl; + } + + std::ofstream fout("figs/johnson-eg.dot"); + fout << "digraph A {\n" + << " rankdir=LR\n" + << "size=\"5,3\"\n" + << "ratio=\"fill\"\n" + << "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n"; + + graph_traits < Graph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) + fout << source(*ei, g) << " -> " << target(*ei, g) + << "[label=" << get(edge_weight, g)[*ei] << "]\n"; + + fout << "}\n"; + return 0; +} diff --git a/example/kevin-bacon.cpp b/example/kevin-bacon.cpp new file mode 100644 index 00000000..f8135ed0 --- /dev/null +++ b/example/kevin-bacon.cpp @@ -0,0 +1,132 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename DistanceMap > class bacon_number_recorder:public default_bfs_visitor +{ +public: +bacon_number_recorder(DistanceMap dist):d(dist) { + } + + template < typename Edge, typename Graph > + void tree_edge(Edge e, const Graph & g) const + { + typename graph_traits < Graph >::vertex_descriptor + u = source(e, g), v = target(e, g); + d[v] = d[u] + 1; + } +private: + DistanceMap d; +}; + +// Convenience function +template < typename DistanceMap > + bacon_number_recorder < DistanceMap > record_bacon_number(DistanceMap d) +{ + return bacon_number_recorder < DistanceMap > (d); +} + + +int +main() +{ + std::ifstream datafile("./kevin-bacon.dat"); + if (!datafile) { + cerr << "No ./kevin-bacon.dat file" << endl; + return EXIT_FAILURE; + } + + typedef adjacency_list < vecS, vecS, undirectedS, property < vertex_name_t, + string >, property < edge_name_t, string > >Graph; + Graph g; + + typedef property_map < Graph, vertex_name_t >::type actor_name_map_t; + actor_name_map_t actor_name = get(vertex_name, g); + typedef property_map < Graph, edge_name_t >::type movie_name_map_t; + movie_name_map_t connecting_movie = get(edge_name, g); + + typedef graph_traits < Graph >::vertex_descriptor Vertex; + typedef std::map < string, Vertex > NameVertexMap; + NameVertexMap actors; + + for (std::string line; std::getline(datafile, line);) { + char_delimiters_separator < char >sep(false, "", ";"); + tokenizer <> line_toks(line, sep); + tokenizer <>::iterator i = line_toks.begin(); + std::string actors_name = *i++; + NameVertexMap::iterator pos; + bool inserted; + Vertex u, v; + tie(pos, inserted) = actors.insert(make_pair(actors_name, Vertex())); + if (inserted) { + u = add_vertex(g); + actor_name[u] = actors_name; + pos->second = u; + } else + u = pos->second; + + std::string movie_name = *i++; + + tie(pos, inserted) = actors.insert(make_pair(*i, Vertex())); + if (inserted) { + v = add_vertex(g); + actor_name[v] = *i; + pos->second = v; + } else + v = pos->second; + + graph_traits < Graph >::edge_descriptor e; + tie(e, inserted) = add_edge(u, v, g); + if (inserted) + connecting_movie[e] = movie_name; + + } + + std::vector < int >bacon_number(num_vertices(g)); + + Vertex src = actors["Kevin Bacon"]; + bacon_number[src] = 0; + + breadth_first_search(g, src, + visitor(record_bacon_number(&bacon_number[0]))); + + graph_traits < Graph >::vertex_iterator i, end; + for (tie(i, end) = vertices(g); i != end; ++i) { + std::cout << actor_name[*i] << " has a Bacon number of " + << bacon_number[*i] << std::endl; + } + + return 0; +} diff --git a/example/knights-tour.cpp b/example/knights-tour.cpp new file mode 100644 index 00000000..292ed3cc --- /dev/null +++ b/example/knights-tour.cpp @@ -0,0 +1,358 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; + +typedef +std::pair < int, int > + Position; +Position + knight_jumps[8] = { + Position(2, -1), + Position(1, -2), + Position(-1, -2), + Position(-2, -1), + Position(-2, 1), + Position(-1, 2), + Position(1, 2), + Position(2, 1) +}; + + +Position +operator + (const Position & p1, const Position & p2) +{ + return Position(p1.first + p2.first, p1.second + p2.second); +} + +struct knights_tour_graph; +struct knight_adjacency_iterator: + public + boost::forward_iterator_helper < + knight_adjacency_iterator, + Position, + std::ptrdiff_t, + Position *, + Position > +{ + knight_adjacency_iterator() + { + } + knight_adjacency_iterator(int ii, Position p, const knights_tour_graph & g) + : + m_pos(p), + m_g(&g), + m_i(ii) + { + valid_position(); + } + Position operator *() const + { + return + m_pos + + knight_jumps[m_i]; + } + void + operator++ () + { + ++m_i; + valid_position(); + } + bool + operator == (const knight_adjacency_iterator & x) const { + return + m_i == + x. + m_i; + } +protected: + void + valid_position(); + Position + m_pos; + const knights_tour_graph * + m_g; + int + m_i; +}; + +struct knights_tour_graph +{ + typedef Position + vertex_descriptor; + typedef + std::pair < + vertex_descriptor, + vertex_descriptor > + edge_descriptor; + typedef knight_adjacency_iterator + adjacency_iterator; + typedef void + out_edge_iterator; + typedef void + in_edge_iterator; + typedef void + edge_iterator; + typedef void + vertex_iterator; + typedef int + degree_size_type; + typedef int + vertices_size_type; + typedef int + edges_size_type; + typedef directed_tag + directed_category; + typedef disallow_parallel_edge_tag + edge_parallel_category; + typedef adjacency_graph_tag + traversal_category; + knights_tour_graph(int n): + m_board_size(n) + { + } + int + m_board_size; +}; +int +num_vertices(const knights_tour_graph & g) +{ + return g.m_board_size * g.m_board_size; +} + +void +knight_adjacency_iterator::valid_position() +{ + Position new_pos = m_pos + knight_jumps[m_i]; + while (m_i < 8 && (new_pos.first < 0 || new_pos.second < 0 + || new_pos.first >= m_g->m_board_size + || new_pos.second >= m_g->m_board_size)) { + ++m_i; + new_pos = m_pos + knight_jumps[m_i]; + } +} + + +std::pair < knights_tour_graph::adjacency_iterator, + knights_tour_graph::adjacency_iterator > +adjacent_vertices(knights_tour_graph::vertex_descriptor v, + const knights_tour_graph & g) +{ + typedef knights_tour_graph::adjacency_iterator Iter; + return std::make_pair(Iter(0, v, g), Iter(8, v, g)); +} + + +struct compare_first +{ + template < typename P > bool operator() (const P & x, const P & y) + { + return x.first < y.first; + } +}; + +template < typename Graph, typename TimePropertyMap > + bool backtracking_search(Graph & g, + typename graph_traits < + Graph >::vertex_descriptor src, + TimePropertyMap time_map) +{ + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + typedef std::pair < int, Vertex > P; + std::stack < P > S; + int time_stamp = 0; + + S.push(std::make_pair(time_stamp, src)); + while (!S.empty()) { + Vertex x; + tie(time_stamp, x) = S.top(); + put(time_map, x, time_stamp); + // all vertices have been visited, success! + if (time_stamp == num_vertices(g) - 1) + return true; + + bool deadend = true; + typename graph_traits < Graph >::adjacency_iterator i, end; + for (tie(i, end) = adjacent_vertices(x, g); i != end; ++i) + if (get(time_map, *i) == -1) { + S.push(std::make_pair(time_stamp + 1, *i)); + deadend = false; + } + + if (deadend) { + put(time_map, x, -1); + S.pop(); + tie(time_stamp, x) = S.top(); + while (get(time_map, x) != -1) { // unwind stack to last unexplored vertex + put(time_map, x, -1); + S.pop(); + tie(time_stamp, x) = S.top(); + } + } + + } // while (!S.empty()) + return false; +} + +template < typename Vertex, typename Graph, typename TimePropertyMap > int +number_of_successors(Vertex x, Graph & g, TimePropertyMap time_map) +{ + int s_x = 0; + typename graph_traits < Graph >::adjacency_iterator i, end; + for (tie(i, end) = adjacent_vertices(x, g); i != end; ++i) + if (get(time_map, *i) == -1) + ++s_x; + return s_x; +} + +template < typename Graph, typename TimePropertyMap > + bool warnsdorff(Graph & g, + typename graph_traits < Graph >::vertex_descriptor src, + TimePropertyMap time_map) +{ + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + typedef std::pair < int, Vertex > P; + std::stack < P > S; + int time_stamp = 0; + + S.push(std::make_pair(time_stamp, src)); + while (!S.empty()) { + Vertex x; + tie(time_stamp, x) = S.top(); + put(time_map, x, time_stamp); + // all vertices have been visited, success! + if (time_stamp == num_vertices(g) - 1) + return true; + + // Put adjacent vertices into a local priority queue + std::priority_queue < P, std::vector < P >, compare_first > Q; + typename graph_traits < Graph >::adjacency_iterator i, end; + int num_succ; + for (tie(i, end) = adjacent_vertices(x, g); i != end; ++i) + if (get(time_map, *i) == -1) { + num_succ = number_of_successors(*i, g, time_map); + Q.push(std::make_pair(num_succ, *i)); + } + bool deadend = Q.empty(); + // move vertices from local priority queue to the stack + for (; !Q.empty(); Q.pop()) { + tie(num_succ, x) = Q.top(); + S.push(std::make_pair(time_stamp + 1, x)); + } + if (deadend) { + put(time_map, x, -1); + S.pop(); + tie(time_stamp, x) = S.top(); + while (get(time_map, x) != -1) { // unwind stack to last unexplored vertex + put(time_map, x, -1); + S.pop(); + tie(time_stamp, x) = S.top(); + } + } + + } // while (!S.empty()) + return false; +} + + +struct board_map +{ + typedef int value_type; + typedef Position key_type; + typedef read_write_property_map_tag category; + board_map(int *b, int n):m_board(b), m_size(n) + { + } + friend int get(const board_map & ba, Position p); + friend void put(const board_map & ba, Position p, int v); + friend std::ostream & operator << (std::ostream & os, const board_map & ba); +private: + int *m_board; + int m_size; +}; + +int +get(const board_map & ba, Position p) +{ + return ba.m_board[p.first * ba.m_size + p.second]; +} + +void +put(const board_map & ba, Position p, int v) +{ + ba.m_board[p.first * ba.m_size + p.second] = v; +} + +std::ostream & operator << (std::ostream & os, const board_map & ba) { + for (int i = 0; i < ba.m_size; ++i) { + for (int j = 0; j < ba.m_size; ++j) + os << get(ba, Position(i, j)) << "\t"; + os << std::endl; + } + return os; +} + +int +main(int argc, char *argv[]) +{ + int + N; + if (argc == 2) + N = atoi(argv[1]); + else + N = 8; + + knights_tour_graph + g(N); + int * + board = + new int[num_vertices(g)]; + board_map + chessboard(board, N); + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + put(chessboard, Position(i, j), -1); + + bool + ret = + warnsdorff(g, Position(0, 0), chessboard); + + if (ret) + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) + std::cout << get(chessboard, Position(i, j)) << "\t"; + std::cout << std::endl; + } else + std::cout << "method failed" << std::endl; + return 0; +} diff --git a/example/koenig-lookup.cpp b/example/koenig-lookup.cpp new file mode 100644 index 00000000..3961e8b8 --- /dev/null +++ b/example/koenig-lookup.cpp @@ -0,0 +1,72 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +namespace lib_jack +{ + class graph + { /* ... */ + }; + int num_vertices(const graph &) + { /* ... */ + } +} +namespace lib_jill +{ + class graph + { /* ... */ + }; + int num_vertices(const graph &) + { /* ... */ + } +} + +namespace boost +{ + template <> struct graph_traits + { + typedef int vertices_size_type; + }; + template <> struct graph_traits + { + typedef int vertices_size_type; + }; +} + +namespace boost +{ + template < typename Graph > void pail(Graph & g) + { + typename graph_traits < Graph >::vertices_size_type N = num_vertices(g); // Koenig lookup will resolve + // ... + } +} // namespace boost + +int +main() +{ + lib_jack::graph g1; + boost::pail(g1); + lib_jill::graph g2; + boost::pail(g2); +} diff --git a/example/kruskal-example.cpp b/example/kruskal-example.cpp new file mode 100644 index 00000000..edaff72a --- /dev/null +++ b/example/kruskal-example.cpp @@ -0,0 +1,75 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, undirectedS, + no_property, property < edge_weight_t, int >>Graph; + typedef graph_traits < Graph >::edge_descriptor Edge; + typedef graph_traits < Graph >::vertex_descriptor Vertex; + typedef std::pair < int, int >E; + + const int num_nodes = 5; + E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), + E(3, 4), E(4, 0), E(4, 1) + }; + int weights[] = { 1, 1, 2, 7, 3, 1, 1, 1 }; + int num_edges = sizeof(edge_array) / sizeof(E); + Graph g(num_nodes, edge_array, edge_array + num_edges, weights); + property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); + std::vector < Edge > spanning_tree; + + kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree)); + + std::cout << "Print the edges in the MST:" << std::endl; + for (std::vector < Edge >::iterator ei = spanning_tree.begin(); + ei != spanning_tree.end(); ++ei) { + std::cout << source(*ei, g) << " <--> " << target(*ei, g) + << " with weight of " << weight[*ei] + << std::endl; + } + + std::ofstream fout("figs/kruskal-eg.dot"); + fout << "graph A {\n" + << " rankdir=LR\n" + << " size=\"3,3\"\n" + << " ratio=\"filled\"\n" + << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; + graph_traits < Graph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { + fout << source(*ei, g) << " -- " << target(*ei, g); + if (std::find(spanning_tree.begin(), spanning_tree.end(), *ei) + != spanning_tree.end()) + fout << "[color=\"black\"]\n"; + else + fout << "[color=\"gray\"]\n"; + } + fout << "}\n"; + return EXIT_SUCCESS; +} diff --git a/example/kruskal-telephone.cpp b/example/kruskal-telephone.cpp new file mode 100644 index 00000000..bd10c2a1 --- /dev/null +++ b/example/kruskal-telephone.cpp @@ -0,0 +1,71 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + GraphvizGraph g_dot; + read_graphviz("figs/telephone-network.dot", g_dot); + + typedef adjacency_list < vecS, vecS, undirectedS, no_property, + property < edge_weight_t, int >>Graph; + Graph g(num_vertices(g_dot)); + property_map < GraphvizGraph, edge_attribute_t >::type + edge_attr_map = get(edge_attribute, g_dot); + graph_traits < GraphvizGraph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { + int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); + property < edge_weight_t, int >edge_property(weight); + add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + } + + std::vector < graph_traits < Graph >::edge_descriptor > mst; + kruskal_minimum_spanning_tree(g, std::back_inserter(mst)); + + property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); + int total_weight = 0; + for (int e = 0; e < mst.size(); ++e) + total_weight += get(weight, mst[e]); + std::cout << "total weight: " << total_weight << std::endl; + + typedef graph_traits < Graph >::vertex_descriptor Vertex; + for (int i = 0; i < mst.size(); ++i) { + Vertex u = source(mst[i], g), v = target(mst[i], g); + edge_attr_map[edge(u, v, g_dot).first]["color"] = "black"; + } + std::ofstream out("figs/telephone-mst-kruskal.dot"); + graph_property < GraphvizGraph, graph_edge_attribute_t >::type & + graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); + graph_edge_attr_map["color"] = "gray"; + graph_edge_attr_map["style"] = "bold"; + write_graphviz(out, g_dot); + + return EXIT_SUCCESS; +} diff --git a/example/last-mod-time.cpp b/example/last-mod-time.cpp new file mode 100644 index 00000000..71a16e69 --- /dev/null +++ b/example/last-mod-time.cpp @@ -0,0 +1,106 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + // Create storage for last modified times + std::vector < time_t > last_mod_vec(num_vertices(g)); + // Create nickname for the property map type + typedef iterator_property_map < std::vector < time_t >::iterator, + property_map < graph_type, vertex_index_t >::type > iter_map_t; + // Create last modified time property map + iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g)); + + property_map < graph_type, vertex_name_t >::type name = get(vertex_name, g); + struct stat stat_buf; + graph_traits < graph_type >::vertex_descriptor u; + typedef graph_traits < graph_type >::vertex_iterator vertex_iter_t; + std::pair < vertex_iter_t, vertex_iter_t > p; + for (p = vertices(g); p.first != p.second; ++p.first) { + u = *p.first; + if (stat(name[u].c_str(), &stat_buf) != 0) + std::cerr << "error in stat() for file " << name[u] << std::endl; + put(mod_time_map, u, stat_buf.st_mtime); + } + + for (p = vertices(g); p.first != p.second; ++p.first) { + std::cout << name[*p.first] << " was last modified at " + << ctime(&mod_time_map[*p.first]); + } + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/leda-concept-check.cpp b/example/leda-concept-check.cpp new file mode 100644 index 00000000..e4ac7398 --- /dev/null +++ b/example/leda-concept-check.cpp @@ -0,0 +1,36 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +int +main() +{ + typedef GRAPH < int, int >Graph; + function_requires < VertexListGraphConcept < Graph > >(); + function_requires < BidirectionalGraphConcept < Graph > >(); + function_requires < VertexMutableGraphConcept < Graph > >(); + function_requires < EdgeMutableGraphConcept < Graph > >(); + return EXIT_SUCCESS; +} diff --git a/example/leda-graph-eg.cpp b/example/leda-graph-eg.cpp new file mode 100644 index 00000000..57a79aa2 --- /dev/null +++ b/example/leda-graph-eg.cpp @@ -0,0 +1,44 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#undef string // LEDA macro! +int +main() +{ + using namespace boost; + typedef GRAPH < std::string, int >graph_t; + graph_t g; + g.new_node("Philoctetes"); + g.new_node("Heracles"); + g.new_node("Alcmena"); + g.new_node("Eurystheus"); + g.new_node("Amphitryon"); + typedef property_map < graph_t, vertex_all_t >::type NodeMap; + NodeMap node_name_map = get(vertex_all, g); + graph_traits < graph_t >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + std::cout << node_name_map[*vi] << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/lic.cpp b/example/lic.cpp new file mode 100644 index 00000000..9c311ae8 --- /dev/null +++ b/example/lic.cpp @@ -0,0 +1,46 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= diff --git a/example/loops_dfs.cpp b/example/loops_dfs.cpp new file mode 100644 index 00000000..fdff8243 --- /dev/null +++ b/example/loops_dfs.cpp @@ -0,0 +1,165 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename OutputIterator > class back_edge_recorder:public default_dfs_visitor +{ +public: +back_edge_recorder(OutputIterator out):m_out(out) { + } + template < typename Edge, typename Graph > + void back_edge(Edge e, const Graph &) + { + *m_out++ = e; + } +private: + OutputIterator m_out; +}; + +// object generator function +template < typename OutputIterator > + back_edge_recorder < OutputIterator > +make_back_edge_recorder(OutputIterator out) +{ + return back_edge_recorder < OutputIterator > (out); +} + +template < typename Graph, typename Loops > void +find_loops(typename graph_traits < Graph >::vertex_descriptor entry, const Graph & g, Loops & loops) // A container of sets of vertices +{ + function_requires < BidirectionalGraphConcept < Graph > >(); + typedef typename graph_traits < Graph >::edge_descriptor Edge; + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + std::vector < Edge > back_edges; + std::vector < default_color_type > color_map(num_vertices(g)); + depth_first_visit(g, entry, + make_back_edge_recorder(std::back_inserter(back_edges)), + make_iterator_property_map(color_map.begin(), + get(vertex_index, g))); + + for (std::vector < Edge >::size_type i = 0; i < back_edges.size(); ++i) { + loops.push_back(typename Loops::value_type()); + compute_loop_extent(back_edges[i], g, loops.back()); + } + +} + +template < typename Graph, typename Set > void +compute_loop_extent(typename graph_traits < + Graph >::edge_descriptor back_edge, const Graph & g, + Set & loop_set) +{ + function_requires < BidirectionalGraphConcept < Graph > >(); + typedef typename graph_traits < Graph >::vertex_descriptor Vertex; + typedef color_traits < default_color_type > Color; + + Vertex loop_head, loop_tail, u, v; + loop_tail = source(back_edge, g); + loop_head = target(back_edge, g); + + std::vector < default_color_type > + reachable_from_head(num_vertices(g), Color::white()); + depth_first_visit(g, loop_head, default_dfs_visitor(), + make_iterator_property_map(reachable_from_head.begin(), + get(vertex_index, g))); + + std::vector < default_color_type > reachable_to_tail(num_vertices(g)); + reverse_graph < Graph > reverse_g(g); + depth_first_visit(reverse_g, loop_tail, default_dfs_visitor(), + make_iterator_property_map(reachable_to_tail.begin(), + get(vertex_index, g))); + + typename graph_traits < Graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + if (reachable_from_head[*vi] != Color::white() + && reachable_to_tail[*vi] != Color::white()) + loop_set.insert(*vi); + +} + + +int +main(int argc, char *argv[]) +{ + if (argc < 3) { + std::cerr << "usage: loops_dfs " << std::endl; + return -1; + } + GraphvizDigraph g_in; + read_graphviz(argv[1], g_in); + + typedef adjacency_list < vecS, vecS, bidirectionalS, + GraphvizVertexProperty, + GraphvizEdgeProperty, GraphvizGraphProperty > Graph; + typedef graph_traits < Graph >::vertex_descriptor Vertex; + + Graph g; + get_property(g, graph_name) = "loops"; + copy_graph(g_in, g); + + typedef std::set < Vertex > set_t; + typedef std::list < set_t > list_of_sets_t; + list_of_sets_t loops; + + Vertex entry = *vertices(g).first; + + find_loops(entry, g, loops); + + for (list_of_sets_t::iterator i = loops.begin(); i != loops.end(); ++i) { + std::vector < bool > in_loop(num_vertices(g), false); + for (set_t::iterator j = (*i).begin(); j != (*i).end(); ++j) { + get(vertex_attribute, g)[*j]["color"] = "gray"; + in_loop[*j] = true; + } + graph_traits < Graph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) + if (in_loop[source(*ei, g)] && in_loop[target(*ei, g)]) + get(edge_attribute, g)[*ei]["color"] = "gray"; + } + + get_property(g, graph_graph_attribute)["size"] = "3,3"; + get_property(g, graph_graph_attribute)["ratio"] = "fill"; + get_property(g, graph_vertex_attribute)["shape"] = "box"; + + std::ofstream loops_out(argv[2]); + write_graphviz(loops_out, g, + make_vertex_attributes_writer(g), + make_edge_attributes_writer(g), + make_graph_attributes_writer(g)); + + + return 0; +} diff --git a/example/ospf-example.cpp b/example/ospf-example.cpp new file mode 100644 index 00000000..323dacb3 --- /dev/null +++ b/example/ospf-example.cpp @@ -0,0 +1,98 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include // for file I/O +#include // for read/write_graphviz() +#include +#include +int +main() +{ + using namespace boost; + GraphvizDigraph g_dot; + read_graphviz("figs/ospf-graph.dot", g_dot); + + typedef adjacency_list < vecS, vecS, directedS, no_property, + property < edge_weight_t, int >>Graph; + typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor; + Graph g(num_vertices(g_dot)); + property_map < GraphvizDigraph, edge_attribute_t >::type + edge_attr_map = get(edge_attribute, g_dot); + graph_traits < GraphvizDigraph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { + int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); + property < edge_weight_t, int >edge_property(weight); + add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + } + + vertex_descriptor router_six; + property_map < GraphvizDigraph, vertex_attribute_t >::type + vertex_attr_map = get(vertex_attribute, g_dot); + graph_traits < GraphvizDigraph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi) + if ("RT6" == vertex_attr_map[*vi]["label"]) { + router_six = *vi; + break; + } + + std::vector < vertex_descriptor > parent(num_vertices(g)); + // All vertices start out as there own parent + typedef graph_traits < Graph >::vertices_size_type size_type; + for (size_type p = 0; p < num_vertices(g); ++p) + parent[p] = p; + + dijkstra_shortest_paths(g, router_six, predecessor_map(&parent[0])); + + graph_traits < GraphvizDigraph >::edge_descriptor e; + for (size_type i = 0; i < num_vertices(g); ++i) + if (parent[i] != i) { + e = edge(parent[i], i, g_dot).first; + edge_attr_map[e]["color"] = "black"; + } + + graph_property < GraphvizDigraph, graph_edge_attribute_t >::type & + graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); + graph_edge_attr_map["color"] = "grey"; + write_graphviz("figs/ospf-sptree.dot", g_dot); + + std::ofstream rtable("routing-table.dat"); + rtable << "Dest Next Hop Total Cost" << std::endl; + for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi) + if (parent[*vi] != *vi) { + rtable << vertex_attr_map[*vi]["label"] << " "; + vertex_descriptor v = *vi, child; + int path_cost = 0; + property_map < Graph, edge_weight_t >::type + weight_map = get(edge_weight, g); + do { + path_cost += get(weight_map, edge(parent[v], v, g).first); + child = v; + v = parent[v]; + } while (v != parent[v]); + rtable << vertex_attr_map[child]["label"] << " "; + rtable << path_cost << std::endl; + + } + + return EXIT_SUCCESS; +} diff --git a/example/parallel-compile-time.cpp b/example/parallel-compile-time.cpp new file mode 100644 index 00000000..ca89aa15 --- /dev/null +++ b/example/parallel-compile-time.cpp @@ -0,0 +1,259 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include // for default_dfs_visitor + +namespace std +{ + template < typename T > + std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return in; + } +} + +namespace boost +{ + enum vertex_compile_cost_t + { vertex_compile_cost = 111 }; // a unique # + BOOST_INSTALL_PROPERTY(vertex, compile_cost); +} + +using namespace boost; + +typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + listS, // Store vertex set in a std::list + directedS, // The file dependency graph is directed + // vertex properties + property < vertex_name_t, + std::string, + property < + vertex_compile_cost_t, float, + property < + vertex_distance_t, float, + property < + vertex_color_t, +default_color_type > >>>, + // an edge property + property < +edge_weight_t, float >> + file_dep_graph2; + +typedef + graph_traits < + file_dep_graph2 >::vertex_descriptor + vertex_t; +typedef + graph_traits < + file_dep_graph2 >::edge_descriptor + edge_t; + + +template < typename Graph, typename ColorMap, typename Visitor > void +dfs_v2(const Graph & g, + typename graph_traits < Graph >::vertex_descriptor u, + ColorMap color, Visitor vis) +{ + typedef typename + property_traits < + ColorMap >::value_type + color_type; + typedef + color_traits < + color_type > + ColorT; + color[u] = ColorT::gray(); + vis.discover_vertex(u, g); + typename + graph_traits < + Graph >::out_edge_iterator + ei, + ei_end; + for (tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) + if (color[target(*ei, g)] == ColorT::white()) { + vis.tree_edge(*ei, g); + dfs_v2(g, target(*ei, g), color, vis); + } else if (color[target(*ei, g)] == ColorT::gray()) + vis.back_edge(*ei, g); + else + vis.forward_or_cross_edge(*ei, g); + color[u] = ColorT::black(); + vis.finish_vertex(u, g); +} + +template < typename Graph, typename Visitor, typename ColorMap > void +generic_dfs_v2(const Graph & g, Visitor vis, ColorMap color) +{ + typedef + color_traits < + typename + property_traits < + ColorMap >::value_type > + ColorT; + typename + graph_traits < + Graph >::vertex_iterator + vi, + vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + color[*vi] = ColorT::white(); + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + if (color[*vi] == ColorT::white()) + dfs_v2(g, *vi, color, vis); +} + + +template < typename OutputIterator > struct topo_visitor: + public + default_dfs_visitor +{ + topo_visitor(OutputIterator & order): + topo_order(order) + { + } + template < + typename + Graph > void + finish_vertex(typename graph_traits < Graph >::vertex_descriptor u, + const Graph &) + { + *topo_order++ = u; + } + OutputIterator & topo_order; +}; + +template < typename Graph, typename OutputIterator, typename ColorMap > void +topo_sort(const Graph & g, OutputIterator topo_order, ColorMap color) +{ + topo_visitor < OutputIterator > vis(topo_order); + generic_dfs_v2(g, vis, color); +} + + +typedef + property_map < + file_dep_graph2, + vertex_name_t >::type + name_map_t; +typedef + property_map < + file_dep_graph2, + vertex_compile_cost_t >::type + compile_cost_map_t; +typedef + property_map < + file_dep_graph2, + vertex_distance_t >::type + distance_map_t; +typedef + property_map < + file_dep_graph2, + vertex_color_t >::type + color_map_t; + + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef + graph_traits < + file_dep_graph2 >::vertices_size_type + size_type; + size_type + n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph2 + g(input_begin, input_end, n_vertices); + + name_map_t + name_map = + get(vertex_name, g); + compile_cost_map_t + compile_cost_map = + get(vertex_compile_cost, g); + distance_map_t + distance_map = + get(vertex_distance, g); + color_map_t + color_map = + get(vertex_color, g); + + { + std::ifstream name_in("makefile-target-names.dat"); + std::ifstream compile_cost_in("target-compile-costs.dat"); + graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + name_in >> name_map[*vi]; + compile_cost_in >> compile_cost_map[*vi]; + } + + } + std::vector < vertex_t > topo_order(num_vertices(g)); + topo_sort(g, topo_order.rbegin(), color_map); + + graph_traits < file_dep_graph2 >::vertex_iterator i, i_end; + graph_traits < file_dep_graph2 >::adjacency_iterator vi, vi_end; + + // find source vertices with zero in-degree by marking all vertices with incoming edges + for (tie(i, i_end) = vertices(g); i != i_end; ++i) + color_map[*i] = white_color; + for (tie(i, i_end) = vertices(g); i != i_end; ++i) + for (tie(vi, vi_end) = adjacent_vertices(*i, g); vi != vi_end; ++vi) + color_map[*vi] = black_color; + + // initialize distances to zero, or for source vertices, to the compile cost + for (tie(i, i_end) = vertices(g); i != i_end; ++i) + if (color_map[*i] == white_color) + distance_map[*i] = compile_cost_map[*i]; + else + distance_map[*i] = 0; + + std::vector < vertex_t >::iterator ui; + for (ui = topo_order.begin(); ui != topo_order.end(); ++ui) { + vertex_t + u = * + ui; + for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) + if (distance_map[*vi] < distance_map[u] + compile_cost_map[*vi]) + distance_map[*vi] = distance_map[u] + compile_cost_map[*vi]; + } + + graph_property_iter_range < file_dep_graph2, + vertex_distance_t >::iterator ci, ci_end; + tie(ci, ci_end) = get_property_iter_range(g, vertex_distance); + std::cout << "total (parallel) compile time: " + << *std::max_element(ci, ci_end) << std::endl; + + return 0; +} diff --git a/example/prim-example.cpp b/example/prim-example.cpp new file mode 100644 index 00000000..4126d31e --- /dev/null +++ b/example/prim-example.cpp @@ -0,0 +1,54 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, undirectedS, + property < vertex_distance_t, int, property < edge_weight_t, int >>>Graph; + typedef std::pair < int, int >E; + const int num_nodes = 5; + E edges[] = { E(0, 2), E(1, 1), E(1, 3), E(1, 4), E(2, 1), E(2, 3), + E(3, 4), E(4, 0) + }; + int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1 }; + Graph g(num_nodes, edges, edges + sizeof(edges) / sizeof(E), weights); + std::vector < graph_traits < Graph >::vertex_descriptor > + p(num_vertices(G)); + + prim_minimum_spanning_tree(g, &p[0]); + + std::vector < Graph::vertex_descriptor >::iterator i; + for (i = p.begin(); i != p.end(); ++i) + cout << "parent[" << i - p.begin() + << "] = " << identity_property_map()[*i] << endl; + else + cout << "parent[" << i - p.begin() << "] = no parent" << endl; + + return EXIT_SUCCESS; +} diff --git a/example/prim-telephone.cpp b/example/prim-telephone.cpp new file mode 100644 index 00000000..746ee247 --- /dev/null +++ b/example/prim-telephone.cpp @@ -0,0 +1,70 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +int +main() +{ + using namespace boost; + GraphvizGraph g_dot; + read_graphviz("figs/telephone-network.dot", g_dot); + + typedef adjacency_list < vecS, vecS, undirectedS, no_property, + property < edge_weight_t, int >>Graph; + Graph g(num_vertices(g_dot)); + property_map < GraphvizGraph, edge_attribute_t >::type + edge_attr_map = get(edge_attribute, g_dot); + graph_traits < GraphvizGraph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { + int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); + property < edge_weight_t, int >edge_property(weight); + add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + } + + typedef graph_traits < Graph >::vertex_descriptor Vertex; + std::vector < Vertex > parent(num_vertices(g)); + prim_minimum_spanning_tree(g, &parent[0]); + + property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); + int total_weight = 0; + for (int v = 0; v < num_vertices(g); ++v) + if (parent[v] != v) + total_weight += get(weight, edge(parent[v], v, g).first); + std::cout << "total weight: " << total_weight << std::endl; + + for (int u = 0; u < num_vertices(g); ++u) + if (parent[u] != u) + edge_attr_map[edge(parent[u], u, g_dot).first]["color"] = "black"; + std::ofstream out("figs/telephone-mst-prim.dot"); + graph_property < GraphvizGraph, graph_edge_attribute_t >::type & + graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); + graph_edge_attr_map["color"] = "gray"; + write_graphviz(out, g_dot); + + return EXIT_SUCCESS; +} diff --git a/example/print-adjacent-vertices.cpp b/example/print-adjacent-vertices.cpp new file mode 100644 index 00000000..ce11974e --- /dev/null +++ b/example/print-adjacent-vertices.cpp @@ -0,0 +1,121 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + +template < typename Graph, typename VertexNameMap > void +output_adjacent_vertices(std::ostream & out, + typename graph_traits < Graph >::vertex_descriptor u, + const Graph & g, VertexNameMap name_map) +{ + typename graph_traits < Graph >::adjacency_iterator vi, vi_end; + out << get(name_map, u) << " -> { "; + for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) + out << get(name_map, *vi) << " "; + out << "}" << std::endl; +} + +template < typename NameMap > class name_equals_t { +public: + name_equals_t(const std::string & n, NameMap map) + : m_name(n), m_name_map(map) + { + } + template < typename Vertex > bool operator()(Vertex u) const + { + return get(m_name_map, u) == m_name; + } +private: + std::string m_name; + NameMap m_name_map; +}; + +// object generator function +template < typename NameMap > + inline name_equals_t < NameMap > +name_equals(const std::string & str, NameMap name) +{ + return name_equals_t < NameMap > (str, name); +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + graph_traits < graph_type >::vertex_iterator i, end; + tie(i, end) = vertices(g); + i = std::find_if(i, end, name_equals("dax.h", get(vertex_name, g))); + output_adjacent_vertices(std::cout, *i, g, get(vertex_name, g)); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/print-edges.cpp b/example/print-edges.cpp new file mode 100644 index 00000000..d29452cd --- /dev/null +++ b/example/print-edges.cpp @@ -0,0 +1,93 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + +template < typename Graph, typename VertexNameMap > void +print_dependencies(std::ostream & out, const Graph & g, + VertexNameMap name_map) +{ + typename graph_traits < Graph >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) + out << get(name_map, source(*ei, g)) << " -$>$ " + << get(name_map, target(*ei, g)) << std::endl; +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + print_dependencies(std::cout, g, get(vertex_name, g)); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/print-in-edges.cpp b/example/print-in-edges.cpp new file mode 100644 index 00000000..308ea79d --- /dev/null +++ b/example/print-in-edges.cpp @@ -0,0 +1,118 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + +template < typename Graph, typename VertexNameMap > void +output_in_edges(std::ostream & out, const Graph & g, + typename graph_traits < Graph >::vertex_descriptor v, + VertexNameMap name_map) +{ + typename graph_traits < Graph >::in_edge_iterator ei, ei_end; + for (tie(ei, ei_end) = in_edges(v, g); ei != ei_end; ++ei) + out << get(name_map, source(*ei, g)) << " -> " + << get(name_map, target(*ei, g)) << std::endl; +} + +template < typename NameMap > class name_equals_t { +public: + name_equals_t(const std::string & n, NameMap map) + : m_name(n), m_name_map(map) + { + } + template < typename Vertex > bool operator()(Vertex u) const + { + return get(m_name_map, u) == m_name; + } +private: + std::string m_name; + NameMap m_name_map; +}; + +// object generator function +template < typename NameMap > + inline name_equals_t < NameMap > +name_equals(const std::string & str, NameMap name) +{ + return name_equals_t < NameMap > (str, name); +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + bidirectionalS, // The graph is directed, with both out-edges and in-edges + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + graph_traits < graph_type >::vertex_iterator i, end; + tie(i, end) = vertices(g); + typedef property_map < graph_type, vertex_name_t >::type name_map_t; +i = std::find_if(i, end, name_equals("libzigzag.a", get(vertex_name, g)); + output_in_edges(std::cout, g, *i, get(vertex_name, g)); + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); return 0;} diff --git a/example/print-out-edges.cpp b/example/print-out-edges.cpp new file mode 100644 index 00000000..1046ffcb --- /dev/null +++ b/example/print-out-edges.cpp @@ -0,0 +1,122 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + +template < typename Graph, typename VertexNameMap > void +output_out_edges(std::ostream & out, const Graph & g, + typename graph_traits < Graph >::vertex_descriptor u, + VertexNameMap name_map) +{ + typename graph_traits < Graph >::out_edge_iterator ei, ei_end; + for (tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) + out << get(name_map, source(*ei, g)) << " -> " + << get(name_map, target(*ei, g)) << std::endl; +} + +template < typename NameMap > class name_equals_t { +public: + name_equals_t(const std::string & n, NameMap map) + : m_name(n), m_name_map(map) + { + } + template < typename Vertex > bool operator()(Vertex u) const + { + return get(m_name_map, u) == m_name; + } +private: + std::string m_name; + NameMap m_name_map; +}; + +// object generator function +template < typename NameMap > + inline name_equals_t < NameMap > +name_equals(const std::string & str, NameMap name) +{ + return name_equals_t < NameMap > (str, name); +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + graph_traits < graph_type >::vertex_iterator i, end; + tie(i, end) = vertices(g); + typedef property_map < graph_type, vertex_name_t >::type name_map_t; + name_equals < name_map_t > predicate("dax.h", get(vertex_name, g)); + i = std::find_if(i, end, predicate); + output_out_edges(std::cout, g, *i, get(vertex_name, g)); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +} diff --git a/example/property-map-traits-eg.cpp b/example/property-map-traits-eg.cpp new file mode 100644 index 00000000..cb8d7612 --- /dev/null +++ b/example/property-map-traits-eg.cpp @@ -0,0 +1,38 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +int +main() +{ + using namespace boost; + typedef adjacency_list < listS, listS, directedS, + property < vertex_name_t, std::string > >graph_t; + graph_t g; + graph_traits < graph_t >::vertex_descriptor u = add_vertex(g); + property_map < graph_t, vertex_name_t >::type + name_map = get(vertex_name, g); + name_map[u] = "Joe"; + std::cout << name_map[u] << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/push-relabel-eg.cpp b/example/push-relabel-eg.cpp new file mode 100644 index 00000000..83bd2490 --- /dev/null +++ b/example/push-relabel-eg.cpp @@ -0,0 +1,62 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +int +main() +{ + using namespace boost; + typedef adjacency_list_traits < vecS, vecS, directedS > Traits; + typedef adjacency_list < vecS, vecS, directedS, + property < vertex_name_t, std::string >, + property < edge_capacity_t, long, + property < edge_residual_capacity_t, long, + property < edge_reverse_t, Traits::edge_descriptor > >>>Graph; + Graph g; + + property_map < Graph, edge_capacity_t >::type + capacity = get(edge_capacity, g); + property_map < Graph, edge_residual_capacity_t >::type + residual_capacity = get(edge_residual_capacity, g); + property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); + Traits::vertex_descriptor s, t; + read_dimacs_max_flow(g, capacity, rev, s, t); + + long flow = push_relabel_max_flow(g, s, t); + + std::cout << "c The total flow:" << std::endl; + std::cout << "s " << flow << std::endl << std::endl; + std::cout << "c flow values:" << std::endl; + graph_traits < Graph >::vertex_iterator u_iter, u_end; + graph_traits < Graph >::out_edge_iterator ei, e_end; + for (tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) + for (tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) + if (capacity[*ei] > 0) + std::cout << "f " << *u_iter << " " << target(*ei, g) << " " + << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/put-get-helper-eg.cpp b/example/put-get-helper-eg.cpp new file mode 100644 index 00000000..34de9b6f --- /dev/null +++ b/example/put-get-helper-eg.cpp @@ -0,0 +1,68 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include + +namespace foo +{ + using namespace boost; + template < class RandomAccessIterator, class IndexMap > + class iterator_property_map:public boost::put_get_helper < + typename std::iterator_traits < RandomAccessIterator >::reference, + iterator_property_map < RandomAccessIterator, IndexMap > > + { + public: + typedef std::ptrdiff_t key_type; + typedef typename std::iterator_traits < RandomAccessIterator >::value_type + value_type; + typedef typename std::iterator_traits < RandomAccessIterator >::reference + reference; + typedef boost::lvalue_property_map_tag category; + + iterator_property_map(RandomAccessIterator cc = RandomAccessIterator(), + const IndexMap & _id = + IndexMap()):iter(cc), index(_id) + { + } + reference operator[] (std::ptrdiff_t v) const + { + return *(iter + get(index, v)); + } + protected: + RandomAccessIterator iter; + IndexMap index; + }; + +} + +int +main() +{ + typedef std::vector < std::string > vec_t; + typedef foo::iterator_property_map < vec_t::iterator, + boost::identity_property_map > pmap_t; + using namespace boost; + function_requires < Mutable_LvaluePropertyMapConcept < pmap_t, int >>(); + return 0; +} diff --git a/example/quick-tour.cpp b/example/quick-tour.cpp new file mode 100644 index 00000000..063474a5 --- /dev/null +++ b/example/quick-tour.cpp @@ -0,0 +1,121 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +using namespace boost; + +template < typename VertexDescriptor, typename VertexNameMap > void +print_vertex_name(VertexDescriptor v, VertexNameMap name_map) +{ + std::cout << get(name_map, v); +} + +template < typename Graph, typename TransDelayMap, typename VertexNameMap > void +print_trans_delay(typename graph_traits < Graph >::edge_descriptor e, + const Graph & g, TransDelayMap delay_map, + VertexNameMap name_map) +{ + std::cout << "trans-delay(" << get(name_map, source(e, g)) << "," + << get(name_map, target(e, g)) << ") = " << get(delay_map, e); +} + +template < typename Graph, typename VertexNameMap > void +print_vertex_names(const Graph & g, VertexNameMap name_map) +{ + std::cout << "vertices(g) = { "; + typedef typename graph_traits < Graph >::vertex_iterator iter_t; + for (std::pair < iter_t, iter_t > p = vertices(g); p.first != p.second; + ++p.first) { + print_vertex_name(*p.first, name_map); + std::cout << ' '; + } + std::cout << "}" << std::endl; +} + +template < typename Graph, typename TransDelayMap, typename VertexNameMap > void +print_trans_delays(const Graph & g, TransDelayMap trans_delay_map, + VertexNameMap name_map) +{ + typename graph_traits < Graph >::edge_iterator first, last; + for (tie(first, last) = edges(g); first != last; ++first) { + print_trans_delay(*first, g, trans_delay_map, name_map); + std::cout << std::endl; + } +} + +template < typename Graph, typename VertexNameMap, typename TransDelayMap > void +build_router_network(Graph & g, VertexNameMap name_map, + TransDelayMap delay_map) +{ + typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e; + a = add_vertex(g); + name_map[a] = 'a'; + b = add_vertex(g); + name_map[b] = 'b'; + c = add_vertex(g); + name_map[c] = 'c'; + d = add_vertex(g); + name_map[d] = 'd'; + e = add_vertex(g); + name_map[e] = 'e'; + + typename graph_traits < Graph >::edge_descriptor ed; + bool inserted; + + tie(ed, inserted) = add_edge(a, b, g); + delay_map[ed] = 1.2; + tie(ed, inserted) = add_edge(a, d, g); + delay_map[ed] = 4.5; + tie(ed, inserted) = add_edge(b, d, g); + delay_map[ed] = 1.8; + tie(ed, inserted) = add_edge(c, a, g); + delay_map[ed] = 2.6; + tie(ed, inserted) = add_edge(c, e, g); + delay_map[ed] = 5.2; + tie(ed, inserted) = add_edge(d, c, g); + delay_map[ed] = 0.4; + tie(ed, inserted) = add_edge(d, e, g); + delay_map[ed] = 3.3; + +} + + +int +main() +{ + typedef adjacency_list < listS, listS, directedS, + property < vertex_name_t, char >, + property < edge_weight_t, double >>graph_t; + graph_t g; + + property_map < graph_t, vertex_name_t >::type name_map = + get(vertex_name, g); + property_map < graph_t, edge_weight_t >::type delay_map = + get(edge_weight, g); + + build_router_network(g, name_map, delay_map); + print_vertex_names(g, name_map); + print_trans_delays(g, delay_map, name_map); +} diff --git a/example/reachable-loop-head.cpp b/example/reachable-loop-head.cpp new file mode 100644 index 00000000..09da2914 --- /dev/null +++ b/example/reachable-loop-head.cpp @@ -0,0 +1,72 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + if (argc < 3) { + std::cerr << "usage: reachable-loop-head.exe " + << std::endl; + return -1; + } + using namespace boost; + GraphvizDigraph g; + read_graphviz(argv[1], g); + graph_traits < GraphvizDigraph >::vertex_descriptor loop_head = 1; + typedef color_traits < default_color_type > Color; + + std::vector < default_color_type > + reachable_from_head(num_vertices(g), Color::white()); + depth_first_visit(g, loop_head, default_dfs_visitor(), + make_iterator_property_map(reachable_from_head.begin(), + get(vertex_index, g))); + + + graph_traits < GraphvizDigraph >::vertex_iterator i, i_end; + for (tie(i, i_end) = vertices(g); i != i_end; ++i) + if (reachable_from_head[*i] != Color::white()) { + get(vertex_attribute, g)[*i]["color"] = "gray"; + get(vertex_attribute, g)[*i]["style"] = "filled"; + } + + get_property(g, graph_graph_attribute)["size"] = "3,3"; + get_property(g, graph_graph_attribute)["ratio"] = "fill"; + get_property(g, graph_vertex_attribute)["shape"] = "box"; + + std::ofstream loops_out(argv[2]); + write_graphviz(loops_out, g, + make_vertex_attributes_writer(g), + make_edge_attributes_writer(g), + make_graph_attributes_writer(g)); + + + return EXIT_SUCCESS; +} diff --git a/example/reachable-loop-tail.cpp b/example/reachable-loop-tail.cpp new file mode 100644 index 00000000..4d1278bd --- /dev/null +++ b/example/reachable-loop-tail.cpp @@ -0,0 +1,80 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + if (argc < 3) { + std::cerr << "usage: reachable-loop-tail.exe " + << std::endl; + return -1; + } + using namespace boost; + GraphvizDigraph g_in; + read_graphviz(argv[1], g_in); + + typedef adjacency_list < vecS, vecS, bidirectionalS, + GraphvizVertexProperty, + GraphvizEdgeProperty, GraphvizGraphProperty > Graph; + Graph g; + copy_graph(g_in, g); + + graph_traits < GraphvizDigraph >::vertex_descriptor loop_tail = 6; + typedef color_traits < default_color_type > Color; + + std::vector < default_color_type > reachable_to_tail(num_vertices(g)); + reverse_graph < Graph > reverse_g(g); + depth_first_visit(reverse_g, loop_tail, default_dfs_visitor(), + make_iterator_property_map(reachable_to_tail.begin(), + get(vertex_index, g))); + + + std::ofstream loops_out(argv[2]); + loops_out << "digraph G {\n" + << " graph [ratio=\"fill\",size=\"3,3\"];\n" + << " node [shape=\"box\"];\n" << " edge [style=\"bold\"];\n"; + + graph_traits < GraphvizDigraph >::vertex_iterator i, i_end; + for (tie(i, i_end) = vertices(g_in); i != i_end; ++i) { + loops_out << *i << "[label=\"" << get(vertex_attribute, g)[*i]["label"] + << "\""; + if (reachable_to_tail[*i] != Color::white()) { + loops_out << ", color=\"gray\", style=\"filled\""; + } + loops_out << "]\n"; + } + graph_traits < GraphvizDigraph >::edge_iterator e, e_end; + for (tie(e, e_end) = edges(g_in); e != e_end; ++e) + loops_out << source(*e, g) << " -> " << target(*e, g) << ";\n"; + loops_out << "}\n"; + return EXIT_SUCCESS; +} diff --git a/example/reverse-graph-eg.cpp b/example/reverse-graph-eg.cpp new file mode 100644 index 00000000..a49394d8 --- /dev/null +++ b/example/reverse-graph-eg.cpp @@ -0,0 +1,61 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include + +#include +#include +#include +#include + +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, bidirectionalS > Graph; + + Graph G(5); + add_edge(0, 2, G); + add_edge(1, 1, G); + add_edge(1, 3, G); + add_edge(1, 4, G); + add_edge(2, 1, G); + add_edge(2, 3, G); + add_edge(2, 4, G); + add_edge(3, 1, G); + add_edge(3, 4, G); + add_edge(4, 0, G); + add_edge(4, 1, G); + + std::cout << "original graph:" << std::endl; + print_graph(G, get(vertex_index, G)); + + std::cout << std::endl << "reversed graph:" << std::endl; + print_graph(make_reverse_graph(G), get(vertex_index, G)); + + return EXIT_SUCCESS; +} diff --git a/example/scc.cpp b/example/scc.cpp new file mode 100644 index 00000000..e3075303 --- /dev/null +++ b/example/scc.cpp @@ -0,0 +1,56 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + GraphvizDigraph g; + read_graphviz("figs/scc.dot", g); + + typedef graph_traits < GraphvizDigraph >::vertex_descriptor vertex_t; + std::map < vertex_t, int >component; + + int num_comp = strong_components(g, make_assoc_property_map(component)); + + property_map < GraphvizDigraph, vertex_attribute_t >::type + vertex_attr_map = get(vertex_attribute, g); + std::string color[] = { + "white", "gray", "black", "lightgray"}; + graph_traits < GraphvizDigraph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + vertex_attr_map[*vi]["color"] = color[component[*vi]]; + vertex_attr_map[*vi]["style"] = "filled"; + if (vertex_attr_map[*vi]["color"] == "black") + vertex_attr_map[*vi]["fontcolor"] = "white"; + } + write_graphviz("figs/scc-out.dot", g); + + return EXIT_SUCCESS; +} diff --git a/example/strong-components.cpp b/example/strong-components.cpp new file mode 100644 index 00000000..d1bcd319 --- /dev/null +++ b/example/strong-components.cpp @@ -0,0 +1,57 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef adjacency_list < vecS, vecS, directedS > Graph; + const int N = 6; + Graph G(N); + add_edge(0, 1, G); + add_edge(1, 1, G); + add_edge(1, 3, G); + add_edge(1, 4, G); + add_edge(3, 4, G); + add_edge(3, 0, G); + add_edge(4, 3, G); + add_edge(5, 2, G); + + std::vector < int >c(N); + int num = strong_components(G, + make_iterator_property_map(c.begin(), + get(vertex_index, + G))); + + std::cout << "Total number of components: " << num << std::endl; + std::vector < int >::iterator i; + for (i = c.begin(); i != c.end(); ++i) + std::cout << "Vertex " << i - c.begin() + << " is in component " << *i << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/test-adj-list.cpp b/example/test-adj-list.cpp new file mode 100644 index 00000000..2a876dee --- /dev/null +++ b/example/test-adj-list.cpp @@ -0,0 +1,122 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +// need to fixed test-mutable-graph.w, update it to changes +// in the concept docs, names of parts, etc. +// test-mutable-graph.w#test mutable graph + +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + int V = 10; + int E = 50; + int T = 5; + if (argc > 2) { + V = atoi(argv[1]); + E = atoi(argv[2]); + T = atoi(argv[3]); + } + using namespace boost; + for (int i = 0; i < T; ++i) { + // test adjacency list without properties + { + adjacency_list < vecS, vecS, directedS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < vecS, listS, undirectedS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < vecS, vecS, bidirectionalS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + test_mutable_bidir_graph(g); + } + { + adjacency_list < setS, listS, directedS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < setS, vecS, undirectedS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < setS, listS, bidirectionalS > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + test_mutable_bidir_graph(g); + } +#if 0 + // test adjacency list with properties + typedef property < vertex_name_t, std::string > VertexProperties; + typedef property < edge_name_t, std::string > EdgeProperties; + { + adjacency_list < vecS, vecS, directedS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < vecS, listS, undirectedS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < vecS, vecS, bidirectionalS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + test_mutable_bidir_graph(g); + } + { + adjacency_list < setS, listS, directedS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < setS, vecS, undirectedS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + } + { + adjacency_list < setS, listS, bidirectionalS, VertexProperties, + EdgeProperties > g; + generate_random_graph(g, V, E); + test_mutable_graph(g); + test_mutable_bidir_graph(g); + } +#endif + } // for +} diff --git a/example/topo-sort-example.cpp b/example/topo-sort-example.cpp new file mode 100644 index 00000000..cd5fc83b --- /dev/null +++ b/example/topo-sort-example.cpp @@ -0,0 +1,79 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#line 132 "topological-sort.w" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct format_writer +{ + void operator() (std::ostream & out) const + { + out << "node[shape=\"circle\"]\nsize=\"3,3\"\nratio=\"fill\"\n"; + } +}; + +int +main() +{ + using namespace boost; + +#line 100 "topological-sort.w" + + typedef adjacency_list < vecS, vecS, directedS > Graph; + typedef graph_traits < Graph >::vertex_descriptor Vertex; + typedef graph_traits < Graph >::vertices_size_type size_type; + typedef std::pair < size_type, size_type > Pair; + enum verts + { a, b, c, d, e, f }; + const char name[] = "abcdef"; + Pair edges[7] = { Pair(a, b), Pair(c, e), Pair(c, f), Pair(a, d), + Pair(b, e), Pair(e, d), Pair(f, f) + }; + Graph G(6, edges, edges + 7); + std::vector < Vertex > v; + + topological_sort(G, std::back_inserter(v)); + + std::cout << "A topological ordering: "; + std::vector < Vertex >::reverse_iterator ii; + for (ii = v.rbegin(); ii != v.rend(); ++ii) + std::cout << name[*ii] << " "; + std::cout << std::endl; + +#line 154 "topological-sort.w" + + std::ofstream file("figs/topo-sort-eg.dot"); + write_graphviz(file, G, make_label_writer(name), + default_writer(), format_writer()); + return 0; +} diff --git a/example/topo-sort-file-dep.cpp b/example/topo-sort-file-dep.cpp new file mode 100644 index 00000000..b1907292 --- /dev/null +++ b/example/topo-sort-file-dep.cpp @@ -0,0 +1,97 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +namespace std +{ + template < typename T > + std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return in; + } +} + +typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The file dependency graph is directed +> file_dep_graph; + +typedef graph_traits < file_dep_graph >::vertex_descriptor vertex_t; +typedef graph_traits < file_dep_graph >::edge_descriptor edge_t; + +void +topo_sort_dfs(const file_dep_graph & g, vertex_t u, vertex_t * &topo_order, + int *mark) +{ + mark[u] = 1; // 1 means visited, 0 means not yet visited + graph_traits < file_dep_graph >::adjacency_iterator vi, vi_end; + for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) + if (mark[*vi] == 0) + topo_sort_dfs(g, *vi, topo_order, mark); + + *--topo_order = u; +} + +void +topo_sort(const file_dep_graph & g, vertex_t * topo_order) +{ + std::vector < int >mark(num_vertices(g), 0); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + if (mark[*vi] == 0) + topo_sort_dfs(g, *vi, topo_order, &mark[0]); +} + + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef graph_traits < file_dep_graph >::vertices_size_type size_type; + size_type n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph g(input_begin, input_end, n_vertices); + + std::vector < std::string > name(num_vertices(g)); + std::ifstream name_in("makefile-target-names.dat"); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + name_in >> name[*vi]; + + std::vector < vertex_t > order(num_vertices(g)); + topo_sort(g, &order[0] + num_vertices(g)); + for (int i = 0; i < num_vertices(g); ++i) + std::cout << name[order[i]] << std::endl; + + return 0; +} diff --git a/example/topo-sort-file-dep2.cpp b/example/topo-sort-file-dep2.cpp new file mode 100644 index 00000000..587f425d --- /dev/null +++ b/example/topo-sort-file-dep2.cpp @@ -0,0 +1,204 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +// can't do using namespace boost because then +// we get conflict with boost::default_dfs_visitor. +using + boost::graph_traits; +using + boost::listS; +using + boost::vecS; +using + boost::directedS; +using + boost::adjacency_list; +using + boost::default_color_type; +using + boost::white_color; +using + boost::gray_color; +using + boost::black_color; + +namespace + std +{ + template < + typename + T > + std::istream & + operator >> (std::istream & in, std::pair < T, T > &p) + { + in >> p.first >> p.second; + return + in; + } +} + +typedef + adjacency_list < + listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS // The file dependency graph is directed + > + file_dep_graph; + +typedef + graph_traits < + file_dep_graph >::vertex_descriptor + vertex_t; +typedef + graph_traits < + file_dep_graph >::edge_descriptor + edge_t; + +template < typename Visitor > void +dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color, + Visitor vis) +{ + color[u] = gray_color; + vis.discover_vertex(u, g); + graph_traits < file_dep_graph >::out_edge_iterator ei, ei_end; + for (tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) { + if (color[target(*ei, g)] == white_color) { + vis.tree_edge(*ei, g); + dfs_v1(g, target(*ei, g), color, vis); + } else if (color[target(*ei, g)] == gray_color) + vis.back_edge(*ei, g); + else + vis.forward_or_cross_edge(*ei, g); + } + color[u] = black_color; + vis.finish_vertex(u, g); +} + +template < typename Visitor > void +generic_dfs_v1(const file_dep_graph & g, Visitor vis) +{ + std::vector < default_color_type > color(num_vertices(g), white_color); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { + if (color[*vi] == white_color) + dfs_v1(g, *vi, &color[0], vis); + } +} + +struct default_dfs_visitor +{ + template < + typename + V, + typename + G > void + discover_vertex(V, const G &) + { + } + + template < + typename + E, + typename + G > void + tree_edge(E, const G &) + { + } + + template < typename E, typename G > void + back_edge(E, const G &) + { + } + + template < typename E, typename G > void + forward_or_cross_edge(E, const G &) + { + } + + template < typename V, typename G > void + finish_vertex(V, const G &) + { + } +}; + +struct topo_visitor: + public + default_dfs_visitor +{ + topo_visitor(vertex_t * &order): + topo_order(order) + { + } + void + finish_vertex(vertex_t u, const file_dep_graph &) + { + *--topo_order = u; + } + vertex_t *& + topo_order; +}; + +void +topo_sort(const file_dep_graph & g, vertex_t * topo_order) +{ + topo_visitor + vis(topo_order); + generic_dfs_v1(g, vis); +} + + +int +main() +{ + std::ifstream file_in("makefile-dependencies.dat"); + typedef + graph_traits < + file_dep_graph >::vertices_size_type + size_type; + size_type + n_vertices; + file_in >> n_vertices; // read in number of vertices + std::istream_iterator < std::pair < size_type, + size_type > >input_begin(file_in), input_end; + file_dep_graph + g(input_begin, input_end, n_vertices); + + std::vector < std::string > name(num_vertices(g)); + std::ifstream name_in("makefile-target-names.dat"); + graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + name_in >> name[*vi]; + + std::vector < vertex_t > order(num_vertices(g)); + topo_sort(g, &order[0] + num_vertices(g)); + for (int i = 0; i < num_vertices(g); ++i) + std::cout << name[order[i]] << std::endl; + + return 0; +} diff --git a/example/topo-sort-with-leda.cpp b/example/topo-sort-with-leda.cpp new file mode 100644 index 00000000..7d23788c --- /dev/null +++ b/example/topo-sort-with-leda.cpp @@ -0,0 +1,70 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +// Undefine macros from LEDA that conflict with the C++ Standard Library. +#undef string +#undef vector + +int +main() +{ + using namespace boost; + typedef GRAPH < std::string, char >graph_t; + graph_t leda_g; + typedef graph_traits < graph_t >::vertex_descriptor vertex_t; + std::vector < vertex_t > vert(7); + vert[0] = add_vertex(std::string("pick up kids from school"), leda_g); + vert[1] = add_vertex(std::string("buy groceries (and snacks)"), leda_g); + vert[2] = add_vertex(std::string("get cash at ATM"), leda_g); + vert[3] = + add_vertex(std::string("drop off kids at soccer practice"), leda_g); + vert[4] = add_vertex(std::string("cook dinner"), leda_g); + vert[5] = add_vertex(std::string("pick up kids from soccer"), leda_g); + vert[6] = add_vertex(std::string("eat dinner"), leda_g); + + add_edge(vert[0], vert[3], leda_g); + add_edge(vert[1], vert[3], leda_g); + add_edge(vert[1], vert[4], leda_g); + add_edge(vert[2], vert[1], leda_g); + add_edge(vert[3], vert[5], leda_g); + add_edge(vert[4], vert[6], leda_g); + add_edge(vert[5], vert[6], leda_g); + + std::vector < vertex_t > topo_order; + node_array < default_color_type > color_array(leda_g); + + topological_sort(leda_g, std::back_inserter(topo_order), + color_map(make_leda_node_property_map(color_array))); + + std::reverse(topo_order.begin(), topo_order.end()); + int n = 1; + for (std::vector < vertex_t >::iterator i = topo_order.begin(); + i != topo_order.end(); ++i, ++n) + std::cout << n << ": " << leda_g[*i] << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/topo-sort-with-sgb.cpp b/example/topo-sort-with-sgb.cpp new file mode 100644 index 00000000..9029f2d5 --- /dev/null +++ b/example/topo-sort-with-sgb.cpp @@ -0,0 +1,67 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + const int n_vertices = 7; + Graph *sgb_g = gb_new_graph(n_vertices); + + const char *tasks[] = { + "pick up kids from school", + "buy groceries (and snacks)", + "get cash at ATM", + "drop off kids at soccer practice", + "cook dinner", + "pick up kids from soccer", + "eat dinner" + }; + const int n_tasks = sizeof(tasks) / sizeof(char *); + + gb_new_arc(sgb_g->vertices + 0, sgb_g->vertices + 3, 0); + gb_new_arc(sgb_g->vertices + 1, sgb_g->vertices + 3, 0); + gb_new_arc(sgb_g->vertices + 1, sgb_g->vertices + 4, 0); + gb_new_arc(sgb_g->vertices + 2, sgb_g->vertices + 1, 0); + gb_new_arc(sgb_g->vertices + 3, sgb_g->vertices + 5, 0); + gb_new_arc(sgb_g->vertices + 4, sgb_g->vertices + 6, 0); + gb_new_arc(sgb_g->vertices + 5, sgb_g->vertices + 6, 0); + + typedef graph_traits < Graph * >::vertex_descriptor vertex_t; + std::vector < vertex_t > topo_order; + topological_sort(sgb_g, std::back_inserter(topo_order), + vertex_index_map(get(vertex_index, sgb_g))); + int n = 1; + for (std::vector < vertex_t >::reverse_iterator i = topo_order.rbegin(); + i != topo_order.rend(); ++i, ++n) + std::cout << n << ": " << tasks[get(vertex_index, sgb_g)[*i]] << std:: + endl; + + gb_recycle(sgb_g); + return EXIT_SUCCESS; +} diff --git a/example/topo-sort1.cpp b/example/topo-sort1.cpp new file mode 100644 index 00000000..5e665d57 --- /dev/null +++ b/example/topo-sort1.cpp @@ -0,0 +1,65 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include // to store the vertex ordering +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + const char *tasks[] = { + "pick up kids from school", + "buy groceries (and snacks)", + "get cash at ATM", + "drop off kids at soccer practice", + "cook dinner", + "pick up kids from soccer", + "eat dinner" + }; + const int n_tasks = sizeof(tasks) / sizeof(char *); + + std::vector < std::list < int >>g(n_tasks); + g[0].push_back(3); + g[1].push_back(3); + g[1].push_back(4); + g[2].push_back(1); + g[3].push_back(5); + g[4].push_back(6); + g[5].push_back(6); + + std::deque < int >topo_order; + + topological_sort(g, std::front_inserter(topo_order), + vertex_index_map(identity_property_map())); + + int n = 1; + for (std::deque < int >::iterator i = topo_order.begin(); + i != topo_order.end(); ++i, ++n) + std::cout << tasks[*i] << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/topo-sort2.cpp b/example/topo-sort2.cpp new file mode 100644 index 00000000..11a4a48b --- /dev/null +++ b/example/topo-sort2.cpp @@ -0,0 +1,64 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +int +main() +{ + using namespace boost; + const char *tasks[] = { + "pick up kids from school", + "buy groceries (and snacks)", + "get cash at ATM", + "drop off kids at soccer practice", + "cook dinner", + "pick up kids from soccer", + "eat dinner" + }; + const int n_tasks = sizeof(tasks) / sizeof(char *); + + adjacency_list < listS, vecS, directedS > g(n_tasks); + + add_edge(0, 3, g); + add_edge(1, 3, g); + add_edge(1, 4, g); + add_edge(2, 1, g); + add_edge(3, 5, g); + add_edge(4, 6, g); + add_edge(5, 6, g); + + std::deque < int >topo_order; + + topological_sort(g, std::front_inserter(topo_order), + vertex_index_map(identity_property_map())); + + int n = 1; + for (std::deque < int >::iterator i = topo_order.begin(); + i != topo_order.end(); ++i, ++n) + std::cout << tasks[*i] << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/transpose-example.cpp b/example/transpose-example.cpp new file mode 100644 index 00000000..eac4ab24 --- /dev/null +++ b/example/transpose-example.cpp @@ -0,0 +1,67 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +int +main() +{ + using namespace boost; + typedef int weight_t; + typedef adjacency_list < vecS, vecS, bidirectionalS, + property < vertex_name_t, char >>graph_t; + + enum + { a, b, c, d, e, f, g, N }; + graph_t G(N); + property_map < graph_t, vertex_name_t >::type + name_map = get(vertex_name, G); + char name = 'a'; + graph_traits < graph_t >::vertex_iterator v, v_end; + for (tie(v, v_end) = vertices(G); v != v_end; ++v, ++name) + name_map[*v] = name; + + typedef std::pair < int, int >E; + E edge_array[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), + E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) + }; + for (int i = 0; i < 12; ++i) + add_edge(edge_array[i].first, edge_array[i].second, G); + + print_graph(G, name_map); + std::cout << std::endl; + + graph_t G_T; + transpose_graph(G, G_T); + + print_graph(G_T, name_map); + + graph_traits < graph_t >::edge_iterator ei, ei_end; + for (tie(ei, ei_end) = edges(G); ei != ei_end; ++ei) + assert(edge(target(*ei, G), source(*ei, G), G_T).second == true); + return 0; +} diff --git a/example/undirected.cpp b/example/undirected.cpp index 3f4df79f..6b84b119 100644 --- a/example/undirected.cpp +++ b/example/undirected.cpp @@ -1,19 +1,18 @@ //======================================================================= -// Copyright 1997, 1998, 1999, 2000 University of Notre Dame. -// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, // // This file is part of the Boost Graph Library // // You should have received a copy of the License Agreement for the // Boost Graph Library along with the software; see the file LICENSE. -// If not, contact Office of Research, University of Notre Dame, Notre -// Dame, IN 46556. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. // -// Permission to modify the code and to distribute modified code is -// granted, provided the text of this NOTICE is retained, a notice that -// the code was modified is included with the above COPYRIGHT NOTICE and -// with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE -// file is distributed with the modified code. +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. // // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. // By way of example, but not limitation, Licensor MAKES NO @@ -22,95 +21,95 @@ // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS // OR OTHER RIGHTS. //======================================================================= -#include #include #include -#include -#include +using namespace boost; -/* - This example demonstrates the differences between directed and - undirected graphs. +template < typename UndirectedGraph > void +undirected_graph_demo1() +{ + const int V = 3; + UndirectedGraph undigraph(V); + typename graph_traits < UndirectedGraph >::vertex_descriptor zero, one, two; + typename graph_traits < UndirectedGraph >::out_edge_iterator out, out_end; + typename graph_traits < UndirectedGraph >::in_edge_iterator in, in_end; - Sample Output - - in a directed graph is (u,v) == (v,u) ? 0 - weight[(u,v)] = 1.2 - weight[(v,u)] = 2.4 - in an undirected graph is (u,v) == (v,u) ? 1 - weight[(u,v)] = 3.1 - weight[(v,u)] = 3.1 - the edges incident to v: (0,1) - + zero = vertex(0, undigraph); + one = vertex(1, undigraph); + two = vertex(2, undigraph); + add_edge(zero, one, undigraph); + add_edge(zero, two, undigraph); + add_edge(one, two, undigraph); + + std::cout << "out_edges(0): "; + for (tie(out, out_end) = out_edges(zero, undigraph); out != out_end; ++out) + std::cout << *out; + std::cout << std::endl << "in_edges(0): "; + for (tie(in, in_end) = in_edges(zero, undigraph); in != in_end; ++in) + std::cout << *in; + std::cout << std::endl; +} + +template < typename DirectedGraph > void +directed_graph_demo() +{ + const int V = 2; + DirectedGraph digraph(V); + typename graph_traits < DirectedGraph >::vertex_descriptor u, v; + typedef typename DirectedGraph::edge_property_type Weight; + typename property_map < DirectedGraph, edge_weight_t >::type + weight = get(edge_weight, digraph); + typename graph_traits < DirectedGraph >::edge_descriptor e1, e2; + bool found; + + u = vertex(0, digraph); + v = vertex(1, digraph); + add_edge(u, v, Weight(1.2), digraph); + add_edge(v, u, Weight(2.4), digraph); + tie(e1, found) = edge(u, v, digraph); + tie(e2, found) = edge(v, u, digraph); + std::cout << "in a directed graph is "; + std::cout << "(u,v) == (v,u) ? " + << std::boolalpha << (e1 == e2) << std::endl; + std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl; + std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl; +} + +template < typename UndirectedGraph > void +undirected_graph_demo2() +{ + const int V = 2; + UndirectedGraph undigraph(V); + typename graph_traits < UndirectedGraph >::vertex_descriptor u, v; + typedef typename UndirectedGraph::edge_property_type Weight; + typename property_map < UndirectedGraph, edge_weight_t >::type + weight = get(edge_weight, undigraph); + typename graph_traits < UndirectedGraph >::edge_descriptor e1, e2; + bool found; + + u = vertex(0, undigraph); + v = vertex(1, undigraph); + add_edge(u, v, Weight(3.1), undigraph); + tie(e1, found) = edge(u, v, undigraph); + tie(e2, found) = edge(v, u, undigraph); + std::cout << "in an undirected graph is "; + std::cout << "(u,v) == (v,u) ? " + << std::boolalpha << (e1 == e2) << std::endl; + std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl; + std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl; +} - */ int -main(int, char*[]) +main() { - using namespace boost; - using namespace std; - - const int V = 2; - - typedef property Weight; - typedef adjacency_list UndirectedGraph; - UndirectedGraph undigraph(V); - - typedef adjacency_list DirectedGraph; - DirectedGraph digraph(V); - - - { - graph_traits::vertex_descriptor u, v; - u = vertex(0, digraph); - v = vertex(1, digraph); - add_edge(u, v, Weight(1.2), digraph); - add_edge(v, u, Weight(2.4), digraph); - graph_traits::edge_descriptor e1, e2; - bool found; - tie(e1,found) = edge(u, v, digraph); - tie(e2,found) = edge(v, u, digraph); - cout << "in a directed graph is "; - cout << "(u,v) == (v,u) ? " << (e1 == e2) << endl; - - property_map::type - weight = get(edge_weight, digraph); - cout << "weight[(u,v)] = " << get(weight, e1) << endl; - cout << "weight[(v,u)] = " << get(weight, e2) << endl; - } - { - graph_traits::vertex_descriptor u, v; - u = vertex(0, undigraph); - v = vertex(1, undigraph); - add_edge(u, v, Weight(3.1), undigraph); - graph_traits::edge_descriptor e1, e2; - bool found; - tie(e1,found) = edge(u, v, undigraph); - tie(e2,found) = edge(v, u, undigraph); - cout << "in an undirected graph is "; - cout << "(u,v) == (v,u) ? " << (e1 == e2) << endl; - - property_map::type - weight = get(edge_weight, undigraph); - cout << "weight[(u,v)] = " << get(weight, e1) << endl; - cout << "weight[(v,u)] = " << get(weight, e2) << endl; - } - - // Vertices in undirected graphs don't have "out-edges", they have - // "incident" edges, but we still use the out_edge() function. - // Similarly, "in" and "out" have no meaning in undirected graphs - // but we still use source() and target() to access the unordered - // pair of vertices connected by the edge. - cout << "the edges incident to v: "; - graph_traits::out_edge_iterator e, e_end; - graph_traits::vertex_descriptor - s = vertex(0, undigraph); - for (tie(e, e_end) = out_edges(s, undigraph); e != e_end; ++e) - cout << "(" << source(*e, undigraph) - << "," << target(*e, undigraph) << ")" << endl; - + typedef property < edge_weight_t, double >Weight; + typedef adjacency_list < vecS, vecS, undirectedS, + no_property, Weight > UndirectedGraph; + typedef adjacency_list < vecS, vecS, directedS, + no_property, Weight > DirectedGraph; + undirected_graph_demo1 < UndirectedGraph > (); + undirected_graph_demo2 < UndirectedGraph > (); + directed_graph_demo < DirectedGraph > (); return 0; } diff --git a/example/vector-as-graph.cpp b/example/vector-as-graph.cpp new file mode 100644 index 00000000..69e3e039 --- /dev/null +++ b/example/vector-as-graph.cpp @@ -0,0 +1,49 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include + +int +main() +{ + enum + { r, s, t, u, v, w, x, y, N }; + char name[] = "rstuvwxy"; + typedef std::vector < std::list < int >>Graph; + Graph g(N); + g[r].push_back(v); + g[s].push_back(r); + g[s].push_back(r); + g[s].push_back(w); + g[t].push_back(x); + g[u].push_back(t); + g[w].push_back(t); + g[w].push_back(x); + g[x].push_back(y); + g[y].push_back(u); + boost::print_graph(g, name); + return 0; +} diff --git a/example/vertex-name-property.cpp b/example/vertex-name-property.cpp new file mode 100644 index 00000000..9fd10ecd --- /dev/null +++ b/example/vertex-name-property.cpp @@ -0,0 +1,89 @@ +//======================================================================= +// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, +// +// This file is part of the Boost Graph Library +// +// You should have received a copy of the License Agreement for the +// Boost Graph Library along with the software; see the file LICENSE. +// If not, contact Office of Research, Indiana University, +// Bloomington, IN 47405. +// +// Permission to modify the code and to distribute the code is +// granted, provided the text of this NOTICE is retained, a notice if +// the code was modified is included with the above COPYRIGHT NOTICE +// and with the COPYRIGHT NOTICE in the LICENSE file, and that the +// LICENSE file is distributed with the modified code. +// +// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. +// By way of example, but not limitation, Licensor MAKES NO +// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY +// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS +// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS +// OR OTHER RIGHTS. +//======================================================================= +#include +#include +#include +#include +#include + +using namespace boost; + +template < typename Graph, typename VertexNamePropertyMap > void +read_graph_file(std::istream & graph_in, std::istream & name_in, + Graph & g, VertexNamePropertyMap name_map) +{ + typedef typename graph_traits < Graph >::vertices_size_type size_type; + size_type n_vertices; + typename graph_traits < Graph >::vertex_descriptor u; + typename property_traits < VertexNamePropertyMap >::value_type name; + + graph_in >> n_vertices; // read in number of vertices + for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph + u = add_vertex(g); + name_in >> name; + put(name_map, u, name); // ** Attach name property to vertex u ** + } + size_type src, targ; + while (graph_in >> src) // Read in edges + if (graph_in >> targ) + add_edge(src, targ, g); // add an edge to the graph + else + break; +} + + +int +main() +{ + typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + vecS, // Store vertex set in a std::vector + directedS, // The graph is directed + property < vertex_name_t, std::string > // Add a vertex property + >graph_type; + + graph_type g; // use default constructor to create empty graph + std::ifstream file_in("makefile-dependencies.dat"), + name_in("makefile-target-names.dat"); + if (!file_in) { + std::cerr << "** Error: could not open file makefile-target-names.dat" + << std::endl; + exit(-1); + } + // Obtain internal property map from the graph + property_map < graph_type, vertex_name_t >::type name_map = + get(vertex_name, g); + read_graph_file(file_in, name_in, g, name_map); + + // Create storage for last modified times + std::vector < time_t > last_mod_vec(num_vertices(g)); + // Create nickname for the property map type + typedef iterator_property_map < std::vector < time_t >::iterator, + property_map < graph_type, vertex_index_t >::type > iter_map_t; + // Create last modified time property map + iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g)); + + assert(num_vertices(g) == 15); + assert(num_edges(g) == 19); + return 0; +}