1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2011-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard st, Cambridge MA, 02139 USA
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 
main()26 int main() {
27 
28     int nodes = 10;
29     igraph_t g;
30     igraph_matrix_t L, R;
31     igraph_sparsemat_t Lsparse, Rsparse;
32     igraph_matrix_t V;
33     igraph_matrix_complex_t V2;
34     igraph_sparsemat_t laplacian;
35     igraph_vector_t groups;
36     igraph_eigen_which_t which;
37 
38     igraph_matrix_init(&L, 0, 0);
39     igraph_matrix_init(&R, 0, 0);
40     igraph_matrix_init(&V, 0, 0);
41     igraph_matrix_complex_init(&V2, 0, 0);
42     igraph_vector_init(&groups, 0);
43 
44     igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
45 
46     igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g) * 2);
47 
48     igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian,
49                      /*normalized=*/ 0, /*weights=*/ 0);
50 
51     which.pos = IGRAPH_EIGEN_LM;
52     which.howmany = 1;
53 
54     igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, 10,
55                                     /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK,
56                                     &which, /*options=*/ 0, /*storage=*/ 0,
57                                     /*values=*/ 0, &V2);
58     igraph_matrix_complex_real(&V2, &V);
59 
60 #define SEMI()                              \
61     do {                                  \
62         igraph_scg_semiprojectors(&groups, IGRAPH_SCG_LAPLACIAN, &L, &R,    \
63                                   &Lsparse, &Rsparse, /*p=*/ 0,     \
64                                   IGRAPH_SCG_NORM_ROW);         \
65     } while(0)
66 
67 #define PRINTRES()              \
68     do {                      \
69         printf("----------------------\n");     \
70         igraph_matrix_print(&L);            \
71         printf("---\n");                \
72         igraph_matrix_print(&R);            \
73         printf("---\n");                \
74         igraph_sparsemat_destroy(&Lsparse);         \
75         igraph_sparsemat_destroy(&Rsparse);         \
76     } while (0)
77 
78     /* -------------- */
79 
80     igraph_scg_grouping(&V, &groups, /*intervals=*/ 3,
81                         /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
82                         IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000);
83     SEMI();
84     PRINTRES();
85 
86     /* -------------- */
87 
88     igraph_scg_grouping(&V, &groups, /*intervals=*/ 2,
89                         /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
90                         IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000);
91     SEMI();
92     PRINTRES();
93 
94     /* -------------- */
95 
96     igraph_scg_grouping(&V, &groups, /*intervals=*/ 2,
97                         /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
98                         IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000);
99     SEMI();
100     PRINTRES();
101 
102     /* -------------- */
103 
104     igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0,
105                         /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
106                         IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000);
107     SEMI();
108     PRINTRES();
109 
110     /* -------------- */
111 
112     igraph_matrix_destroy(&L);
113     igraph_matrix_destroy(&R);
114     igraph_matrix_destroy(&V);
115     igraph_matrix_complex_destroy(&V2);
116     igraph_vector_destroy(&groups);
117     igraph_sparsemat_destroy(&laplacian);
118     igraph_destroy(&g);
119 
120     return 0;
121 }
122