Permalink
Browse files

examples from the book

[SVN r11503]
  • Loading branch information...
jsiek committed Nov 1, 2001
1 parent d5f559e commit 5215e9b4f201bf4509a78abe89d16cb8f4199bbd
Showing with 6,203 additions and 86 deletions.
  1. +143 −0 example/accum-compile-times.cpp
  2. +57 −0 example/accumulate-eg.cpp
  3. +134 −0 example/bellman-example.cpp
  4. +80 −0 example/bellman-ford-internet.cpp
  5. +88 −0 example/bfs-example.cpp
  6. +104 −0 example/bfs-name-printer.cpp
  7. +89 −0 example/binary-method-problem.cpp
  8. +59 −0 example/cc-internet.cpp
  9. +57 −0 example/connected-components.cpp
  10. +64 −0 example/copy-example.cpp
  11. +98 −0 example/cycle-file-dep.cpp
  12. +203 −0 example/cycle-file-dep2.cpp
  13. +62 −0 example/default-constructor.cpp
  14. +65 −0 example/default-constructor2.cpp
  15. +101 −0 example/dfs-example.cpp
  16. +74 −0 example/dfs-parenthesis.cpp
  17. +89 −0 example/dijkstra-example.cpp
  18. +192 −0 example/edge-connectivity.cpp
  19. +146 −0 example/edge-function.cpp
  20. +72 −0 example/edge-iter-constructor.cpp
  21. +70 −0 example/edmunds-karp-eg.cpp
  22. +67 −0 example/family-tree-eg.cpp
  23. +120 −0 example/graph-assoc-types.cpp
  24. +46 −0 example/graph-property-iter-eg.cpp
  25. +81 −0 example/incremental-components-eg.cpp
  26. +1 −1 example/incremental_components.cpp
  27. +37 −0 example/iterator-property-map-eg.cpp
  28. +92 −0 example/johnson-eg.cpp
  29. +132 −0 example/kevin-bacon.cpp
  30. +358 −0 example/knights-tour.cpp
  31. +72 −0 example/koenig-lookup.cpp
  32. +75 −0 example/kruskal-example.cpp
  33. +71 −0 example/kruskal-telephone.cpp
  34. +106 −0 example/last-mod-time.cpp
  35. +36 −0 example/leda-concept-check.cpp
  36. +44 −0 example/leda-graph-eg.cpp
  37. +46 −0 example/lic.cpp
  38. +165 −0 example/loops_dfs.cpp
  39. +98 −0 example/ospf-example.cpp
  40. +259 −0 example/parallel-compile-time.cpp
  41. +54 −0 example/prim-example.cpp
  42. +70 −0 example/prim-telephone.cpp
  43. +121 −0 example/print-adjacent-vertices.cpp
  44. +93 −0 example/print-edges.cpp
  45. +118 −0 example/print-in-edges.cpp
  46. +122 −0 example/print-out-edges.cpp
  47. +38 −0 example/property-map-traits-eg.cpp
  48. +62 −0 example/push-relabel-eg.cpp
  49. +68 −0 example/put-get-helper-eg.cpp
  50. +121 −0 example/quick-tour.cpp
  51. +72 −0 example/reachable-loop-head.cpp
  52. +80 −0 example/reachable-loop-tail.cpp
  53. +61 −0 example/reverse-graph-eg.cpp
  54. +56 −0 example/scc.cpp
  55. +57 −0 example/strong-components.cpp
  56. +122 −0 example/test-adj-list.cpp
  57. +79 −0 example/topo-sort-example.cpp
  58. +97 −0 example/topo-sort-file-dep.cpp
  59. +204 −0 example/topo-sort-file-dep2.cpp
  60. +70 −0 example/topo-sort-with-leda.cpp
  61. +67 −0 example/topo-sort-with-sgb.cpp
  62. +65 −0 example/topo-sort1.cpp
  63. +64 −0 example/topo-sort2.cpp
  64. +67 −0 example/transpose-example.cpp
  65. +84 −85 example/undirected.cpp
  66. +49 −0 example/vector-as-graph.cpp
  67. +89 −0 example/vertex-name-property.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 <boost/config.hpp>
#include <fstream>
#include <iostream>
#include <numeric>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/property_iter_range.hpp>

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;
}
@@ -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 <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <cassert>

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;
}
@@ -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 <boost/config.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/array.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>

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;
}
Oops, something went wrong.

0 comments on commit 5215e9b

Please sign in to comment.