warshall.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Generate transitive closure of a matrix,
  2. Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. #include <stdio.h>
  18. #include "machine.h"
  19. /* given n by n matrix of bits R, modify its contents
  20. to be the transive closure of what was given. */
  21. TC(R, n)
  22. unsigned *R;
  23. int n;
  24. {
  25. register int rowsize;
  26. register unsigned mask;
  27. register unsigned *rowj;
  28. register unsigned *rp;
  29. register unsigned *rend;
  30. register unsigned *ccol;
  31. unsigned *relend;
  32. unsigned *cword;
  33. unsigned *rowi;
  34. rowsize = WORDSIZE(n) * sizeof(unsigned);
  35. relend = (unsigned *) ((char *) R + (n * rowsize));
  36. cword = R;
  37. mask = 1;
  38. rowi = R;
  39. while (rowi < relend)
  40. {
  41. ccol = cword;
  42. rowj = R;
  43. while (rowj < relend)
  44. {
  45. if (*ccol & mask)
  46. {
  47. rp = rowi;
  48. rend = (unsigned *) ((char *) rowj + rowsize);
  49. while (rowj < rend)
  50. *rowj++ |= *rp++;
  51. }
  52. else
  53. {
  54. rowj = (unsigned *) ((char *) rowj + rowsize);
  55. }
  56. ccol = (unsigned *) ((char *) ccol + rowsize);
  57. }
  58. mask <<= 1;
  59. if (mask == 0)
  60. {
  61. mask = 1;
  62. cword++;
  63. }
  64. rowi = (unsigned *) ((unsigned) rowi + rowsize);
  65. }
  66. }
  67. /* Reflexive Transitive Closure. Same as TC
  68. and then set all the bits on the diagonal of R. */
  69. RTC(R, n)
  70. unsigned *R;
  71. int n;
  72. {
  73. register int rowsize;
  74. register unsigned mask;
  75. register unsigned *rp;
  76. register unsigned *relend;
  77. TC(R, n);
  78. rowsize = WORDSIZE(n) * sizeof(unsigned);
  79. relend = (unsigned *) ((unsigned) R + n*rowsize);
  80. mask = 1;
  81. rp = R;
  82. while (rp < relend)
  83. {
  84. *rp |= mask;
  85. mask <<= 1;
  86. if (mask == 0)
  87. {
  88. mask = 1;
  89. rp++;
  90. }
  91. rp = (unsigned *) ((unsigned) rp + rowsize);
  92. }
  93. }