File: labeled_graph.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (210 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (5)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// (C) Copyright 2009 Andrew Sutton
//
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

#include <string>

#include <boost/assert.hpp>
#include <boost/concept/assert.hpp>
#include <boost/range.hpp>

#include <boost/graph/graph_concepts.hpp>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/directed_graph.hpp>
#include <boost/graph/labeled_graph.hpp>

#include "typestr.hpp"

using std::string;
using namespace boost;

void test_concepts();
void test_norm();
void test_temp();
void test_bacon();
void test_remove_labeled_vertex();
void test_multiple_associative_container();

int main()
{
    test_concepts();
    test_norm();
    test_temp();
    test_bacon();
    test_remove_labeled_vertex();
    test_multiple_associative_container();
}

//////////////////////////////////////
// Graph Concepts
//////////////////////////////////////

void test_concepts()
{
    // The labeled mutable graph hides the add_ and remove_ vertex functions
    // from the mutable graph concept, so VertexMutableGraphConcept will not be
    // tested here.
    {
        typedef labeled_graph< directed_graph<>, unsigned > Graph;
        BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
        BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >));
        BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept< Graph >));
    }
    {
        typedef labeled_graph< undirected_graph<>, unsigned > Graph;
        BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph >));
        BOOST_CONCEPT_ASSERT((AdjacencyGraphConcept< Graph >));
        BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept< Graph >));
    }
}

//////////////////////////////////////
// Utility Functions and Types
//////////////////////////////////////

struct Actor
{
    Actor() {}
    Actor(string const& s) : name(s) {}
    string name;
};

struct Movie
{
    Movie() {}
    Movie(string const& s) : name(s) {}
    string name;
};

template < typename Graph > void init_graph(Graph& g)
{
    for (int i = 0; i < 6; ++i)
    {
        add_vertex(i, g);
    }
}

template < typename Graph > void label_graph(Graph& g)
{
    typedef typename graph_traits< Graph >::vertex_iterator Iter;
    Iter f, l;
    int x = 0;
    for (boost::tie(f, l) = vertices(g); f != l; ++f, ++x)
    {
        label_vertex(*f, x, g);
    }
}

template < typename Graph > void build_graph(Graph& g)
{
    // This is the graph shown on the wikipedia page for Graph Theory.
    add_edge_by_label(5, 3, g);
    add_edge_by_label(3, 4, g);
    add_edge_by_label(3, 2, g);
    add_edge_by_label(4, 1, g);
    add_edge_by_label(4, 0, g);
    add_edge_by_label(2, 1, g);
    add_edge_by_label(1, 0, g);
    BOOST_ASSERT(num_vertices(g) == 6);
    BOOST_ASSERT(num_edges(g) == 7);
}

//////////////////////////////////////
// Temporal Labelings
//////////////////////////////////////

void test_norm()
{
    {
        typedef labeled_graph< undirected_graph<>, unsigned > Graph;
        Graph g;
        init_graph(g);
        build_graph(g);
    }

    {
        typedef labeled_graph< directed_graph<>, unsigned > Graph;
        Graph g;
        init_graph(g);
        build_graph(g);
    }
}

//////////////////////////////////////
// Temporal Labelings
//////////////////////////////////////

void test_temp()
{
    typedef undirected_graph<> Graph;
    typedef labeled_graph< Graph*, int > LabGraph;
    Graph g(6);
    LabGraph lg(&g);
    label_graph(lg);
    build_graph(lg);
}

//////////////////////////////////////
// Labeled w/ Properties
//////////////////////////////////////

void test_bacon()
{
    string bacon("Kevin Bacon");
    string slater("Christian Slater");
    Movie murder("Murder in the First");
    {

        typedef labeled_graph< undirected_graph< Actor, Movie >, string > Graph;
        Graph g;
        add_vertex(bacon, g);
        add_vertex(slater, g);
        add_edge_by_label(bacon, slater, murder, g);
    }

    {
        string bacon = "Kevin Bacon";
        string slater = "Christian Slater";

        typedef labeled_graph< directed_graph< Actor, Movie >, string > Graph;
        Graph g;
        add_vertex(bacon, g);
        add_vertex(slater, g);
        add_edge_by_label(bacon, slater, murder, g);
    }
}

void test_remove_labeled_vertex()
{
    typedef labeled_graph< directed_graph<>, string > Graph;

    Graph g;
    g.add_vertex("foo");

    auto v = g.vertex("foo");
    BOOST_ASSERT(v != nullptr);

    g.remove_vertex("foo");

    auto v2 = g.vertex("foo");
    BOOST_ASSERT(v2 == nullptr);
}

void test_multiple_associative_container()
{
    typedef labeled_graph<adjacency_list<listS, multisetS, directedS>, string, multimapS> Graph;

    Graph g;
    g.add_vertex("test");
    g.add_vertex("test");

    BOOST_ASSERT(num_vertices(g) == 2);

    g.remove_vertex("test");
    BOOST_ASSERT(num_vertices(g) == 1);

    g.remove_vertex("test");
    BOOST_ASSERT(num_vertices(g) == 0);
}