Add example for CSR graph

[SVN r31880]
This commit is contained in:
Douglas Gregor 2005-12-02 15:24:02 +00:00
parent 9be555c4f5
commit aa3dcfb2e1
2 changed files with 110 additions and 0 deletions

View File

@ -77,6 +77,8 @@ function address(host, user) {
<li><a href="#edge-access">Edge access</a></li>
<li><a href="#property-map-accessors">Property map accessors</a></li>
</ul></li>
<li><a href="#example">Example</a></li>
</ul>
<a name="synopsis"></a><h2>Synopsis</h2>
@ -586,6 +588,54 @@ void set_property(const compressed_sparse_row_graph&amp; g, GraphPropertyTag,
is attached to the graph object <tt>g</tt>.
</p>
<hr></hr>
<a name="example"></a><h2>Example</h2>
<br>[<a
href="../example/csr-example.cpp">libs/graph/example/csr-example.cpp</a>]
<p>We will use the <tt>compressed_sparse_row_graph</tt> graph
class to store a simple Web graph. In this web graph the vertices
represent web pages and the edges represent links from one web
page to another. With each web page we want to associate a URL, so
we initially create a <tt>WebPage</tt> class that stores the
URL. Then we can create our graph type by providing
<tt>WebPage</tt> as a parameter to the
<tt>compressed_sparse_row_graph</tt> class template.</p>
<pre>
class WebPage
{
public:
std::string url;
};
// ...
typedef compressed_sparse_row_graph&lt;WebPage&gt; WebGraph;
WebGraph g(&the_edges[0], &the_edges[0] + sizeof(the_edges)/sizeof(E), 6);
</pre>
<p>We can then set the properties on the vertices of the graph
using the <a href="bundles.html">bundled properties</a> syntax,
and display the edges for the user.</p>
<pre>
// Set the URLs of each vertex
int index = 0;
BGL_FORALL_VERTICES(v, g, WebGraph)
g[v].url = urls[index++];
// Output each of the links
std::cout &lt;&lt; "The web graph:" &lt;&lt; std::endl;
BGL_FORALL_EDGES(e, g, WebGraph)
std::cout &lt;&lt; " " &lt;&lt; g[source(e, g)].url &lt;&lt; " -> " &lt;&lt; g[target(e, g)].url
&lt;&lt; std::endl;
</pre>
<p>See the <a href="../example/csr-example.cpp">complete example
source</a> for other operations one can perform with a
<tt>compressed_sparse_row_graph</tt>.</p>
<br>
<HR>
<TABLE>

60
example/csr-example.cpp Normal file
View File

@ -0,0 +1,60 @@
// Copyright 2005 The Trustees of Indiana University.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
// Andrew Lumsdaine
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <string>
#include <boost/graph/iteration_macros.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graphviz.hpp>
using namespace boost;
class WebPage
{
public:
std::string url;
};
int main()
{
typedef std::pair<int, int> E;
const char* urls[6] = {
"http://www.boost.org/libs/graph/doc/index.html",
"http://www.boost.org/libs/graph/doc/table_of_contents.html",
"http://www.boost.org/libs/graph/doc/adjacency_list.html",
"http://www.boost.org/libs/graph/doc/history.html",
"http://www.boost.org/libs/graph/doc/bundles.html",
"http://www.boost.org/libs/graph/doc/using_adjacency_list.html",
};
E the_edges[] = { E(0, 1), E(0, 2), E(0, 3), E(1, 0), E(1, 3), E(1, 5),
E(2, 0), E(2, 5), E(3, 1), E(3, 4), E(4, 1), E(5, 0),
E(5, 2) };
typedef compressed_sparse_row_graph<WebPage> WebGraph;
WebGraph g(&the_edges[0], &the_edges[0] + sizeof(the_edges)/sizeof(E), 6);
// Set the URLs of each vertex
int index = 0;
BGL_FORALL_VERTICES(v, g, WebGraph)
g[v].url = urls[index++];
// Output each of the links
std::cout << "The web graph:" << std::endl;
BGL_FORALL_EDGES(e, g, WebGraph)
std::cout << " " << g[source(e, g)].url << " -> " << g[target(e, g)].url
<< std::endl;
// Output the graph in DOT format
dynamic_properties dp;
dp.property("label", get(&WebPage::url, g));
std::ofstream out("web-graph.dot");
write_graphviz(out, g, dp, std::string(), get(vertex_index, g));
return 0;
}