1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 
10 #include <boost/config.hpp>
11 #include <iostream>
12 #include <algorithm>
13 #include <boost/graph/adjacency_list.hpp>
14 
15 using namespace std;
16 using namespace boost;
17 
18 /*
19   Edge Basics
20 
21   This example demonstrates the GGCL Edge interface
22 
23   There is not much to the Edge interface. Basically just two
24   functions to access the source and target vertex:
25 
26   source(e)
27   target(e)
28 
29   and one associated type for the vertex type:
30 
31   edge_traits<Edge>::vertex_type
32 
33   Sample output:
34 
35   (0,1) (0,2) (0,3) (0,4) (2,0) (2,4) (3,0) (3,1)
36 
37  */
38 
39 template < class Graph > struct exercise_edge
40 {
exercise_edgeexercise_edge41     exercise_edge(Graph& g) : G(g) {}
42 
43     typedef typename boost::graph_traits< Graph >::edge_descriptor Edge;
44     typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
operator ()exercise_edge45     void operator()(Edge e) const
46     {
47         // begin
48         // Get the associated vertex type out of the edge using the
49         // edge_traits class
50         // Use the source() and target() functions to access the vertices
51         // that belong to Edge e
52         Vertex src = source(e, G);
53         Vertex targ = target(e, G);
54 
55         // print out the vertex id's just because
56         cout << "(" << src << "," << targ << ") ";
57         // end
58     }
59 
60     Graph& G;
61 };
62 
main()63 int main()
64 {
65     typedef adjacency_list<> MyGraph;
66 
67     typedef pair< int, int > Pair;
68     Pair edge_array[8] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
69         Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1) };
70 
71     // Construct a graph using the edge_array (passing in pointers
72     // (iterators) to the beginning and end of the array), and
73     // specifying the number of vertices as 5
74     MyGraph G(5);
75     for (int i = 0; i < 8; ++i)
76         add_edge(edge_array[i].first, edge_array[i].second, G);
77 
78     // Use the STL for_each algorithm to "exercise" all of the edges in
79     // the graph
80     for_each(edges(G).first, edges(G).second, exercise_edge< MyGraph >(G));
81     cout << endl;
82     return 0;
83 }
84