Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm attempting to implement a basic hierarchical agglomerative clustering algorithm to be used to create an arbitrary number of clusters from an existing dataset. More reading about the general concept can be found at at http://cgm.cs.mcgill.ca/~soss/cs644/projects/siourbas/sect5.html or your friendly neighborhood google.

A word about the dataset.

My data consists of some 1500-5000 "items" each of which contains a set of "words". These words are 5-30 character strings. Each set of words contains no duplicates. There are between 5-100 "words" in a set.

Some words about the existing code.

The theoretical complexity of such an algorithm is something like O(cn2d2) but I suspect my implementation is considerably worse since I ran it for over 11 hours and it only managed to consolidate 500 of the 1600 items.

The "merge" function is obviously very silly, I wrote it without thinking very hard and it doesn't do much. On the other hand I don't think it impacts the performance.

The vast majority of the time spent is going to be in the max_diff function, which appears to get exponentially slower as the program continues to run.

The datastructure being produced is necessary, that is it should be a binary tree made of array-refs where each leaf is either another tree or an actual item. (Its necessary because we don't know how many clusters we want to produce).

Suggestions for optimizations or even different algorithms gratefully received.

while( @$items > 10) { my( $item1, $item2 ); my( $item1_idx, $item2_idx ); my $difference = 9999; #Arbitrary large number for my $i ( 0 .. $#$items ) { my $d1 = $items->[$i]; for my $j ( 0 .. $#$items ) { my $d2 = $items->[$j]; next if $i == $j; my $diff = max_diff( $d1, $d2 ); if( $diff < $difference ) { $difference = $diff; ($item1,$item2) = ($d1,$d2); ($item1_idx,$item2_idx) = ($i,$j); } last if $difference == 0; } last if $difference == 0; } splice( @$items, $item1_idx, 1 ); splice( @$items, $item2_idx, 1 ); my $c = merge( $item1, $item2 ); push @$items, $c; print " \r"; print scalar @$items, "\r"; } sub merge { my( $x, $y ) = @_; # Both non-clusters if( ref $x eq 'HASH' and ref $y eq 'HASH' ) { return [$x,$y]; } # $x cluster elsif( ref $x eq 'ARRAY' and ref $y eq 'HASH' ) { return [$x,$y]; } # $y cluster elsif( ref $x eq 'HASH' and ref $y eq 'ARRAY' ) { return [$y,$x]; } elsif( ref $x eq 'ARRAY' and ref $y eq 'ARRAY' ) { return [$x,$y]; } else { die "Wtf? $x $y"; } } sub max_diff { my( $d1, $d2 ) = @_; #my %x1 = map { $_->name, undef } $d1->words; #my %x2 = map { $_->name, undef } $d2->words; if( ref $d1 eq 'HASH' and ref $d2 eq 'HASH' ) { my %x1 = %{$d1->{words}}; my %x2 = %{$d2->{words}}; my %y1 = %x1; my %y2 = %x2; delete @x1{keys %x2}; delete @y2{keys %y1}; return( ( scalar keys %x1 ) + ( scalar keys %y2 ) ); } elsif( ref $d1 eq 'ARRAY' and ref $d2 eq 'HASH' ) { my $x = max_diff( $d1->[0], $d2 ); my $y = max_diff( $d1->[1], $d2 ); return $x > $y ? $x : $y; } elsif( ref $d1 eq 'HASH' and ref $d2 eq 'ARRAY' ) { my $x = max_diff( $d2->[0], $d1 ); my $y = max_diff( $d2->[1], $d1 ); return $x > $y ? $x : $y; } elsif( ref $d1 eq 'ARRAY' and ref $d2 eq 'ARRAY' ) { my $x = max_diff( $d1->[0], $d2->[0] ); my $y = max_diff( $d1->[1], $d2->[1] ); my $xx = max_diff( $d1->[0], $d2->[1] ); my $yy = max_diff( $d1->[1], $d2->[0] ); return max( $x, $y, $xx, $yy ); } else { die "Wtffffff $d1 $d2"; } }

In reply to Optimizing a naive clustering algorithm by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-06-23 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (110 votes). Check out past polls.

    Notices?