-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.pl
executable file
·53 lines (47 loc) · 1.78 KB
/
preprocess.pl
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
#!/usr/bin/env perl
############################################################################
## Preprocesses an input graph of undirected links to a format required by
## graph statistics programs; namely,
##
## 1) each undirected link is converted to a pair of directed links, and
## 2) links are grouped by the source node.
##
## Property 2 ensures that all outgoing links for a given node appear
## in consecutive lines in the output graph file. We don't actually
## have to sort the output lines to achieve property 2, but we do so
## anyway in case it proves useful.
##
## ---------------------------------------------------------------------
## Copyright (C) 2010 The Regents of the University of California.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
############################################################################
open SORT, "| sort -k 1,2n -k 2,3n"
or die "ERROR: couldn't open pipe to 'sort' command";
while (<>) {
next if /^\s*$/ || /^\#/;
if (/^(\d+)\s+(\d+)\s*$/) {
my $s = $1;
my $d = $2;
if ($s == $d) {
warn "discarding self loop on node $s\n";
next;
}
print SORT "$s $d\n$d $s\n";
}
else {
die "ERROR: malformed line: $_";
}
}