-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectedComponentsLabeling.sas
334 lines (268 loc) · 11.8 KB
/
connectedComponentsLabeling.sas
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
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/* */
/* SAS Macro to identify connected components of a large undirected */
/* graph. */
/* */
/* Author: Youri Baeyens */
/* */
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
%macro to_text(variable);
trim(left(put(&variable,best12.)))
%mend;
%macro connectedComponentsLabeling(inputDatasetEdges=, /* Input dataset of type Edges */
edgeEndA=, /* Variable from input dataset */
edgeEndB=, /* Variable from input dataset */
outputDatasetVertices=, /* Output dataset of type Vertices */
outputDatasetEdges= /* Output dataset of type Edges */
);
%let startTime=%sysfunc(datetime());
/*******************************************************************/
/* */
/* Parameters validation */
/* */
/*******************************************************************/
%if not %sysfunc(exist(&inputDatasetEdges))
%then %do;
%put ERROR: Input file &inputDatasetEdges does not exist;
%goto exit;
%end;
proc contents data=&inputDatasetEdges out=CG_000_CONTENTS noprint;
run;
proc sql NOPRINT;
select count(*), count(distinct type), count(distinct length), max(type), max(length)
into :nb_variables_found, :nb_distinct_types, :nb_distinct_length, :type, :length
from CG_000_CONTENTS
where upcase(NAME) in (%upcase("&edgeEndA"),%upcase("&edgeEndB"));
quit;
%if &nb_variables_found ne 2
%then %do;
%put ERROR: Either &edgeEndA or &edgeEndB not present in &inputDatasetEdges;
%goto exit;
%end;
%if &nb_distinct_types ne 1
%then %do;
%put ERROR: &edgeEndA and &edgeEndB not of the same type in &inputDatasetEdges (one is numeric, the other is character);
%goto exit;
%end;
%if &nb_distinct_length ne 1
%then %do;
%put ERROR: &edgeEndA and &edgeEndB not of the same length in &inputDatasetEdges;
%goto exit;
%end;
%if &length >16
%then %do;
%put ERROR: length of &edgeEndA and &edgeEndB in &inputDatasetEdges is greater than 16 (length is &length);
%put ERROR: are you sure &edgeEndA and &edgeEndB are id variables?;
%goto exit;
%end;
%if "&outputDatasetVertices" eq "" and "&outputDatasetEdges" eq ""
%then %do;
%put ERROR: either outputDatasetVertices or outputDatasetEdges should be specified;
%goto exit;
%end;
%if &type=2 %then %let myType=$&length;
%else %let myType=&length;
/*******************************************************************/
/* */
/* Extract list of vertices */
/* */
/*******************************************************************/
proc sql;
create table CG_001_VERTICES as
select distinct UNIT
from (
select distinct &edgeEndA as UNIT from &inputDatasetEdges
UNION
select distinct &edgeEndB as UNIT from &inputDatasetEdges
);
quit;
/*************************************************************************/
/* */
/* Rename of variables */
/* */
/*************************************************************************/
proc sql;
create view CG_001_EDGES as
select &edgeEndA as VerticeLabel_of_edgeOrigin,
&edgeEndB as VerticeLabel_of_edgeDestination
from &inputDatasetEdges;
quit;
/*************************************************************************/
/* */
/* Define the bijective application */
/* */
/*************************************************************************/
data CG_002_VERTICES;
set CG_001_VERTICES(rename=(UNIT=VerticeLabel)) end=fin;
verticeSerial+1;
if fin then call symput('number_of_vertices',%to_text(_N_));
run;
/*************************************************************************/
/* */
/* Apply bijective application using a hash table */
/* */
/*************************************************************************/
data CG_003_EDGES(keep=verticeLabel_of_edgeOrigin
verticeLabel_of_edgeDestination
verticeSerial_of_edgeOrigin
verticeSerial_of_edgeDestination);
if _N_ = 1
then do;
declare hash h(dataset: "CG_002_VERTICES", hashexp: 16);
h.defineKey('VerticeLabel');
h.defineData('verticeSerial');
h.defineDone();
end;
set CG_001_EDGES;
VerticeLabel=verticeLabel_of_edgeOrigin; rc = h.find();
if (rc = 0) then verticeSerial_of_edgeOrigin=verticeSerial;
else verticeSerial_of_edgeOrigin=.;
VerticeLabel=verticeLabel_of_edgeDestination; rc=h.find(key: verticeLabel_of_edgeDestination);
if (rc = 0) then verticeSerial_of_edgeDestination=verticeSerial;
else verticeSerial_of_edgeDestination=.;
run;
proc datasets library=work nolist;
delete CG_000_CONTENTS;
delete CG_001_EDGES;
delete CG_001_VERTICES;
run;
quit;
/*************************************************************************/
/* */
/* Connexity algorithm */
/* */
/*************************************************************************/
data CG_004_FOREST( keep=node root
rename=(
node=nodeSerial_of_tree
root=rootSerial_of_tree
)
);
array pointer_of_node {&number_of_vertices } 8 _TEMPORARY_;
* INITIALISATION OF POINTERS;
* --------------------------;
if _N_=1 then do node=1 to &number_of_vertices;
pointer_of_node{node}=node;
end;
* PROCESSING OF EDGES;
* -------------------;
set CG_003_EDGES end=fin nobs=nobs;
id=verticeSerial_of_edgeOrigin; link root; root1=root;
id=verticeSerial_of_edgeDestination; link root; root2=root;
if root1 ne root2
then do;
pointer_of_node{root2}=root1;
end;
* EXPORT OF RESULTS;
* -----------------;
if fin then do;
link restructure_tree;
do node=1 to &number_of_vertices;
root=pointer_of_node{node};
output;
end;
end;
return;
* ROOT FUNCTION;
* -------------;
root:
F_root=0;
element_current=id;
element_next=pointer_of_node{id};
longueur_path=1;
do while(F_root=0);
if element_next=element_current
then do;
F_root=1;
root=element_current;
end;
else do;
element_current=element_next;
element_next=pointer_of_node{element_current};
end;
longueur_path=longueur_path+1;
end;
if longueur_path>2 then link restructure_path;
return;
* RESTRUCTURE_TREE FUNCTION;
* -------------------------;
restructure_tree:
do node=1 to &number_of_vertices;
id=node; link root; pointer_of_node{node}=root;
end;
return;
* RESTRUCTURE_PATH FUNCTION;
* -------------------------;
restructure_path:
F_root=0;
element_current=id;
element_next=pointer_of_node{id};
longueur_path=1;
do while(F_root=0);
if element_next=element_current
then F_root=1;
else do;
pointer_of_node{element_current}=root;
element_current=element_next;
element_next=pointer_of_node{element_current};
end;
end;
return;
run;
/*************************************************************************/
/* */
/* Create output dataset */
/* */
/*************************************************************************/
%if "&outputDatasetVertices" ne ""
%then %do;
data CG_005_FOREST(keep=nodeLabel_of_tree
rootLabel_of_tree
);
array label_of {&number_of_vertices } &myType _TEMPORARY_;
* Initialise reverse bijective application;
if _N_=1 then do until(fin);
set Cg_002_VERTICES end=fin;
label_of{VerticeSerial}=VerticeLabel;
end;
* Apply reverse bijective application;
set CG_004_FOREST end=fin;
nodeLabel_of_tree=label_of{nodeSerial_of_tree};
rootLabel_of_tree=label_of{rootSerial_of_tree};
run;
data &outputDatasetVertices(drop=rootLabel_of_tree);
length connectedComponentLabel $16;
set CG_005_FOREST;
rename nodeLabel_of_tree=vertex;
connectedComponentLabel=put(md5(rootLabel_of_tree),$hex16.);
run;
%end;
%if "&outputDatasetEdges" ne ""
%then %do;
proc sql;
create table &outputDatasetEdges as
select A.*, B.connectedComponentLabel
from &inputDatasetEdges A, &outputDatasetVertices B
where A.&edgeEndA=B.vertex;
quit;
%end;
proc datasets library=work nolist;
delete CG_002_VERTICES;
delete CG_003_EDGES;
delete CG_004_FOREST;
delete CG_005_FOREST;
run; quit;
proc sql;
drop view CG_001_EDGES;
quit;
%exit:
%let stopTime=%sysfunc(datetime());
data _NULL_;
elapsedTime=&stopTime-&startTime;
put "NOTE: Time to process the whole graph: " elapsedTime=time.;
run;
%mend;