~ubuntu-branches/ubuntu/saucy/igraph/saucy

« back to all changes in this revision

Viewing changes to examples/simple/igraph_decompose.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) 2006  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
#include <stdlib.h>
 
26
 
 
27
void free_complist(igraph_vector_ptr_t *complist) {
 
28
  long int i;
 
29
  for (i=0; i<igraph_vector_ptr_size(complist); i++) {
 
30
    igraph_destroy(VECTOR(*complist)[i]);
 
31
    free(VECTOR(*complist)[i]);
 
32
  }
 
33
}
 
34
 
 
35
int main() {
 
36
  
 
37
  igraph_t ring, g;
 
38
  igraph_vector_ptr_t complist;
 
39
  long int i;
 
40
  igraph_real_t edges[]= { 0,1,1,2,2,0,
 
41
                    3,4,4,5,5,6,
 
42
                    8,9,9,10 };
 
43
  igraph_vector_t v;
 
44
  igraph_vector_t idvect;
 
45
 
 
46
  /* A ring, a single component */
 
47
  igraph_ring(&ring, 10, IGRAPH_UNDIRECTED, 0, 1);
 
48
  
 
49
  igraph_vector_ptr_init(&complist, 0);
 
50
  igraph_decompose(&ring, &complist, IGRAPH_WEAK, -1, 0);
 
51
  igraph_write_graph_edgelist(VECTOR(complist)[0], stdout);
 
52
  free_complist(&complist);
 
53
  igraph_destroy(&ring);
 
54
  
 
55
  /* random graph with a giant component */
 
56
  igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, 4.0/100, 
 
57
                          IGRAPH_UNDIRECTED, 0);
 
58
  igraph_decompose(&g, &complist, IGRAPH_WEAK, -1, 20);
 
59
  if (igraph_vector_ptr_size(&complist) != 1) { 
 
60
    return 1;
 
61
  }
 
62
  free_complist(&complist);
 
63
  igraph_destroy(&g);
 
64
  
 
65
  /* a toy graph, three components maximum, with at least 2 vertices each */
 
66
  igraph_create(&g, 
 
67
                igraph_vector_view(&v, edges, sizeof(edges)/sizeof(igraph_real_t)), 
 
68
                0, IGRAPH_DIRECTED);
 
69
  igraph_decompose(&g, &complist, IGRAPH_WEAK, 3, 2);
 
70
  for (i=0; i<igraph_vector_ptr_size(&complist); i++) {
 
71
    igraph_write_graph_edgelist(VECTOR(complist)[i], stdout);
 
72
  }
 
73
  free_complist(&complist);
 
74
  igraph_destroy(&g);
 
75
 
 
76
  /* The same graph, this time with vertex attributes */
 
77
/*   igraph_vector_init_seq(&idvect, 0, igraph_vcount(&g)-1); */
 
78
/*   igraph_add_vertex_attribute(&g, "id", IGRAPH_ATTRIBUTE_NUM); */
 
79
/*   igraph_set_vertex_attributes(&g, "id", IGRAPH_VS_ALL(&g), &idvect); */
 
80
/*   igraph_vector_destroy(&idvect); */
 
81
 
 
82
/*   igraph_decompose(&g, &complist, IGRAPH_WEAK, 3, 2); */
 
83
/*   for (i=0; i<igraph_vector_ptr_size(&complist); i++) { */
 
84
/*     igraph_t *comp=VECTOR(complist)[i]; */
 
85
/*     igraph_es_t es; */
 
86
/*     igraph_es_all(comp, &es); */
 
87
/*     while (!igraph_es_end(comp, &es)) { */
 
88
/*       igraph_real_t *from, *to;  */
 
89
/*       igraph_get_vertex_attribute(comp, "id", igraph_es_from(comp, &es), */
 
90
/*                                (void**) &from, 0); */
 
91
/*       igraph_get_vertex_attribute(comp, "id", igraph_es_to(comp, &es), */
 
92
/*                                (void**) &to, 0); */
 
93
/*       printf("%li %li\n", (long int) *from, (long int) *to); */
 
94
/*       igraph_es_next(comp, &es); */
 
95
/*     } */
 
96
/*   } */
 
97
  
 
98
/*   free_complist(&complist); */
 
99
/*   igraph_destroy(&g);   */
 
100
  
 
101
  igraph_vector_ptr_destroy(&complist);
 
102
 
 
103
  return 0;
 
104
}