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

« back to all changes in this revision

Viewing changes to examples/simple/igraph_community_label_propagation.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
/* vim:set ts=2 sw=2 sts=2 et: */
 
3
/* 
 
4
   IGraph library.
 
5
   Copyright (C) 2007  Gabor Csardi <csardi@rmki.kfki.hu>
 
6
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
7
   
 
8
   This program is free software; you can redistribute it and/or modify
 
9
   it under the terms of the GNU General Public License as published by
 
10
   the Free Software Foundation; either version 2 of the License, or
 
11
   (at your option) any later version.
 
12
   
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
   
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program; if not, write to the Free Software
 
20
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
 
21
   02110-1301 USA
 
22
 
 
23
*/
 
24
 
 
25
#include <igraph.h>
 
26
 
 
27
int main() {
 
28
  igraph_t g;
 
29
  igraph_vector_t membership, weights, initial;
 
30
  igraph_vector_bool_t fixed;
 
31
  igraph_bool_t ok;
 
32
  long int i, j;
 
33
  long int no_of_nodes;
 
34
 
 
35
  /* Zachary Karate club -- this is just a quick smoke test */
 
36
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
 
37
               0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
 
38
               0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
 
39
               0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
 
40
               0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
 
41
               1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
 
42
               2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
 
43
               2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
 
44
               4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
 
45
               6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
 
46
               13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
 
47
               18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
 
48
               22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
 
49
               23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
 
50
               25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
 
51
               28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
 
52
               31, 32, 31, 33, 32, 33,
 
53
               -1);  
 
54
  
 
55
  igraph_vector_init(&membership, 0);
 
56
 
 
57
  if (igraph_vector_max(&membership) > 3) {
 
58
    printf("Resulting graph had more than four clusters:\n");
 
59
    for (i=0; i<igraph_vcount(&g); i++)
 
60
      printf("%li ", (long)VECTOR(membership)[i]);
 
61
    printf("\n");
 
62
    return 1;
 
63
  }
 
64
 
 
65
  igraph_destroy(&g);
 
66
  
 
67
  /* Simple star graph to test weights */
 
68
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
 
69
               0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
 
70
               2,  3,  2,  4,  3,  4,  3,  5,  4,  5,  -1);
 
71
  igraph_vector_init_int_end(&weights, -1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1);
 
72
  igraph_vector_init_int_end(&initial, -1, 0, 0, 1, 1, 1, 1, -1);
 
73
  igraph_vector_bool_init(&fixed, 6);
 
74
  VECTOR(fixed)[3] = 1;
 
75
  VECTOR(fixed)[4] = 1;
 
76
  VECTOR(fixed)[5] = 1;
 
77
  igraph_community_label_propagation(&g, &membership, &weights,
 
78
                  &initial, &fixed);
 
79
  for (i=0; i<igraph_vcount(&g); i++)
 
80
    if (VECTOR(membership)[i] != (i < 2 ? 0 : 1)) return 3;
 
81
  igraph_community_label_propagation(&g, &membership, 0,
 
82
                  &initial, &fixed);
 
83
  for (i=0; i<igraph_vcount(&g); i++)
 
84
    if (VECTOR(membership)[i] != 0) return 4;
 
85
 
 
86
  igraph_vector_bool_destroy(&fixed);
 
87
  igraph_vector_destroy(&weights);
 
88
  igraph_vector_destroy(&initial);
 
89
  igraph_destroy(&g);
 
90
 
 
91
  igraph_vector_destroy(&membership);
 
92
 
 
93
  return 0;
 
94
}
 
95