~ubuntu-branches/ubuntu/lucid/igraph/lucid

« back to all changes in this revision

Viewing changes to examples/simple/igraph_community_leading_eigenvector.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Malaterre
  • Date: 2009-11-16 18:12:42 UTC
  • Revision ID: james.westby@ubuntu.com-20091116181242-mzv9p5fz9uj57xd1
Tags: upstream-0.5.3
ImportĀ upstreamĀ versionĀ 0.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C -*-  */
 
2
/* 
 
3
   IGraph library.
 
4
   Copyright (C) 2007  Gabor Csardi <csardi@rmki.kfki.hu>
 
5
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
6
   
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2 of the License, or
 
10
   (at your option) any later version.
 
11
   
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
   
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
 
20
   02110-1301 USA
 
21
 
 
22
*/
 
23
 
 
24
#include <igraph.h>
 
25
 
 
26
int print_vector(const igraph_vector_t *v) {
 
27
  long int i, n=igraph_vector_size(v);
 
28
  for (i=0; i<n; i++) {
 
29
    printf("%.2g", (double)VECTOR(*v)[i]);
 
30
    if (i!=n-1) { printf(" "); }
 
31
  }
 
32
  printf("\n");
 
33
  return 0;
 
34
}
 
35
 
 
36
int print_matrix(const igraph_matrix_t *m) {
 
37
  long int i, j, nrow=igraph_matrix_nrow(m), ncol=igraph_matrix_ncol(m);
 
38
  for (i=0; i<nrow; i++) {
 
39
    for (j=0; j<ncol; j++) {
 
40
      printf("%.2g", (double)MATRIX(*m, i, j));
 
41
      if (j!=ncol-1) { printf(" "); }
 
42
    }
 
43
    printf("\n");
 
44
  }
 
45
  return 0;
 
46
}
 
47
 
 
48
int main() {
 
49
 
 
50
  igraph_t g;
 
51
  igraph_matrix_t merges;
 
52
  igraph_vector_t membership;
 
53
  long int i, j;
 
54
  igraph_bool_t split;
 
55
  igraph_vector_t x;
 
56
  igraph_real_t val;
 
57
  igraph_arpack_options_t options;
 
58
  
 
59
  /* Zachary Karate club */
 
60
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
 
61
               0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
 
62
               0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
 
63
               0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
 
64
               0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
 
65
               1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
 
66
               2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
 
67
               2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
 
68
               4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
 
69
               6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
 
70
               13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
 
71
               18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
 
72
               22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
 
73
               23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
 
74
               25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
 
75
               28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
 
76
               31, 32, 31, 33, 32, 33,
 
77
               -1);  
 
78
 
 
79
  /* Make one step with all methods */
 
80
  igraph_matrix_init(&merges, 0, 0);
 
81
  igraph_vector_init(&membership, 0);
 
82
  igraph_vector_init(&x, 0);
 
83
  igraph_arpack_options_init(&options);
 
84
  igraph_community_leading_eigenvector_naive(&g, &merges, &membership, 1, &options);
 
85
 
 
86
  print_matrix(&merges);
 
87
  print_vector(&membership);
 
88
 
 
89
  igraph_community_leading_eigenvector(&g, &merges, &membership, 1, &options);
 
90
 
 
91
  print_matrix(&merges);
 
92
  print_vector(&membership);
 
93
 
 
94
  igraph_vector_null(&membership);
 
95
  igraph_community_leading_eigenvector_step(&g, &membership, 0, &split,
 
96
                                            &x, &val, &options, 0);
 
97
 
 
98
  print_vector(&membership);
 
99
  print_vector(&x);
 
100
 
 
101
  printf("\n");
 
102
 
 
103
  /* Make all the steps */
 
104
  igraph_community_leading_eigenvector(&g, &merges, &membership, igraph_vcount(&g),
 
105
                                       &options);
 
106
 
 
107
  print_matrix(&merges);
 
108
  print_vector(&membership);
 
109
 
 
110
  /* Try to make one more step from here, should fail */
 
111
  for (i=0; i<igraph_matrix_nrow(&merges)+1; i++) {
 
112
    igraph_community_leading_eigenvector_step(&g, &membership,
 
113
                                              i, &split, &x, &val, &options, 0);
 
114
    if (split) {
 
115
      printf("Impossible, community %li splitted.\n", i);
 
116
      return 1;
 
117
    }
 
118
  }
 
119
  
 
120
  igraph_vector_destroy(&x);
 
121
  igraph_vector_destroy(&membership);
 
122
  igraph_matrix_destroy(&merges);
 
123
  igraph_destroy(&g);
 
124
  
 
125
  return 0;
 
126
}