#!/usr/bin/perl -w
# Generate sparse adjacency matrix for GO class graph
# Output format is MATLAB-compatible

# open(IN, "mips_class_graph.txt");
open(IN, "go_process_graph.txt");
$line = <IN>; chomp $line;
close(IN);
@edge = split /\s+/, $line;


# Generate list of nodes

for $edge (@edge) {
	($parent, $child) = split /->/, $edge;
	$node{$parent} = 1;
	$node{$child} = 1;
}

@node = sort(keys %node);

for $i (0 .. $#node) {
	$index = $i + 1;
	$index{$node[$i]} = $index;
	print "node{";
	print "$index";
	print "} = '$node[$i]';\n";
}

#for $i (0 .. $#node) {
#	print "$i\t$node[$i]\n";
#}
#die;


# Print non-zero elements of adjacency matrix

print "A_sparse = [";

for $edge (@edge) {
	($parent, $child) = split /->/, $edge;

	# Convention:
	# +1 if row->column is parent->child
	# +1 if column->row is parent->child

	print "$index{$parent}\t$index{$child}\t1;\n";	
	print "$index{$child}\t$index{$parent}\t1;\n";	
}

print "];\n";
