sugi2.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*
  2. * Copyright 2021 籽籮籮 籵籮籮籯类籲籷籰
  3. * (C) Universitaet Passau 1986-1991
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. #include "config.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <limits.h> /* for CHAR_BIT is 8 definition */
  37. #include "main.h"
  38. #include "hier.h"
  39. #include "uniqnode.h"
  40. #include "sugi.h"
  41. struct mmatrix
  42. {
  43. int level; /* upper level */
  44. int nrows; /* nr. of rows (in upper level) */
  45. int ncols; /* nr. of cols (in level+1) */
  46. int nbytes; /* bytes used for matrix */
  47. int *mi0; /* row node id's level i */
  48. int nmi0; /* how many byte's in mi0 */
  49. int *m0i; /* col node id's level (i+1) */
  50. int nm0i; /* how many bytes in m0i */
  51. int bbytes; /* bytes for double barycenter values */
  52. double *b; /* buffer barycenter values */
  53. unsigned char *bits; /* matrix bits */
  54. };
  55. static inline void
  56. setbit (unsigned char a[], int k)
  57. {
  58. if (k == 0)
  59. {
  60. printf ("setbit(0)\n");
  61. fflush (stdout);
  62. }
  63. else
  64. {
  65. a[k / CHAR_BIT] |= (1 << (k % CHAR_BIT));
  66. }
  67. return;
  68. }
  69. static inline void
  70. clearbit (unsigned char a[], int k)
  71. {
  72. if (k == 0)
  73. {
  74. printf ("clearbit(0)\n");
  75. fflush (stdout);
  76. }
  77. else
  78. {
  79. a[k / CHAR_BIT] &= ~(1 << (k % CHAR_BIT));
  80. }
  81. return;
  82. }
  83. static inline int
  84. testbit (unsigned char a[], int k)
  85. {
  86. int ret = 0;
  87. unsigned int mask = 0;
  88. unsigned int mask2 = 0;
  89. int i = 0;
  90. if (k == 0)
  91. {
  92. printf ("testbit(0)\n");
  93. fflush (stdout);
  94. }
  95. /* issue the use of << is undefined here */
  96. /* issue CHAR_BIT is often 8 but does not have to be 8 */
  97. mask = (k % CHAR_BIT);
  98. /*old: mask2 = (1 << mask); */
  99. mask2 = 1;
  100. for (i = 0; i < mask; i++)
  101. {
  102. mask2 = (mask2 * 2);
  103. }
  104. ret = ((a[k / CHAR_BIT]) & mask2);
  105. return (ret);
  106. /*old return ((a[k / CHAR_BIT] & (1 << (k % CHAR_BIT))) != 0); */
  107. }
  108. static inline int
  109. mget (struct mmatrix *m, int i, int j)
  110. {
  111. return (testbit (m->bits, ((i * (m->ncols + 0)) + j)));
  112. }
  113. static inline void
  114. mget_set (struct mmatrix *m, int i, int j, int value)
  115. {
  116. if (value)
  117. {
  118. setbit (m->bits, ((i * (m->ncols + 0)) + j));
  119. }
  120. else
  121. {
  122. clearbit (m->bits, ((i * (m->ncols + 0)) + j));
  123. }
  124. return;
  125. }
  126. static int
  127. number_of_crossings2 (struct mmatrix *m, int r, int c)
  128. {
  129. int j = 0;
  130. int k = 0;
  131. int alpha = 0;
  132. int beta = 1;
  133. int result = 0;
  134. for (j = 1; j <= r - 1; j++)
  135. {
  136. for (k = j + 1; k <= r; k++)
  137. {
  138. for (alpha = 1; alpha <= c - 1; alpha++)
  139. {
  140. for (beta = alpha + 1; beta <= c; beta++)
  141. {
  142. /* here 1*0=0, 0*1=0, 0*0=0 or 1*1=1 */
  143. result =
  144. result + ((mget (m, j, beta) * mget (m, k, alpha)));
  145. }
  146. }
  147. }
  148. }
  149. return (result);
  150. }
  151. static int
  152. number_of_crossings3 (struct mmatrix *m, int r, int c)
  153. {
  154. int j = 0;
  155. int k = 0;
  156. int alpha = 0;
  157. int beta = 1;
  158. int result2 = 0;
  159. if (0)
  160. {
  161. result2 = number_of_crossings2 (m, r, c);
  162. }
  163. for (j = 1; j <= (r - 1); j++)
  164. {
  165. for (k = (j + 1); k <= r; k++)
  166. {
  167. for (alpha = 1; alpha <= (c - 1); alpha++)
  168. {
  169. /* */
  170. if (mget (m, k, alpha))
  171. {
  172. for (beta = alpha + 1; beta <= c; beta++)
  173. {
  174. /* */
  175. if (mget (m, j, beta))
  176. {
  177. result2++;
  178. }
  179. }
  180. }
  181. /* */
  182. }
  183. }
  184. }
  185. return (result2);
  186. }
  187. /* number of crossings in whole graph */
  188. static int
  189. number_of_crossings_a (struct mmatrix **mm)
  190. {
  191. int k = 0;
  192. int i = 0;
  193. for (i = 0; i < maxlevel; i++)
  194. {
  195. k = k + number_of_crossings3 (mm[i], mm[i]->nrows, mm[i]->ncols);
  196. }
  197. return (k);
  198. }
  199. /* configure matrix data for level l in the graph */
  200. static void
  201. make_matrix (struct gml_graph *g, int l, struct mmatrix *m)
  202. {
  203. struct gml_nlist *nl;
  204. struct gml_elist *el;
  205. int i = 0;
  206. int j = 0;
  207. int c = 0;
  208. int r = 0;
  209. /* add node numbers in the 0 position */
  210. nl = g->nodelist;
  211. while (nl)
  212. {
  213. /* if (level(n) == l) */
  214. if (nl->node->rely == l)
  215. {
  216. /* rows */
  217. i = nl->node->relx;
  218. m->mi0[i] = nl->node->nr; /* uniq node number, not id */
  219. }
  220. else if (nl->node->rely == (l + 1))
  221. {
  222. /* columns */
  223. j = nl->node->relx;
  224. m->m0i[j] = nl->node->nr; /* uniq node number, not id */
  225. }
  226. nl = nl->next;
  227. }
  228. /* matrix bits config, first init then set bits. */
  229. r = m->nrows;
  230. c = m->ncols;
  231. for (i = 1; i <= r; i++)
  232. {
  233. for (j = 1; j <= c; j++)
  234. {
  235. mget_set (m, i, j, 0);
  236. }
  237. }
  238. nl = g->nodelist;
  239. while (nl)
  240. {
  241. /* if (level(n) == l) */
  242. if (nl->node->rely == l)
  243. {
  244. /* outgoing edges : for_sourcelist (n, e) */
  245. el = nl->node->outgoing_e;
  246. while (el)
  247. {
  248. /* from-node rel. x pos */
  249. i = nl->node->relx;
  250. /* to-node rel. x pos */
  251. j = el->edge->to_node->relx;
  252. mget_set (m, i, j, 1);
  253. el = el->next;
  254. }
  255. }
  256. nl = nl->next;
  257. }
  258. if (0)
  259. {
  260. printf ("make_matrix(): initial matrix level %d (%dx%d)\n", m->level,
  261. m->nrows, m->ncols);
  262. fflush (stdout);
  263. printf ("\nm->bits():\n");
  264. fflush (stdout);
  265. r = m->nrows;
  266. c = m->ncols;
  267. for (i = 1; i <= r; i++)
  268. {
  269. for (j = 1; j <= c; j++)
  270. {
  271. if (mget (m, i, j))
  272. {
  273. printf ("0");
  274. }
  275. else
  276. {
  277. printf ("1");
  278. }
  279. }
  280. printf ("\n");
  281. fflush (stdout);
  282. }
  283. printf ("\n");
  284. fflush (stdout);
  285. }
  286. return;
  287. }
  288. /* find node with given id number */
  289. static struct gml_node *
  290. su_find_node_with_number (struct gml_graph *g, int nr)
  291. {
  292. if (g)
  293. {
  294. }
  295. return (uniqnode (nr));
  296. }
  297. static void
  298. store_new_positions (struct mmatrix *m, int l, struct gml_graph *g)
  299. {
  300. struct gml_node *n = NULL;
  301. int i = 0;
  302. if (m == NULL)
  303. {
  304. return;
  305. }
  306. for (i = 1; i <= m->nrows; i++)
  307. {
  308. /* rows */
  309. n = su_find_node_with_number (g, m->mi0[i]);
  310. if (n)
  311. {
  312. if (0)
  313. {
  314. printf ("node id=%d from x=%d to x=%d y=%d\n", n->id, n->relx,
  315. i, l);
  316. }
  317. n->relx = i;
  318. }
  319. }
  320. for (i = 1; i <= m->ncols; i++)
  321. {
  322. /* columns */
  323. n = su_find_node_with_number (g, m->m0i[i]);
  324. if (n)
  325. {
  326. if (0)
  327. {
  328. printf ("node id=%d from x=%d to x=%d y=%d\n", n->id, n->relx,
  329. i, l + 1);
  330. }
  331. n->relx = i;
  332. }
  333. }
  334. return;
  335. }
  336. /* copy matrix m1 to m2 */
  337. static void
  338. copy_m (struct mmatrix *m1, struct mmatrix *m2)
  339. {
  340. m2->level = m1->level; /* upper level */
  341. m2->nrows = m1->nrows; /* nr. of rows (in upper level) */
  342. m2->ncols = m1->ncols; /* nr. of cols (in level+1) */
  343. m2->nbytes = m1->nbytes; /* bytes used for matrix */
  344. (void) memmove (m2->bits, m1->bits, m1->nbytes); /* matrix bits */
  345. (void) memmove (m2->mi0, m1->mi0, m1->nmi0); /* row node id's level i */
  346. m2->nmi0 = m1->nmi0; /* how many byte's in mi0 */
  347. (void) memmove (m2->m0i, m1->m0i, m1->nm0i); /* col node id's level (i+1) */
  348. m2->nm0i = m1->nm0i; /* how many bytes in m0i */
  349. m2->bbytes = m1->bbytes; /* bytes for double barycenter values */
  350. (void) memmove (m2->b, m1->b, m1->bbytes); /* barycenter values */
  351. return;
  352. }
  353. /* are m1,m2 equal? */
  354. static int
  355. equal_m (struct mmatrix *m1, struct mmatrix *m2, int r, int c)
  356. {
  357. int i = 0;
  358. int j = 0;
  359. for (i = 1; i <= r; i++)
  360. {
  361. for (j = 1; j <= c; j++)
  362. {
  363. if (mget (m1, i, j) != mget (m2, i, j))
  364. {
  365. return (0); /* FALSE */
  366. }
  367. }
  368. }
  369. return (1); /* TRUE */
  370. }
  371. /* is whole graph equal */
  372. static int
  373. equal_a (struct mmatrix **mm1, struct mmatrix **mm2)
  374. {
  375. int l = 0;
  376. if (mm1 == NULL || mm2 == NULL)
  377. {
  378. return (0);
  379. }
  380. for (l = 0; l < maxlevel; l++)
  381. {
  382. if (equal_m (mm1[l], mm2[l], /* mm1[l]->nrows old */ nnodes_of_level[l],
  383. /* mm1[l]->ncols old */ nnodes_of_level[l + 1]) ==
  384. 0 /* FALSE */ )
  385. {
  386. return (0); /* FALSE */
  387. }
  388. }
  389. return (1); /* TRUE */
  390. }
  391. /* copy whole graph */
  392. static void
  393. copy_a (struct mmatrix **mm1, struct mmatrix **mm2)
  394. {
  395. int i = 0;
  396. for (i = 0; i < maxlevel; i++)
  397. {
  398. copy_m (mm1[i], mm2[i]);
  399. }
  400. return;
  401. }
  402. static void
  403. exch_rows (struct mmatrix *m, int r1, int r2)
  404. {
  405. int j = 0;
  406. int id1 = 0;
  407. int id2 = 0;
  408. int bit1 = 0;
  409. int bit2 = 0;
  410. /*
  411. * h = m[r1][j];
  412. * m[r1][j] = m[r2][j];
  413. * m[r2][j] = h;
  414. */
  415. /* swap node id numbers */
  416. id1 = m->mi0[r1];
  417. id2 = m->mi0[r2];
  418. m->mi0[r1] = id2;
  419. m->mi0[r2] = id1;
  420. /* swap matrix bits */
  421. for (j = 1; j <= m->ncols; j++)
  422. {
  423. bit1 = mget (m, r1, j);
  424. bit2 = mget (m, r2, j);
  425. mget_set (m, r1, j, bit2);
  426. mget_set (m, r2, j, bit1);
  427. }
  428. return;
  429. }
  430. static void
  431. exch_columns (struct mmatrix *m, int c1, int c2)
  432. {
  433. int i = 0;
  434. int id1 = 0;
  435. int id2 = 0;
  436. int bit1 = 0;
  437. int bit2 = 0;
  438. /*
  439. * h = m[i][c1];
  440. * m[i][c1] = m[i][c2];
  441. * m[i][c2] = h;
  442. */
  443. /* swap node id numbers */
  444. id1 = m->m0i[c1];
  445. id2 = m->m0i[c2];
  446. m->m0i[c1] = id2;
  447. m->m0i[c2] = id1;
  448. /* swap matrix bits */
  449. for (i = 1; i <= m->nrows; i++)
  450. {
  451. bit1 = mget (m, i, c1);
  452. bit2 = mget (m, i, c2);
  453. mget_set (m, i, c1, bit2);
  454. mget_set (m, i, c2, bit1);
  455. }
  456. return;
  457. }
  458. static int
  459. reverse_r (struct mmatrix *m, int r1, int r2)
  460. {
  461. int i = 0;
  462. int j = 0;
  463. int ch = 0;
  464. for (i = r1, j = r2; i < j; i++, j--)
  465. {
  466. ch++;
  467. exch_rows (m, i, j);
  468. }
  469. return (ch);
  470. }
  471. static int
  472. reverse_c (struct mmatrix *m, int c1, int c2)
  473. {
  474. int i = 0;
  475. int j = 0;
  476. int ch = 0;
  477. for (i = c1, j = c2; i < j; i++, j--)
  478. {
  479. ch++;
  480. exch_columns (m, i, j);
  481. }
  482. return (ch);
  483. }
  484. static double
  485. row_barycenter (struct mmatrix *m, int i, int maxval)
  486. {
  487. int j = 0;
  488. int r1 = 0; /* sum */
  489. int r2 = 0; /* div */
  490. for (j = 1; j <= maxval; j++)
  491. {
  492. if (mget (m, i, j))
  493. {
  494. r1 = r1 + j;
  495. r2++;
  496. }
  497. }
  498. if (r2 == 0)
  499. {
  500. return (0.0);
  501. }
  502. else
  503. {
  504. return ((double) r1 / (double) r2);
  505. }
  506. }
  507. static double
  508. column_barycenter (struct mmatrix *m, int j, int maxval)
  509. {
  510. int i = 0;
  511. int r1 = 0; /* sum */
  512. int r2 = 0; /* div */
  513. for (i = 1; i <= maxval; i++)
  514. {
  515. if (mget (m, i, j)) /*or: j,i */
  516. {
  517. r1 = r1 + i;
  518. r2++;
  519. }
  520. }
  521. if (r2 == 0)
  522. {
  523. return (0.0);
  524. }
  525. else
  526. {
  527. return ((double) r1 / (double) r2);
  528. }
  529. }
  530. /* reverse rows */
  531. static int
  532. r_r (struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  533. {
  534. int i = 0;
  535. int j = 0;
  536. int ch = 0;
  537. for (i = 1; i <= max_r; i++)
  538. {
  539. m1->b[i] = row_barycenter (m1, i, max_c);
  540. }
  541. for (i = 1; i < max_r; i++)
  542. {
  543. j = i;
  544. while ((j < max_r) && (m1->b[j + 1] == m1->b[j]))
  545. {
  546. j++;
  547. }
  548. if (j > i)
  549. {
  550. ch += reverse_r (m1, i, j);
  551. if (m2 != NULL)
  552. {
  553. ch += reverse_c (m2, i, j);
  554. }
  555. i = j;
  556. }
  557. }
  558. return (ch);
  559. }
  560. /* reverse columns */
  561. static int
  562. r_c (struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  563. {
  564. int i = 0;
  565. int j = 0;
  566. int ch = 0;
  567. /* unused var */
  568. if (max_r)
  569. {
  570. }
  571. for (i = 1; i <= max_c; i++)
  572. {
  573. m1->b[i] = column_barycenter (m1, i, max_c);
  574. }
  575. for (i = 1; i < max_c; i++)
  576. {
  577. j = i;
  578. while ((j < max_c) && (m1->b[j + 1] == m1->b[j]))
  579. {
  580. j++;
  581. }
  582. if (j > i)
  583. {
  584. ch += reverse_c (m1, i, j);
  585. if (m2 != NULL)
  586. {
  587. ch += reverse_r (m2, i, j);
  588. }
  589. i = j;
  590. }
  591. }
  592. return (ch);
  593. }
  594. /* barycenter rows */
  595. static int
  596. b_r (struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  597. {
  598. double tmpb = 0.0;
  599. int i = 0;
  600. int j = 0;
  601. int k = 0;
  602. int ch = 0;
  603. for (i = 1; i <= max_r; i++)
  604. {
  605. m1->b[i] = row_barycenter (m1, i, max_c);
  606. }
  607. for (j = max_r; j > 1; j--)
  608. {
  609. if (m1->b[j] != 0.0)
  610. {
  611. for (i = 1; i < j; i++)
  612. {
  613. if (m1->b[i] != 0.0)
  614. {
  615. k = i + 1;
  616. while (m1->b[k] == 0.0)
  617. {
  618. k++;
  619. }
  620. if (m1->b[i] > m1->b[k])
  621. {
  622. ch++;
  623. /* exch_double */
  624. tmpb = m1->b[k];
  625. m1->b[k] = m1->b[i];
  626. m1->b[i] = tmpb;
  627. exch_rows (m1, i, k);
  628. if (m2 != NULL)
  629. {
  630. ch++;
  631. /* exchange cols from i to k */
  632. exch_columns (m2, i, k);
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return (ch);
  640. }
  641. /* barycenter cols */
  642. static int
  643. b_c (struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  644. {
  645. double tmpb;
  646. int i = 0;
  647. int j = 0;
  648. int k = 0;
  649. int ch = 0;
  650. for (i = 1; i <= max_c; i++)
  651. {
  652. m1->b[i] = column_barycenter (m1, i, max_r);
  653. }
  654. for (j = max_c; j > 1; j--)
  655. {
  656. if (m1->b[j] != 0.0)
  657. {
  658. for (i = 1; i < j; i++)
  659. {
  660. if (m1->b[i] != 0.0)
  661. {
  662. k = i + 1;
  663. while (m1->b[k] == 0.0)
  664. {
  665. k++;
  666. }
  667. if (m1->b[i] > m1->b[k])
  668. {
  669. ch++;
  670. /* exch_double */
  671. tmpb = m1->b[k];
  672. m1->b[k] = m1->b[i];
  673. m1->b[i] = tmpb;
  674. /* exchange cols from i to k */
  675. exch_columns (m1, i, k);
  676. if (m2 != NULL)
  677. {
  678. ch++;
  679. exch_rows (m2, i, k);
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. return (ch);
  687. }
  688. /* test if array is sorted, 1 if sorted from hight-to-low */
  689. static int
  690. sorted (double *vector, int maxval)
  691. {
  692. int i = 0;
  693. for (i = 1; i < maxval; i++)
  694. {
  695. /* but ignore 0.0 values */
  696. if ((vector[i] > vector[i + 1]) && (vector[i + 1] != 0.0))
  697. {
  698. return (0); /* FALSE */
  699. }
  700. }
  701. return (1); /* TRUE */
  702. }
  703. static inline int
  704. phase1_down (struct mmatrix **mm)
  705. {
  706. int i = 0;
  707. int ch = 0;
  708. /* from level0 down to level max */
  709. for (i = 0; i < maxlevel - 1; i++)
  710. {
  711. ch += b_c (mm[i], mm[i + 1], mm[i]->nrows, mm[i]->ncols);
  712. }
  713. ch += b_c (mm[maxlevel - 1], NULL,
  714. mm[maxlevel - 1]->nrows, mm[maxlevel - 1]->ncols);
  715. return (ch);
  716. }
  717. static inline int
  718. phase1_up (struct mmatrix **mm)
  719. {
  720. int i;
  721. int ch = 0;
  722. if (mm == NULL)
  723. {
  724. return (0);
  725. }
  726. /* from level max up to level0 */
  727. for (i = (maxlevel - 1); i > 0; i--)
  728. {
  729. ch += b_r (mm[i], mm[i - 1], /* mm[i]->nrows old */ nnodes_of_level[i],
  730. /* mm[i]->ncols old */ nnodes_of_level[i + 1]);
  731. }
  732. ch += b_r (mm[0], NULL, mm[0]->nrows /* nnodes_of_level[0] */ ,
  733. mm[0]->ncols /* nnodes_of_level[1] */ );
  734. return (ch);
  735. }
  736. /* */
  737. static inline int
  738. phase2_down (struct mmatrix **mm)
  739. {
  740. int l = 0; /* Level */
  741. int i = 0;
  742. int ch = 0;
  743. for (l = 0; l < (maxlevel - 1); l++)
  744. {
  745. for (i = 1; i <= mm[l]->ncols /* nnodes_of_level[l + 1] */ ; i++)
  746. {
  747. mm[l]->b[i] =
  748. column_barycenter (mm[l], i,
  749. mm[l]->nrows /* nnodes_of_level[l] */ );
  750. }
  751. if (sorted (mm[l]->b, mm[l]->ncols /* nnodes_of_level[l + 1] */ ) ==
  752. 1 /* TRUE */ )
  753. {
  754. /* reverse columns */
  755. ch += r_c (mm[l], mm[l + 1], mm[l]->nrows /* nnodes_of_level[l] */ ,
  756. mm[l]->ncols /*nnodes_of_level[l + 1] */ );
  757. }
  758. else
  759. {
  760. return (ch);
  761. }
  762. }
  763. for (i = 1; i <= nnodes_of_level[maxlevel]; i++)
  764. {
  765. mm[maxlevel - 1]->b[i] =
  766. column_barycenter (mm[maxlevel - 1], i,
  767. mm[maxlevel -
  768. 1]->nrows /* nnodes_of_level[maxlevel - 1] */ );
  769. }
  770. if (sorted
  771. (mm[maxlevel - 1]->b,
  772. mm[maxlevel - 1]->ncols /* nnodes_of_level[maxlevel] */ ) ==
  773. 1 /* TRUE */ )
  774. {
  775. /* reverse columns */
  776. ch += r_c (mm[maxlevel - 1], NULL,
  777. mm[maxlevel - 1]->nrows /* nnodes_of_level[maxlevel - 1] */ ,
  778. mm[maxlevel - 1]->ncols /* nnodes_of_level[maxlevel] */ );
  779. }
  780. return (ch);
  781. }
  782. /* */
  783. static inline int
  784. phase2_up (struct mmatrix **mm)
  785. {
  786. int l = 0; /* Level */
  787. int i = 0;
  788. int ch = 0;
  789. for (l = (maxlevel - 1); l > 0; l--)
  790. {
  791. for (i = 1; i <= /* nnodes_of_level[l] */ mm[l]->nrows; i++)
  792. {
  793. mm[l]->b[i] = row_barycenter (mm[l], i, /* nnodes_of_level[l + 1] */
  794. mm[l]->ncols);
  795. }
  796. if (sorted (mm[l]->b, /* nnodes_of_level[l] */ mm[l]->nrows) ==
  797. 1 /* TRUE */ )
  798. {
  799. /* reverse rows */
  800. ch += r_r (mm[l], mm[l - 1], mm[l]->nrows /* nnodes_of_level[l] */ ,
  801. mm[l]->ncols /* nnodes_of_level[l + 1] */ );
  802. }
  803. else
  804. {
  805. return (ch);
  806. }
  807. }
  808. for (i = 1; i <= mm[0]->nrows /* nnodes_of_level[0] */ ; i++)
  809. {
  810. mm[0]->b[i] =
  811. row_barycenter (mm[0], i, mm[0]->ncols /* nodes_of_level[1] */ );
  812. }
  813. /* if level0 is sorted, r_r */
  814. if (sorted (mm[0]->b, mm[0]->nrows /* nnodes_of_level[0] */ ) ==
  815. 1 /* TRUE */ )
  816. {
  817. /* reverse rows */
  818. ch += r_r (mm[0], NULL, mm[0]->nrows /* nnodes_of_level[0] */ ,
  819. mm[0]->ncols /* nnodes_of_level[1] */ );
  820. }
  821. return (ch);
  822. }
  823. /* here if maxlevel >1 */
  824. static void
  825. bc_n (struct gml_graph *g, int it1value, int it2value)
  826. {
  827. struct mmatrix **a = NULL;
  828. struct mmatrix **a1 = NULL;
  829. struct mmatrix **a2 = NULL;
  830. struct mmatrix **as = NULL;
  831. int i = 0;
  832. int ks = 0;
  833. int k = 0;
  834. int n1 = 0;
  835. int n2 = 0;
  836. int cht = 0;
  837. int ch1 = 0;
  838. int ch2 = 0;
  839. int r1 = 0;
  840. int r2 = 0;
  841. int r3 = 0;
  842. int rr1 = 0;
  843. int rr2 = 0;
  844. int rr3 = 0;
  845. int it1 = 20; /* iterations phase1 */
  846. int it2 = 40; /* iterations pahse2 */
  847. if (it1value == 0)
  848. {
  849. it1 = 20;
  850. }
  851. else
  852. {
  853. it1 = it1value;
  854. }
  855. if (it2value == 0)
  856. {
  857. it2 = 40;
  858. }
  859. else
  860. {
  861. it2 = it2value;
  862. }
  863. /* the whole graph structures */
  864. a = calloc (1, maxlevel * sizeof (struct mmatrix *));
  865. a1 = calloc (1, maxlevel * sizeof (struct mmatrix *));
  866. a2 = calloc (1, maxlevel * sizeof (struct mmatrix *));
  867. as = calloc (1, maxlevel * sizeof (struct mmatrix *));
  868. /* get matrix struct */
  869. for (i = 0; i < maxlevel; i++)
  870. {
  871. a[i] = calloc (1, sizeof (struct mmatrix));
  872. a1[i] = calloc (1, sizeof (struct mmatrix));
  873. a2[i] = calloc (1, sizeof (struct mmatrix));
  874. as[i] = calloc (1, sizeof (struct mmatrix));
  875. }
  876. /* get data inside struct */
  877. for (i = 0; i < maxlevel; i++)
  878. {
  879. /* upper level */
  880. a[i]->level = i;
  881. a1[i]->level = i;
  882. a2[i]->level = i;
  883. as[i]->level = i;
  884. /* number of rows */
  885. a[i]->nrows = nnodes_of_level[i];
  886. a1[i]->nrows = nnodes_of_level[i];
  887. a2[i]->nrows = nnodes_of_level[i];
  888. as[i]->nrows = nnodes_of_level[i];
  889. /* number of columns */
  890. a[i]->ncols = nnodes_of_level[i + 1];
  891. a1[i]->ncols = nnodes_of_level[i + 1];
  892. a2[i]->ncols = nnodes_of_level[i + 1];
  893. as[i]->ncols = nnodes_of_level[i + 1];
  894. /* buffer for barycenter values */
  895. if (a[i]->nrows > a[i]->ncols)
  896. {
  897. a[i]->bbytes = ((a[i]->nrows + 1) * sizeof (double));
  898. a1[i]->bbytes = ((a1[i]->nrows + 1) * sizeof (double));
  899. a2[i]->bbytes = ((a2[i]->nrows + 1) * sizeof (double));
  900. as[i]->bbytes = ((as[i]->nrows + 1) * sizeof (double));
  901. }
  902. else
  903. {
  904. a[i]->bbytes = ((a[i]->ncols + 1) * sizeof (double));
  905. a1[i]->bbytes = ((a1[i]->ncols + 1) * sizeof (double));
  906. a2[i]->bbytes = ((a2[i]->ncols + 1) * sizeof (double));
  907. as[i]->bbytes = ((as[i]->ncols + 1) * sizeof (double));
  908. }
  909. a[i]->b = calloc (1, a[i]->bbytes);
  910. a1[i]->b = calloc (1, a1[i]->bbytes);
  911. a2[i]->b = calloc (1, a2[i]->bbytes);
  912. as[i]->b = calloc (1, as[i]->bbytes);
  913. /* number of bytes used */
  914. a[i]->nmi0 = ((a[i]->nrows + 1) * sizeof (int));
  915. a1[i]->nmi0 = ((a[i]->nrows + 1) * sizeof (int));
  916. a2[i]->nmi0 = ((a[i]->nrows + 1) * sizeof (int));
  917. as[i]->nmi0 = ((a[i]->nrows + 1) * sizeof (int));
  918. /* row node id's (int's) */
  919. a[i]->mi0 = calloc (1, a[i]->nmi0);
  920. a1[i]->mi0 = calloc (1, a1[i]->nmi0);
  921. a2[i]->mi0 = calloc (1, a2[i]->nmi0);
  922. as[i]->mi0 = calloc (1, as[i]->nmi0);
  923. /* number of bytes used */
  924. a[i]->nm0i = ((a[i]->ncols + 1) * sizeof (int));
  925. a1[i]->nm0i = ((a[i]->ncols + 1) * sizeof (int));
  926. a2[i]->nm0i = ((a[i]->ncols + 1) * sizeof (int));
  927. as[i]->nm0i = ((a[i]->ncols + 1) * sizeof (int));
  928. /* col node id's (int's) */
  929. a[i]->m0i = calloc (1, a[i]->nm0i);
  930. a1[i]->m0i = calloc (1, a1[i]->nm0i);
  931. a2[i]->m0i = calloc (1, a2[i]->nm0i);
  932. as[i]->m0i = calloc (1, as[i]->nm0i);
  933. /* bits array for the matrix */
  934. a[i]->nbytes = 1 +
  935. ((((a[i]->nrows + 1) * (a[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  936. a1[i]->nbytes = 1 +
  937. ((((a1[i]->nrows + 1) * (a1[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  938. a2[i]->nbytes = 1 +
  939. ((((a2[i]->nrows + 1) * (a2[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  940. as[i]->nbytes = 1 +
  941. ((((as[i]->nrows + 1) * (as[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  942. /* XXX valgrind issue here */
  943. a[i]->bits = calloc (1, a[i]->nbytes);
  944. a1[i]->bits = calloc (1, a1[i]->nbytes);
  945. a2[i]->bits = calloc (1, a2[i]->nbytes);
  946. as[i]->bits = calloc (1, as[i]->nbytes);
  947. }
  948. /* fill the matrix with data for all levels */
  949. for (i = 0; i < maxlevel; i++)
  950. {
  951. make_matrix (g, i, a[i]);
  952. }
  953. copy_a (a, as);
  954. ks = number_of_crossings_a (as);
  955. printf ("bc_n(++): initial crossings is %d\n", ks);
  956. g->sugi_icrossings = ks; /* sugiyama initial crossings */
  957. if (ks > 0)
  958. {
  959. /* Phase1 */
  960. ch1 = 0;
  961. /* first does alwasy improve */
  962. ch1 += phase1_down (a);
  963. copy_a (a, as);
  964. ch1 += phase1_up (a);
  965. copy_a (a, as);
  966. /* loop phase1 */
  967. n1 = 0;
  968. do
  969. {
  970. copy_a (a, a1);
  971. ch1 += phase1_down (a);
  972. k = number_of_crossings_a (a);
  973. if (k < ks)
  974. {
  975. /* reduced crossings, save the new state */
  976. ks = k;
  977. copy_a (a, as);
  978. }
  979. ch1 += phase1_up (a);
  980. k = number_of_crossings_a (a);
  981. if (k < ks)
  982. {
  983. ks = k;
  984. copy_a (a, as);
  985. }
  986. cht += ch1;
  987. printf
  988. ("bc_n(++): current crossings phase1 is %d (%d:%d) ch1=%d cht=%d\n",
  989. ks, n1, n2, ch1, cht);
  990. if (ks == 0)
  991. {
  992. break;
  993. }
  994. /* check if number of crossings changed */
  995. r1 = r2;
  996. r2 = r3;
  997. r3 = ks;
  998. if (r1 == r2)
  999. {
  1000. if (r2 == r3)
  1001. {
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. while (++n1 < it1 && (equal_a (a, a1) == 0 /* FALSE */ ));
  1007. /* if matrices differ, save state */
  1008. if (equal_a (a, as) == 0 /* FALSE */ )
  1009. {
  1010. copy_a (as, a);
  1011. }
  1012. if (ks > 0)
  1013. {
  1014. /* Phase2 */
  1015. n2 = 0;
  1016. cht += ch1;
  1017. do
  1018. {
  1019. ch2 = 0;
  1020. copy_a (a, a2);
  1021. ch2 += phase2_down (a);
  1022. n1 = 0;
  1023. do
  1024. {
  1025. ch1 = 0;
  1026. copy_a (a, a1);
  1027. ch1 += phase1_down (a);
  1028. k = number_of_crossings_a (a);
  1029. if (k < ks)
  1030. {
  1031. ks = k;
  1032. copy_a (a, as);
  1033. }
  1034. ch1 += phase1_up (a);
  1035. k = number_of_crossings_a (a);
  1036. if (k < ks)
  1037. {
  1038. ks = k;
  1039. copy_a (a, as);
  1040. }
  1041. if (ks == 0)
  1042. {
  1043. break;
  1044. }
  1045. /* check if number of crossings changed */
  1046. rr1 = rr2;
  1047. rr2 = rr3;
  1048. rr3 = ks;
  1049. if (rr1 == rr2)
  1050. {
  1051. if (rr2 == rr3)
  1052. {
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. while (++n1 < it1 && equal_a (a, a1) == 0 /* FALSE */ );
  1058. ch2 += phase2_up (a);
  1059. n1 = 0;
  1060. do
  1061. {
  1062. copy_a (a, a1);
  1063. ch1 += phase1_up (a);
  1064. k = number_of_crossings_a (a);
  1065. if (k < ks)
  1066. {
  1067. ks = k;
  1068. copy_a (a, as);
  1069. }
  1070. ch1 += phase1_down (a);
  1071. k = number_of_crossings_a (a);
  1072. if (k < ks)
  1073. {
  1074. ks = k;
  1075. copy_a (a, as);
  1076. }
  1077. cht += ch1;
  1078. printf
  1079. ("bc_n(++): current crossings phase2 is %d (%d:%d) ch1=%d ch2=%d cht=%d\n",
  1080. ks, n1, n2, ch1, ch2, cht);
  1081. fflush (stdout);
  1082. if (ks == 0)
  1083. {
  1084. break;
  1085. }
  1086. /* check if number of crossings changed */
  1087. rr1 = rr2;
  1088. rr2 = rr3;
  1089. rr3 = ks;
  1090. if (rr1 == rr2)
  1091. {
  1092. if (rr2 == rr3)
  1093. {
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. while (++n1 < it1 && equal_a (a, a1) == 0 /* FALSE */ );
  1099. cht += ch1;
  1100. cht += ch2;
  1101. if (ks == 0)
  1102. {
  1103. break;
  1104. }
  1105. /* check if number of crossings changed */
  1106. r1 = r2;
  1107. r2 = r3;
  1108. r3 = ks;
  1109. if (r1 == r2)
  1110. {
  1111. if (r2 == r3)
  1112. {
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. while (++n2 < it2 && equal_a (a, a2) == 0 /* FALSE */ );
  1118. }
  1119. }
  1120. /* sugiyama final crossings */
  1121. g->sugi_fcrossings = ks;
  1122. /* sugiyama changes made */
  1123. g->sugi_changes = cht;
  1124. printf ("bc_n(++): final crossings is %d after %d changes made\n", ks, cht);
  1125. fflush (stdout);
  1126. for (i = 0; i < maxlevel; i += 2)
  1127. {
  1128. /* set new node positions for 2 levels */
  1129. store_new_positions (as[i], i, g);
  1130. }
  1131. if (i == maxlevel)
  1132. {
  1133. store_new_positions (as[maxlevel - 1], (maxlevel - 1), g);
  1134. }
  1135. for (i = 0; i < maxlevel; i++)
  1136. {
  1137. if (a[i])
  1138. {
  1139. free (a[i]->b);
  1140. free (a[i]->mi0);
  1141. free (a[i]->m0i);
  1142. free (a[i]->bits);
  1143. }
  1144. if (a1[i])
  1145. {
  1146. free (a1[i]->b);
  1147. free (a1[i]->mi0);
  1148. free (a1[i]->m0i);
  1149. free (a1[i]->bits);
  1150. }
  1151. if (a2[i])
  1152. {
  1153. free (a2[i]->b);
  1154. free (a2[i]->mi0);
  1155. free (a2[i]->m0i);
  1156. free (a2[i]->bits);
  1157. }
  1158. if (as[i])
  1159. {
  1160. free (as[i]->b);
  1161. free (as[i]->mi0);
  1162. free (as[i]->m0i);
  1163. free (as[i]->bits);
  1164. }
  1165. }
  1166. for (i = 0; i < maxlevel; i++)
  1167. {
  1168. free (a[i]);
  1169. free (a1[i]);
  1170. free (a2[i]);
  1171. free (as[i]);
  1172. }
  1173. free (a);
  1174. free (a1);
  1175. free (a2);
  1176. free (as);
  1177. return;
  1178. }
  1179. /*
  1180. This algorithm is for routing hierarchies of elements. A "good route" is
  1181. one that has a minimum number of link crossings. An algorithm that was
  1182. truly optimal (for minimizing link crossings) would be combinatorial and
  1183. therefore cost prohibitive; therefore, this algorithm uses a heuristic
  1184. approach that finds a route with close to the minimum number of crossings
  1185. in a reasonable amount of time.
  1186. This algorithm assumes that all the elements form a directed acyclic graph
  1187. (DAG), which means (1) that links flow one way between elements and (2) for
  1188. any given node there is no way to get back to the node if, starting at the
  1189. node, you traverse the links going from node to node. This algorithm also
  1190. assumes that AT MOST only ONE link may exist between a pair of nodes.
  1191. -------------------------------------------------------------------------------
  1192. OVERVIEW OF ALGORITHM
  1193. All elements that do not have any parents are placed in the first row (row 0).
  1194. Elements are assigned to rows, where the row number for each child is equal to
  1195. the [maximum(row number of all its parents) + 1]. Crossovers are determined
  1196. by examining links between elements on adjacent rows, so if a parent is in a
  1197. row that is not adjacent to its child's row, "dummy" nodes are created on the
  1198. rows in between the parent and child, and the parent and child are connected
  1199. via these dummy nodes.
  1200. Once the elements (now called nodes) are assigned to individual rows, the
  1201. rows are sorted (repeatedly) in order to minimize link crossings. The
  1202. sort criteria involves attempting to both center children under parents and
  1203. to center parents over children. The sort orders are then tweaked by swapping
  1204. nodes that have the same sort value.
  1205. After the column orders are finalized, the nodes are spread out so they are
  1206. more or less centered above their children and below their parents. When
  1207. centering children below parents, a row of children is sorted by which node
  1208. has the greatest number of parents. These get first choice of where to be
  1209. placed under the parents (actually, dummy nodes get first preference, then
  1210. all of the others). Centering parents above children is analogous.
  1211. When done with node placement, there may be some empty columns, and the
  1212. numbering scheme may not start at 0. Therefore, the empty columns must
  1213. be eliminated and every node needs its column renumbered, starting at 0.
  1214. Then you are done.
  1215. -------------------------------------------------------------------------------
  1216. REALIZATION MATRIX
  1217. When it comes to both sorting nodes and horizontally spacing the nodes, two
  1218. adjacent rows are always involved. For example, if we are sorting row[i]
  1219. based on the children of row[i]'s nodes, then row[i+1] is also involved
  1220. at this step. These two rows are called the "i-th realization", and form
  1221. the "i-th realization matrix". A realization matrix shows the parent-child
  1222. relationships between adjacent rows, with the parents on the rows and the
  1223. children on the columns. If there is a parent-child relationship, a 1 is
  1224. stored in the matrix at the position, else a 0 is stored.
  1225. An example:
  1226. A B C D
  1227. \ \ / \ / / |
  1228. \ /\ / \ / |
  1229. /\ / \ / \ |
  1230. / /\ /\ \ |
  1231. // \ / \ \|
  1232. E F G H
  1233. E F G H
  1234. A 0 1 1 0 In this example, parent 'A' has children 'F' and 'G',
  1235. B 1 0 0 1 parent 'B' has children 'E' and 'H',
  1236. C 1 1 0 0 parent 'C' has children 'E' and 'F',
  1237. D 0 0 0 1 and parent 'D' has child 'H'.
  1238. -------------------------------------------------------------------------------
  1239. ROW AND COLUMN BARYCENTERS
  1240. Two other important concepts are the "row barycenter" and the "column
  1241. barycenter" for a node. The "row barycenter" is the basically the average
  1242. of the positions of a node's children. The "column barycenter" is the average
  1243. of the positions of a node's parents. These numbers tell us where a node
  1244. would like to be positioned in its row, depending whether we are positioning
  1245. relative to children or parents.
  1246. For example, using the above realization matrix, we can calculate the row
  1247. barycenters for A, B, C, and D, and the column barycenters for E, F, G, and H.
  1248. Since the row barycenter of a node is equal to the sum of the positions of
  1249. the node's children divided by the number of children of the node, the row
  1250. barycenter for A is (1 + 2)/2 = 1.5. This assumes that we start numbering
  1251. rows and columns at 0. Similarly, the column barycenter of a node is equal
  1252. to the sum of the positions of the node's parents divided by the number of
  1253. parents of the node. So, the column barycenter of F is (0 + 2)/2 = 1.0.
  1254. The complete example is as follows:
  1255. Row
  1256. | E F G H | Barys
  1257. ------+--------------------+-----
  1258. A | 0 1 1 0 | 1.5
  1259. B | 1 0 0 1 | 1.5
  1260. C | 1 1 0 0 | 0.5
  1261. D | 0 0 0 1 | 3.0
  1262. ------+--------------------+-----
  1263. Col | 1.5 1.0 0.0 2.0 |
  1264. Barys | |
  1265. If we were to sort the child nodes here by their column barycenters, the new
  1266. order would be G, F, E, H. If we were to sort the parent nodes here by their
  1267. row barycenters, then the order would be C, A, B, D (if two or more nodes have
  1268. the same value, be sure to keep the same precedence that already exists
  1269. between them, e.g., make sure that order after sorting is not C, B, A, D).
  1270. If a node has no parents then it can't have a column barycenter. This case
  1271. should never happen, as all nodes that have no parents should be in the first
  1272. level of the hierarchy, and these nodes would only be represented in
  1273. realization matrix 0, and they would only have row barycenters.
  1274. If a node has no children then it can't have a row barycenter. In this case,
  1275. while sorting based on row barycenters, sort AROUND these nodes, i.e., do
  1276. not change their positions at all. For example, if we had the following:
  1277. Row
  1278. | W X Y Z | Barys
  1279. ------+--------------------+-----
  1280. Q | 0 1 1 1 | 2.0
  1281. R | 0 0 0 0 | ???
  1282. S | 1 0 0 0 | 0.0
  1283. T | 0 1 0 1 | 2.0
  1284. ------+--------------------+-----
  1285. Col | 2.0 1.5 0.0 1.5 |
  1286. Barys | |
  1287. and we were to sort by row barycenters, the resulting order should be S, R,
  1288. Q, T. Notice how R stayed in its position, and even though Q and T had the
  1289. same barycentric value, Q stayed before T.
  1290. The whole reason for sorting rows and columns by their barycenters is to
  1291. decrease the number of crossovers.
  1292. -------------------------------------------------------------------------------
  1293. CROSSOVERS
  1294. The realization matrix is also used to count the number of crossovers between
  1295. two adjacent rows of nodes. For each row, starting with the second row, if
  1296. a row element has a 1, then sum up all of the matrix elements that are above
  1297. AND to the right of this element. Looking again at the first example:
  1298. A B C D
  1299. \ \ / \ / / |
  1300. \ /\ / \ / |
  1301. /\ / \ / \ |
  1302. / /\ /\ \ |
  1303. // \ / \ \|
  1304. E F G H
  1305. Row
  1306. | E F G H | Barys
  1307. ------+--------------------+-----
  1308. A | 0 1 1 0 | 1.5
  1309. B | 1 0 0 1 | 1.5
  1310. C | 1 1 0 0 | 0.5
  1311. D | 0 0 0 1 | 3.0
  1312. ------+--------------------+-----
  1313. Col | 1.5 1.0 0.0 2.0 |
  1314. Barys | |
  1315. Starting with the second row (parent B's row), position B-E has a 1. Looking
  1316. at positions above and to the right, we see positions A-F and A-G both have
  1317. a 1, so the number of crossovers is currently = 2. Position B-H has a 1, but
  1318. there are no elements above and to the right, so crossovers is still = 2.
  1319. For parent row of C, position C-E crosses over with B-H, A-F, and A-G, so
  1320. crossovers = 5. C-F crosses over with B-H and A-G, so crossovers = 7. For
  1321. parent row D, position D-H doesn't cross over with any other link. So for
  1322. this realization matrix representing these two rows, the number of crossovers
  1323. is 7.
  1324. The total number of crossovers for the whole graph would be the sum of the
  1325. crossovers from each matrix realization.
  1326. -------------------------------------------------------------------------------
  1327. NODE CENTERING
  1328. After the nodes for each row have their final sort order, the nodes need to
  1329. be assigned to grid positions. Their initial grid position will be their
  1330. column position, by which we mean their array position in the row. From now
  1331. on, when we take a row or column barycenter, we will be using grid positions
  1332. instead of column positions.
  1333. Note: The following examples will be based on centering children with respect
  1334. to their parents' positions. Centering parents based on their children's
  1335. positions is analogous.
  1336. When positioning the nodes on a row based on their parents' positions, the
  1337. nodes must be initially sorted to see which nodes get first choice. The dummy
  1338. nodes go first, and the rest of nodes are sorted in descending order based on
  1339. the number of parents the node has. If a dummy node has a parent that has
  1340. multiple dummy nodes, all of these dummy nodes are again sorted by how close
  1341. to the center of the parent's children they are. This is a confusing
  1342. statement, best illustrated by example:
  1343. P1 P2
  1344. \ |
  1345. \ __________^__________
  1346. \| | | | |
  1347. C1 D1 D2 C2 D3
  1348. Here, parent P1 has one child, C1. Parent P2 has five children, and three of
  1349. the child nodes are dummy nodes: D1, D2, and D3. C1 is child 0 of P2, D1 is
  1350. child 1 of P2, D2 is child 2 of P2, C2 is child 3 of P2, and D3 is child 4 of
  1351. P2. The child midpoint underneath the parent is equal to
  1352. (the number of children - 1) / 2, so (5 - 1) / 2 = 2. Since the dummy nodes
  1353. go first, the initial order is D1, D2, D3, C1 (because it has 2 parents), and
  1354. finally C2. All of the dummy nodes have the same parent, so we will sort them
  1355. based on how far away they are from the parent's child midpoint. D1 is child
  1356. 1 of P2, so it is 1 away. D2 is child 2 of P2, so it is 0 away. D3 is child
  1357. 4 of P2, so it is 2 away. Therefore, the final order for choosing positions
  1358. is D2, D1, D3, C1, C2.
  1359. In a situation similar to the dummy nodes, if a non-dummy node has a only one
  1360. parent, and that parent has other children with just one parent, then these
  1361. one parent child nodes that have the same parent need additional sorting in
  1362. the exact same manner that we just did the dummy nodes.
  1363. The whole purpose behind this is so that the left most node doesn't get first
  1364. choice. If it did, we would get graphs that look like:
  1365. A A
  1366. | |
  1367. |_________ instead of _____^_____
  1368. | | | | | |
  1369. B C D B C D
  1370. Anyway, once we have a sort order for the nodes of a row, we place the nodes
  1371. in their preferred positions. Using the previous example, assume that P1
  1372. is in grid position 2 and P2 is in grid position 5. D2 gets first choice,
  1373. and its column barycenter (based now on parent grid positions, not column
  1374. positions) is 5, so we place D2 in position 5. D1 is next, its barycenter
  1375. is also 5. We can't give it 5 since that position is occupied, so we give
  1376. it the closest possible position we can, which in this case is 4. D3 is next,
  1377. and its barycenter is also 5. The closest position that we can give it is
  1378. position 7, since we must allow room for C2. C1 is next, and its barycenter
  1379. is (2 + 5)/2 = 3.5, which we round to 3. Position 3 is open, so we go ahead
  1380. and give it position 3. C2 is last, and its barycenter is 5. However, the
  1381. first position available to it based on its left neighbor is position 6, so
  1382. we assign it position 6.
  1383. -------------------------------------------------------------------------------
  1384. GOING 'UP' OR 'DOWN' THE GRAPH
  1385. "Going down the graph" means taking each realization matrix situation,
  1386. starting with Realization Matrix 0, and performing some action on it, then
  1387. going to the next realization matrix, and continuing until all of the
  1388. realization matrices have been visited.
  1389. "Going up the graph" is analogous, except you start at the bottom with the
  1390. last realization matrix and work up to Realization Matrix 0.
  1391. */
  1392. void
  1393. reduce_crossings2 (struct gml_graph *g, int it1v, int it2v)
  1394. {
  1395. if (maxlevel == 0)
  1396. {
  1397. /* if graph has only 1 or more nodes */
  1398. return;
  1399. }
  1400. bc_n (g, it1v, it2v);
  1401. return;
  1402. }
  1403. /* end */