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