File: exterior_properties.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (123 lines) | stat: -rw-r--r-- 3,970 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//=======================================================================
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// Distributed under 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)
//=======================================================================
//
// This example is similar to the one in edge_property.cpp.
// The only difference is that this example uses exterior
// property storage instead of interior (properties).
//
//  Sample output:
//
//    0        --(10, 8)--> 1
//
//    1        --(20, 12)--> 4        --(40, 12)--> 3
//            <--(10,8)-- 0        <--(20,16)-- 2
//    2        --(20, 16)--> 1
//            <--(20,16)-- 5
//    3        --(40, 12)--> 6
//            <--(40,12)-- 1
//    4        --(20, 12)--> 7
//            <--(20,12)-- 1
//    5        --(20, 16)--> 2
//            <--(20,16)-- 6
//    6        --(20, 16)--> 5         --(10, 8)--> 8
//            <--(20,12)-- 7 <--(40,12)-- 3
//    7        --(20, 12)--> 6
//            <--(20,12)-- 4
//    8
//            <--(10,8)-- 6

#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>

template < class Graph, class Capacity, class Flow >
void print_network(Graph& G, Capacity capacity, Flow flow)
{
    typedef typename boost::graph_traits< Graph >::vertex_iterator Viter;
    typedef
        typename boost::graph_traits< Graph >::out_edge_iterator OutEdgeIter;
    typedef typename boost::graph_traits< Graph >::in_edge_iterator InEdgeIter;

    Viter ui, uiend;
    for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui)
    {
        OutEdgeIter out, out_end;
        std::cout << *ui << "\t";

        for (boost::tie(out, out_end) = boost::out_edges(*ui, G);
             out != out_end; ++out)
            std::cout << "--(" << boost::get(capacity, *out) << ", "
                      << boost::get(flow, *out) << ")--> "
                      << boost::target(*out, G) << "\t";
        std::cout << std::endl << "\t";

        InEdgeIter in, in_end;
        for (boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end;
             ++in)
            std::cout << "<--(" << boost::get(capacity, *in) << ","
                      << boost::get(flow, *in) << ")-- "
                      << boost::source(*in, G) << "\t";
        std::cout << std::endl;
    }
}

int main(int, char*[])
{

    typedef boost::adjacency_list< boost::vecS, boost::vecS,
        boost::bidirectionalS, boost::no_property,
        boost::property< boost::edge_index_t, std::size_t > >
        Graph;

    const int num_vertices = 9;
    Graph G(num_vertices);

    /*          2<----5
               /       ^
              /         \
             V           \
      0 ---->1---->3----->6--->8
             \           ^
              \         /
               V       /
               4----->7
     */

    int capacity[] = { 10, 20, 20, 20, 40, 40, 20, 20, 20, 10 };
    int flow[] = { 8, 12, 12, 12, 12, 12, 16, 16, 16, 8 };

    // insert edges into the graph, and assign each edge an ID number
    // to index into the property arrays
    boost::add_edge(0, 1, 0, G);

    boost::add_edge(1, 4, 1, G);
    boost::add_edge(4, 7, 2, G);
    boost::add_edge(7, 6, 3, G);

    boost::add_edge(1, 3, 4, G);
    boost::add_edge(3, 6, 5, G);

    boost::add_edge(6, 5, 6, G);
    boost::add_edge(5, 2, 7, G);
    boost::add_edge(2, 1, 8, G);

    boost::add_edge(6, 8, 9, G);

    typedef boost::property_map< Graph, boost::edge_index_t >::type
        EdgeIndexMap;
    EdgeIndexMap edge_id = boost::get(boost::edge_index, G);

    typedef boost::iterator_property_map< int*, EdgeIndexMap, int, int& >
        IterMap;

    print_network(G, IterMap(capacity, edge_id), IterMap(flow, edge_id));

    return 0;
}