-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcm.f90
458 lines (406 loc) · 10.3 KB
/
rcm.f90
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
subroutine i4_swap ( i, j )
!*****************************************************************************80
!
!! I4_SWAP swaps two I4's.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 30 November 1998
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input/output, integer ( kind = 4 ) I, J. On output, the values of I and
! J have been interchanged.
!
implicit none
integer ( kind = 4 ) i
integer ( kind = 4 ) j
integer ( kind = 4 ) k
k = i
i = j
j = k
return
end
subroutine i4vec_reverse ( n, a )
!*****************************************************************************80
!
!! I4VEC_REVERSE reverses the elements of an I4VEC.
!
! Example:
!
! Input:
!
! N = 5,
! A = ( 11, 12, 13, 14, 15 ).
!
! Output:
!
! A = ( 15, 14, 13, 12, 11 ).
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 26 July 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) N, the number of entries in the array.
!
! Input/output, integer ( kind = 4 ) A(N), the array to be reversed.
!
implicit none
integer ( kind = 4 ) n
integer ( kind = 4 ) a(n)
integer ( kind = 4 ) i
do i = 1, n/2
call i4_swap ( a(i), a(n+1-i) )
end do
return
end
subroutine degree ( root, adj_num, adj_row, adj, mask, deg, iccsze, ls, &
node_num )
!*****************************************************************************80
!
!! DEGREE computes the degrees of the nodes in the connected component.
!
! Discussion:
!
! The connected component is specified by MASK and ROOT.
! Nodes for which MASK is zero are ignored.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 05 January 2003
!
! Author:
!
! Original FORTRAN77 version by Alan George, Joseph Liu.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! Alan George, Joseph Liu,
! Computer Solution of Large Sparse Positive Definite Systems,
! Prentice Hall, 1981.
!
! Parameters:
!
! Input, integer ( kind = 4 ) ROOT, the node that defines the connected
! component.
!
! Input, integer ( kind = 4 ) ADJ_NUM, the number of adjacency entries.
!
! Input, integer ( kind = 4 ) ADJ_ROW(NODE_NUM+1). Information about
! row I is stored in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ.
!
! Input, integer ( kind = 4 ) ADJ(ADJ_NUM), the adjacency structure.
! For each row, it contains the column indices of the nonzero entries.
!
! Input, integer ( kind = 4 ) MASK(NODE_NUM), is nonzero for those nodes
! which are to be considered.
!
! Output, integer ( kind = 4 ) DEG(NODE_NUM), contains, for each node in
! the connected component, its degree.
!
! Output, integer ( kind = 4 ) ICCSIZE, the number of nodes in the
! connected component.
!
! Output, integer ( kind = 4 ) LS(NODE_NUM), stores in entries 1 through
! ICCSIZE the nodes in the connected component, starting with ROOT, and
! proceeding by levels.
!
! Input, integer ( kind = 4 ) NODE_NUM, the number of nodes.
!
implicit none
integer ( kind = 4 ) adj_num
integer ( kind = 4 ) node_num
integer ( kind = 4 ) adj(adj_num)
integer ( kind = 4 ) adj_row(node_num+1)
integer ( kind = 4 ) deg(node_num)
integer ( kind = 4 ) i
integer ( kind = 4 ) iccsze
integer ( kind = 4 ) ideg
integer ( kind = 4 ) j
integer ( kind = 4 ) jstop
integer ( kind = 4 ) jstrt
integer ( kind = 4 ) lbegin
integer ( kind = 4 ) ls(node_num)
integer ( kind = 4 ) lvlend
integer ( kind = 4 ) lvsize
integer ( kind = 4 ) mask(node_num)
integer ( kind = 4 ) nbr
integer ( kind = 4 ) node
integer ( kind = 4 ) root
!
! The sign of ADJ_ROW(I) is used to indicate if node I has been considered.
!
ls(1) = root
adj_row(root) = -adj_row(root)
lvlend = 0
iccsze = 1
!
! LBEGIN is the pointer to the beginning of the current level, and
! LVLEND points to the end of this level.
!
do
lbegin = lvlend + 1
lvlend = iccsze
!
! Find the degrees of nodes in the current level,
! and at the same time, generate the next level.
!
do i = lbegin, lvlend
node = ls(i)
jstrt = -adj_row(node)
jstop = abs ( adj_row(node+1) ) - 1
ideg = 0
do j = jstrt, jstop
nbr = adj(j)
if ( mask(nbr) /= 0 ) then
ideg = ideg + 1
if ( 0 <= adj_row(nbr) ) then
adj_row(nbr) = -adj_row(nbr)
iccsze = iccsze + 1
ls(iccsze) = nbr
end if
end if
end do
deg(node) = ideg
end do
!
! Compute the current level width.
!
lvsize = iccsze - lvlend
!
! If the current level width is nonzero, generate another level.
!
if ( lvsize == 0 ) then
exit
end if
end do
!
! Reset ADJ_ROW to its correct sign and return.
!
do i = 1, iccsze
node = ls(i)
adj_row(node) = -adj_row(node)
end do
return
end
subroutine rcm ( root, adj_num, adj_row, adj, mask, perm, iccsze, node_num )
!*****************************************************************************80
!
!! RCM renumbers a connected component by the reverse Cuthill McKee algorithm.
!
! Discussion:
!
! The connected component is specified by a node ROOT and a mask.
! The numbering starts at the root node.
!
! An outline of the algorithm is as follows:
!
! X(1) = ROOT.
!
! for ( I = 1 to N-1 )
! Find all unlabeled neighbors of X(I),
! assign them the next available labels, in order of increasing degree.
!
! When done, reverse the ordering.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 09 August 2013
!
! Author:
!
! Original FORTRAN77 version by Alan George, Joseph Liu.
! FORTRAN90 version by John Burkardt
!
! Reference:
!
! Alan George, Joseph Liu,
! Computer Solution of Large Sparse Positive Definite Systems,
! Prentice Hall, 1981.
!
! Parameters:
!
! Input, integer ( kind = 4 ) ROOT, the node that defines the connected
! component. It is used as the starting point for the RCM ordering.
! 1 <= ROOT <= NODE_NUM.
!
! Input, integer ( kind = 4 ) ADJ_NUM, the number of adjacency entries.
!
! Input, integer ( kind = 4 ) ADJ_ROW(NODE_NUM+1). Information about
! row I is stored in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ.
!
! Input, integer ( kind = 4 ) ADJ(ADJ_NUM), the adjacency structure.
! For each row, it contains the column indices of the nonzero entries.
!
! Input/output, integer ( kind = 4 ) MASK(NODE_NUM), a mask for the nodes.
! Only those nodes with nonzero input mask values are considered by the
! routine. The nodes numbered by RCM will have their mask values
! set to zero.
!
! Output, integer ( kind = 4 ) PERM(NODE_NUM), the RCM ordering.
!
! Output, integer ( kind = 4 ) ICCSZE, the size of the connected component
! that has been numbered.
!
! Input, integer ( kind = 4 ) NODE_NUM, the number of nodes.
! 1 <= NODE_NUM.
!
! Local Parameters:
!
! Workspace, integer DEG(NODE_NUM), a temporary vector used to hold
! the degree of the nodes in the section graph specified by mask and root.
!
implicit none
integer ( kind = 4 ) adj_num
integer ( kind = 4 ) node_num
integer ( kind = 4 ) adj(adj_num)
integer ( kind = 4 ) adj_row(node_num+1)
integer ( kind = 4 ) deg(node_num)
integer ( kind = 4 ) fnbr
integer ( kind = 4 ) i
integer ( kind = 4 ) iccsze
integer ( kind = 4 ) j
integer ( kind = 4 ) jstop
integer ( kind = 4 ) jstrt
integer ( kind = 4 ) k
integer ( kind = 4 ) l
integer ( kind = 4 ) lbegin
integer ( kind = 4 ) lnbr
integer ( kind = 4 ) lperm
integer ( kind = 4 ) lvlend
integer ( kind = 4 ) mask(node_num)
integer ( kind = 4 ) nbr
integer ( kind = 4 ) node
integer ( kind = 4 ) perm(node_num)
integer ( kind = 4 ) root
!
! Make sure NODE_NUM is legal.
!
if ( node_num < 1 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RCM - Fatal error!'
write ( *, '(a,i4)' ) ' Illegal input value of NODE_NUM = ', node_num
write ( *, '(a,i4)' ) ' Acceptable values must be positive.'
stop 1
end if
!
! Make sure ROOT is legal.
!
if ( root < 1 .or. node_num < root ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RCM - Fatal error!'
write ( *, '(a,i4)' ) ' Illegal input value of ROOT = ', root
write ( *, '(a,i4)' ) ' Acceptable values are between 1 and ', node_num
stop 1
end if
!
! Find the degrees of the nodes in the component specified by MASK and ROOT.
!
call degree ( root, adj_num, adj_row, adj, mask, deg, iccsze, perm, node_num )
mask(root) = 0
if ( iccsze < 1 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RCM - Fatal error!'
write ( *, '(a,i4)' ) ' Inexplicable component size ICCSZE = ', iccsze
stop 1
end if
!
! If the connected component is a singleton, there is no reordering to do.
!
if ( iccsze == 1 ) then
return
end if
!
! Carry out the reordering procedure.
!
! LBEGIN and LVLEND point to the beginning and
! the end of the current level respectively.
!
lvlend = 0
lnbr = 1
do while ( lvlend < lnbr )
lbegin = lvlend + 1
lvlend = lnbr
do i = lbegin, lvlend
!
! For each node in the current level...
!
node = perm(i)
jstrt = adj_row(node)
jstop = adj_row(node+1) - 1
!
! Find the unnumbered neighbors of NODE.
!
! FNBR and LNBR point to the first and last neighbors
! of the current node in PERM.
!
fnbr = lnbr + 1
do j = jstrt, jstop
nbr = adj(j)
if ( mask(nbr) /= 0 ) then
lnbr = lnbr + 1
mask(nbr) = 0
perm(lnbr) = nbr
end if
end do
!
! If no neighbors, skip to next node in this level.
!
if ( lnbr <= fnbr ) then
cycle
end if
!
! Sort the neighbors of NODE in increasing order by degree.
! Linear insertion is used.
!
k = fnbr
do while ( k < lnbr )
l = k
k = k + 1
nbr = perm(k)
do while ( fnbr < l )
lperm = perm(l)
if ( deg(lperm) <= deg(nbr) ) then
exit
end if
perm(l+1) = lperm
l = l - 1
end do
perm(l+1) = nbr
end do
end do
end do
!
! We now have the Cuthill-McKee ordering.
! Reverse it to get the Reverse Cuthill-McKee ordering.
!
call i4vec_reverse ( iccsze, perm )
return
end