CuthillMcKee.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // -----------------------------------------------------------------------
  2. // <copyright file="CuthillMcKee.cs" company="">
  3. // Original Matlab code by John Burkardt, Florida State University
  4. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  5. // </copyright>
  6. // -----------------------------------------------------------------------
  7. namespace UnityEngine.U2D.Animation.TriangleNet
  8. .Tools
  9. {
  10. using System;
  11. /// <summary>
  12. /// Applies the Cuthill and McKee renumbering algorithm to reduce the bandwidth of
  13. /// the adjacency matrix associated with the mesh.
  14. /// </summary>
  15. internal class CuthillMcKee
  16. {
  17. // The adjacency matrix of the mesh.
  18. AdjacencyMatrix matrix;
  19. /// <summary>
  20. /// Gets the permutation vector for the Reverse Cuthill-McKee numbering.
  21. /// </summary>
  22. /// <param name="mesh">The mesh.</param>
  23. /// <returns>Permutation vector.</returns>
  24. public int[] Renumber(Mesh mesh)
  25. {
  26. // Algorithm needs linear numbering of the nodes.
  27. mesh.Renumber(NodeNumbering.Linear);
  28. return Renumber(new AdjacencyMatrix(mesh));
  29. }
  30. /// <summary>
  31. /// Gets the permutation vector for the Reverse Cuthill-McKee numbering.
  32. /// </summary>
  33. /// <param name="mesh">The mesh.</param>
  34. /// <returns>Permutation vector.</returns>
  35. public int[] Renumber(AdjacencyMatrix matrix)
  36. {
  37. this.matrix = matrix;
  38. int bandwidth1 = matrix.Bandwidth();
  39. var pcol = matrix.ColumnPointers;
  40. // Adjust column pointers (1-based indexing).
  41. Shift(pcol, true);
  42. // TODO: Make RCM work with 0-based matrix.
  43. // Compute the RCM permutation.
  44. int[] perm = GenerateRcm();
  45. int[] perm_inv = PermInverse(perm);
  46. int bandwidth2 = PermBandwidth(perm, perm_inv);
  47. if (Log.Verbose)
  48. {
  49. Log.Instance.Info(String.Format("Reverse Cuthill-McKee (Bandwidth: {0} > {1})",
  50. bandwidth1, bandwidth2));
  51. }
  52. // Adjust column pointers (0-based indexing).
  53. Shift(pcol, false);
  54. return perm_inv;
  55. }
  56. #region RCM
  57. /// <summary>
  58. /// Finds the reverse Cuthill-Mckee ordering for a general graph.
  59. /// </summary>
  60. /// <returns>The RCM ordering.</returns>
  61. /// <remarks>
  62. /// For each connected component in the graph, the routine obtains
  63. /// an ordering by calling RCM.
  64. /// </remarks>
  65. int[] GenerateRcm()
  66. {
  67. // Number of nodes in the mesh.
  68. int n = matrix.N;
  69. int[] perm = new int[n];
  70. int i, num, root;
  71. int iccsze = 0;
  72. int level_num = 0;
  73. /// Index vector for a level structure. The level structure is stored in the
  74. /// currently unused spaces in the permutation vector PERM.
  75. int[] level_row = new int[n + 1];
  76. /// Marks variables that have been numbered.
  77. int[] mask = new int[n];
  78. for (i = 0; i < n; i++)
  79. {
  80. mask[i] = 1;
  81. }
  82. num = 1;
  83. for (i = 0; i < n; i++)
  84. {
  85. // For each masked connected component...
  86. if (mask[i] != 0)
  87. {
  88. root = i;
  89. // Find a pseudo-peripheral node ROOT. The level structure found by
  90. // ROOT_FIND is stored starting at PERM(NUM).
  91. FindRoot(ref root, mask, ref level_num, level_row, perm, num - 1);
  92. // RCM orders the component using ROOT as the starting node.
  93. Rcm(root, mask, perm, num - 1, ref iccsze);
  94. num += iccsze;
  95. // We can stop once every node is in one of the connected components.
  96. if (n < num)
  97. {
  98. return perm;
  99. }
  100. }
  101. }
  102. return perm;
  103. }
  104. /// <summary>
  105. /// RCM renumbers a connected component by the reverse Cuthill McKee algorithm.
  106. /// </summary>
  107. /// <param name="root">the node that defines the connected component. It is used as the starting
  108. /// point for the RCM ordering.</param>
  109. /// <param name="mask">Input/output, int MASK(NODE_NUM), a mask for the nodes. Only those nodes with
  110. /// nonzero input mask values are considered by the routine. The nodes numbered by RCM will have
  111. /// their mask values set to zero.</param>
  112. /// <param name="perm">Output, int PERM(NODE_NUM), the RCM ordering.</param>
  113. /// <param name="iccsze">Output, int ICCSZE, the size of the connected component that has been numbered.</param>
  114. /// <param name="node_num">the number of nodes.</param>
  115. /// <remarks>
  116. /// The connected component is specified by a node ROOT and a mask.
  117. /// The numbering starts at the root node.
  118. ///
  119. /// An outline of the algorithm is as follows:
  120. ///
  121. /// X(1) = ROOT.
  122. ///
  123. /// for ( I = 1 to N-1)
  124. /// Find all unlabeled neighbors of X(I),
  125. /// assign them the next available labels, in order of increasing degree.
  126. ///
  127. /// When done, reverse the ordering.
  128. /// </remarks>
  129. void Rcm(int root, int[] mask, int[] perm, int offset, ref int iccsze)
  130. {
  131. int[] pcol = matrix.ColumnPointers;
  132. int[] irow = matrix.RowIndices;
  133. int fnbr;
  134. int i, j, k, l;
  135. int jstop, jstrt;
  136. int lbegin, lnbr, lperm, lvlend;
  137. int nbr, node;
  138. // Number of nodes in the mesh.
  139. int n = matrix.N;
  140. /// Workspace, int DEG[NODE_NUM], a temporary vector used to hold
  141. /// the degree of the nodes in the section graph specified by mask and root.
  142. int[] deg = new int[n];
  143. // Find the degrees of the nodes in the component specified by MASK and ROOT.
  144. Degree(root, mask, deg, ref iccsze, perm, offset);
  145. mask[root] = 0;
  146. if (iccsze <= 1)
  147. {
  148. return;
  149. }
  150. lvlend = 0;
  151. lnbr = 1;
  152. // LBEGIN and LVLEND point to the beginning and
  153. // the end of the current level respectively.
  154. while (lvlend < lnbr)
  155. {
  156. lbegin = lvlend + 1;
  157. lvlend = lnbr;
  158. for (i = lbegin; i <= lvlend; i++)
  159. {
  160. // For each node in the current level...
  161. node = perm[offset + i - 1];
  162. jstrt = pcol[node];
  163. jstop = pcol[node + 1] - 1;
  164. // Find the unnumbered neighbors of NODE.
  165. // FNBR and LNBR point to the first and last neighbors
  166. // of the current node in PERM.
  167. fnbr = lnbr + 1;
  168. for (j = jstrt; j <= jstop; j++)
  169. {
  170. nbr = irow[j - 1];
  171. if (mask[nbr] != 0)
  172. {
  173. lnbr += 1;
  174. mask[nbr] = 0;
  175. perm[offset + lnbr - 1] = nbr;
  176. }
  177. }
  178. // Node has neighbors
  179. if (lnbr > fnbr)
  180. {
  181. // Sort the neighbors of NODE in increasing order by degree.
  182. // Linear insertion is used.
  183. k = fnbr;
  184. while (k < lnbr)
  185. {
  186. l = k;
  187. k = k + 1;
  188. nbr = perm[offset + k - 1];
  189. while (fnbr < l)
  190. {
  191. lperm = perm[offset + l - 1];
  192. if (deg[lperm - 1] <= deg[nbr - 1])
  193. {
  194. break;
  195. }
  196. perm[offset + l] = lperm;
  197. l = l - 1;
  198. }
  199. perm[offset + l] = nbr;
  200. }
  201. }
  202. }
  203. }
  204. // We now have the Cuthill-McKee ordering. Reverse it.
  205. ReverseVector(perm, offset, iccsze);
  206. }
  207. /// <summary>
  208. /// Finds a pseudo-peripheral node.
  209. /// </summary>
  210. /// <param name="root">On input, ROOT is a node in the the component of the graph for
  211. /// which a pseudo-peripheral node is sought. On output, ROOT is the pseudo-peripheral
  212. /// node obtained.</param>
  213. /// <param name="mask">MASK[NODE_NUM], specifies a section subgraph. Nodes for which MASK
  214. /// is zero are ignored by FNROOT.</param>
  215. /// <param name="level_num">Output, int LEVEL_NUM, is the number of levels in the level
  216. /// structure rooted at the node ROOT.</param>
  217. /// <param name="level_row">Output, int LEVEL_ROW(NODE_NUM+1), the level structure array pair
  218. /// containing the level structure found.</param>
  219. /// <param name="level">Output, int LEVEL(NODE_NUM), the level structure array pair
  220. /// containing the level structure found.</param>
  221. /// <param name="node_num">the number of nodes.</param>
  222. /// <remarks>
  223. /// The diameter of a graph is the maximum distance (number of edges)
  224. /// between any two nodes of the graph.
  225. ///
  226. /// The eccentricity of a node is the maximum distance between that
  227. /// node and any other node of the graph.
  228. ///
  229. /// A peripheral node is a node whose eccentricity equals the
  230. /// diameter of the graph.
  231. ///
  232. /// A pseudo-peripheral node is an approximation to a peripheral node;
  233. /// it may be a peripheral node, but all we know is that we tried our
  234. /// best.
  235. ///
  236. /// The routine is given a graph, and seeks pseudo-peripheral nodes,
  237. /// using a modified version of the scheme of Gibbs, Poole and
  238. /// Stockmeyer. It determines such a node for the section subgraph
  239. /// specified by MASK and ROOT.
  240. ///
  241. /// The routine also determines the level structure associated with
  242. /// the given pseudo-peripheral node; that is, how far each node
  243. /// is from the pseudo-peripheral node. The level structure is
  244. /// returned as a list of nodes LS, and pointers to the beginning
  245. /// of the list of nodes that are at a distance of 0, 1, 2, ...,
  246. /// NODE_NUM-1 from the pseudo-peripheral node.
  247. ///
  248. /// Reference:
  249. /// Alan George, Joseph Liu,
  250. /// Computer Solution of Large Sparse Positive Definite Systems,
  251. /// Prentice Hall, 1981.
  252. ///
  253. /// Norman Gibbs, William Poole, Paul Stockmeyer,
  254. /// An Algorithm for Reducing the Bandwidth and Profile of a Sparse Matrix,
  255. /// SIAM Journal on Numerical Analysis,
  256. /// Volume 13, pages 236-250, 1976.
  257. ///
  258. /// Norman Gibbs,
  259. /// Algorithm 509: A Hybrid Profile Reduction Algorithm,
  260. /// ACM Transactions on Mathematical Software,
  261. /// Volume 2, pages 378-387, 1976.
  262. /// </remarks>
  263. void FindRoot(ref int root, int[] mask, ref int level_num, int[] level_row,
  264. int[] level, int offset)
  265. {
  266. int[] pcol = matrix.ColumnPointers;
  267. int[] irow = matrix.RowIndices;
  268. int iccsze;
  269. int j, jstrt;
  270. int k, kstop, kstrt;
  271. int mindeg;
  272. int nghbor, ndeg;
  273. int node;
  274. int level_num2 = 0;
  275. // Determine the level structure rooted at ROOT.
  276. GetLevelSet(ref root, mask, ref level_num, level_row, level, offset);
  277. // Count the number of nodes in this level structure.
  278. iccsze = level_row[level_num] - 1;
  279. // Extreme cases:
  280. // A complete graph has a level set of only a single level.
  281. // Every node is equally good (or bad).
  282. // or
  283. // A "line graph" 0--0--0--0--0 has every node in its only level.
  284. // By chance, we've stumbled on the ideal root.
  285. if (level_num == 1 || level_num == iccsze)
  286. {
  287. return;
  288. }
  289. // Pick any node from the last level that has minimum degree
  290. // as the starting point to generate a new level set.
  291. for (;;)
  292. {
  293. mindeg = iccsze;
  294. jstrt = level_row[level_num - 1];
  295. root = level[offset + jstrt - 1];
  296. if (jstrt < iccsze)
  297. {
  298. for (j = jstrt; j <= iccsze; j++)
  299. {
  300. node = level[offset + j - 1];
  301. ndeg = 0;
  302. kstrt = pcol[node - 1];
  303. kstop = pcol[node] - 1;
  304. for (k = kstrt; k <= kstop; k++)
  305. {
  306. nghbor = irow[k - 1];
  307. if (mask[nghbor] > 0)
  308. {
  309. ndeg += 1;
  310. }
  311. }
  312. if (ndeg < mindeg)
  313. {
  314. root = node;
  315. mindeg = ndeg;
  316. }
  317. }
  318. }
  319. // Generate the rooted level structure associated with this node.
  320. GetLevelSet(ref root, mask, ref level_num2, level_row, level, offset);
  321. // If the number of levels did not increase, accept the new ROOT.
  322. if (level_num2 <= level_num)
  323. {
  324. break;
  325. }
  326. level_num = level_num2;
  327. // In the unlikely case that ROOT is one endpoint of a line graph,
  328. // we can exit now.
  329. if (iccsze <= level_num)
  330. {
  331. break;
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// Generates the connected level structure rooted at a given node.
  337. /// </summary>
  338. /// <param name="root">the node at which the level structure is to be rooted.</param>
  339. /// <param name="mask">MASK[NODE_NUM]. On input, only nodes with nonzero MASK are to be processed.
  340. /// On output, those nodes which were included in the level set have MASK set to 1.</param>
  341. /// <param name="level_num">Output, int LEVEL_NUM, the number of levels in the level structure. ROOT is
  342. /// in level 1. The neighbors of ROOT are in level 2, and so on.</param>
  343. /// <param name="level_row">Output, int LEVEL_ROW[NODE_NUM+1], the rooted level structure.</param>
  344. /// <param name="level">Output, int LEVEL[NODE_NUM], the rooted level structure.</param>
  345. /// <param name="node_num">the number of nodes.</param>
  346. /// <remarks>
  347. /// Only nodes for which MASK is nonzero will be considered.
  348. ///
  349. /// The root node chosen by the user is assigned level 1, and masked.
  350. /// All (unmasked) nodes reachable from a node in level 1 are
  351. /// assigned level 2 and masked. The process continues until there
  352. /// are no unmasked nodes adjacent to any node in the current level.
  353. /// The number of levels may vary between 2 and NODE_NUM.
  354. ///
  355. /// Reference:
  356. /// Alan George, Joseph Liu,
  357. /// Computer Solution of Large Sparse Positive Definite Systems,
  358. /// Prentice Hall, 1981.
  359. /// </remarks>
  360. void GetLevelSet(ref int root, int[] mask, ref int level_num, int[] level_row,
  361. int[] level, int offset)
  362. {
  363. int[] pcol = matrix.ColumnPointers;
  364. int[] irow = matrix.RowIndices;
  365. int i, iccsze;
  366. int j, jstop, jstrt;
  367. int lbegin, lvlend, lvsize;
  368. int nbr;
  369. int node;
  370. mask[root] = 0;
  371. level[offset] = root;
  372. level_num = 0;
  373. lvlend = 0;
  374. iccsze = 1;
  375. // LBEGIN is the pointer to the beginning of the current level, and
  376. // LVLEND points to the end of this level.
  377. for (;;)
  378. {
  379. lbegin = lvlend + 1;
  380. lvlend = iccsze;
  381. level_num += 1;
  382. level_row[level_num - 1] = lbegin;
  383. // Generate the next level by finding all the masked neighbors of nodes
  384. // in the current level.
  385. for (i = lbegin; i <= lvlend; i++)
  386. {
  387. node = level[offset + i - 1];
  388. jstrt = pcol[node];
  389. jstop = pcol[node + 1] - 1;
  390. for (j = jstrt; j <= jstop; j++)
  391. {
  392. nbr = irow[j - 1];
  393. if (mask[nbr] != 0)
  394. {
  395. iccsze += 1;
  396. level[offset + iccsze - 1] = nbr;
  397. mask[nbr] = 0;
  398. }
  399. }
  400. }
  401. // Compute the current level width (the number of nodes encountered.)
  402. // If it is positive, generate the next level.
  403. lvsize = iccsze - lvlend;
  404. if (lvsize <= 0)
  405. {
  406. break;
  407. }
  408. }
  409. level_row[level_num] = lvlend + 1;
  410. // Reset MASK to 1 for the nodes in the level structure.
  411. for (i = 0; i < iccsze; i++)
  412. {
  413. mask[level[offset + i]] = 1;
  414. }
  415. }
  416. /// <summary>
  417. /// Computes the degrees of the nodes in the connected component.
  418. /// </summary>
  419. /// <param name="root">the node that defines the connected component.</param>
  420. /// <param name="mask">MASK[NODE_NUM], is nonzero for those nodes which are to be considered.</param>
  421. /// <param name="deg">Output, int DEG[NODE_NUM], contains, for each node in the connected component, its degree.</param>
  422. /// <param name="iccsze">Output, int ICCSIZE, the number of nodes in the connected component.</param>
  423. /// <param name="ls">Output, int LS[NODE_NUM], stores in entries 1 through ICCSIZE the nodes in the
  424. /// connected component, starting with ROOT, and proceeding by levels.</param>
  425. /// <param name="node_num">the number of nodes.</param>
  426. /// <remarks>
  427. /// The connected component is specified by MASK and ROOT.
  428. /// Nodes for which MASK is zero are ignored.
  429. ///
  430. /// Reference:
  431. /// Alan George, Joseph Liu,
  432. /// Computer Solution of Large Sparse Positive Definite Systems,
  433. /// Prentice Hall, 1981.
  434. /// </remarks>
  435. void Degree(int root, int[] mask, int[] deg, ref int iccsze, int[] ls, int offset)
  436. {
  437. int[] pcol = matrix.ColumnPointers;
  438. int[] irow = matrix.RowIndices;
  439. int i, ideg;
  440. int j, jstop, jstrt;
  441. int lbegin, lvlend;
  442. int lvsize = 1;
  443. int nbr, node;
  444. // The sign of ADJ_ROW(I) is used to indicate if node I has been considered.
  445. ls[offset] = root;
  446. pcol[root] = -pcol[root];
  447. lvlend = 0;
  448. iccsze = 1;
  449. // If the current level width is nonzero, generate another level.
  450. while (lvsize > 0)
  451. {
  452. // LBEGIN is the pointer to the beginning of the current level, and
  453. // LVLEND points to the end of this level.
  454. lbegin = lvlend + 1;
  455. lvlend = iccsze;
  456. // Find the degrees of nodes in the current level,
  457. // and at the same time, generate the next level.
  458. for (i = lbegin; i <= lvlend; i++)
  459. {
  460. node = ls[offset + i - 1];
  461. jstrt = -pcol[node];
  462. jstop = Math.Abs(pcol[node + 1]) - 1;
  463. ideg = 0;
  464. for (j = jstrt; j <= jstop; j++)
  465. {
  466. nbr = irow[j - 1];
  467. if (mask[nbr] != 0) // EDIT: [nbr - 1]
  468. {
  469. ideg = ideg + 1;
  470. if (0 <= pcol[nbr]) // EDIT: [nbr - 1]
  471. {
  472. pcol[nbr] = -pcol[nbr]; // EDIT: [nbr - 1]
  473. iccsze = iccsze + 1;
  474. ls[offset + iccsze - 1] = nbr;
  475. }
  476. }
  477. }
  478. deg[node] = ideg;
  479. }
  480. // Compute the current level width.
  481. lvsize = iccsze - lvlend;
  482. }
  483. // Reset ADJ_ROW to its correct sign and return.
  484. for (i = 0; i < iccsze; i++)
  485. {
  486. node = ls[offset + i];
  487. pcol[node] = -pcol[node];
  488. }
  489. }
  490. #endregion
  491. #region Tools
  492. /// <summary>
  493. /// Computes the bandwidth of a permuted adjacency matrix.
  494. /// </summary>
  495. /// <param name="perm">The permutation.</param>
  496. /// <param name="perm_inv">The inverse permutation.</param>
  497. /// <returns>Bandwidth of the permuted adjacency matrix.</returns>
  498. /// <remarks>
  499. /// The matrix is defined by the adjacency information and a permutation.
  500. /// The routine also computes the bandwidth and the size of the envelope.
  501. /// </remarks>
  502. int PermBandwidth(int[] perm, int[] perm_inv)
  503. {
  504. int[] pcol = matrix.ColumnPointers;
  505. int[] irow = matrix.RowIndices;
  506. int col, i, j;
  507. int band_lo = 0;
  508. int band_hi = 0;
  509. int n = matrix.N;
  510. for (i = 0; i < n; i++)
  511. {
  512. for (j = pcol[perm[i]]; j < pcol[perm[i] + 1]; j++)
  513. {
  514. col = perm_inv[irow[j - 1]];
  515. band_lo = Math.Max(band_lo, i - col);
  516. band_hi = Math.Max(band_hi, col - i);
  517. }
  518. }
  519. return band_lo + 1 + band_hi;
  520. }
  521. /// <summary>
  522. /// Produces the inverse of a given permutation.
  523. /// </summary>
  524. /// <param name="n">Number of items permuted.</param>
  525. /// <param name="perm">PERM[N], a permutation.</param>
  526. /// <returns>The inverse permutation.</returns>
  527. int[] PermInverse(int[] perm)
  528. {
  529. int n = matrix.N;
  530. int[] perm_inv = new int[n];
  531. for (int i = 0; i < n; i++)
  532. {
  533. perm_inv[perm[i]] = i;
  534. }
  535. return perm_inv;
  536. }
  537. /// <summary>
  538. /// Reverses the elements of an integer vector.
  539. /// </summary>
  540. /// <param name="size">number of entries in the array.</param>
  541. /// <param name="a">the array to be reversed.</param>
  542. /// <example>
  543. /// Input:
  544. /// N = 5,
  545. /// A = ( 11, 12, 13, 14, 15 ).
  546. ///
  547. /// Output:
  548. /// A = ( 15, 14, 13, 12, 11 ).
  549. /// </example>
  550. void ReverseVector(int[] a, int offset, int size)
  551. {
  552. int i;
  553. int j;
  554. for (i = 0; i < size / 2; i++)
  555. {
  556. j = a[offset + i];
  557. a[offset + i] = a[offset + size - 1 - i];
  558. a[offset + size - 1 - i] = j;
  559. }
  560. }
  561. void Shift(int[] a, bool up)
  562. {
  563. int length = a.Length;
  564. if (up)
  565. {
  566. for (int i = 0; i < length; a[i]++, i++) ;
  567. }
  568. else
  569. {
  570. for (int i = 0; i < length; a[i]--, i++) ;
  571. }
  572. }
  573. #endregion
  574. }
  575. }