File: core_numbers_test.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 (194 lines) | stat: -rw-r--r-- 4,611 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
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
// (C) Copyright David Gleich 2007
//
// 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 <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/core_numbers.hpp>
#include <boost/property_map/property_map.hpp>
#include <stdio.h>

using namespace boost;

const char* errstr = "";

int test_1()
{
    // core numbers of sample graph
    typedef adjacency_list< vecS, vecS, undirectedS > Graph;

    Graph G(21);
    add_edge(0, 1, G);
    add_edge(1, 2, G);
    add_edge(1, 3, G);
    add_edge(2, 3, G);
    add_edge(1, 4, G);
    add_edge(3, 4, G);
    add_edge(4, 5, G);
    add_edge(4, 6, G);
    add_edge(5, 6, G);
    add_edge(4, 7, G);
    add_edge(5, 7, G);
    add_edge(6, 7, G);
    add_edge(7, 8, G);
    add_edge(3, 9, G);
    add_edge(8, 9, G);
    add_edge(8, 10, G);
    add_edge(9, 10, G);
    add_edge(10, 11, G);
    add_edge(10, 12, G);
    add_edge(3, 13, G);
    add_edge(9, 13, G);
    add_edge(3, 14, G);
    add_edge(9, 14, G);
    add_edge(13, 14, G);
    add_edge(16, 17, G);
    add_edge(16, 18, G);
    add_edge(17, 19, G);
    add_edge(18, 19, G);
    add_edge(19, 20, G);

    std::vector< int > core_nums(num_vertices(G));
    core_numbers(
        G, make_iterator_property_map(core_nums.begin(), get(vertex_index, G)));

    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
    }

    int correct[21]
        = { 1, 2, 2, 3, 3, 3, 3, 3, 2, 3, 2, 1, 1, 3, 3, 0, 2, 2, 2, 2, 1 };
    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        if (core_nums[i] != correct[i])
        {
            return 1; // error!
        }
    }
    return 0;
}

int test_2()
{
    // core numbers of sample graph
    typedef adjacency_list< listS, vecS, undirectedS, no_property,
        property< edge_weight_t, int > >
        graph_t;
    int num_nodes = 3;
    typedef std::pair< int, int > Edge;

    Edge edge_array[] = { Edge(0, 1), Edge(0, 2), Edge(1, 2) };
    int weights[] = { -1, -2, -2 };
    int num_arcs = sizeof(edge_array) / sizeof(Edge);

    graph_t G(edge_array, edge_array + num_arcs, weights, num_nodes);

    std::vector< int > core_nums(num_vertices(G));
    weighted_core_numbers(
        G, make_iterator_property_map(core_nums.begin(), get(vertex_index, G)));

    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
    }

    int correct[3] = { -1, -1, -4 };
    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        if (core_nums[i] != correct[i])
        {
            return 1; // error!
        }
    }
    return 0;
}

int test_3()
{
    // core numbers of a directed graph, the core numbers of a directed
    // cycle are always one
    typedef adjacency_list< vecS, vecS, directedS > graph_t;
    int num_nodes = 5;
    typedef std::pair< int, int > Edge;

    Edge edge_array[]
        = { Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 0) };
    int num_arcs = sizeof(edge_array) / sizeof(Edge);

    graph_t G(edge_array, edge_array + num_arcs, num_nodes);

    std::vector< int > core_nums(num_vertices(G));
    core_numbers(
        G, make_iterator_property_map(core_nums.begin(), get(vertex_index, G)));

    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        printf("vertex %3lu : %i\n", (unsigned long)i, core_nums[i]);
    }

    int correct[5] = { 1, 1, 1, 1, 1 };
    for (size_t i = 0; i < num_vertices(G); ++i)
    {
        if (core_nums[i] != correct[i])
        {
            return 1; // error!
        }
    }
    return 0;
}

int main(int, char**)
{
    int nfail = 0, ntotal = 0;
    int rval;

    const char* name;

    name = "core_numbers";
    rval = test_1();
    ntotal++;
    if (rval != 0)
    {
        nfail++;
        printf("%20s  %50s\n", name, errstr);
    }
    else
    {
        printf("%20s  success\n", name);
    }

    name = "weighted_core_numbers";
    rval = test_2();
    ntotal++;
    if (rval != 0)
    {
        nfail++;
        printf("%20s  %50s\n", name, errstr);
    }
    else
    {
        printf("%20s  success\n", name);
    }

    name = "directed_corenums";
    rval = test_3();
    ntotal++;
    if (rval != 0)
    {
        nfail++;
        printf("%20s  %50s\n", name, errstr);
    }
    else
    {
        printf("%20s  success\n", name);
    }

    printf("\n");
    printf("Total tests  : %3i\n", ntotal);
    printf("Total failed : %3i\n", nfail);

    return nfail != 0;
}