#!/usr/bin/perl #with graph use strict; use warnings; use Data::Dumper; use Graph; my $g =Graph::Undirected->new; my @a=(1,1,2,3,4,4,8,8); my @b=(3,4,3,5,6,7,9,10); for ( 0 .. $#a){ $g->add_edge($a[$_], $b[$_]); } print Dumper $g->connected_components; __DATA__ this prints ...$VAR1 = [ 9, 8, 10 ]; $VAR2 = [ 6, 4, 1, 3, 2, 5, 7 ];