• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Copyright 2004 Trustees of Indiana University
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Douglas Gregor
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
10 
11 #include <boost/config.hpp>
12 #include <iostream>
13 #include <algorithm>
14 #include <boost/graph/adjacency_list.hpp>
15 #include <boost/property_map/property_map.hpp>
16 #include <string>
17 
18 using namespace std;
19 using namespace boost;
20 
21 /*
22   Interior Property Map Basics
23 
24   An interior property map is a way of associating properties
25   with the vertices or edges of a graph. The "interior" part means
26   that the properties are stored inside the graph object. This can be
27   convenient when the need for the properties is somewhat permanent,
28   and when the properties will be with a graph for the duration of its
29   lifetime. A "distance from source vertex" property is often of this
30   kind.
31 
32   Sample Output
33 
34   Jeremy owes Rich some money
35   Jeremy owes Andrew some money
36   Jeremy owes Jeff some money
37   Jeremy owes Kinis some money
38   Andrew owes Jeremy some money
39   Andrew owes Kinis some money
40   Jeff owes Jeremy some money
41   Jeff owes Rich some money
42   Jeff owes Kinis some money
43   Kinis owes Jeremy some money
44   Kinis owes Rich some money
45 
46  */
47 
48 template < class EdgeIter, class Graph >
who_owes_who(EdgeIter first,EdgeIter last,const Graph & G)49 void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G)
50 {
51     while (first != last)
52     {
53         cout << G[source(*first, G)].first_name << " owes "
54              << G[target(*first, G)].first_name << " some money" << endl;
55         ++first;
56     }
57 }
58 
59 struct VertexData
60 {
61     string first_name;
62 };
63 
main()64 int main()
65 {
66     {
67         // Create the graph, and specify that we will use std::string to
68         // store the first name's.
69         typedef adjacency_list< vecS, vecS, directedS, VertexData > MyGraphType;
70 
71         typedef pair< int, int > Pair;
72         Pair edge_array[11] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
73             Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), Pair(3, 4),
74             Pair(4, 0), Pair(4, 1) };
75 
76         MyGraphType G(5);
77         for (int i = 0; i < 11; ++i)
78             add_edge(edge_array[i].first, edge_array[i].second, G);
79 
80         G[0].first_name = "Jeremy";
81         G[1].first_name = "Rich";
82         G[2].first_name = "Andrew";
83         G[3].first_name = "Jeff";
84         G[4].first_name = "Doug";
85 
86         who_owes_who(edges(G).first, edges(G).second, G);
87     }
88 
89     cout << endl;
90 
91     return 0;
92 }
93