csr-example.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #include <boost/graph/compressed_sparse_row_graph.hpp>
  8. #include <string>
  9. #include <boost/graph/iteration_macros.hpp>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <boost/graph/graphviz.hpp>
  13. using namespace boost;
  14. class WebPage
  15. {
  16. public:
  17. std::string url;
  18. };
  19. int main()
  20. {
  21. typedef std::pair<int, int> E;
  22. const char* urls[6] = {
  23. "http://www.boost.org/libs/graph/doc/index.html",
  24. "http://www.boost.org/libs/graph/doc/table_of_contents.html",
  25. "http://www.boost.org/libs/graph/doc/adjacency_list.html",
  26. "http://www.boost.org/libs/graph/doc/history.html",
  27. "http://www.boost.org/libs/graph/doc/bundles.html",
  28. "http://www.boost.org/libs/graph/doc/using_adjacency_list.html",
  29. };
  30. E the_edges[] = { E(0, 1), E(0, 2), E(0, 3), E(1, 0), E(1, 3), E(1, 5),
  31. E(2, 0), E(2, 5), E(3, 1), E(3, 4), E(4, 1), E(5, 0),
  32. E(5, 2) };
  33. typedef compressed_sparse_row_graph<directedS, WebPage> WebGraph;
  34. WebGraph g(boost::edges_are_sorted, &the_edges[0], &the_edges[0] + sizeof(the_edges)/sizeof(E), 6);
  35. // Set the URLs of each vertex
  36. int index = 0;
  37. BGL_FORALL_VERTICES(v, g, WebGraph)
  38. g[v].url = urls[index++];
  39. // Output each of the links
  40. std::cout << "The web graph:" << std::endl;
  41. BGL_FORALL_EDGES(e, g, WebGraph)
  42. std::cout << " " << g[source(e, g)].url << " -> " << g[target(e, g)].url
  43. << std::endl;
  44. // Output the graph in DOT format
  45. dynamic_properties dp;
  46. dp.property("label", get(&WebPage::url, g));
  47. std::ofstream out("web-graph.dot");
  48. write_graphviz_dp(out, g, dp, std::string(), get(vertex_index, g));
  49. return 0;
  50. }