File: igraph_eigenvector_centrality.c

package info (click to toggle)
igraph 0.10.15%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,364 kB
  • sloc: ansic: 131,663; cpp: 20,571; xml: 2,747; python: 411; makefile: 162; javascript: 20; sh: 9
file content (126 lines) | stat: -rw-r--r-- 4,227 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
/* -*- mode: C -*-  */
/*
   IGraph library.
   Copyright (C) 2022  The igraph development team <igraph@igraph.org>

   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, see <https://www.gnu.org/licenses/>.
*/

#include "igraph.h"
#include "test_utilities.h"

#include <math.h>

void print_and_destroy(igraph_t *g, igraph_vector_t *weights)
{
    igraph_vector_t v;
    igraph_real_t value;

    igraph_vector_init(&v, 0);
    igraph_eigenvector_centrality(g, &v, &value, /* directed */ true,
                                  /* scale */ true, weights,
                                  /* options */ NULL);

    printf("Eigenvalue: %g\n", value);
    printf("Eigenvector:\n");
    igraph_vector_print(&v);
    printf("\n");

    igraph_destroy(g);
    igraph_vector_destroy(&v);
}

int main(void) {

    igraph_t g;
    igraph_vector_t weights;

    printf("Undirected graph with no vertices:\n");
    igraph_small(&g, 0, IGRAPH_UNDIRECTED, -1);
    print_and_destroy(&g, NULL);

    printf("Directed graph with no vertices:\n");
    igraph_small(&g, 0, IGRAPH_DIRECTED, -1);
    print_and_destroy(&g, NULL);

    printf("Undirected graph with no edges:\n");
    igraph_small(&g, 5, IGRAPH_UNDIRECTED, -1);
    print_and_destroy(&g, NULL);

    printf("Directed graph with no edges:\n");
    igraph_small(&g, 5, IGRAPH_DIRECTED, -1);
    print_and_destroy(&g, NULL);

    printf("Undirected full graph:\n");
    igraph_full(&g, 5, IGRAPH_UNDIRECTED, /* loops */ false);
    print_and_destroy(&g, NULL);

    printf("Directed full graph:\n");
    igraph_full(&g, 5, IGRAPH_DIRECTED, /* loops */ false);
    print_and_destroy(&g, NULL);

    printf("Undirected full graph with weights:\n");
    igraph_vector_init(&weights, 10);
    igraph_vector_fill(&weights, 1);
    igraph_full(&g, 5, IGRAPH_UNDIRECTED, /* loops */ false);
    print_and_destroy(&g, &weights);
    igraph_vector_destroy(&weights);

    printf("Directed full graph with weights:\n");
    igraph_full(&g, 5, IGRAPH_DIRECTED, /* loops */ false);
    igraph_vector_init(&weights, 20);
    igraph_vector_fill(&weights, 1);
    print_and_destroy(&g, &weights);
    igraph_vector_destroy(&weights);

    printf("Undirected star graph with weights:\n");
    igraph_vector_init(&weights, 4);
    igraph_vector_fill(&weights, 1);
    igraph_star(&g, 5, IGRAPH_STAR_UNDIRECTED, 0);
    print_and_destroy(&g, &weights);
    igraph_vector_destroy(&weights);

    printf("Directed out-star graph with weights:\n");
    igraph_star(&g, 5, IGRAPH_STAR_OUT, 0);
    igraph_vector_init(&weights, 4);
    igraph_vector_fill(&weights, 1);
    print_and_destroy(&g, &weights);
    igraph_vector_destroy(&weights);

    printf("Directed in-star graph with weights:\n");
    igraph_star(&g, 5, IGRAPH_STAR_IN, 0);
    igraph_vector_init(&weights, 4);
    igraph_vector_fill(&weights, 2);
    print_and_destroy(&g, &weights);
    igraph_vector_destroy(&weights);

    VERIFY_FINALLY_STACK();

    printf("Check handling of wrong number of weights.\n");
    igraph_vector_init(&weights, 2);
    igraph_vector_fill(&weights, 1);
    igraph_full(&g, 5, IGRAPH_DIRECTED, /* loops */ false);
    CHECK_ERROR(igraph_eigenvector_centrality(&g, NULL, NULL, /* directed */ true,
                                  /* scale */ true, &weights,
                                  /* options */ NULL), IGRAPH_EINVAL);
    CHECK_ERROR(igraph_eigenvector_centrality(&g, NULL, NULL, /* directed */ false,
                                  /* scale */ true, &weights,
                                  /* options */ NULL), IGRAPH_EINVAL);
    igraph_vector_destroy(&weights);
    igraph_destroy(&g);

    VERIFY_FINALLY_STACK();

    return 0;
}