Created
February 1, 2012 08:04
-
-
Save rindai87/1715893 to your computer and use it in GitHub Desktop.
A sample of Calculation For Cluster Coefficient Using Tokyo Cabinet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use TokyoCabinet; | |
sub print_usage { | |
print << 'END;' ; | |
perl cluster_single.pl [id] | |
END; | |
exit 0; | |
} | |
sub open_db { | |
my $db_path = '/path/to/hdb'; | |
my $hdb = TokyoCabinet::HDB->new(); | |
if(!$hdb->open($db_path, $hdb->OREADER)){ | |
my $ecode = $hdb->ecode(); | |
printf STDERR ("open error: %s\n", $hdb->errmsg($ecode)); | |
} | |
return $hdb; | |
} | |
sub get_friends_ids { | |
my $key_id = shift; | |
my $hdb = shift; | |
my $friends_buff = $hdb->get($key_id); | |
return undef unless defined $friends_buff; | |
return [split "\t", $friends_buff]; | |
} | |
sub main { | |
print_usage() if @ARGV != 1; | |
my $id = $ARGV[0]; | |
my $hdb = open_db(); | |
my $friends_ids = get_friends_ids($id, $hdb); | |
my $size; | |
if (defined $friends_ids) { | |
$size = @{$friends_ids}; | |
} else { | |
print "$id,0,0,0,NA\n"; | |
next; | |
} | |
my $count = 0; | |
for my $friend1 (@{$friends_ids}) { | |
for my $friend2 (@{$friends_ids}) { | |
if ($friend1 eq $friend2) { | |
next; | |
} | |
my $friends_of_friend2 = get_friends_ids($friend2, $hdb); | |
for my $friend_of_friend2 (@{$friends_of_friend2}) { | |
if ($friend1 eq $friend_of_friend2) { | |
$count++; | |
next; | |
} | |
} | |
} | |
} | |
$count /= 2; | |
my $cluster_coeff = 0; | |
if ($size != 1) { | |
$cluster_coeff = $count / (($size)*($size-1)/2); | |
} else { | |
print "$id,$size,0,0,NA\n"; | |
next; | |
} | |
print "\nInputID : $id\n"; | |
print "The number of Friends : $size\n"; | |
print "The number of connection between friends of InputID : $count\n"; | |
print "Cluster Coefficience : $cluster_coeff\n\n"; | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TokyoCabinetにKey : Value = (ID) : (隣接点のID) という形式でデータを保持させてクラスタ係数を求めるコード
ある1つのノードに関してのクラスタ係数のみを算出する