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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
|
/* -*- mode: C -*- */
/*
IGraph library.
Copyright (C) 2011-12 Gabor Csardi <csardi.gabor@gmail.com>
334 Harvard st, Cambridge, MA, 02138 USA
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
* SCGlib : A C library for the spectral coarse graining of matrices
* as described in the paper: Shrinking Matrices while preserving their
* eigenpairs with Application to the Spectral Coarse Graining of Graphs.
* Preprint available at <http://people.epfl.ch/david.morton>
*
* Copyright (C) 2008 David Morton de Lachapelle <david.morton@a3.epfl.ch>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* DESCRIPTION
* -----------
* The grouping function takes as argument 'nev' eigenvectors and
* and tries to minimize the eigenpair shifts induced by the coarse
* graining (Section 5 of the above reference). The eigenvectors are
* stored in a 'nev'x'n' matrix 'v'.
* The 'algo' parameter can take the following values
* 1 -> Optimal method (sec. 5.3.1)
* 2 -> Intervals+k-means (sec. 5.3.3)
* 3 -> Intervals (sec. 5.3.2)
* 4 -> Exact SCG (sec. 5.4.1--last paragraph)
* 'nt' is a vector of length 'nev' giving either the size of the
* partitions (if algo = 1) or the number of intervals to cut the
* eigenvectors if algo = 2 or algo = 3. When algo = 4 this parameter
* is ignored. 'maxiter' fixes the maximum number of iterations of
* the k-means algorithm, and is only considered when algo = 2.
* All the algorithms try to find a minimizing partition of
* ||v_i-Pv_i|| where P is a problem-specific projector and v_i denotes
* the eigenvectors stored in v. The final partition is worked out
* as decribed in Method 1 of Section 5.4.2.
* 'matrix' provides the type of SCG (i.e. the form of P). So far,
* the options are those described in section 6, that is:
* 1 -> Symmetric (sec. 6.1)
* 2 -> Laplacian (sec. 6.2)
* 3 -> Stochastic (sec. 6.3)
* In the stochastic case, a valid distribution probability 'p' must be
* provided. In all other cases, 'p' is ignored and can be set to NULL.
* The group labels in the final partition are given in 'gr' as positive
* consecutive integers starting from 0.
*/
#include "igraph_scg.h"
#include "igraph_eigen.h"
#include "igraph_interface.h"
#include "igraph_structural.h"
#include "igraph_constructors.h"
#include "igraph_conversion.h"
#include "igraph_memory.h"
#include "scg_headers.h"
#include "math.h"
/**
* \section about_scg
*
* <para>
* The SCG functions provide a framework, called Spectral Coarse Graining
* (SCG), for reducing large graphs while preserving their
* <emphasis>spectral-related features</emphasis>, that is features
* closely related with the eigenvalues and eigenvectors of a graph
* matrix (which for now can be the adjacency, the stochastic, or the
* Laplacian matrix).
* </para>
*
* <para>
* Common examples of such features comprise the first-passage-time of
* random walkers on Markovian graphs, thermodynamic properties of
* lattice models in statistical physics (e.g. Ising model), and the
* epidemic threshold of epidemic network models (SIR and SIS models).
* </para>
*
* <para>
* SCG differs from traditional clustering schemes by producing a
* <emphasis>coarse-grained graph</emphasis> (not just a partition of
* the vertices), representative of the original one. As shown in [1],
* Principal Component Analysis can be viewed as a particular SCG,
* called <emphasis>exact SCG</emphasis>, where the matrix to be
* coarse-grained is the covariance matrix of some data set.
* </para>
*
* <para>
* SCG should be of interest to practitioners of various
* fields dealing with problems where matrix eigenpairs play an important
* role, as for instance is the case of dynamical processes on networks.
* </para>
*
* <section><title>SCG in brief</title>
* <para>
* The main idea of SCG is to operate on a matrix a shrinkage operation
* specifically designed to preserve some of the matrix eigenpairs while
* not altering other important matrix features (such as its structure).
* Mathematically, this idea was expressed as follows. Consider a
* (complex) n x n matrix M and form the product
* <blockquote><para><phrase role="math">
* M'=LMR*,
* </phrase></para></blockquote>
* where n' < n and L, R are from C[n'xn]} and are such
* that LR*=I[n'] (R* denotes the conjugate transpose of R). Under
* these assumptions, it can be shown that P=R*L is an n'-rank
* projector and that, if (lambda, v) is a (right)
* eigenpair of M (i.e. Mv=lambda v} and P is orthogonal, there exists
* an eigenvalue lambda' of M' such that
* <blockquote><para><phrase role="math">
* |lambda-lambda'| <= const ||e[P](v)||
* [1+O(||e[P](v)||<superscript>2</superscript>)],
* </phrase></para></blockquote>
* where ||e[P](v)||=||v-Pv||. Hence, if P (or equivalently
* L, R) is chosen so as to make ||e[P](v)|| as small as possible, one
* can preserve to any desired level the original eigenvalue
* lambda in the coarse-grained matrix M';
* under extra assumptions on M, this result can be generalized to
* eigenvectors [1]. This leads to the following generic definition of a
* SCG problem.
* </para>
*
* <para>
* Given M (C[nxn]) and (lambda, v), a (right) eigenpair of M to be
* preserved by the coarse graining, the problem is to find a projector
* P' solving
* <blockquote><para><phrase role="math">
* min(||e[P](v)||, p in Omega),
* </phrase></para></blockquote>
* where Omega is a set of projectors in C[nxn] described by some
* ad hoc constraints c[1], ..., c[r]
* (e.g. c[1]: P in R[nxn], c[2]: P=t(P), c[3]: P[i,j] >= 0}, etc).
* </para>
*
* <para>
* Choosing pertinent constraints to solve the SCG problem is of great
* importance in applications. For instance, in the absence of
* constraints the SCG problem is solved trivially by
* P'=vv* (v is assumed normalized). We have designed a particular
* constraint, called <emphasis>homogeneous mixing</emphasis>, which
* ensures that vertices belonging to the same group are merged
* consistently from a physical point of view (see [1] for
* details). Under this constraint the SCG problem reduces to finding
* the partition of 1, ..., n (labeling the original vertices)
* minimizing
* <blockquote><para><phrase role="math">
* ||e[P](v)||<superscript>2</superscript> =
* sum([v(i)-(Pv)(i)]<superscript>2</superscript>;
* alpha=1,...,n', i in alpha),
* </phrase></para></blockquote>
* where alpha denotes a group (i.e. a block) in a partition of
* {1, ..., n}, and |alpha| is the number of elements in alpha.
* </para>
*
* <para>
* If M is symmetric or stochastic, for instance, then it may be
* desirable (or mandatory) to choose L, R so that M' is symmetric or
* stochastic as well. This <emphasis>structural constraint</emphasis>
* has led to the construction of particular semi-projectors for
* symmetric [1], stochastic [3] and Laplacian [2] matrices, that are
* made available.
* </para>
*
* <para>
* In short, the coarse graining of matrices and graphs involves:
* \olist
* \oli Retrieving a matrix or a graph matrix M from the
* problem.
* \oli Computing the eigenpairs of M to be preserved in the
* coarse-grained graph or matrix.
* \oli Setting some problem-specific constraints (e.g. dimension of
* the coarse-grained object).
* \oli Solving the constrained SCG problem, that is finding P'.
* \oli Computing from P' two semi-projectors L' and R'
* (e.g. following the method proposed in [1]).
* \oli Working out the product M'=L'MR'* and, if needed, defining
* from M' a coarse-grained graph.
* \endolist
* </para>
* </section>
*
* <section><title>Functions for performing SCG</title>
* <para>
* The main functions are \ref igraph_scg_adjacency(), \ref
* igraph_scg_laplacian() and \ref igraph_scg_stochastic().
* These functions handle all the steps involved in the
* Spectral Coarse Graining (SCG) of some particular matrices and graphs
* as described above and in reference [1]. In more details,
* they compute some prescribed eigenpairs of a matrix or a
* graph matrix, (for now adjacency, Laplacian and stochastic matrices are
* available), work out an optimal partition to preserve the eigenpairs,
* and finally output a coarse-grained matrix or graph along with other
* useful information.
* </para>
*
* <para>
* These steps can also be carried out independently: (1) Use
* \ref igraph_get_adjacency(), \ref igraph_get_sparsemat(),
* \ref igraph_laplacian(), \ref igraph_get_stochastic() or \ref
* igraph_get_stochastic_sparsemat() to compute a matrix M.
* (2) Work out some prescribed eigenpairs of M e.g. by
* means of \ref igraph_arpack_rssolve() or \ref
* igraph_arpack_rnsolve(). (3) Invoke one the four
* algorithms of the function \ref igraph_scg_grouping() to get a
* partition that will preserve the eigenpairs in the coarse-grained
* matrix. (4) Compute the semi-projectors L and R using
* \ref igraph_scg_semiprojectors() and from there the coarse-grained
* matrix M'=LMR*. If necessary, construct a coarse-grained graph from
* M' (e.g. as in [1]).
* </para>
* </section>
*
* <section><title>References</title>
* <para>
* [1] D. Morton de Lachapelle, D. Gfeller, and P. De Los Rios,
* Shrinking Matrices while Preserving their Eigenpairs with Application
* to the Spectral Coarse Graining of Graphs. Submitted to
* <emphasis>SIAM Journal on Matrix Analysis and
* Applications</emphasis>, 2008.
* http://people.epfl.ch/david.morton
* </para>
* <para>
* [2] D. Gfeller, and P. De Los Rios, Spectral Coarse Graining and
* Synchronization in Oscillator Networks.
* <emphasis>Physical Review Letters</emphasis>,
* <emphasis role="strong">100</emphasis>(17), 2008.
* http://arxiv.org/abs/0708.2055
* </para>
* <para>
* [3] D. Gfeller, and P. De Los Rios, Spectral Coarse Graining of Complex
* Networks, <emphasis>Physical Review Letters</emphasis>,
* <emphasis role="strong">99</emphasis>(3), 2007.
* http://arxiv.org/abs/0706.0812
* </para>
* </section>
*/
/**
* \function igraph_scg_grouping
* \brief SCG problem solver
*
* This function solves the Spectral Coarse Graining (SCG) problem;
* either exactly, or approximately but faster.
*
* </para><para>
* The algorithm \c IGRAPH_SCG_OPTIMUM solves exactly the SCG problem
* for each eigenvector in \p V. The running time of this algorithm is
* O(max(nt) m^2) for the symmetric and laplacian matrix problems
* It is O(m^3) for the stochastic problem. Here m is the number
* of rows in \p V. In all three cases, the memory usage is O(m^2).
*
* </para><para>
* The algorithms \c IGRAPH_SCG_INTERV and \c IGRAPH_SCG_INTERV_KM solve
* approximately the SCG problem by performing a (for now) constant
* binning of the components of the eigenvectors, that is \p nt
* <code>VECTOR(nt_vec)[i]</code>) constant-size bins are used to
* partition <code>V[,i]</code>. When \p algo is \c
* IGRAPH_SCG_INTERV_KM, the (Lloyd) k-means algorithm is
* run on each partition obtained by \c IGRAPH_SCG_INTERV to improve
* accuracy.
*
* </para><para>
* Once a minimizing partition (either exact or approximate) has been
* found for each eigenvector, the final grouping is worked out as
* follows: two vertices are grouped together in the final partition if
* they are grouped together in each minimizing partition. In general the
* size of the final partition is not known in advance when the number
* of columns in \p V is larger than one.
*
* </para><para>
* Finally, the algorithm \c IGRAPH_SCG_EXACT groups the vertices with
* equal components in each eigenvector. The last three algorithms
* essentially have linear running time and memory load.
*
* \param V The matrix of eigenvectors to be preserved by coarse
* graining, each column is an eigenvector.
* \param groups Pointer to an initialized vector, the result of the
* SCG is stored here.
* \param nt Positive integer. When \p algo is \c IGRAPH_SCG_OPTIMUM,
* it gives the number of groups to partition each eigenvector
* separately. When \p algo is \c IGRAPH_SCG_INTERV or \c
* IGRAPH_SCG_INTERV_KM, it gives the number of intervals to
* partition each eigenvector. This is ignored when \p algo is \c
* IGRAPH_SCG_EXACT.
* \param nt_vec A numeric vector of length one or the length must
* match the number of eigenvectors given in \p V, or a \c NULL
* pointer. If not \c NULL, then this argument gives the number of
* groups or intervals, and \p nt is ignored. Different number of
* groups or intervals can be specified for each eigenvector.
* \param mtype The type of semi-projectors used in the SCG. Possible
* values are \c IGRAPH_SCG_SYMMETRIC, \c IGRAPH_SCG_STOCHASTIC and
* \c IGRAPH_SCG_LAPLACIAN.
* \param algo The algorithm to solve the SCG problem. Possible
* values: \c IGRAPH_SCG_OPTIMUM, \c IGRAPH_SCG_INTERV_KM, \c
* IGRAPH_SCG_INTERV and \c IGRAPH_SCG_EXACT. Please see the
* details about them above.
* \param p A probability vector, or \c NULL. This argument must be
* given if \p mtype is \c IGRAPH_SCG_STOCHASTIC, but it is ignored
* otherwise. For the stochastic case it gives the stationary
* probability distribution of a Markov chain, the one specified by
* the graph/matrix under study.
* \param maxiter A positive integer giving the number of iterations
* of the k-means algorithm when \p algo is \c
* IGRAPH_SCG_INTERV_KM. It is ignored in other cases. A reasonable
* (initial) value for this argument is 100.
* \return Error code.
*
* Time complexity: see description above.
*
* \sa \ref igraph_scg_adjacency(), \ref igraph_scg_laplacian(), \ref
* igraph_scg_stochastic().
*
* \example examples/simple/igraph_scg_grouping.c
* \example examples/simple/igraph_scg_grouping2.c
* \example examples/simple/igraph_scg_grouping3.c
* \example examples/simple/igraph_scg_grouping4.c
*/
int igraph_scg_grouping(const igraph_matrix_t *V,
igraph_vector_t *groups,
igraph_integer_t nt,
const igraph_vector_t *nt_vec,
igraph_scg_matrix_t mtype,
igraph_scg_algorithm_t algo,
const igraph_vector_t *p,
igraph_integer_t maxiter) {
int no_of_nodes=(int) igraph_matrix_nrow(V);
int nev=(int) igraph_matrix_ncol(V);
igraph_matrix_int_t gr_mat;
int i;
if (nt_vec && igraph_vector_size(nt_vec) != 1 &&
igraph_vector_size(nt_vec) != nev) {
IGRAPH_ERROR("Invalid length for interval specification", IGRAPH_EINVAL);
}
if (nt_vec && igraph_vector_size(nt_vec) == 1) {
nt=(igraph_integer_t) VECTOR(*nt_vec)[0];
nt_vec=0;
}
if (!nt_vec && algo != IGRAPH_SCG_EXACT) {
if (nt <= 1 || nt >= no_of_nodes) {
IGRAPH_ERROR("Invalid interval specification", IGRAPH_EINVAL);
}
} else if (algo != IGRAPH_SCG_EXACT) {
igraph_real_t min, max;
igraph_vector_minmax(nt_vec, &min, &max);
if (min <= 1 || max >= no_of_nodes) {
IGRAPH_ERROR("Invalid interval specification", IGRAPH_EINVAL);
}
}
if (mtype == IGRAPH_SCG_STOCHASTIC && !p) {
IGRAPH_ERROR("`p' must be given for the stochastic matrix case",
IGRAPH_EINVAL);
}
if (p && igraph_vector_size(p) != no_of_nodes) {
IGRAPH_ERROR("Invalid `p' vector size", IGRAPH_EINVAL);
}
IGRAPH_CHECK(igraph_vector_resize(groups, no_of_nodes));
#define INVEC(i) (nt_vec ? VECTOR(*nt_vec)[i] : nt)
IGRAPH_CHECK(igraph_matrix_int_init(&gr_mat, no_of_nodes, nev));
IGRAPH_FINALLY(igraph_matrix_int_destroy, &gr_mat);
switch (algo) {
case IGRAPH_SCG_OPTIMUM:
for (i=0; i<nev; i++) {
IGRAPH_CHECK(igraph_i_optimal_partition(&MATRIX(*V, 0, i),
&MATRIX(gr_mat, 0, i),
no_of_nodes, (int) INVEC(i),
mtype,
p ? VECTOR(*p) : 0, 0));
}
break;
case IGRAPH_SCG_INTERV_KM:
for (i=0; i<nev; i++) {
igraph_vector_t tmpv;
igraph_vector_view(&tmpv, &MATRIX(*V, 0, i), no_of_nodes);
IGRAPH_CHECK(igraph_i_intervals_plus_kmeans(&tmpv,
&MATRIX(gr_mat, 0, i),
no_of_nodes, (int) INVEC(i),
maxiter));
}
break;
case IGRAPH_SCG_INTERV:
for (i=0; i<nev; i++) {
igraph_vector_t tmpv;
igraph_vector_view(&tmpv, &MATRIX(*V, 0, i), no_of_nodes);
IGRAPH_CHECK(igraph_i_intervals_method(&tmpv,
&MATRIX(gr_mat, 0, i),
no_of_nodes, (int) INVEC(i)));
}
break;
case IGRAPH_SCG_EXACT:
for (i=0; i<nev; i++) {
IGRAPH_CHECK(igraph_i_exact_coarse_graining(&MATRIX(*V, 0, i),
&MATRIX(gr_mat, 0, i),
no_of_nodes));
}
break;
}
#undef INVEC
if (nev==1) {
for (i=0; i<no_of_nodes; i++) {
VECTOR(*groups)[i] = MATRIX(gr_mat, i, 0);
}
} else {
igraph_i_scg_groups_t *g = igraph_Calloc(no_of_nodes,
igraph_i_scg_groups_t);
int gr_nb=0;
IGRAPH_CHECK(igraph_matrix_int_transpose(&gr_mat));
for(i=0; i<no_of_nodes; i++){
g[i].ind = i;
g[i].n = nev;
g[i].gr = &MATRIX(gr_mat, 0, i);
}
qsort(g, (size_t) no_of_nodes, sizeof(igraph_i_scg_groups_t),
igraph_i_compare_groups);
VECTOR(*groups)[g[0].ind] = gr_nb;
for(i=1; i<no_of_nodes; i++){
if(igraph_i_compare_groups(&g[i], &g[i-1]) != 0) gr_nb++;
VECTOR(*groups)[g[i].ind] = gr_nb;
}
igraph_Free(g);
}
igraph_matrix_int_destroy(&gr_mat);
IGRAPH_FINALLY_CLEAN(1);
return 0;
}
int igraph_i_scg_semiprojectors_sym(const igraph_vector_t *groups,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse,
int no_of_groups,
int no_of_nodes) {
igraph_vector_t tab;
int i;
IGRAPH_VECTOR_INIT_FINALLY(&tab, no_of_groups);
for (i=0; i<no_of_nodes; i++) {
VECTOR(tab)[ (int) VECTOR(*groups)[i] ] += 1;
}
for (i=0; i<no_of_groups; i++) {
VECTOR(tab)[i] = sqrt(VECTOR(tab)[i]);
}
if (L) {
IGRAPH_CHECK(igraph_matrix_resize(L, no_of_groups, no_of_nodes));
igraph_matrix_null(L);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*L, g, i) = 1/VECTOR(tab)[g];
}
}
if (R) {
if (L) {
IGRAPH_CHECK(igraph_matrix_update(R, L));
} else {
IGRAPH_CHECK(igraph_matrix_resize(R, no_of_groups, no_of_nodes));
igraph_matrix_null(R);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*R, g, i) = 1/VECTOR(tab)[g];
}
}
}
if (Lsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Lsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Lsparse, g, i, 1/VECTOR(tab)[g]));
}
}
if (Rsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Rsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Rsparse, g, i, 1/VECTOR(tab)[g]));
}
}
igraph_vector_destroy(&tab);
IGRAPH_FINALLY_CLEAN(1);
return 0;
}
int igraph_i_scg_semiprojectors_lap(const igraph_vector_t *groups,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse,
int no_of_groups,
int no_of_nodes,
igraph_scg_norm_t norm) {
igraph_vector_t tab;
int i;
IGRAPH_VECTOR_INIT_FINALLY(&tab, no_of_groups);
for (i=0; i<no_of_nodes; i++) {
VECTOR(tab)[ (int) VECTOR(*groups)[i] ] += 1;
}
for (i=0; i<no_of_groups; i++) {
VECTOR(tab)[i] = VECTOR(tab)[i];
}
if (norm == IGRAPH_SCG_NORM_ROW) {
if (L) {
IGRAPH_CHECK(igraph_matrix_resize(L, no_of_groups, no_of_nodes));
igraph_matrix_null(L);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*L, g, i) = 1.0 / VECTOR(tab)[g];
}
}
if (R) {
IGRAPH_CHECK(igraph_matrix_resize(R, no_of_groups, no_of_nodes));
igraph_matrix_null(R);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*R, g, i) = 1.0;
}
}
if (Lsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Lsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Lsparse, g, i,
1.0 / VECTOR(tab)[g]));
}
}
if (Rsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Rsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Rsparse, g, i, 1.0));
}
}
} else {
if (L) {
IGRAPH_CHECK(igraph_matrix_resize(L, no_of_groups, no_of_nodes));
igraph_matrix_null(L);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*L, g, i) = 1.0;
}
}
if (R) {
IGRAPH_CHECK(igraph_matrix_resize(R, no_of_groups, no_of_nodes));
igraph_matrix_null(R);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*R, g, i) = 1.0 / VECTOR(tab)[g];
}
}
if (Lsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Lsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Lsparse, g, i, 1.0));
}
}
if (Rsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Rsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Rsparse, g, i,
1.0 / VECTOR(tab)[g]));
}
}
}
igraph_vector_destroy(&tab);
IGRAPH_FINALLY_CLEAN(1);
return 0;
}
int igraph_i_scg_semiprojectors_sto(const igraph_vector_t *groups,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse,
int no_of_groups,
int no_of_nodes,
const igraph_vector_t *p,
igraph_scg_norm_t norm) {
igraph_vector_t pgr, pnormed;
int i;
IGRAPH_VECTOR_INIT_FINALLY(&pgr, no_of_groups);
IGRAPH_VECTOR_INIT_FINALLY(&pnormed, no_of_nodes);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
VECTOR(pgr)[g] += VECTOR(*p)[i];
}
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
VECTOR(pnormed)[i] = VECTOR(*p)[i] / VECTOR(pgr)[g];
}
if (norm == IGRAPH_SCG_NORM_ROW) {
if (L) {
IGRAPH_CHECK(igraph_matrix_resize(L, no_of_groups, no_of_nodes));
igraph_matrix_null(L);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*L, g, i) = VECTOR(pnormed)[i];
}
}
if (R) {
IGRAPH_CHECK(igraph_matrix_resize(R, no_of_groups, no_of_nodes));
igraph_matrix_null(R);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*R, g, i) = 1.0;
}
}
if (Lsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Lsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Lsparse, g, i,
VECTOR(pnormed)[i]));
}
}
if (Rsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Rsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Rsparse, g, i, 1.0));
}
}
} else {
if (L) {
IGRAPH_CHECK(igraph_matrix_resize(L, no_of_groups, no_of_nodes));
igraph_matrix_null(L);
for (i=0; i<no_of_nodes; i++) {
int g=(int ) VECTOR(*groups)[i];
MATRIX(*L, g, i) = 1.0;
}
}
if (R) {
IGRAPH_CHECK(igraph_matrix_resize(R, no_of_groups, no_of_nodes));
igraph_matrix_null(R);
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
MATRIX(*R, g, i) = VECTOR(pnormed)[i];
}
}
if (Lsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Lsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Lsparse, g, i, 1.0));
}
}
if (Rsparse) {
IGRAPH_CHECK(igraph_sparsemat_init(Rsparse, no_of_groups, no_of_nodes,
/* nzmax= */ no_of_nodes));
for (i=0; i<no_of_nodes; i++) {
int g=(int) VECTOR(*groups)[i];
IGRAPH_CHECK(igraph_sparsemat_entry(Rsparse, g, i,
VECTOR(pnormed)[i]));
}
}
}
igraph_vector_destroy(&pnormed);
igraph_vector_destroy(&pgr);
IGRAPH_FINALLY_CLEAN(2);
return 0;
}
/**
* \function igraph_scg_semiprojectors
* \brief Compute SCG semi-projectors for a given partition
*
* The three types of semi-projectors are defined as follows.
* Let gamma(j) label the group of vertex j in a partition of all the
* vertices.
*
* </para><para>
* The symmetric semi-projectors are defined as
* <blockquote><para><phrase role="math">
* L[alpha,j] = R[alpha,j] = 1/sqrt(|alpha|) delta[alpha,gamma(j)],
* </phrase></para></blockquote>
* the (row) Laplacian semi-projectors as
* <blockquote><para><phrase role="math">
* L[alpha,j] = 1/|alpha| delta[alpha,gamma(j)]
* </phrase></para></blockquote>
* and
* <blockquote><para><phrase role="math">
* R[alpha,j] = delta[alpha,gamma(j)],
* </phrase></para></blockquote>
* and the (row) stochastic semi-projectors as
* <blockquote><para><phrase role="math">
* L[alpha,j] = p[1][j] / sum(p[1][k]; k in gamma(j))
* delta[alpha,gamma(j)]
* </phrase></para></blockquote>
* and
* <blockquote><para><phrase role="math">
* R[alpha,j] = delta[alpha,gamma(j)],
* </phrase></para></blockquote>
* where p[1] is the (left) eigenvector associated with the
* one-eigenvalue of the stochastic matrix. L and R are
* defined in a symmetric way when \p norm is \c
* IGRAPH_SCG_NORM_COL. All these semi-projectors verify various
* properties described in the reference.
* \param groups A vector of integers, giving the group label of every
* vertex in the partition. Group labels should start at zero and
* should be sequential.
* \param mtype The type of semi-projectors. For now \c
* IGRAPH_SCG_SYMMETRIC, \c IGRAPH_SCG_STOCHASTIC and \c
* IGRAP_SCG_LAPLACIAN are supported.
* \param L If not a \c NULL pointer, then it must be a pointer to
* an initialized matrix. The left semi-projector is stored here.
* \param R If not a \c NULL pointer, then it must be a pointer to
* an initialized matrix. The right semi-projector is stored here.
* \param Lsparse If not a \c NULL pointer, then it must be a pointer
* to an uninitialized sparse matrix. The left semi-projector is
* stored here.
* \param Rsparse If not a \c NULL pointer, then it must be a pointer
* to an uninitialized sparse matrix. The right semi-projector is
* stored here.
* \param p \c NULL, or a probability vector of the same length as \p
* groups. \p p is the stationary probability distribution of a
* Markov chain when \p mtype is \c IGRAPH_SCG_STOCHASTIC. This
* argument is ignored in all other cases.
* \param norm Either \c IGRAPH_SCG_NORM_ROW or \c IGRAPH_SCG_NORM_COL.
* Specifies whether the rows or the columns of the Laplacian
* matrix sum up to zero, or whether the rows or the columns of the
* stochastic matrix sum up to one.
* \return Error code.
*
* Time complexity: TODO.
*
* \sa \ref igraph_scg_adjacency(), \ref igraph_scg_stochastic() and
* \ref igraph_scg_laplacian(), \ref igraph_scg_grouping().
*
* \example examples/simple/igraph_scg_semiprojectors.c
* \example examples/simple/igraph_scg_semiprojectors2.c
* \example examples/simple/igraph_scg_semiprojectors3.c
*/
int igraph_scg_semiprojectors(const igraph_vector_t *groups,
igraph_scg_matrix_t mtype,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse,
const igraph_vector_t *p,
igraph_scg_norm_t norm) {
int no_of_nodes=(int) igraph_vector_size(groups);
int no_of_groups;
igraph_real_t min, max;
igraph_vector_minmax(groups, &min, &max);
no_of_groups=(int) max+1;
if (min < 0 || max >= no_of_nodes) {
IGRAPH_ERROR("Invalid membership vector", IGRAPH_EINVAL);
}
if (mtype == IGRAPH_SCG_STOCHASTIC && !p) {
IGRAPH_ERROR("`p' must be given for the stochastic matrix case",
IGRAPH_EINVAL);
}
if (p && igraph_vector_size(p) != no_of_nodes) {
IGRAPH_ERROR("Invalid `p' vector length, should match number of vertices",
IGRAPH_EINVAL);
}
switch (mtype) {
case IGRAPH_SCG_SYMMETRIC:
IGRAPH_CHECK(igraph_i_scg_semiprojectors_sym(groups, L, R, Lsparse,
Rsparse, no_of_groups,
no_of_nodes));
break;
case IGRAPH_SCG_LAPLACIAN:
IGRAPH_CHECK(igraph_i_scg_semiprojectors_lap(groups, L, R, Lsparse,
Rsparse, no_of_groups,
no_of_nodes, norm));
break;
case IGRAPH_SCG_STOCHASTIC:
IGRAPH_CHECK(igraph_i_scg_semiprojectors_sto(groups, L, R, Lsparse,
Rsparse, no_of_groups,
no_of_nodes, p, norm));
break;
}
return 0;
}
/**
* \function igraph_scg_norm_eps
* Calculate SCG residuals
*
* Computes |v[i]-Pv[i]|, where v[i] is the i-th eigenvector in \p V
* and P is the projector corresponding to the \p mtype argument.
*
* \param V The matrix of eigenvectors to be preserved by coarse
* graining, each column is an eigenvector.
* \param groups A vector of integers, giving the group label of every
* vertex in the partition. Group labels should start at zero and
* should be sequential.
* \param eps Pointer to a real value, the result is stored here.
* \param mtype The type of semi-projectors. For now \c
* IGRAPH_SCG_SYMMETRIC, \c IGRAPH_SCG_STOCHASTIC and \c
* IGRAP_SCG_LAPLACIAN are supported.
* \param p \c NULL, or a probability vector of the same length as \p
* groups. \p p is the stationary probability distribution of a
* Markov chain when \p mtype is \c IGRAPH_SCG_STOCHASTIC. This
* argument is ignored in all other cases.
* \param norm Either \c IGRAPH_SCG_NORM_ROW or \c IGRAPH_SCG_NORM_COL.
* Specifies whether the rows or the columns of the Laplacian
* matrix sum up to zero, or whether the rows or the columns of the
* stochastic matrix sum up to one.
* \return Error code.
*
* Time complexity: TODO.
*
* \sa \ref igraph_scg_adjacency(), \ref igraph_scg_stochastic() and
* \ref igraph_scg_laplacian(), \ref igraph_scg_grouping(), \ref
* igraph_scg_semiprojectors().
*/
int igraph_scg_norm_eps(const igraph_matrix_t *V,
const igraph_vector_t *groups,
igraph_vector_t *eps,
igraph_scg_matrix_t mtype,
const igraph_vector_t *p,
igraph_scg_norm_t norm) {
int no_of_nodes=(int) igraph_vector_size(groups);
int no_of_groups;
int no_of_vectors=(int) igraph_matrix_ncol(V);
igraph_real_t min, max;
igraph_sparsemat_t Lsparse, Rsparse, Lsparse2, Rsparse2, Rsparse3, proj;
igraph_vector_t x, res;
int k, i;
if (igraph_matrix_nrow(V) != no_of_nodes) {
IGRAPH_ERROR("Eigenvector length and group vector length do not match",
IGRAPH_EINVAL);
}
igraph_vector_minmax(groups, &min, &max);
no_of_groups=(int) max+1;
if (min < 0 || max >= no_of_nodes) {
IGRAPH_ERROR("Invalid membership vector", IGRAPH_EINVAL);
}
if (mtype == IGRAPH_SCG_STOCHASTIC && !p) {
IGRAPH_ERROR("`p' must be given for the stochastic matrix case",
IGRAPH_EINVAL);
}
if (p && igraph_vector_size(p) != no_of_nodes) {
IGRAPH_ERROR("Invalid `p' vector length, should match number of vertices",
IGRAPH_EINVAL);
}
IGRAPH_CHECK(igraph_scg_semiprojectors(groups, mtype, /* L= */ 0,
/* R= */ 0, &Lsparse, &Rsparse, p,
norm));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Lsparse);
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse);
IGRAPH_CHECK(igraph_sparsemat_compress(&Lsparse, &Lsparse2));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Lsparse2);
IGRAPH_CHECK(igraph_sparsemat_compress(&Rsparse, &Rsparse2));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse2);
IGRAPH_CHECK(igraph_sparsemat_transpose(&Rsparse2, &Rsparse3,
/*values=*/ 1));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse3);
IGRAPH_CHECK(igraph_sparsemat_multiply(&Rsparse3, &Lsparse2, &proj));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &proj);
IGRAPH_VECTOR_INIT_FINALLY(&res, no_of_nodes);
IGRAPH_CHECK(igraph_vector_resize(eps, no_of_vectors));
for (k = 0; k < no_of_vectors; k++) {
igraph_vector_view(&x, &MATRIX(*V, 0, k), no_of_nodes);
igraph_vector_null(&res);
IGRAPH_CHECK(igraph_sparsemat_gaxpy(&proj, &x, &res));
VECTOR(*eps)[k] = 0.0;
for (i = 0; i < no_of_nodes; i++) {
igraph_real_t di=MATRIX(*V, i, k) - VECTOR(res)[i];
VECTOR(*eps)[k] += di * di;
}
VECTOR(*eps)[k] = sqrt(VECTOR(*eps)[k]);
}
igraph_vector_destroy(&res);
igraph_sparsemat_destroy(&proj);
igraph_sparsemat_destroy(&Rsparse3);
igraph_sparsemat_destroy(&Rsparse2);
igraph_sparsemat_destroy(&Lsparse2);
igraph_sparsemat_destroy(&Rsparse);
igraph_sparsemat_destroy(&Lsparse);
IGRAPH_FINALLY_CLEAN(7);
return 0;
}
int igraph_i_matrix_laplacian(const igraph_matrix_t *matrix,
igraph_matrix_t *mymatrix,
igraph_scg_norm_t norm) {
igraph_vector_t degree;
int i, j, n=(int) igraph_matrix_nrow(matrix);
IGRAPH_CHECK(igraph_matrix_resize(mymatrix, n, n));
IGRAPH_VECTOR_INIT_FINALLY(°ree, n);
if (norm==IGRAPH_SCG_NORM_ROW) {
IGRAPH_CHECK(igraph_matrix_rowsum(matrix, °ree));
} else {
IGRAPH_CHECK(igraph_matrix_colsum(matrix, °ree));
}
for (i=0; i<n; i++) {
VECTOR(degree)[i] -= MATRIX(*matrix, i, i);
}
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
MATRIX(*mymatrix, i, j) = - MATRIX(*matrix, i, j);
}
MATRIX(*mymatrix, i, i) = VECTOR(degree)[i];
}
igraph_vector_destroy(°ree);
IGRAPH_FINALLY_CLEAN(1);
return 0;
}
int igraph_i_sparsemat_laplacian(const igraph_sparsemat_t *sparse,
igraph_sparsemat_t *mysparse,
igraph_scg_norm_t norm) {
igraph_vector_t degree;
int i, n=(int) igraph_sparsemat_nrow(sparse);
int nzmax=igraph_sparsemat_nzmax(sparse);
igraph_sparsemat_iterator_t it;
IGRAPH_CHECK(igraph_sparsemat_init(mysparse, n, n, nzmax+n));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparse);
igraph_sparsemat_iterator_init(&it, (igraph_sparsemat_t *) sparse);
IGRAPH_VECTOR_INIT_FINALLY(°ree, n);
for (igraph_sparsemat_iterator_reset(&it);
!igraph_sparsemat_iterator_end(&it);
igraph_sparsemat_iterator_next(&it)) {
int row=igraph_sparsemat_iterator_row(&it);
int col=igraph_sparsemat_iterator_col(&it);
if (row != col) {
igraph_real_t val=igraph_sparsemat_iterator_get(&it);
if (norm == IGRAPH_SCG_NORM_ROW) {
VECTOR(degree)[row] += val;
} else {
VECTOR(degree)[col] += val;
}
}
}
/* Diagonal */
for (i=0; i<n; i++) {
igraph_sparsemat_entry(mysparse, i, i, VECTOR(degree)[i]);
}
/* And the rest, filter out diagonal elements */
for (igraph_sparsemat_iterator_reset(&it);
!igraph_sparsemat_iterator_end(&it);
igraph_sparsemat_iterator_next(&it)) {
int row=igraph_sparsemat_iterator_row(&it);
int col=igraph_sparsemat_iterator_col(&it);
if (row != col) {
igraph_real_t val=igraph_sparsemat_iterator_get(&it);
igraph_sparsemat_entry(mysparse, row, col, -val);
}
}
igraph_vector_destroy(°ree);
IGRAPH_FINALLY_CLEAN(2); /* + mysparse */
return 0;
}
int igraph_i_matrix_stochastic(const igraph_matrix_t *matrix,
igraph_matrix_t *mymatrix,
igraph_scg_norm_t norm) {
int i, j, n=(int) igraph_matrix_nrow(matrix);
IGRAPH_CHECK(igraph_matrix_copy(mymatrix, matrix));
if (norm==IGRAPH_SCG_NORM_ROW){
for (i=0; i<n; i++) {
igraph_real_t sum=0.0;
for (j=0; j<n; j++) {
sum += MATRIX(*matrix, i, j);
}
if (sum == 0) { IGRAPH_WARNING("Zero degree vertices"); }
for (j=0; j<n; j++) {
MATRIX(*mymatrix, i, j) = MATRIX(*matrix, i, j) / sum;
}
}
} else {
for (i=0; i<n; i++) {
igraph_real_t sum=0.0;
for (j=0; j<n; j++) {
sum += MATRIX(*matrix, j, i);
}
if (sum == 0) { IGRAPH_WARNING("Zero degree vertices"); }
for (j=0; j<n; j++) {
MATRIX(*mymatrix, j, i) = MATRIX(*matrix, j, i) / sum;
}
}
}
return 0;
}
int igraph_i_normalize_sparsemat(igraph_sparsemat_t *sparsemat,
igraph_bool_t column_wise);
int igraph_i_sparsemat_stochastic(const igraph_sparsemat_t *sparse,
igraph_sparsemat_t *mysparse,
igraph_scg_norm_t norm) {
IGRAPH_CHECK(igraph_sparsemat_copy(mysparse, sparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparse);
IGRAPH_CHECK(igraph_i_normalize_sparsemat(mysparse,
norm==IGRAPH_SCG_NORM_COL));
IGRAPH_FINALLY_CLEAN(1);
return 0;
}
int igraph_i_scg_get_result(igraph_scg_matrix_t type,
const igraph_matrix_t *matrix,
const igraph_sparsemat_t *sparsemat,
const igraph_sparsemat_t *Lsparse,
const igraph_sparsemat_t *Rsparse_t,
igraph_t *scg_graph,
igraph_matrix_t *scg_matrix,
igraph_sparsemat_t *scg_sparsemat,
igraph_bool_t directed) {
/* We need to calculate either scg_matrix (if input is dense), or
scg_sparsemat (if input is sparse). For the latter we might need
to temporarily use another matrix. */
if (matrix) {
igraph_matrix_t *my_scg_matrix=scg_matrix, v_scg_matrix;
igraph_matrix_t tmp;
igraph_sparsemat_t *myLsparse=(igraph_sparsemat_t *) Lsparse, v_Lsparse;
if (!scg_matrix) {
my_scg_matrix=&v_scg_matrix;
IGRAPH_CHECK(igraph_matrix_init(my_scg_matrix, 0, 0));
IGRAPH_FINALLY(igraph_matrix_destroy, my_scg_matrix);
}
if (!igraph_sparsemat_is_cc(Lsparse)) {
myLsparse=&v_Lsparse;
IGRAPH_CHECK(igraph_sparsemat_compress(Lsparse, myLsparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, myLsparse);
}
IGRAPH_CHECK(igraph_matrix_init(&tmp, 0, 0));
IGRAPH_FINALLY(igraph_matrix_destroy, &tmp);
IGRAPH_CHECK(igraph_sparsemat_dense_multiply(matrix, Rsparse_t, &tmp));
IGRAPH_CHECK(igraph_sparsemat_multiply_by_dense(myLsparse, &tmp,
my_scg_matrix));
igraph_matrix_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
if (scg_sparsemat) {
IGRAPH_CHECK(igraph_matrix_as_sparsemat(scg_sparsemat, my_scg_matrix,
/* tol= */ 0));
IGRAPH_FINALLY(igraph_sparsemat_destroy, scg_sparsemat);
}
if (scg_graph) {
if (type != IGRAPH_SCG_LAPLACIAN) {
IGRAPH_CHECK(igraph_weighted_adjacency(scg_graph, my_scg_matrix,
directed ?
IGRAPH_ADJ_DIRECTED :
IGRAPH_ADJ_UNDIRECTED,
"weight", /*loops=*/ 1));
} else {
int i, j, n=(int) igraph_matrix_nrow(my_scg_matrix);
igraph_matrix_t tmp;
IGRAPH_MATRIX_INIT_FINALLY(&tmp, n, n);
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
MATRIX(tmp, i, j) = -MATRIX(*my_scg_matrix, i, j);
}
MATRIX(tmp, i, i) = 0;
}
IGRAPH_CHECK(igraph_weighted_adjacency(scg_graph, &tmp, directed ?
IGRAPH_ADJ_DIRECTED :
IGRAPH_ADJ_UNDIRECTED,
"weight", /*loops=*/ 0));
igraph_matrix_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
}
IGRAPH_FINALLY(igraph_destroy, scg_graph);
}
if (scg_graph) { IGRAPH_FINALLY_CLEAN(1); }
if (scg_sparsemat) { IGRAPH_FINALLY_CLEAN(1); }
if (!igraph_sparsemat_is_cc(Lsparse)) {
igraph_sparsemat_destroy(myLsparse);
IGRAPH_FINALLY_CLEAN(1);
}
if (!scg_matrix) {
igraph_matrix_destroy(my_scg_matrix);
IGRAPH_FINALLY_CLEAN(1);
}
} else { /* sparsemat */
igraph_sparsemat_t *my_scg_sparsemat=scg_sparsemat, v_scg_sparsemat;
igraph_sparsemat_t tmp, *mysparsemat=(igraph_sparsemat_t *) sparsemat,
v_sparsemat, *myLsparse=(igraph_sparsemat_t *) Lsparse, v_Lsparse;
if (!scg_sparsemat) {
my_scg_sparsemat=&v_scg_sparsemat;
}
if (!igraph_sparsemat_is_cc(sparsemat)) {
mysparsemat=&v_sparsemat;
IGRAPH_CHECK(igraph_sparsemat_compress(sparsemat, mysparsemat));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
}
if (!igraph_sparsemat_is_cc(Lsparse)) {
myLsparse=&v_Lsparse;
IGRAPH_CHECK(igraph_sparsemat_compress(Lsparse, myLsparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, myLsparse);
}
IGRAPH_CHECK(igraph_sparsemat_multiply(mysparsemat, Rsparse_t,
&tmp));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &tmp);
IGRAPH_CHECK(igraph_sparsemat_multiply(myLsparse, &tmp,
my_scg_sparsemat));
igraph_sparsemat_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
IGRAPH_FINALLY(igraph_sparsemat_destroy, my_scg_sparsemat);
if (scg_matrix) {
IGRAPH_CHECK(igraph_sparsemat_as_matrix(scg_matrix, my_scg_sparsemat));
}
if (scg_graph) {
if (type != IGRAPH_SCG_LAPLACIAN) {
IGRAPH_CHECK(igraph_weighted_sparsemat(scg_graph, my_scg_sparsemat,
directed, "weight",
/*loops=*/ 1));
} else {
igraph_sparsemat_t tmp;
IGRAPH_CHECK(igraph_sparsemat_copy(&tmp, my_scg_sparsemat));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &tmp);
IGRAPH_CHECK(igraph_sparsemat_neg(&tmp));
IGRAPH_CHECK(igraph_weighted_sparsemat(scg_graph, &tmp, directed,
"weight", /*loops=*/ 0));
igraph_sparsemat_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
}
IGRAPH_FINALLY(igraph_destroy, scg_graph);
}
if (scg_graph) { IGRAPH_FINALLY_CLEAN(1); }
if (!scg_sparsemat) {
igraph_sparsemat_destroy(my_scg_sparsemat);
}
IGRAPH_FINALLY_CLEAN(1); /* my_scg_sparsemat */
if (!igraph_sparsemat_is_cc(Lsparse)) {
igraph_sparsemat_destroy(myLsparse);
IGRAPH_FINALLY_CLEAN(1);
}
if (!igraph_sparsemat_is_cc(sparsemat)) {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
}
}
return 0;
}
int igraph_i_scg_common_checks(const igraph_t *graph,
const igraph_matrix_t *matrix,
const igraph_sparsemat_t *sparsemat,
const igraph_vector_t *ev,
igraph_integer_t nt,
const igraph_vector_t *nt_vec,
const igraph_matrix_t *vectors,
const igraph_matrix_complex_t *vectors_cmplx,
const igraph_vector_t *groups,
const igraph_t *scg_graph,
const igraph_matrix_t *scg_matrix,
const igraph_sparsemat_t *scg_sparsemat,
const igraph_vector_t *p,
igraph_real_t *evmin, igraph_real_t *evmax) {
int no_of_nodes=-1;
igraph_real_t min, max;
int no_of_ev=(int) igraph_vector_size(ev);
if ( (graph?1:0) + (matrix?1:0) + (sparsemat?1:0) != 1 ) {
IGRAPH_ERROR("Give exactly one of `graph', `matrix' and `sparsemat'",
IGRAPH_EINVAL);
}
if (graph) {
no_of_nodes = igraph_vcount(graph);
} else if (matrix) {
no_of_nodes = (int) igraph_matrix_nrow(matrix);
} else if (sparsemat) {
no_of_nodes = (int) igraph_sparsemat_nrow(sparsemat);
}
if ((matrix && igraph_matrix_ncol(matrix) != no_of_nodes) ||
(sparsemat && igraph_sparsemat_ncol(sparsemat) != no_of_nodes)) {
IGRAPH_ERROR("Matrix must be square", IGRAPH_NONSQUARE);
}
igraph_vector_minmax(ev, evmin, evmax);
if (*evmin < 0 || *evmax >= no_of_nodes) {
IGRAPH_ERROR("Invalid eigenvectors given", IGRAPH_EINVAL);
}
if (!nt_vec && (nt <= 1 || nt >= no_of_nodes)) {
IGRAPH_ERROR("Invalid interval specification", IGRAPH_EINVAL);
}
if (nt_vec) {
if (igraph_vector_size(nt_vec) != 1 &&
igraph_vector_size(nt_vec) != no_of_ev) {
IGRAPH_ERROR("Invalid length for interval specification",
IGRAPH_EINVAL);
}
igraph_vector_minmax(nt_vec, &min, &max);
if (min <= 1 || max >= no_of_nodes) {
IGRAPH_ERROR("Invalid interval specification", IGRAPH_EINVAL);
}
}
if (vectors && igraph_matrix_size(vectors) != 0 &&
(igraph_matrix_ncol(vectors) != no_of_ev ||
igraph_matrix_nrow(vectors) != no_of_nodes)) {
IGRAPH_ERROR("Invalid eigenvector matrix size", IGRAPH_EINVAL);
}
if (vectors_cmplx && igraph_matrix_complex_size(vectors_cmplx) != 0 &&
(igraph_matrix_complex_ncol(vectors_cmplx) != no_of_ev ||
igraph_matrix_complex_nrow(vectors_cmplx) != no_of_nodes)) {
IGRAPH_ERROR("Invalid eigenvector matrix size", IGRAPH_EINVAL);
}
if (groups && igraph_vector_size(groups) != 0 &&
igraph_vector_size(groups) != no_of_nodes) {
IGRAPH_ERROR("Invalid `groups' vector size", IGRAPH_EINVAL);
}
if ( (scg_graph!=0) + (scg_matrix!=0) + (scg_sparsemat!=0) == 0 ) {
IGRAPH_ERROR("No output is requested, please give at least one of "
"`scg_graph', `scg_matrix' and `scg_sparsemat'",
IGRAPH_EINVAL);
}
if (p && igraph_vector_size(p) != 0 &&
igraph_vector_size(p) != no_of_nodes) {
IGRAPH_ERROR("Invalid `p' vector size", IGRAPH_EINVAL);
}
return 0;
}
/**
* \function igraph_scg_adjacency
* Spectral coarse graining, symmetric case.
*
* This function handles all the steps involved in the Spectral Coarse
* Graining (SCG) of some matrices and graphs as described in the
* reference below.
*
* \param graph The input graph. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param matrix The input matrix. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param sparsemat The input sparse matrix. Exactly one of \p graph,
* \p matrix and \p sparsemat must be given, the other two must be
* \c NULL pointers.
* \param ev A vector of positive integers giving the indexes of the
* eigenpairs to be preserved. 1 designates the eigenvalue with
* largest algebraic value, 2 the one with second largest algebraic
* value, etc.
* \param nt Positive integer. When \p algo is \c IGRAPH_SCG_OPTIMUM,
* it gives the number of groups to partition each eigenvector
* separately. When \p algo is \c IGRAPH_SCG_INTERV or \c
* IGRAPH_SCG_INTERV_KM, it gives the number of intervals to
* partition each eigenvector. This is ignored when \p algo is \c
* IGRAPH_SCG_EXACT.
* \param nt_vec A numeric vector of length one or the length must
* match the number of eigenvectors given in \p V, or a \c NULL
* pointer. If not \c NULL, then this argument gives the number of
* groups or intervals, and \p nt is ignored. Different number of
* groups or intervals can be specified for each eigenvector.
* \param algo The algorithm to solve the SCG problem. Possible
* values: \c IGRAPH_SCG_OPTIMUM, \c IGRAPH_SCG_INTERV_KM, \c
* IGRAPH_SCG_INTERV and \c IGRAPH_SCG_EXACT. Please see the
* details about them above.
* \param values If this is not \c NULL and the eigenvectors are
* re-calculated, then the eigenvalues are stored here.
* \param vectors If this is not \c NULL, and not a zero-length
* matrix, then it is interpreted as the eigenvectors to use for
* the coarse-graining. Otherwise the eigenvectors are
* re-calculated, and they are stored here. (If this is not \c NULL.)
* \param groups If this is not \c NULL, and not a zero-length vector,
* then it is interpreted as the vector of group labels. (Group
* labels are integers from zero and are sequential.) Otherwise
* group labels are re-calculated and stored here, if this argument
* is not a null pointer.
* \param use_arpack Whether to use ARPACK for solving the
* eigenproblem. Currently ARPACK is not implemented.
* \param maxiter A positive integer giving the number of iterations
* of the k-means algorithm when \p algo is \c
* IGRAPH_SCG_INTERV_KM. It is ignored in other cases. A reasonable
* (initial) value for this argument is 100.
* \param scg_graph If not a \c NULL pointer, then the coarse-grained
* graph is returned here.
* \param scg_matrix If not a \c NULL pointer, then it must be an
* initialied matrix, and the coarse-grained matrix is returned
* here.
* \param scg_sparsemat If not a \c NULL pointer, then the coarse
* grained matrix is returned here, in sparse matrix form.
* \param L If not a \c NULL pointer, then it must be an initialized
* matrix and the left semi-projector is returned here.
* \param R If not a \c NULL pointer, then it must be an initialized
* matrix and the right semi-projector is returned here.
* \param Lsparse If not a \c NULL pointer, then the left
* semi-projector is returned here.
* \param Rsparse If not a \c NULL pointer, then the right
* semi-projector is returned here.
* \return Error code.
*
* Time complexity: TODO.
*
* \sa \ref igraph_scg_grouping(), \ref igraph_scg_semiprojectors(),
* \ref igraph_scg_stochastic() and \ref igraph_scg_laplacian().
*
* \example examples/simple/scg.c
*/
int igraph_scg_adjacency(const igraph_t *graph,
const igraph_matrix_t *matrix,
const igraph_sparsemat_t *sparsemat,
const igraph_vector_t *ev,
igraph_integer_t nt,
const igraph_vector_t *nt_vec,
igraph_scg_algorithm_t algo,
igraph_vector_t *values,
igraph_matrix_t *vectors,
igraph_vector_t *groups,
igraph_bool_t use_arpack,
igraph_integer_t maxiter,
igraph_t *scg_graph,
igraph_matrix_t *scg_matrix,
igraph_sparsemat_t *scg_sparsemat,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse) {
igraph_sparsemat_t *mysparsemat=(igraph_sparsemat_t*) sparsemat,
real_sparsemat;
int no_of_ev=(int) igraph_vector_size(ev);
/* eigenvectors are calculated and returned */
igraph_bool_t do_vectors= vectors && igraph_matrix_size(vectors)==0;
/* groups are calculated */
igraph_bool_t do_groups= !groups || igraph_vector_size(groups)==0;
/* eigenvectors are not returned but must be calculated for groups */
igraph_bool_t tmp_vectors= !do_vectors && do_groups;
/* need temporary vector for groups */
igraph_bool_t tmp_groups= !groups;
igraph_matrix_t myvectors;
igraph_vector_t mygroups;
igraph_bool_t tmp_lsparse=!Lsparse, tmp_rsparse=!Rsparse;
igraph_sparsemat_t myLsparse, myRsparse, tmpsparse, Rsparse_t;
int no_of_nodes;
igraph_real_t evmin, evmax;
igraph_bool_t directed;
/* --------------------------------------------------------------------*/
/* Argument checks */
IGRAPH_CHECK(igraph_i_scg_common_checks(graph, matrix, sparsemat,
ev, nt, nt_vec,
vectors, 0, groups, scg_graph,
scg_matrix, scg_sparsemat,
/*p=*/ 0, &evmin, &evmax));
if (graph) {
no_of_nodes=igraph_vcount(graph);
directed=igraph_is_directed(graph);
} else if (matrix) {
no_of_nodes=(int) igraph_matrix_nrow(matrix);
directed=!igraph_matrix_is_symmetric(matrix);
} else {
no_of_nodes=(int) igraph_sparsemat_nrow(sparsemat);
directed=!igraph_sparsemat_is_symmetric(sparsemat);
}
/* -------------------------------------------------------------------- */
/* Convert graph, if needed */
if (graph) {
mysparsemat=&real_sparsemat;
IGRAPH_CHECK(igraph_get_sparsemat(graph, mysparsemat));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
}
/* -------------------------------------------------------------------- */
/* Compute eigenpairs, if needed */
if (tmp_vectors) {
vectors=&myvectors;
IGRAPH_MATRIX_INIT_FINALLY(vectors, no_of_nodes, no_of_ev);
}
if (do_vectors || tmp_vectors) {
igraph_arpack_options_t options;
igraph_eigen_which_t which;
igraph_matrix_t tmp;
igraph_vector_t tmpev;
igraph_vector_t tmpeval;
int i;
which.pos = IGRAPH_EIGEN_SELECT;
which.il = (int) (no_of_nodes-evmax+1);
which.iu = (int) (no_of_nodes-evmin+1);
if (values) { IGRAPH_VECTOR_INIT_FINALLY(&tmpeval, 0); }
IGRAPH_CHECK(igraph_matrix_init(&tmp, no_of_nodes,
which.iu-which.il+1));
IGRAPH_FINALLY(igraph_matrix_destroy, &tmp);
IGRAPH_CHECK(igraph_eigen_matrix_symmetric(matrix, mysparsemat,
/* fun= */ 0, no_of_nodes,
/* extra= */ 0,
/* algorithm= */
use_arpack ?
IGRAPH_EIGEN_ARPACK :
IGRAPH_EIGEN_LAPACK, &which,
&options, /*storage=*/ 0,
values ? &tmpeval : 0,
&tmp));
IGRAPH_VECTOR_INIT_FINALLY(&tmpev, no_of_ev);
for (i=0; i<no_of_ev; i++) {
VECTOR(tmpev)[i] = evmax - VECTOR(*ev)[i];
}
if (values) { IGRAPH_CHECK(igraph_vector_index(&tmpeval, values, &tmpev)); }
IGRAPH_CHECK(igraph_matrix_select_cols(&tmp, vectors, &tmpev));
igraph_vector_destroy(&tmpev);
igraph_matrix_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(2);
if (values) {
igraph_vector_destroy(&tmpeval);
IGRAPH_FINALLY_CLEAN(1);
}
}
/* -------------------------------------------------------------------- */
/* Work out groups, if needed */
if (tmp_groups) {
groups=&mygroups;
IGRAPH_VECTOR_INIT_FINALLY((igraph_vector_t*)groups, no_of_nodes);
}
if (do_groups) {
IGRAPH_CHECK(igraph_scg_grouping(vectors, (igraph_vector_t*)groups,
nt, nt_vec,
IGRAPH_SCG_SYMMETRIC, algo,
/*p=*/ 0, maxiter));
}
/* -------------------------------------------------------------------- */
/* Perform coarse graining */
if (tmp_lsparse) {
Lsparse=&myLsparse;
}
if (tmp_rsparse) {
Rsparse=&myRsparse;
}
IGRAPH_CHECK(igraph_scg_semiprojectors(groups, IGRAPH_SCG_SYMMETRIC,
L, R, Lsparse, Rsparse, /*p=*/ 0,
IGRAPH_SCG_NORM_ROW));
if (tmp_groups) {
igraph_vector_destroy((igraph_vector_t*) groups);
IGRAPH_FINALLY_CLEAN(1);
}
if (tmp_vectors) {
igraph_matrix_destroy(vectors);
IGRAPH_FINALLY_CLEAN(1);
}
if (Rsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Rsparse); }
if (Lsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Lsparse); }
/* -------------------------------------------------------------------- */
/* Compute coarse grained matrix/graph/sparse matrix */
IGRAPH_CHECK(igraph_sparsemat_compress(Rsparse, &tmpsparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &tmpsparse);
IGRAPH_CHECK(igraph_sparsemat_transpose(&tmpsparse, &Rsparse_t,
/*values=*/ 1));
igraph_sparsemat_destroy(&tmpsparse);
IGRAPH_FINALLY_CLEAN(1);
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse_t);
IGRAPH_CHECK(igraph_i_scg_get_result(IGRAPH_SCG_SYMMETRIC,
matrix, mysparsemat,
Lsparse, &Rsparse_t,
scg_graph, scg_matrix,
scg_sparsemat, directed));
/* -------------------------------------------------------------------- */
/* Clean up */
igraph_sparsemat_destroy(&Rsparse_t);
IGRAPH_FINALLY_CLEAN(1);
if (Lsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (Rsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (graph) {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
}
return 0;
}
/**
* \function igraph_scg_stochastic
* Spectral coarse graining, stochastic case.
*
* This function handles all the steps involved in the Spectral Coarse
* Graining (SCG) of some matrices and graphs as described in the
* reference below.
*
* \param graph The input graph. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param matrix The input matrix. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param sparsemat The input sparse matrix. Exactly one of \p graph,
* \p matrix and \p sparsemat must be given, the other two must be
* \c NULL pointers.
* \param ev A vector of positive integers giving the indexes of the
* eigenpairs to be preserved. 1 designates the eigenvalue with
* largest magnitude, 2 the one with second largest magnitude, etc.
* \param nt Positive integer. When \p algo is \c IGRAPH_SCG_OPTIMUM,
* it gives the number of groups to partition each eigenvector
* separately. When \p algo is \c IGRAPH_SCG_INTERV or \c
* IGRAPH_SCG_INTERV_KM, it gives the number of intervals to
* partition each eigenvector. This is ignored when \p algo is \c
* IGRAPH_SCG_EXACT.
* \param nt_vec A numeric vector of length one or the length must
* match the number of eigenvectors given in \p V, or a \c NULL
* pointer. If not \c NULL, then this argument gives the number of
* groups or intervals, and \p nt is ignored. Different number of
* groups or intervals can be specified for each eigenvector.
* \param algo The algorithm to solve the SCG problem. Possible
* values: \c IGRAPH_SCG_OPTIMUM, \c IGRAPH_SCG_INTERV_KM, \c
* IGRAPH_SCG_INTERV and \c IGRAPH_SCG_EXACT. Please see the
* details about them above.
* \param norm Either \c IGRAPH_SCG_NORM_ROW or \c IGRAPH_SCG_NORM_COL.
* Specifies whether the rows or the columns of the
* stochastic matrix sum up to one.
* \param values If this is not \c NULL and the eigenvectors are
* re-calculated, then the eigenvalues are stored here.
* \param vectors If this is not \c NULL, and not a zero-length
* matrix, then it is interpreted as the eigenvectors to use for
* the coarse-graining. Otherwise the eigenvectors are
* re-calculated, and they are stored here. (If this is not \c NULL.)
* \param groups If this is not \c NULL, and not a zero-length vector,
* then it is interpreted as the vector of group labels. (Group
* labels are integers from zero and are sequential.) Otherwise
* group labels are re-calculated and stored here, if this argument
* is not a null pointer.
* \param p If this is not \c NULL, and not zero length, then it is
* interpreted as the stationary probability distribution of the
* Markov chain corresponding to the input matrix/graph. Its length
* must match the number of vertices in the input graph (or number
* of rows in the input matrix). If not given, then the stationary
* distribution is calculated and stored here. (Unless this
* argument is a \c NULL pointer, in which case it is not stored.)
* \param use_arpack Whether to use ARPACK for solving the
* eigenproblem. Currently ARPACK is not implemented.
* \param maxiter A positive integer giving the number of iterations
* of the k-means algorithm when \p algo is \c
* IGRAPH_SCG_INTERV_KM. It is ignored in other cases. A reasonable
* (initial) value for this argument is 100.
* \param scg_graph If not a \c NULL pointer, then the coarse-grained
* graph is returned here.
* \param scg_matrix If not a \c NULL pointer, then it must be an
* initialied matrix, and the coarse-grained matrix is returned
* here.
* \param scg_sparsemat If not a \c NULL pointer, then the coarse
* grained matrix is returned here, in sparse matrix form.
* \param L If not a \c NULL pointer, then it must be an initialized
* matrix and the left semi-projector is returned here.
* \param R If not a \c NULL pointer, then it must be an initialized
* matrix and the right semi-projector is returned here.
* \param Lsparse If not a \c NULL pointer, then the left
* semi-projector is returned here.
* \param Rsparse If not a \c NULL pointer, then the right
* semi-projector is returned here.
* \return Error code.
*
* Time complexity: TODO.
*
* \sa \ref igraph_scg_grouping(), \ref igraph_scg_semiprojectors(),
* \ref igraph_scg_adjacency() and \ref igraph_scg_laplacian().
*
* \example examples/simple/scg2.c
*/
int igraph_scg_stochastic(const igraph_t *graph,
const igraph_matrix_t *matrix,
const igraph_sparsemat_t *sparsemat,
const igraph_vector_t *ev,
igraph_integer_t nt,
const igraph_vector_t *nt_vec,
igraph_scg_algorithm_t algo,
igraph_scg_norm_t norm,
igraph_vector_complex_t *values,
igraph_matrix_complex_t *vectors,
igraph_vector_t *groups,
igraph_vector_t *p,
igraph_bool_t use_arpack,
igraph_integer_t maxiter,
igraph_t *scg_graph,
igraph_matrix_t *scg_matrix,
igraph_sparsemat_t *scg_sparsemat,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse) {
igraph_matrix_t *mymatrix=(igraph_matrix_t*) matrix, real_matrix;
igraph_sparsemat_t *mysparsemat=(igraph_sparsemat_t*) sparsemat,
real_sparsemat;
int no_of_nodes;
igraph_real_t evmin, evmax;
igraph_arpack_options_t options;
igraph_eigen_which_t which;
/* eigenvectors are calculated and returned */
igraph_bool_t do_vectors= vectors && igraph_matrix_complex_size(vectors)==0;
/* groups are calculated */
igraph_bool_t do_groups= !groups || igraph_vector_size(groups)==0;
igraph_bool_t tmp_groups= !groups;
/* eigenvectors are not returned but must be calculated for groups */
igraph_bool_t tmp_vectors= !do_vectors && do_groups;
igraph_matrix_complex_t myvectors;
igraph_vector_t mygroups;
igraph_bool_t do_p= !p || igraph_vector_size(p)==0;
igraph_vector_t *myp=(igraph_vector_t *) p, real_p;
int no_of_ev=(int) igraph_vector_size(ev);
igraph_bool_t tmp_lsparse=!Lsparse, tmp_rsparse=!Rsparse;
igraph_sparsemat_t myLsparse, myRsparse, tmpsparse, Rsparse_t;
/* --------------------------------------------------------------------*/
/* Argument checks */
IGRAPH_CHECK(igraph_i_scg_common_checks(graph, matrix, sparsemat,
ev, nt, nt_vec,
0, vectors, groups, scg_graph,
scg_matrix, scg_sparsemat, p,
&evmin, &evmax));
if (graph) {
no_of_nodes=igraph_vcount(graph);
} else if (matrix) {
no_of_nodes=(int) igraph_matrix_nrow(matrix);
} else {
no_of_nodes=(int) igraph_sparsemat_nrow(sparsemat);
}
/* -------------------------------------------------------------------- */
/* Convert graph, if needed */
if (graph) {
mysparsemat=&real_sparsemat;
IGRAPH_CHECK(igraph_get_stochastic_sparsemat(graph, mysparsemat,
norm==IGRAPH_SCG_NORM_COL));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
} else if (matrix) {
mymatrix=&real_matrix;
IGRAPH_CHECK(igraph_i_matrix_stochastic(matrix, mymatrix, norm));
IGRAPH_FINALLY(igraph_matrix_destroy, mymatrix);
} else { /* sparsemat */
mysparsemat=&real_sparsemat;
IGRAPH_CHECK(igraph_i_sparsemat_stochastic(sparsemat, mysparsemat, norm));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
}
/* -------------------------------------------------------------------- */
/* Compute eigenpairs, if needed */
if (tmp_vectors) {
vectors=&myvectors;
IGRAPH_CHECK(igraph_matrix_complex_init(vectors, no_of_nodes, no_of_ev));
IGRAPH_FINALLY(igraph_matrix_complex_destroy, vectors);
}
if (do_vectors || tmp_vectors) {
igraph_matrix_complex_t tmp;
igraph_vector_t tmpev;
igraph_vector_complex_t tmpeval;
int i;
which.pos = IGRAPH_EIGEN_SELECT;
which.il = (int) (no_of_nodes-evmax+1);
which.iu = (int) (no_of_nodes-evmin+1);
if (values) {
IGRAPH_CHECK(igraph_vector_complex_init(&tmpeval, 0));
IGRAPH_FINALLY(igraph_vector_complex_destroy, &tmpeval);
}
IGRAPH_CHECK(igraph_matrix_complex_init(&tmp, no_of_nodes,
which.iu-which.il+1));
IGRAPH_FINALLY(igraph_matrix_complex_destroy, &tmp);
IGRAPH_CHECK(igraph_eigen_matrix(mymatrix, mysparsemat, /*fun=*/ 0,
no_of_nodes, /*extra=*/ 0, use_arpack ?
IGRAPH_EIGEN_ARPACK :
IGRAPH_EIGEN_LAPACK, &which, &options,
/*storage=*/ 0,
values ? &tmpeval: 0, &tmp));
IGRAPH_VECTOR_INIT_FINALLY(&tmpev, no_of_ev);
for (i=0; i<no_of_ev; i++) {
VECTOR(tmpev)[i] = evmax - VECTOR(*ev)[i];
}
if (values) {
IGRAPH_CHECK(igraph_vector_complex_index(&tmpeval, values, &tmpev));
}
IGRAPH_CHECK(igraph_matrix_complex_select_cols(&tmp, vectors, &tmpev));
igraph_vector_destroy(&tmpev);
igraph_matrix_complex_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(2);
if (values) {
igraph_vector_complex_destroy(&tmpeval);
IGRAPH_FINALLY_CLEAN(1);
}
}
/* Compute p if not supplied */
if (do_p) {
igraph_eigen_which_t w;
igraph_matrix_complex_t tmp;
igraph_arpack_options_t o;
igraph_matrix_t trans, *mytrans=&trans;
igraph_sparsemat_t sparse_trans, *mysparse_trans=&sparse_trans;
int i;
igraph_arpack_options_init(&o);
if (!p) {
IGRAPH_VECTOR_INIT_FINALLY(&real_p, no_of_nodes);
myp=&real_p;
} else {
IGRAPH_CHECK(igraph_vector_resize(p, no_of_nodes));
}
IGRAPH_CHECK(igraph_matrix_complex_init(&tmp, 0, 0));
IGRAPH_FINALLY(igraph_matrix_complex_destroy, &tmp);
w.pos=IGRAPH_EIGEN_LR;
w.howmany=1;
if (mymatrix) {
IGRAPH_CHECK(igraph_matrix_copy(&trans, mymatrix));
IGRAPH_FINALLY(igraph_matrix_destroy, &trans);
IGRAPH_CHECK(igraph_matrix_transpose(&trans));
mysparse_trans=0;
} else {
IGRAPH_CHECK(igraph_sparsemat_transpose(mysparsemat, &sparse_trans,
/*values=*/ 1));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparse_trans);
mytrans=0;
}
IGRAPH_CHECK(igraph_eigen_matrix(mytrans, mysparse_trans, /*fun=*/ 0,
no_of_nodes, /*extra=*/ 0, /*algorith=*/
use_arpack ?
IGRAPH_EIGEN_ARPACK :
IGRAPH_EIGEN_LAPACK, &w, &o,
/*storage=*/ 0, /*values=*/ 0, &tmp));
if (mymatrix) {
igraph_matrix_destroy(&trans);
IGRAPH_FINALLY_CLEAN(1);
} else {
igraph_sparsemat_destroy(mysparse_trans);
IGRAPH_FINALLY_CLEAN(1);
}
for (i=0; i<no_of_nodes; i++) {
VECTOR(*myp)[i] = fabs(IGRAPH_REAL(MATRIX(tmp, i, 0)));
}
igraph_matrix_complex_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
}
/* -------------------------------------------------------------------- */
/* Work out groups, if needed */
/* TODO: use complex part as well */
if (tmp_groups) {
groups=&mygroups;
IGRAPH_VECTOR_INIT_FINALLY((igraph_vector_t*)groups, no_of_nodes);
}
if (do_groups) {
igraph_matrix_t tmp;
IGRAPH_MATRIX_INIT_FINALLY(&tmp, 0, 0);
IGRAPH_CHECK(igraph_matrix_complex_real(vectors, &tmp));
IGRAPH_CHECK(igraph_scg_grouping(&tmp, (igraph_vector_t*)groups,
nt, nt_vec,
IGRAPH_SCG_STOCHASTIC, algo,
myp, maxiter));
igraph_matrix_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
}
/* -------------------------------------------------------------------- */
/* Perform coarse graining */
if (tmp_lsparse) {
Lsparse=&myLsparse;
}
if (tmp_rsparse) {
Rsparse=&myRsparse;
}
IGRAPH_CHECK(igraph_scg_semiprojectors(groups, IGRAPH_SCG_STOCHASTIC,
L, R, Lsparse, Rsparse, myp, norm));
if (tmp_groups) {
igraph_vector_destroy((igraph_vector_t*) groups);
IGRAPH_FINALLY_CLEAN(1);
}
if (!p && do_p) {
igraph_vector_destroy(myp);
IGRAPH_FINALLY_CLEAN(1);
}
if (tmp_vectors) {
igraph_matrix_complex_destroy(vectors);
IGRAPH_FINALLY_CLEAN(1);
}
if (Rsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Rsparse); }
if (Lsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Lsparse); }
/* -------------------------------------------------------------------- */
/* Compute coarse grained matrix/graph/sparse matrix */
IGRAPH_CHECK(igraph_sparsemat_compress(Rsparse, &tmpsparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &tmpsparse);
IGRAPH_CHECK(igraph_sparsemat_transpose(&tmpsparse, &Rsparse_t,
/*values=*/ 1));
igraph_sparsemat_destroy(&tmpsparse);
IGRAPH_FINALLY_CLEAN(1);
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse_t);
IGRAPH_CHECK(igraph_i_scg_get_result(IGRAPH_SCG_STOCHASTIC,
mymatrix, mysparsemat,
Lsparse, &Rsparse_t,
scg_graph, scg_matrix,
scg_sparsemat, /*directed=*/ 1));
/* -------------------------------------------------------------------- */
/* Clean up */
igraph_sparsemat_destroy(&Rsparse_t);
IGRAPH_FINALLY_CLEAN(1);
if (Lsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (Rsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (graph) {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
} else if (matrix) {
igraph_matrix_destroy(mymatrix);
IGRAPH_FINALLY_CLEAN(1);
} else {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
}
return 0;
}
/**
* \function igraph_scg_laplacian
* Spectral coarse graining, laplacian matrix.
* This function handles all the steps involved in the Spectral Coarse
* Graining (SCG) of some matrices and graphs as described in the
* reference below.
*
* \param graph The input graph. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param matrix The input matrix. Exactly one of \p graph, \p matrix
* and \p sparsemat must be given, the other two must be \c NULL
* pointers.
* \param sparsemat The input sparse matrix. Exactly one of \p graph,
* \p matrix and \p sparsemat must be given, the other two must be
* \c NULL pointers.
* \param ev A vector of positive integers giving the indexes of the
* eigenpairs to be preserved. 1 designates the eigenvalue with
* largest magnitude, 2 the one with second largest magnitude, etc.
* \param nt Positive integer. When \p algo is \c IGRAPH_SCG_OPTIMUM,
* it gives the number of groups to partition each eigenvector
* separately. When \p algo is \c IGRAPH_SCG_INTERV or \c
* IGRAPH_SCG_INTERV_KM, it gives the number of intervals to
* partition each eigenvector. This is ignored when \p algo is \c
* IGRAPH_SCG_EXACT.
* \param nt_vec A numeric vector of length one or the length must
* match the number of eigenvectors given in \p V, or a \c NULL
* pointer. If not \c NULL, then this argument gives the number of
* groups or intervals, and \p nt is ignored. Different number of
* groups or intervals can be specified for each eigenvector.
* \param algo The algorithm to solve the SCG problem. Possible
* values: \c IGRAPH_SCG_OPTIMUM, \c IGRAPH_SCG_INTERV_KM, \c
* IGRAPH_SCG_INTERV and \c IGRAPH_SCG_EXACT. Please see the
* details about them above.
* \param norm Either \c IGRAPH_SCG_NORM_ROW or \c IGRAPH_SCG_NORM_COL.
* Specifies whether the rows or the columns of the Laplacian
* matrix sum up to zero.
* \param direction Whether to work with left or right eigenvectors.
* Possible values: \c IGRAPH_SCG_DIRECTION_DEFAULT, \c
* IGRAPH_SCG_DIRECTION_LEFT, \c IGRAPH_SCG_DIRECTION_RIGHT. This
* argument is currently ignored and right eigenvectors are always
* used.
* \param values If this is not \c NULL and the eigenvectors are
* re-calculated, then the eigenvalues are stored here.
* \param vectors If this is not \c NULL, and not a zero-length
* matrix, then it is interpreted as the eigenvectors to use for
* the coarse-graining. Otherwise the eigenvectors are
* re-calculated, and they are stored here. (If this is not \c NULL.)
* \param groups If this is not \c NULL, and not a zero-length vector,
* then it is interpreted as the vector of group labels. (Group
* labels are integers from zero and are sequential.) Otherwise
* group labels are re-calculated and stored here, if this argument
* is not a null pointer.
* \param use_arpack Whether to use ARPACK for solving the
* eigenproblem. Currently ARPACK is not implemented.
* \param maxiter A positive integer giving the number of iterations
* of the k-means algorithm when \p algo is \c
* IGRAPH_SCG_INTERV_KM. It is ignored in other cases. A reasonable
* (initial) value for this argument is 100.
* \param scg_graph If not a \c NULL pointer, then the coarse-grained
* graph is returned here.
* \param scg_matrix If not a \c NULL pointer, then it must be an
* initialied matrix, and the coarse-grained matrix is returned
* here.
* \param scg_sparsemat If not a \c NULL pointer, then the coarse
* grained matrix is returned here, in sparse matrix form.
* \param L If not a \c NULL pointer, then it must be an initialized
* matrix and the left semi-projector is returned here.
* \param R If not a \c NULL pointer, then it must be an initialized
* matrix and the right semi-projector is returned here.
* \param Lsparse If not a \c NULL pointer, then the left
* semi-projector is returned here.
* \param Rsparse If not a \c NULL pointer, then the right
* semi-projector is returned here.
* \return Error code.
*
* Time complexity: TODO.
*
* \sa \ref igraph_scg_grouping(), \ref igraph_scg_semiprojectors(),
* \ref igraph_scg_stochastic() and \ref igraph_scg_adjacency().
*
* \example examples/simple/scg3.c
*/
int igraph_scg_laplacian(const igraph_t *graph,
const igraph_matrix_t *matrix,
const igraph_sparsemat_t *sparsemat,
const igraph_vector_t *ev,
igraph_integer_t nt,
const igraph_vector_t *nt_vec,
igraph_scg_algorithm_t algo,
igraph_scg_norm_t norm,
igraph_scg_direction_t direction,
igraph_vector_complex_t *values,
igraph_matrix_complex_t *vectors,
igraph_vector_t *groups,
igraph_bool_t use_arpack,
igraph_integer_t maxiter,
igraph_t *scg_graph,
igraph_matrix_t *scg_matrix,
igraph_sparsemat_t *scg_sparsemat,
igraph_matrix_t *L,
igraph_matrix_t *R,
igraph_sparsemat_t *Lsparse,
igraph_sparsemat_t *Rsparse) {
igraph_matrix_t *mymatrix=(igraph_matrix_t*) matrix, real_matrix;
igraph_sparsemat_t *mysparsemat=(igraph_sparsemat_t*) sparsemat,
real_sparsemat;
int no_of_nodes;
igraph_real_t evmin, evmax;
igraph_arpack_options_t options;
igraph_eigen_which_t which;
/* eigenvectors are calculated and returned */
igraph_bool_t do_vectors= vectors && igraph_matrix_complex_size(vectors)==0;
/* groups are calculated */
igraph_bool_t do_groups= !groups || igraph_vector_size(groups)==0;
igraph_bool_t tmp_groups= !groups;
/* eigenvectors are not returned but must be calculated for groups */
igraph_bool_t tmp_vectors= !do_vectors && do_groups;
igraph_matrix_complex_t myvectors;
igraph_vector_t mygroups;
int no_of_ev=(int) igraph_vector_size(ev);
igraph_bool_t tmp_lsparse=!Lsparse, tmp_rsparse=!Rsparse;
igraph_sparsemat_t myLsparse, myRsparse, tmpsparse, Rsparse_t;
/* --------------------------------------------------------------------*/
/* Argument checks */
IGRAPH_CHECK(igraph_i_scg_common_checks(graph, matrix, sparsemat,
ev, nt, nt_vec,
0, vectors, groups, scg_graph,
scg_matrix, scg_sparsemat, /*p=*/ 0,
&evmin, &evmax));
if (graph) {
no_of_nodes=igraph_vcount(graph);
} else if (matrix) {
no_of_nodes=(int) igraph_matrix_nrow(matrix);
} else {
no_of_nodes=(int) igraph_sparsemat_nrow(sparsemat);
}
/* -------------------------------------------------------------------- */
/* Convert graph, if needed, get Laplacian matrix */
if (graph) {
mysparsemat=&real_sparsemat;
IGRAPH_CHECK(igraph_sparsemat_init(mysparsemat, 0, 0, 0));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
IGRAPH_CHECK(igraph_laplacian(graph, 0, mysparsemat, /*normalized=*/ 0,
/*weights=*/ 0));
} else if (matrix) {
mymatrix=&real_matrix;
IGRAPH_MATRIX_INIT_FINALLY(mymatrix, no_of_nodes, no_of_nodes);
IGRAPH_CHECK(igraph_i_matrix_laplacian(matrix, mymatrix, norm));
} else { /* sparsemat */
mysparsemat=&real_sparsemat;
IGRAPH_CHECK(igraph_i_sparsemat_laplacian(sparsemat, mysparsemat,
norm==IGRAPH_SCG_NORM_COL));
IGRAPH_FINALLY(igraph_sparsemat_destroy, mysparsemat);
}
/* -------------------------------------------------------------------- */
/* Compute eigenpairs, if needed */
if (tmp_vectors) {
vectors=&myvectors;
IGRAPH_CHECK(igraph_matrix_complex_init(vectors, no_of_nodes, no_of_ev));
IGRAPH_FINALLY(igraph_matrix_complex_destroy, vectors);
}
if (do_vectors || tmp_vectors) {
igraph_matrix_complex_t tmp;
igraph_vector_t tmpev;
igraph_vector_complex_t tmpeval;
int i;
which.pos = IGRAPH_EIGEN_SELECT;
which.il = (int) (no_of_nodes-evmax+1);
which.iu = (int) (no_of_nodes-evmin+1);
if (values) {
IGRAPH_CHECK(igraph_vector_complex_init(&tmpeval, 0));
IGRAPH_FINALLY(igraph_vector_complex_destroy, &tmpeval);
}
IGRAPH_CHECK(igraph_matrix_complex_init(&tmp, no_of_nodes,
which.iu-which.il+1));
IGRAPH_FINALLY(igraph_matrix_complex_destroy, &tmp);
IGRAPH_CHECK(igraph_eigen_matrix(mymatrix, mysparsemat, /*fun=*/ 0,
no_of_nodes, /*extra=*/ 0, use_arpack ?
IGRAPH_EIGEN_ARPACK :
IGRAPH_EIGEN_LAPACK, &which, &options,
/*storage=*/ 0,
values ? &tmpeval : 0, &tmp));
IGRAPH_VECTOR_INIT_FINALLY(&tmpev, no_of_ev);
for (i=0; i<no_of_ev; i++) {
VECTOR(tmpev)[i] = evmax - VECTOR(*ev)[i];
}
if (values) {
IGRAPH_CHECK(igraph_vector_complex_index(&tmpeval, values, &tmpev));
}
IGRAPH_CHECK(igraph_matrix_complex_select_cols(&tmp, vectors, &tmpev));
igraph_vector_destroy(&tmpev);
igraph_matrix_complex_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(2);
if (values) {
igraph_vector_complex_destroy(&tmpeval);
IGRAPH_FINALLY_CLEAN(1);
}
}
/* -------------------------------------------------------------------- */
/* Work out groups, if needed */
/* TODO: use complex part as well */
if (tmp_groups) {
groups=&mygroups;
IGRAPH_VECTOR_INIT_FINALLY((igraph_vector_t*)groups, no_of_nodes);
}
if (do_groups) {
igraph_matrix_t tmp;
IGRAPH_MATRIX_INIT_FINALLY(&tmp, 0, 0);
IGRAPH_CHECK(igraph_matrix_complex_real(vectors, &tmp));
IGRAPH_CHECK(igraph_scg_grouping(&tmp, (igraph_vector_t*)groups,
nt, nt_vec,
IGRAPH_SCG_LAPLACIAN, algo,
/*p=*/ 0, maxiter));
igraph_matrix_destroy(&tmp);
IGRAPH_FINALLY_CLEAN(1);
}
/* -------------------------------------------------------------------- */
/* Perform coarse graining */
if (tmp_lsparse) {
Lsparse=&myLsparse;
}
if (tmp_rsparse) {
Rsparse=&myRsparse;
}
IGRAPH_CHECK(igraph_scg_semiprojectors(groups, IGRAPH_SCG_LAPLACIAN,
L, R, Lsparse, Rsparse, /*p=*/ 0,
norm));
if (tmp_groups) {
igraph_vector_destroy((igraph_vector_t*) groups);
IGRAPH_FINALLY_CLEAN(1);
}
if (tmp_vectors) {
igraph_matrix_complex_destroy(vectors);
IGRAPH_FINALLY_CLEAN(1);
}
if (Rsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Rsparse); }
if (Lsparse) { IGRAPH_FINALLY(igraph_sparsemat_destroy, Lsparse); }
/* -------------------------------------------------------------------- */
/* Compute coarse grained matrix/graph/sparse matrix */
IGRAPH_CHECK(igraph_sparsemat_compress(Rsparse, &tmpsparse));
IGRAPH_FINALLY(igraph_sparsemat_destroy, &tmpsparse);
IGRAPH_CHECK(igraph_sparsemat_transpose(&tmpsparse, &Rsparse_t,
/*values=*/ 1));
igraph_sparsemat_destroy(&tmpsparse);
IGRAPH_FINALLY_CLEAN(1);
IGRAPH_FINALLY(igraph_sparsemat_destroy, &Rsparse_t);
IGRAPH_CHECK(igraph_i_scg_get_result(IGRAPH_SCG_LAPLACIAN,
mymatrix, mysparsemat,
Lsparse, &Rsparse_t,
scg_graph, scg_matrix,
scg_sparsemat, /*directed=*/ 1));
/* -------------------------------------------------------------------- */
/* Clean up */
igraph_sparsemat_destroy(&Rsparse_t);
IGRAPH_FINALLY_CLEAN(1);
if (Lsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (Rsparse) { IGRAPH_FINALLY_CLEAN(1); }
if (graph) {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
} else if (matrix) {
igraph_matrix_destroy(mymatrix);
IGRAPH_FINALLY_CLEAN(1);
} else {
igraph_sparsemat_destroy(mysparsemat);
IGRAPH_FINALLY_CLEAN(1);
}
return 0;
}
|