• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This example is similar to the one in edge_property.cpp.
11 // The only difference is that this example uses exterior
12 // property storage instead of interior (properties).
13 //
14 //  Sample output:
15 //
16 //    0        --(10, 8)--> 1
17 //
18 //    1        --(20, 12)--> 4        --(40, 12)--> 3
19 //            <--(10,8)-- 0        <--(20,16)-- 2
20 //    2        --(20, 16)--> 1
21 //            <--(20,16)-- 5
22 //    3        --(40, 12)--> 6
23 //            <--(40,12)-- 1
24 //    4        --(20, 12)--> 7
25 //            <--(20,12)-- 1
26 //    5        --(20, 16)--> 2
27 //            <--(20,16)-- 6
28 //    6        --(20, 16)--> 5         --(10, 8)--> 8
29 //            <--(20,12)-- 7 <--(40,12)-- 3
30 //    7        --(20, 12)--> 6
31 //            <--(20,12)-- 4
32 //    8
33 //            <--(10,8)-- 6
34 
35 #include <boost/config.hpp>
36 #include <iostream>
37 #include <boost/graph/adjacency_list.hpp>
38 #include <boost/property_map/property_map.hpp>
39 
40 template < class Graph, class Capacity, class Flow >
print_network(Graph & G,Capacity capacity,Flow flow)41 void print_network(Graph& G, Capacity capacity, Flow flow)
42 {
43     typedef typename boost::graph_traits< Graph >::vertex_iterator Viter;
44     typedef
45         typename boost::graph_traits< Graph >::out_edge_iterator OutEdgeIter;
46     typedef typename boost::graph_traits< Graph >::in_edge_iterator InEdgeIter;
47 
48     Viter ui, uiend;
49     for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui)
50     {
51         OutEdgeIter out, out_end;
52         std::cout << *ui << "\t";
53 
54         for (boost::tie(out, out_end) = boost::out_edges(*ui, G);
55              out != out_end; ++out)
56             std::cout << "--(" << boost::get(capacity, *out) << ", "
57                       << boost::get(flow, *out) << ")--> "
58                       << boost::target(*out, G) << "\t";
59         std::cout << std::endl << "\t";
60 
61         InEdgeIter in, in_end;
62         for (boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end;
63              ++in)
64             std::cout << "<--(" << boost::get(capacity, *in) << ","
65                       << boost::get(flow, *in) << ")-- "
66                       << boost::source(*in, G) << "\t";
67         std::cout << std::endl;
68     }
69 }
70 
main(int,char * [])71 int main(int, char*[])
72 {
73 
74     typedef boost::adjacency_list< boost::vecS, boost::vecS,
75         boost::bidirectionalS, boost::no_property,
76         boost::property< boost::edge_index_t, std::size_t > >
77         Graph;
78 
79     const int num_vertices = 9;
80     Graph G(num_vertices);
81 
82     /*          2<----5
83                /       ^
84               /         \
85              V           \
86       0 ---->1---->3----->6--->8
87              \           ^
88               \         /
89                V       /
90                4----->7
91      */
92 
93     int capacity[] = { 10, 20, 20, 20, 40, 40, 20, 20, 20, 10 };
94     int flow[] = { 8, 12, 12, 12, 12, 12, 16, 16, 16, 8 };
95 
96     // insert edges into the graph, and assign each edge an ID number
97     // to index into the property arrays
98     boost::add_edge(0, 1, 0, G);
99 
100     boost::add_edge(1, 4, 1, G);
101     boost::add_edge(4, 7, 2, G);
102     boost::add_edge(7, 6, 3, G);
103 
104     boost::add_edge(1, 3, 4, G);
105     boost::add_edge(3, 6, 5, G);
106 
107     boost::add_edge(6, 5, 6, G);
108     boost::add_edge(5, 2, 7, G);
109     boost::add_edge(2, 1, 8, G);
110 
111     boost::add_edge(6, 8, 9, G);
112 
113     typedef boost::property_map< Graph, boost::edge_index_t >::type
114         EdgeIndexMap;
115     EdgeIndexMap edge_id = boost::get(boost::edge_index, G);
116 
117     typedef boost::iterator_property_map< int*, EdgeIndexMap, int, int& >
118         IterMap;
119 
120     print_network(G, IterMap(capacity, edge_id), IterMap(flow, edge_id));
121 
122     return 0;
123 }
124