#!/usr/bin/perl -w use strict; use Graph::Undirected; my $graph = Graph::Undirected->new(); my @data = map { [ grep defined, split /\s+/ ] } ; foreach my $line ( 1 .. scalar @data ) { $graph->add_edges( map { $line => $_ } @{ $data[ $line - 1 ] } ) } my @sets = $graph->strongly_connected_components; for (@sets) { printf "{ %s }\n", join ' ', sort { $a cmp $b } @$_; } __END__ a b c d e f b g h i j k l m f