File: distances.c

package info (click to toggle)
igraph 0.8.5%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,284 kB
  • sloc: ansic: 97,287; cpp: 22,541; yacc: 1,150; makefile: 546; lex: 478; xml: 450; pascal: 82; sh: 9
file content (212 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download | duplicates (2)
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
211
212
/* -*- mode: C -*-  */
/* vim:set ts=4 sw=4 sts=4 et: */
/*
   IGraph library.
   Copyright (C) 2011-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA

*/

#include "igraph_paths.h"
#include "igraph_datatype.h"
#include "igraph_dqueue.h"
#include "igraph_iterators.h"
#include "igraph_interrupt_internal.h"
#include "igraph_vector.h"
#include "igraph_interface.h"
#include "igraph_adjlist.h"

static int igraph_i_eccentricity(const igraph_t *graph,
                                 igraph_vector_t *res,
                                 igraph_vs_t vids,
                                 igraph_neimode_t mode,
                                 const igraph_adjlist_t *adjlist) {

    int no_of_nodes = igraph_vcount(graph);
    igraph_dqueue_long_t q;
    igraph_vit_t vit;
    igraph_vector_int_t counted;
    int i, mark = 1;
    igraph_vector_t vneis;
    igraph_vector_int_t *neis;

    IGRAPH_CHECK(igraph_dqueue_long_init(&q, 100));
    IGRAPH_FINALLY(igraph_dqueue_long_destroy, &q);

    IGRAPH_CHECK(igraph_vit_create(graph, vids, &vit));
    IGRAPH_FINALLY(igraph_vit_destroy, &vit);

    IGRAPH_CHECK(igraph_vector_int_init(&counted, no_of_nodes));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &counted);

    if (!adjlist) {
        IGRAPH_VECTOR_INIT_FINALLY(&vneis, 0);
    }

    IGRAPH_CHECK(igraph_vector_resize(res, IGRAPH_VIT_SIZE(vit)));
    igraph_vector_fill(res, -1);

    for (i = 0, IGRAPH_VIT_RESET(vit);
         !IGRAPH_VIT_END(vit);
         IGRAPH_VIT_NEXT(vit), mark++, i++) {

        long int source;
        source = IGRAPH_VIT_GET(vit);
        IGRAPH_CHECK(igraph_dqueue_long_push(&q, source));
        IGRAPH_CHECK(igraph_dqueue_long_push(&q, 0));
        VECTOR(counted)[source] = mark;

        IGRAPH_ALLOW_INTERRUPTION();

        while (!igraph_dqueue_long_empty(&q)) {
            long int act = igraph_dqueue_long_pop(&q);
            long int dist = igraph_dqueue_long_pop(&q);
            int j, n;

            if (dist > VECTOR(*res)[i]) {
                VECTOR(*res)[i] = dist;
            }

            if (adjlist) {
                neis = igraph_adjlist_get(adjlist, act);
                n = (int) igraph_vector_int_size(neis);
                for (j = 0; j < n; j++) {
                    int nei = (int) VECTOR(*neis)[j];
                    if (VECTOR(counted)[nei] != mark) {
                        VECTOR(counted)[nei] = mark;
                        IGRAPH_CHECK(igraph_dqueue_long_push(&q, nei));
                        IGRAPH_CHECK(igraph_dqueue_long_push(&q, dist + 1));
                    }
                }
            } else {
                IGRAPH_CHECK(igraph_neighbors(graph, &vneis,
                                              (igraph_integer_t) act, mode));
                n = (int) igraph_vector_size(&vneis);
                for (j = 0; j < n; j++) {
                    int nei = (int) VECTOR(vneis)[j];
                    if (VECTOR(counted)[nei] != mark) {
                        VECTOR(counted)[nei] = mark;
                        IGRAPH_CHECK(igraph_dqueue_long_push(&q, nei));
                        IGRAPH_CHECK(igraph_dqueue_long_push(&q, dist + 1));
                    }
                }
            }
        } /* while !igraph_dqueue_long_empty(dqueue) */

    } /* for IGRAPH_VIT_NEXT(vit) */

    if (!adjlist) {
        igraph_vector_destroy(&vneis);
        IGRAPH_FINALLY_CLEAN(1);
    }
    igraph_vector_int_destroy(&counted);
    igraph_vit_destroy(&vit);
    igraph_dqueue_long_destroy(&q);
    IGRAPH_FINALLY_CLEAN(3);

    return 0;
}

/**
 * \function igraph_eccentricity
 * Eccentricity of some vertices
 *
 * The eccentricity of a vertex is calculated by measuring the shortest
 * distance from (or to) the vertex, to (or from) all vertices in the
 * graph, and taking the maximum.
 *
 * </para><para>
 * This implementation ignores vertex pairs that are in different
 * components. Isolated vertices have eccentricity zero.
 *
 * \param graph The input graph, it can be directed or undirected.
 * \param res Pointer to an initialized vector, the result is stored
 *    here.
 * \param vids The vertices for which the eccentricity is calculated.
 * \param mode What kind of paths to consider for the calculation:
 *    \c IGRAPH_OUT, paths that follow edge directions;
 *    \c IGRAPH_IN, paths that follow the opposite directions; and
 *    \c IGRAPH_ALL, paths that ignore edge directions. This argument
 *    is ignored for undirected graphs.
 * \return Error code.
 *
 * Time complexity: O(v*(|V|+|E|)), where |V| is the number of
 * vertices, |E| is the number of edges and v is the number of
 * vertices for which eccentricity is calculated.
 *
 * \sa \ref igraph_radius().
 *
 * \example examples/simple/igraph_eccentricity.c
 */

int igraph_eccentricity(const igraph_t *graph,
                        igraph_vector_t *res,
                        igraph_vs_t vids,
                        igraph_neimode_t mode) {

    return igraph_i_eccentricity(graph, res, vids, mode, /*adjlist=*/ 0);
}

/**
 * \function igraph_radius
 * Radius of a graph
 *
 * The radius of a graph is the defined as the minimum eccentricity of
 * its vertices, see \ref igraph_eccentricity().
 *
 * \param graph The input graph, it can be directed or undirected.
 * \param radius Pointer to a real variable, the result is stored
 *   here.
 * \param mode What kind of paths to consider for the calculation:
 *    \c IGRAPH_OUT, paths that follow edge directions;
 *    \c IGRAPH_IN, paths that follow the opposite directions; and
 *    \c IGRAPH_ALL, paths that ignore edge directions. This argument
 *    is ignored for undirected graphs.
 * \return Error code.
 *
 * Time complexity: O(|V|(|V|+|E|)), where |V| is the number of
 * vertices and |E| is the number of edges.
 *
 * \sa \ref igraph_eccentricity().
 *
 * \example examples/simple/igraph_radius.c
 */

int igraph_radius(const igraph_t *graph, igraph_real_t *radius,
                  igraph_neimode_t mode) {

    int no_of_nodes = igraph_vcount(graph);

    if (no_of_nodes == 0) {
        *radius = IGRAPH_NAN;
    } else {
        igraph_adjlist_t adjlist;
        igraph_vector_t ecc;
        IGRAPH_CHECK(igraph_adjlist_init(graph, &adjlist, mode));
        IGRAPH_FINALLY(igraph_adjlist_destroy, &adjlist);
        IGRAPH_VECTOR_INIT_FINALLY(&ecc, igraph_vcount(graph));
        IGRAPH_CHECK(igraph_i_eccentricity(graph, &ecc, igraph_vss_all(),
                                           mode, &adjlist));
        *radius = igraph_vector_min(&ecc);
        igraph_vector_destroy(&ecc);
        igraph_adjlist_destroy(&adjlist);
        IGRAPH_FINALLY_CLEAN(2);
    }

    return 0;
}